UNPKG

@jsonjoy.com/json-type

Version:

High-performance JSON Pointer implementation

109 lines (108 loc) 4.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RefType = void 0; const tslib_1 = require("tslib"); const schema = tslib_1.__importStar(require("../../schema")); const constants_1 = require("../../constants"); const CborEncoderCodegenContext_1 = require("../../codegen/binary/CborEncoderCodegenContext"); const JsonEncoderCodegenContext_1 = require("../../codegen/binary/JsonEncoderCodegenContext"); const MessagePackEncoderCodegenContext_1 = require("../../codegen/binary/MessagePackEncoderCodegenContext"); const AbsType_1 = require("./AbsType"); class RefType extends AbsType_1.AbsType { constructor(ref) { super(); this.schema = schema.s.Ref(ref); } getRef() { return this.schema.ref; } getOptions() { const { kind, ref, ...options } = this.schema; return options; } codegenValidator(ctx, path, r) { const refErr = (errorRegister) => { switch (ctx.options.errors) { case 'boolean': return errorRegister; case 'string': { return ctx.err(constants_1.ValidationError.REF, [...path, { r: errorRegister }]); } // case 'object': default: { return ctx.err(constants_1.ValidationError.REF, [...path], { refId: this.schema.ref, refError: errorRegister, }); } } }; const system = ctx.options.system || this.system; if (!system) throw new Error('NO_SYSTEM'); const validator = system.resolve(this.schema.ref).type.validator(ctx.options.errors); const d = ctx.codegen.linkDependency(validator); const rerr = ctx.codegen.getRegister(); ctx.js(/* js */ `var ${rerr} = ${d}(${r});`); ctx.js(/* js */ `if (${rerr}) return ${refErr(rerr)};`); } codegenJsonTextEncoder(ctx, value) { const system = ctx.options.system || this.system; if (!system) throw new Error('NO_SYSTEM'); const encoder = system.resolve(this.schema.ref).type.jsonTextEncoder(); const d = ctx.codegen.linkDependency(encoder); ctx.js(/* js */ `s += ${d}(${value.use()});`); } codegenBinaryEncoder(ctx, value) { const system = ctx.options.system || this.system; if (!system) throw new Error('NO_SYSTEM'); const kind = ctx instanceof CborEncoderCodegenContext_1.CborEncoderCodegenContext ? 0 /* EncodingFormat.Cbor */ : ctx instanceof MessagePackEncoderCodegenContext_1.MessagePackEncoderCodegenContext ? 1 /* EncodingFormat.MsgPack */ : 2 /* EncodingFormat.Json */; const targetType = system.resolve(this.schema.ref).type; switch (targetType.getTypeName()) { case 'str': case 'bool': case 'num': case 'any': case 'tup': { if (ctx instanceof CborEncoderCodegenContext_1.CborEncoderCodegenContext) targetType.codegenCborEncoder(ctx, value); else if (ctx instanceof MessagePackEncoderCodegenContext_1.MessagePackEncoderCodegenContext) targetType.codegenMessagePackEncoder(ctx, value); else if (ctx instanceof JsonEncoderCodegenContext_1.JsonEncoderCodegenContext) targetType.codegenJsonEncoder(ctx, value); break; } default: { const encoder = targetType.encoder(kind); const d = ctx.codegen.linkDependency(encoder); ctx.js(/* js */ `${d}(${value.use()}, encoder);`); } } } codegenCborEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenMessagePackEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenJsonEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } toJson(value, system = this.system) { if (!system) return 'null'; const alias = system.resolve(this.schema.ref); return alias.type.toJson(value, system); } toStringTitle(tab = '') { const options = this.toStringOptions(); return `${super.toStringTitle()} → [${this.schema.ref}]` + (options ? ` ${options}` : ''); } } exports.RefType = RefType;