@allgemein/schema-api
Version:
Library for schema api
204 lines • 7.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultPropertyRef = void 0;
const lodash_1 = require("lodash");
const AbstractRef_1 = require("../AbstractRef");
const IClassRef_1 = require("../../api/IClassRef");
const Constants_1 = require("../Constants");
const ClassRef_1 = require("../ClassRef");
const base_1 = require("@allgemein/base");
const AnnotationsHelper_1 = require("../AnnotationsHelper");
const RegistryFactory_1 = require("./RegistryFactory");
const IEntityRef_1 = require("../../api/IEntityRef");
class DefaultPropertyRef extends AbstractRef_1.AbstractRef {
constructor(options = {}) {
super(Constants_1.METATYPE_PROPERTY, options.propertyName, options.target, options.namespace ? options.namespace : Constants_1.DEFAULT_NAMESPACE);
this.cardinality = 1;
this.reference = undefined;
AnnotationsHelper_1.AnnotationsHelper.merge(this.object, options, this.name);
if (options.type === Constants_1.T_ARRAY) {
// if array reset
options.type = Constants_1.T_OBJECT;
options.cardinality = 0;
options.array = true;
}
this.setOptions(options);
this.cardinality = (0, lodash_1.has)(options, Constants_1.C_CARDINALITY) ? options.cardinality : 1;
}
getRegistry() {
return RegistryFactory_1.RegistryFactory.get(this.namespace);
}
/**
* TODO
*/
convert(data, options) {
const sourceType = this.getType();
if (!sourceType || !(0, lodash_1.isString)(sourceType)) {
return data;
}
const jsType = sourceType.toLowerCase();
const [baseType, variant] = jsType.split(':');
switch (baseType) {
case 'datetime':
case 'timestamp':
case 'date':
return new Date(data);
case 'time':
case 'text':
case Constants_1.T_STRING:
if ((0, lodash_1.isString)(data)) {
return data;
}
else if ((0, lodash_1.isArray)(data) && data.length === 1) {
return data[0];
}
else if (data) {
return JSON.stringify(data);
}
else {
return null;
}
break;
case 'boolean':
if ((0, lodash_1.isBoolean)(data)) {
return data;
}
else if ((0, lodash_1.isNumber)(data)) {
return data > 0;
}
else if ((0, lodash_1.isString)(data)) {
if (data.toLowerCase() === 'true' || data.toLowerCase() === '1') {
return true;
}
return false;
}
break;
case 'double':
case 'number':
if ((0, lodash_1.isString)(data)) {
if (/^\d+\.|\,\d+$/.test(data)) {
return parseFloat(data.replace(',', '.'));
}
else if (/^\d+$/.test(data)) {
return parseInt(data, 0);
}
else {
}
}
else if ((0, lodash_1.isNumber)(data)) {
return data;
}
else if ((0, lodash_1.isBoolean)(data)) {
return data ? 1 : 0;
}
else {
// Pass to exception
}
break;
case 'byte':
case 'json':
case Constants_1.T_OBJECT:
case Constants_1.T_ARRAY:
return data;
}
throw new base_1.NotYetImplementedError('value "' + data + '" of type ' + (typeof data) + ' column type=' + jsType);
}
get(instance) {
return (0, lodash_1.get)(instance, this.name, null);
}
getTargetRef() {
this.checkReference();
return this.targetRef;
}
getType() {
return this.getOptions(Constants_1.C_TYPE);
}
isAppended() {
return this.getOptions(Constants_1.C_APPENDED, false);
}
/**
* TODO
*/
id() {
return [this.getClassRef().id(), this.name].join(Constants_1.XS_ID_SEPARATOR);
}
/**
* check if property parameter is an array
*/
isCollection() {
if ((0, lodash_1.isObjectLike)(this.cardinality) &&
this.cardinality.min >= 0 &&
this.cardinality.max >= 0) {
return true;
}
return this.cardinality !== 1;
}
/**
* Return if is identifier
*/
isIdentifier() {
return this.getOptions(Constants_1.C_IDENTIFIER, false);
}
/**
* Return if is a pattern property
*/
isPattern() {
return this.getOptions(Constants_1.K_PATTERN_PROPERTY, false);
}
/**
* Returns info if property parameter results are in classspace
*/
isReference() {
this.checkReference();
return this.reference;
}
/**
* Check if property is a to an other object referring property.
*
* Note:
* types defined as string, must match the following conditions to be interpreted as reference to an other object:
* - not contain any upper case
* - not a defined supported type
*
* @private
*/
checkReference() {
if ((0, lodash_1.isUndefined)(this.reference)) {
this.reference = false;
const type = this.getType();
if (!(0, lodash_1.isString)(type) && ((0, IClassRef_1.isClassRef)(type) || (0, IEntityRef_1.isEntityRef)(type))) {
this.targetRef = (0, IEntityRef_1.isEntityRef)(type) ? type.getClassRef() : type;
this.reference = true;
}
else if ((0, lodash_1.isFunction)(type) || this.isReferencingType(type)) {
// try get existing class
const exists = ClassRef_1.ClassRef.get(type, this.getClassRef().getNamespace(), { checkNamespace: true });
if (exists) {
this.targetRef = exists;
this.reference = true;
}
}
}
return this.reference;
}
/**
* Check type given as string if it is an object type.
*
* Note:
* types defined as string, must match the following conditions to be interpreted as reference to an other object:
* - not contain any upper case
* - not a defined supported type
*
* @private
*/
isReferencingType(type) {
return ((0, lodash_1.isString)(type) &&
(type.toLocaleLowerCase() !== type
&& !this.getSupportedDataTypes().find(t => (new RegExp('^' + t + ':?')).test(type.toLowerCase()))));
}
getClassRefFor(object, type) {
return this.getRegistry().getClassRefFor(object, this.metaType);
}
}
exports.DefaultPropertyRef = DefaultPropertyRef;
//# sourceMappingURL=DefaultPropertyRef.js.map