analytics-sdk-generator
Version:
Generates an SDK from an analytics descriptor file
128 lines • 4.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Types = __importStar(require("./Types"));
var TypeMapper = /** @class */ (function () {
function TypeMapper() {
}
TypeMapper.addStringFormat = function (key, format) {
Types.FormattedStringType.addFormat(key, format);
};
TypeMapper.toSpecificType = function (definition) {
if ('type' in definition) {
return this.toSimpleType(definition);
}
else if ('$ref' in definition) {
return Types.TypeReference.toSpecificType(definition);
}
else if ('oneOf' in definition) {
return this.toUnionType(definition);
}
else if ('const' in definition) {
return this.toConstantType(definition);
}
else {
throw new Error("Unknown type definition");
}
};
TypeMapper.toSimpleType = function (definition) {
if ('enum' in definition) {
return this.toEnumeratedSimpleType(definition);
}
else {
return this.toNonEnumeratedSimpleType(definition);
}
};
TypeMapper.toConstantType = function (definition) {
switch (definition['const'].constructor.name) {
case 'String':
return new Types.Constant(definition['const']).setType(new Types.StringType({}));
case 'Number':
return new Types.Constant(definition['const']).setType(new Types.NumberType({}));
case 'Object':
case 'Array':
default:
throw new Error("Unsupported Constant type");
}
};
TypeMapper.toObjectConstant = function (definition) {
};
TypeMapper.toNonEnumeratedSimpleType = function (definition) {
switch (definition.type) {
case 'number':
case 'integer':
return Types.NumberType.toSpecificType(definition);
case 'string':
return this.toAnyStringType(definition);
case 'boolean':
return Types.BooleanType.toSpecificType(definition);
case 'object':
return Types.ObjectType.toSpecificType(definition);
case 'array':
return Types.ArrayType.toSpecificType(definition);
}
};
TypeMapper.toEnumeratedSimpleType = function (definition) {
if (definition.enum.length == 0) {
throw new Error("Enums must have atleast one option");
}
else if (definition.enum.length == 1) {
var t = Types.Constant.toSpecificType(definition.enum[0]);
delete definition.enum;
t.setType(this.toNonEnumeratedSimpleType(definition));
return t;
}
else {
var t_1 = this.toNonEnumeratedSimpleType(definition);
return new Types.UnionType({
options: definition.enum.map(function (v) { return new Types.Constant(v).setType(t_1); })
});
}
};
TypeMapper.toUnionType = function (definition) {
var _this = this;
if (definition.oneOf.length == 0) {
throw new Error("Unions must have atleast one option");
}
else if (definition.oneOf.length == 1) {
return this.toSpecificType(definition.oneOf[0]);
}
else {
return new Types.UnionType({
options: definition.oneOf.map(function (o) { return _this.toSpecificType(o); })
});
}
};
TypeMapper.toAnyStringType = function (definition) {
if ('format' in definition) {
return this.toFormattedStringType(definition);
}
else {
return new Types.StringType(definition);
}
};
TypeMapper.toFormattedStringType = function (definition) {
return Types.FormattedStringType.toSpecificType(definition);
};
return TypeMapper;
}());
exports.default = TypeMapper;
//# sourceMappingURL=TypeMapper.js.map