diffusion
Version:
Diffusion JavaScript client
233 lines (232 loc) • 9.2 kB
JavaScript
"use strict";
/**
* @module diffusion.datatypes.RecordV2
*/
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaBuilderImpl = void 0;
var errors_1 = require("./../../../../errors/errors");
var field_impl_1 = require("./../../../data/record/schema/field-impl");
var record_impl_1 = require("./../../../data/record/schema/record-impl");
var schema_impl_1 = require("./../../../data/record/schema/schema-impl");
var require_non_null_1 = require("./../../../util/require-non-null");
/**
* Check if `min` and `max` values specify a valid multiplicity
*
* @param name the name of the Field or Record to check
* @param min the minimum number of times the field should occur within the record
* @param max the maximum number of times the field should occur within the record
* @throws a {@link SchemaViolationError} if `min` and `max` values specify an invalid multiplicity
*/
function checkMultiplicity(name, min, max) {
if (min < 0) {
throw new errors_1.SchemaViolationError("'" + name + "' has invalid 'min' value '" + min + "'");
}
if (max !== -1 && (max < 1 || max < min)) {
throw new errors_1.SchemaViolationError("'" + name + "' has invalid 'max' value '" + max + "'");
}
}
/**
* Check if `scale` values specify a valid multiplicity
*
* @param name the name of the Field
* @param scale the scale of the field (number of decimal places). This must be positive.
* @throws a {@link SchemaViolationError} if `scale<1`
*/
function checkScale(name, scale) {
if (scale < 1) {
throw new errors_1.SchemaViolationError("'" + name + "' has invalid 'scale' value '" + scale + "'");
}
}
/**
* Implementation of the {@link SchemaBuilder} interface
*/
var SchemaBuilderImpl = /** @class */ (function () {
/**
* Create a SchemaBuilderImpl
*
* @param recordV2Constructor a constructor function for creating {@link RecordV2} implementations
*/
function SchemaBuilderImpl(recordV2Constructor) {
/**
* The records that have been added to the schema
*/
this.records = [];
this.recordV2Constructor = recordV2Constructor;
}
/**
* @inheritdoc
*/
SchemaBuilderImpl.prototype.record = function (name, min, max) {
if (min === void 0) { min = 1; }
if (max === void 0) { max = min; }
checkMultiplicity(name, min, max);
this.checkRecord(name);
if (this.currentFields !== undefined) {
this.addRecord();
}
this.currentName = name;
this.currentMin = min;
this.currentMax = max;
this.currentFields = [];
return this;
};
/**
* @inheritdoc
*/
SchemaBuilderImpl.prototype.string = function (name, min, max) {
if (min === void 0) { min = 1; }
if (max === void 0) { max = min; }
checkMultiplicity(name, min, max);
this.checkField(name);
this.currentFields.push(new field_impl_1.FieldImpl(name, field_impl_1.Type.STRING, min, max, this.fieldIndex()));
return this;
};
/**
* @inheritdoc
*/
SchemaBuilderImpl.prototype.integer = function (name, min, max) {
if (min === void 0) { min = 1; }
if (max === void 0) { max = min; }
checkMultiplicity(name, min, max);
this.checkField(name);
this.currentFields.push(new field_impl_1.FieldImpl(name, field_impl_1.Type.INTEGER, min, max, this.fieldIndex()));
return this;
};
/**
* @inheritdoc
*/
SchemaBuilderImpl.prototype.decimal = function (name, scale, min, max) {
if (min === void 0) { min = 1; }
if (max === void 0) { max = min; }
checkMultiplicity(name, min, max);
checkScale(name, scale);
this.checkField(name);
this.currentFields.push(new field_impl_1.FieldImpl(name, field_impl_1.Type.DECIMAL, min, max, this.fieldIndex(), scale));
return this;
};
/**
* @inheritdoc
*/
SchemaBuilderImpl.prototype.build = function () {
if (this.currentFields !== undefined) {
this.addRecord();
}
if (this.records.length === 0) {
throw new errors_1.SchemaViolationError('Schema has no records');
}
return new schema_impl_1.SchemaImpl(this.recordV2Constructor, this.records);
};
/**
* Get the index of the next field to be added
*
* @return the current field index plus the current multiplicity
*/
SchemaBuilderImpl.prototype.fieldIndex = function () {
var currentFields = this.currentFields;
if (currentFields.length === 0) {
return 0;
}
var lastField = currentFields[currentFields.length - 1];
return lastField.index + lastField.max;
};
/**
* Add the current record to the {@link SchemaBuilderImpl.records records} array
*
* @throws a {@link SchemaViolationError} if the current record contains no fields
*/
SchemaBuilderImpl.prototype.addRecord = function () {
var currentFields = this.currentFields;
if (currentFields.length === 0) {
throw new errors_1.SchemaViolationError("No fields specified for record: '" + this.currentName + "'");
}
var index;
if (this.records.length === 0) {
index = 0;
}
else {
var lastRecord = this.records[this.records.length - 1];
index = lastRecord.index + lastRecord.max;
}
this.records.push(new record_impl_1.RecordImpl(this.currentName, this.currentMin, this.currentMax, index, currentFields));
};
/**
* Check for duplicate record names or a record after a variable multiplicity record
*
* @param name the record name
* @throws a {@link SchemaViolationError} if a record with that name cannot be added
*/
SchemaBuilderImpl.prototype.checkRecord = function (name) {
var e_1, _a;
require_non_null_1.requireNonNull(name, 'name');
if (name === this.currentName) {
throw new errors_1.SchemaViolationError("Duplicate record name: '" + name + "'");
}
if (this.records.length) {
try {
for (var _b = __values(this.records), _c = _b.next(); !_c.done; _c = _b.next()) {
var record = _c.value;
if (name === record.name) {
throw new errors_1.SchemaViolationError("Duplicate record name: '" + name + "'");
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
}
if (this.currentFields !== undefined && this.currentMin !== this.currentMax) {
throw new errors_1.SchemaViolationError("Record '" + name + "' not allowed after variable multiplicity record: '" + this.currentName + "'");
}
};
/**
* Check for duplicate field names or a field after a variable multiplicity field
*
* @param name the field name
* @throws a {@link SchemaViolationError} if a field with that name cannot be added
*/
SchemaBuilderImpl.prototype.checkField = function (name) {
var e_2, _a;
require_non_null_1.requireNonNull(name, 'name');
if (this.currentFields === undefined) {
throw new errors_1.SchemaViolationError("Cannot add field '" + name + "' as there is no current record");
}
if (this.currentFields.length) {
try {
for (var _b = __values(this.currentFields), _c = _b.next(); !_c.done; _c = _b.next()) {
var field = _c.value;
if (name === field.name) {
throw new errors_1.SchemaViolationError("Duplicate field name: '" + name + "'");
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
var lastField = this.currentFields[this.currentFields.length - 1];
if (lastField.isVariable) {
throw new errors_1.SchemaViolationError("Field '" + name + "' is not allowed after variable multiplicity field '" + lastField.name + "'");
}
}
};
return SchemaBuilderImpl;
}());
exports.SchemaBuilderImpl = SchemaBuilderImpl;