diffusion
Version:
Diffusion JavaScript client
131 lines (130 loc) • 4.03 kB
JavaScript
"use strict";
/**
* @module diffusion.datatypes.RecordV2
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.toJSON = exports.RecordImpl = void 0;
var errors_1 = require("./../../../../errors/errors");
/**
* A Record V2 record schema entry
*/
var RecordImpl = /** @class */ (function () {
/**
* Create a new record
*
* @param name the name of the record
* @param min the minimum index that this record can assume
* @param max the maximum index that this record can assume
* @param index the index of the record
* @param fields an array of fields contained in this record
*/
function RecordImpl(name, min, max, index, fields) {
var _this = this;
this.name = name;
this.min = min;
this.max = max;
this.index = index;
this.isVariable = min !== max;
this.fields = fields;
this.fieldsMap = {};
fields.forEach(function (field) {
_this.fieldsMap[field.name] = field;
});
}
/**
* Get a copy of the internal fields array
*
* @return an array of fields
*/
RecordImpl.prototype.getFields = function () {
return [].concat(this.fields);
};
/**
* Get a field by its name
*
* @param fieldName the name of the field to get
* @return the field with the given name
*
* @throws a {@link SchemaViolationError} if no field with that name exists
*/
RecordImpl.prototype.getField = function (fieldName) {
var field = this.fieldsMap[fieldName];
if (field) {
return field;
}
throw new errors_1.SchemaViolationError("Record '" + this.name + "' has no field named '" + fieldName + "'");
};
/**
* Get the last field in the array
*
* @return the field with the last index in the fields array
*/
RecordImpl.prototype.lastField = function () {
return this.fields[this.fields.length - 1];
};
/**
* Create a model representation of this record schema.
*
* @return an array of model string representations for each field in the record
*/
RecordImpl.prototype.createModel = function () {
var fields = [];
this.fields.forEach(function (field) {
for (var i = 0; i < field.min; ++i) {
fields.push(field.modelValue());
}
});
return fields;
};
/**
* Calculate the absolute index of the record
*
* @param index the index offset
* @return the absolute index
* @throws an error if the index offset is out of range
*/
RecordImpl.prototype.getAbsoluteIndex = function (index) {
if (index < 0 || this.max !== -1 && index > this.max - 1) {
throw new errors_1.IllegalArgumentError("Invalid index '" + index + "' for record '" + this.name + "'");
}
return this.index + index;
};
/**
* Convert the record schema to a string
*
* @return a string representation of the field
*/
RecordImpl.prototype.toString = function () {
var ret = "Name=" + this.name;
if (this.min === this.max) {
ret += ", Occurs=" + this.min;
}
else {
ret += ", Min=" + this.min + ", Max=";
if (this.max === -1) {
ret += 'Unlimited';
}
else {
ret += this.max;
}
}
ret += ", Fields=" + this.fields;
return "Record [" + ret + "]";
};
return RecordImpl;
}());
exports.RecordImpl = RecordImpl;
/**
* Convert the record schema to a raw data object
*
* @return a data object that can be serialised with using JSON
*/
function toJSON(record) {
return {
fields: record.fields.map(function (f) { return f.toJSON(); }),
name: record.name,
min: record.min,
max: record.max
};
}
exports.toJSON = toJSON;