@jtdc/jtd-dialect
Version:
JTD dialect for JTDc.
154 lines (148 loc) • 14.9 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var codegen = require('@smikhalevski/codegen');
var changeCaseAll = require('change-case-all');
var JSON_POINTER_SEPARATOR = '/';
function toJsonPointer(str) {
return JSON_POINTER_SEPARATOR + str.toString().replace(/~/g, '~0').replace(/\//g, '~1');
}
var _a;
/**
* Creates a validator compilation dialect that renders validators which follow the JTD specification.
*/
function createJtdDialect(options) {
var opt = tslib.__assign(tslib.__assign({}, jtdDialectOptions), options);
var renameValidator = opt.renameValidator, renamePropertyKey = opt.renamePropertyKey, renameDiscriminatorKey = opt.renameDiscriminatorKey, rewriteEnumValue = opt.rewriteEnumValue, rewriteMappingKey = opt.rewriteMappingKey, renameTypeGuard = opt.renameTypeGuard, renameType = opt.renameType;
return {
import: function () {
return codegen.template(templateObject_1 || (templateObject_1 = tslib.__makeTemplateObject(["import{_S,_P,_K,_R,_o,_a,_e,_b,_s,_n,_i,_N,_O,Validator as _Validator}from\"@jtdc/jtd-dialect/lib/runtime\";"], ["import{_S,_P,_K,_R,_o,_a,_e,_b,_s,_n,_i,_N,_O,Validator as _Validator}from\"@jtdc/jtd-dialect/lib/runtime\";"])));
},
typeGuard: function (ref, node) {
var name = renameTypeGuard(ref, node);
return codegen.template(codegen.template(templateObject_2 || (templateObject_2 = tslib.__makeTemplateObject(["const ", "=(value:unknown):value is ", "=>!", "(value,{shallow:true});"], ["const ", "=(value:unknown):value is ", "=>!", "(value,{shallow:true});"])), name, renameType(ref, node), renameValidator(ref, node)), codegen.template(templateObject_3 || (templateObject_3 = tslib.__makeTemplateObject(["export{", "};"], ["export{", "};"])), name));
},
validator: function (ref, node, next) {
var valueVar = codegen.template.var();
var ctxVar = codegen.template.var();
var pointerVar = codegen.template.var();
var cacheVar = codegen.template.var();
var name = renameValidator(ref, node);
var cacheSize = 0;
var bodyFrag = codegen.template(codegen.template.assignment(ctxVar, codegen.template(templateObject_4 || (templateObject_4 = tslib.__makeTemplateObject(["", "||{}"], ["", "||{}"])), ctxVar), true), codegen.template.assignment(pointerVar, codegen.template(templateObject_5 || (templateObject_5 = tslib.__makeTemplateObject(["", "||\"\""], ["", "||\"\""])), pointerVar)), codegen.template.assignment(cacheVar, codegen.template(templateObject_6 || (templateObject_6 = tslib.__makeTemplateObject(["(", ".cache||={})"], ["(", ".cache||={})"])), name)), next({
warpCache: function (frag) { return codegen.template(templateObject_7 || (templateObject_7 = tslib.__makeTemplateObject(["", ".", "||=", ""], ["", ".", "||=", ""])), cacheVar, codegen.encodeLetters(cacheSize++), frag); },
valueVar: valueVar,
ctxVar: ctxVar,
pointerVar: pointerVar,
}), codegen.template(templateObject_8 || (templateObject_8 = tslib.__makeTemplateObject(["return ", ".errors;"], ["return ", ".errors;"])), ctxVar));
codegen.inlineVarAssignments(bodyFrag);
var undeclaredVars = codegen.collectVarRefs(bodyFrag, [valueVar, ctxVar, pointerVar]);
return codegen.template(codegen.template.block(templateObject_10 || (templateObject_10 = tslib.__makeTemplateObject(["const ", ":_Validator=(", ",", ",", ")=>{", "};"], ["const ", ":_Validator=(", ",", ",", ")=>{", "};"])), name, valueVar, ctxVar, pointerVar, codegen.template(undeclaredVars.length !== 0 && codegen.template(templateObject_9 || (templateObject_9 = tslib.__makeTemplateObject(["let ", ";"], ["let ", ";"])), codegen.joinFragmentChildren(undeclaredVars, ',')), bodyFrag)), codegen.template(templateObject_11 || (templateObject_11 = tslib.__makeTemplateObject(["export{", "};"], ["export{", "};"])), name));
},
ref: function (node, ctx) {
return codegen.template(templateObject_12 || (templateObject_12 = tslib.__makeTemplateObject(["", "(", ",", ",", ");"], ["", "(", ",", ",", ");"])), renameValidator(node.ref, node), ctx.valueVar, ctx.ctxVar, ctx.pointerVar);
},
nullable: function (node, ctx, next) {
if (isUnconstrainedNode(node.valueNode)) {
return codegen.template(templateObject_13 || (templateObject_13 = tslib.__makeTemplateObject([""], [""])));
}
return codegen.template.block(templateObject_14 || (templateObject_14 = tslib.__makeTemplateObject(["if(_N(", ")){", "}"], ["if(_N(", ")){", "}"])), ctx.valueVar, next(ctx));
},
type: function (node, ctx) {
var typeCheckerName = jtdTypeCheckerMap[node.type];
if (!typeCheckerName) {
throw new Error('Unknown type: ' + node.type);
}
return codegen.template(templateObject_15 || (templateObject_15 = tslib.__makeTemplateObject(["", "(", ",", ",", ");"], ["", "(", ",", ",", ");"])), typeCheckerName, ctx.valueVar, ctx.ctxVar, ctx.pointerVar);
},
enum: function (node, ctx) {
var valuesFrag = ctx.warpCache(codegen.template(templateObject_16 || (templateObject_16 = tslib.__makeTemplateObject(["[", "]"], ["[", "]"])), codegen.joinFragmentChildren(node.values.map(function (value) { return JSON.stringify(rewriteEnumValue(value, node)); }), ',')));
return codegen.template(templateObject_17 || (templateObject_17 = tslib.__makeTemplateObject(["_e(", ",", ",", ",", ");"], ["_e(", ",", ",", ",", ");"])), ctx.valueVar, valuesFrag, ctx.ctxVar, ctx.pointerVar);
},
elements: function (node, ctx, next) {
if (isUnconstrainedNode(node.elementNode)) {
return codegen.template(templateObject_18 || (templateObject_18 = tslib.__makeTemplateObject(["_a(", ",", ",", ");"], ["_a(", ",", ",", ");"])), ctx.valueVar, ctx.ctxVar, ctx.pointerVar);
}
var indexVar = codegen.template.var();
var valueVar = codegen.template.var();
var pointerVar = codegen.template.var();
return codegen.template.block(templateObject_22 || (templateObject_22 = tslib.__makeTemplateObject(["if(_a(", ",", ",", ")){", "}"], ["if(_a(", ",", ",", ")){", "}"])), ctx.valueVar, ctx.ctxVar, ctx.pointerVar, codegen.template.block(templateObject_21 || (templateObject_21 = tslib.__makeTemplateObject(["for(", "=0;", "<", ".length;", "++){", "}"], ["for(", "=0;", "<", ".length;", "++){", "}"])), indexVar, indexVar, ctx.valueVar, indexVar, codegen.template(codegen.template.assignment(valueVar, codegen.template(templateObject_19 || (templateObject_19 = tslib.__makeTemplateObject(["", "[", "]"], ["", "[", "]"])), ctx.valueVar, indexVar)), codegen.template.assignment(pointerVar, codegen.template(templateObject_20 || (templateObject_20 = tslib.__makeTemplateObject(["", "+_S+", ""], ["", "+_S+", ""])), ctx.pointerVar, indexVar)), next(tslib.__assign(tslib.__assign({}, ctx), { pointerVar: pointerVar, valueVar: valueVar })))));
},
values: function (node, ctx, next) {
if (isUnconstrainedNode(node.valueNode)) {
return codegen.template(templateObject_23 || (templateObject_23 = tslib.__makeTemplateObject(["_o(", ",", ",", ");"], ["_o(", ",", ",", ");"])), ctx.valueVar, ctx.ctxVar, ctx.pointerVar);
}
var indexVar = codegen.template.var();
var keysVar = codegen.template.var();
var valueVar = codegen.template.var();
var pointerVar = codegen.template.var();
return codegen.template.block(templateObject_27 || (templateObject_27 = tslib.__makeTemplateObject(["if(_o(", ",", ",", ")){", "}"], ["if(_o(", ",", ",", ")){", "}"])), ctx.valueVar, ctx.ctxVar, ctx.pointerVar, codegen.template.block(templateObject_26 || (templateObject_26 = tslib.__makeTemplateObject(["for(", "=0,", "=_K(", ");", "<", ".length;", "++){", "}"], ["for(", "=0,", "=_K(", ");", "<", ".length;", "++){", "}"])), indexVar, keysVar, ctx.valueVar, indexVar, keysVar, indexVar, codegen.template(codegen.template.assignment(valueVar, codegen.template(templateObject_24 || (templateObject_24 = tslib.__makeTemplateObject(["", "[", "[", "]]"], ["", "[", "[", "]]"])), ctx.valueVar, keysVar, indexVar)), codegen.template.assignment(pointerVar, codegen.template(templateObject_25 || (templateObject_25 = tslib.__makeTemplateObject(["", "+_P(", "[", "])"], ["", "+_P(", "[", "])"])), ctx.pointerVar, keysVar, indexVar)), next(tslib.__assign(tslib.__assign({}, ctx), { valueVar: valueVar, pointerVar: pointerVar })))));
},
object: function (node, ctx, next) {
if (Object.values(node.properties).every(isUnconstrainedNode) && Object.values(node.optionalProperties).every(isUnconstrainedNode)) {
return codegen.template(templateObject_28 || (templateObject_28 = tslib.__makeTemplateObject(["_o(", ",", ",", ")"], ["_o(", ",", ",", ")"])), ctx.valueVar, ctx.ctxVar, ctx.pointerVar);
}
return codegen.template.block(templateObject_29 || (templateObject_29 = tslib.__makeTemplateObject(["if(_o(", ",", ",", ")){", "}"], ["if(_o(", ",", ",", ")){", "}"])), ctx.valueVar, ctx.ctxVar, ctx.pointerVar, next(ctx));
},
property: function (propKey, propNode, objectNode, ctx, next) {
if (isUnconstrainedNode(propNode)) {
return codegen.template(templateObject_30 || (templateObject_30 = tslib.__makeTemplateObject([""], [""])));
}
propKey = renamePropertyKey(propKey, propNode, objectNode);
var valueVar = codegen.template.var();
var pointerVar = codegen.template.var();
return codegen.template.block(codegen.template.assignment(valueVar, codegen.template(templateObject_31 || (templateObject_31 = tslib.__makeTemplateObject(["", "", ""], ["", "", ""])), ctx.valueVar, codegen.compilePropertyAccessor(propKey))), codegen.template.assignment(pointerVar, codegen.template(templateObject_32 || (templateObject_32 = tslib.__makeTemplateObject(["", "+", ""], ["", "+", ""])), ctx.pointerVar, compileJsonPointer(propKey))), next(tslib.__assign(tslib.__assign({}, ctx), { valueVar: valueVar, pointerVar: pointerVar })));
},
optionalProperty: function (propKey, propNode, objectNode, ctx, next) {
if (isUnconstrainedNode(propNode)) {
return codegen.template(templateObject_33 || (templateObject_33 = tslib.__makeTemplateObject([""], [""])));
}
propKey = renamePropertyKey(propKey, propNode, objectNode);
var valueVar = codegen.template.var();
var pointerVar = codegen.template.var();
return codegen.template.block(codegen.template.assignment(valueVar, codegen.template(templateObject_34 || (templateObject_34 = tslib.__makeTemplateObject(["", "", ""], ["", "", ""])), ctx.valueVar, codegen.compilePropertyAccessor(propKey))), codegen.template.block(templateObject_36 || (templateObject_36 = tslib.__makeTemplateObject(["if(_O(", ")){", "}"], ["if(_O(", ")){", "}"])), valueVar, codegen.template(codegen.template.assignment(pointerVar, codegen.template(templateObject_35 || (templateObject_35 = tslib.__makeTemplateObject(["", "+", ""], ["", "+", ""])), ctx.pointerVar, compileJsonPointer(propKey))), next(tslib.__assign(tslib.__assign({}, ctx), { valueVar: valueVar, pointerVar: pointerVar })))));
},
union: function (node, ctx, next) {
var discriminatorKey = renameDiscriminatorKey(node);
return codegen.template.block(templateObject_38 || (templateObject_38 = tslib.__makeTemplateObject(["if(_o(", ",", ",", ")){", "}"], ["if(_o(", ",", ",", ")){", "}"])), ctx.valueVar, ctx.ctxVar, ctx.pointerVar, codegen.template(codegen.template(templateObject_37 || (templateObject_37 = tslib.__makeTemplateObject(["switch(", "", "){", "}_R(", ",", "+", ")"], ["switch(", "", "){", "}_R(", ",", "+", ")"])), ctx.valueVar, codegen.compilePropertyAccessor(discriminatorKey), codegen.template(next(ctx)), ctx.ctxVar, ctx.pointerVar, compileJsonPointer(discriminatorKey))));
},
mapping: function (mappingKey, mappingNode, unionNode, ctx, next) {
return codegen.template.block(codegen.template(templateObject_39 || (templateObject_39 = tslib.__makeTemplateObject(["case ", ":"], ["case ", ":"])), JSON.stringify(rewriteMappingKey(mappingKey, mappingNode, undefined, unionNode))), next(ctx), 'break;');
},
};
}
function compileJsonPointer(key) {
return JSON.stringify(toJsonPointer(key));
}
function isUnconstrainedNode(node) {
return node.nodeType === 0 /* ANY */ || node.nodeType === 2 /* NULLABLE */ && isUnconstrainedNode(node.valueNode);
}
var jtdTypeCheckerMap = (_a = {},
_a["boolean" /* BOOLEAN */] = '_b',
_a["string" /* STRING */] = '_s',
_a["timestamp" /* TIMESTAMP */] = '_s',
_a["float32" /* FLOAT32 */] = '_n',
_a["float64" /* FLOAT64 */] = '_n',
_a["int8" /* INT8 */] = '_i',
_a["uint8" /* UINT8 */] = '_i',
_a["int16" /* INT16 */] = '_i',
_a["uint16" /* UINT16 */] = '_i',
_a["int32" /* INT32 */] = '_i',
_a["uint32" /* UINT32 */] = '_i',
_a);
/**
* Global default options used by {@link createJtdDialect}.
*/
var jtdDialectOptions = {
renameValidator: function (ref) { return 'validate' + changeCaseAll.pascalCase(ref); },
renamePropertyKey: function (propKey) { return propKey; },
renameDiscriminatorKey: function (node) { return node.discriminator; },
rewriteEnumValue: function (value) { return value; },
rewriteMappingKey: function (mappingKey) { return mappingKey; },
renameTypeGuard: function (ref) { return 'is' + changeCaseAll.pascalCase(ref); },
renameType: function (ref) { return changeCaseAll.pascalCase(ref); },
};
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13, templateObject_14, templateObject_15, templateObject_16, templateObject_17, templateObject_18, templateObject_19, templateObject_20, templateObject_21, templateObject_22, templateObject_23, templateObject_24, templateObject_25, templateObject_26, templateObject_27, templateObject_28, templateObject_29, templateObject_30, templateObject_31, templateObject_32, templateObject_33, templateObject_34, templateObject_35, templateObject_36, templateObject_37, templateObject_38, templateObject_39;
exports.createJtdDialect = createJtdDialect;
exports.dialectFactory = createJtdDialect;
exports.jtdDialectOptions = jtdDialectOptions;
;