@jsonjoy.com/json-type
Version:
High-performance JSON Pointer implementation
132 lines (131 loc) • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConstType = void 0;
const json_clone_1 = require("@jsonjoy.com/util/lib/json-clone");
const validate_1 = require("../../schema/validate");
const constants_1 = require("../../constants");
const json_size_1 = require("@jsonjoy.com/util/lib/json-size");
const AbstractType_1 = require("./AbstractType");
const deepEqualCodegen_1 = require("@jsonjoy.com/util/lib/json-equal/deepEqualCodegen");
class ConstType extends AbstractType_1.AbstractType {
constructor(schema) {
super();
this.schema = schema;
this.__json = JSON.stringify(schema.value);
}
value() {
return this.schema.value;
}
toJsonSchema(ctx) {
const schema = this.schema;
return {
type: typeof this.schema.value,
const: schema.value,
...super.toJsonSchema(ctx),
};
}
getOptions() {
const { kind, value, ...options } = this.schema;
return options;
}
validateSchema() {
(0, validate_1.validateTType)(this.getSchema(), 'const');
}
codegenValidator(ctx, path, r) {
const value = this.schema.value;
const equals = (0, deepEqualCodegen_1.deepEqualCodegen)(value);
const fn = ctx.codegen.addConstant(equals);
ctx.js(`if (!${fn}(${r})) return ${ctx.err(constants_1.ValidationError.CONST, path)}`);
ctx.emitCustomValidators(this, path, r);
}
codegenJsonTextEncoder(ctx, value) {
ctx.writeText(JSON.stringify(this.schema.value));
}
codegenBinaryEncoder(ctx, value) {
ctx.blob(ctx.gen((encoder) => {
encoder.writeAny(this.schema.value);
}));
}
codegenCborEncoder(ctx, value) {
this.codegenBinaryEncoder(ctx, value);
}
codegenMessagePackEncoder(ctx, value) {
this.codegenBinaryEncoder(ctx, value);
}
codegenJsonEncoder(ctx, value) {
this.codegenBinaryEncoder(ctx, value);
}
codegenCapacityEstimator(ctx, value) {
ctx.inc((0, json_size_1.maxEncodingCapacity)(this.value()));
}
random() {
return (0, json_clone_1.cloneBinary)(this.schema.value);
}
toTypeScriptAst() {
const value = this.schema.value;
if (value === null) {
const node = { node: 'NullKeyword' };
return node;
}
switch (typeof value) {
case 'string': {
const node = { node: 'StringLiteral', text: value };
return node;
}
case 'number': {
const node = { node: 'NumericLiteral', text: value.toString() };
return node;
}
case 'boolean': {
const node = { node: value ? 'TrueKeyword' : 'FalseKeyword' };
return node;
}
case 'object': {
const node = { node: 'ObjectKeyword' };
return node;
}
default: {
const node = { node: 'UnknownKeyword' };
return node;
}
}
}
toJson(value, system = this.system) {
return this.__json;
}
toString(tab = '') {
return `${super.toString(tab)} → ${JSON.stringify(this.schema.value)}`;
}
toJtdForm() {
const value = this.value();
const type = typeof value;
switch (type) {
case 'boolean':
case 'string':
return { type };
case 'number': {
if (value !== Math.round(value))
return { type: 'float64' };
if (value >= 0) {
if (value <= 255)
return { type: 'uint8' };
if (value <= 65535)
return { type: 'uint16' };
if (value <= 4294967295)
return { type: 'uint32' };
}
else {
if (value >= -128)
return { type: 'int8' };
if (value >= -32768)
return { type: 'int16' };
if (value >= -2147483648)
return { type: 'int32' };
}
return { type: 'float64' };
}
}
return super.toJtdForm();
}
}
exports.ConstType = ConstType;