Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JTD: generate parsers/serializers #1454

Merged
merged 20 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
JTD: serialize (all tests pass)
  • Loading branch information
epoberezkin committed Feb 21, 2021
commit af534f48e1fbc3727c15f8433b1d54d0bfe6e84d
122 changes: 52 additions & 70 deletions lib/compile/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type Ajv from "../core"
import type {SchemaObject} from "../types"
import {SchemaEnv, getCompilingSchema} from "."
import {_, str, and, getProperty, stringify, CodeGen, Code, Name} from "./codegen"
import {_Code} from "./codegen/code"
import {_, str, and, getProperty, CodeGen, Code, Name} from "./codegen"
import {MissingRefError} from "./error_classes"
import N from "./names"
import {isOwnProperty} from "../vocabularies/code"
import {hasRef} from "../vocabularies/jtd/ref"
import quote from "../runtime/quote"

type SchemaObjectMap = {[Ref in string]?: SchemaObject}

Expand Down Expand Up @@ -129,27 +129,30 @@ function serializeValues(cxt: SerializeCxt): void {
const {gen, schema, jsonStr, data} = cxt
gen.add(jsonStr, str`{`)
const first = gen.let("first", true)
gen.forIn("key", data, (key) => {
addComma(cxt, first)
serializeString({...cxt, data: key})
gen.add(jsonStr, str`:`)
const value = gen.const("value", _`${data}${getProperty(key)}`)
serializeCode({...cxt, schema: schema.values, data: value})
})
gen.forIn("key", data, (key) => serializeKeyValue(cxt, key, schema.values, first))
gen.add(jsonStr, str`}`)
}

function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first: Name): void {
const {gen, jsonStr, data} = cxt
addComma(cxt, first)
serializeString({...cxt, data: key})
gen.add(jsonStr, str`:`)
const value = gen.const("value", _`${data}${getProperty(key)}`)
serializeCode({...cxt, schema, data: value})
}

function serializeDiscriminator(cxt: SerializeCxt): void {
const {gen, schema, jsonStr, data} = cxt
const {discriminator} = schema
gen.add(jsonStr, str`{${JSON.stringify(discriminator)}:`)
const tag = gen.const("tag", _`${data}${getProperty(discriminator)}`)
serializeString({...cxt, data: tag})
const first = gen.let("first", false)
gen.if(false)
for (const tagValue in schema.mapping) {
gen.elseIf(_`${tag} === ${tagValue}`)
serializeSchemaProperties({...cxt, schema: schema.mapping[tagValue]}, first)
const sch = schema.mapping[tagValue]
serializeSchemaProperties({...cxt, schema: sch}, discriminator)
}
gen.endIf()
gen.add(jsonStr, str`}`)
Expand All @@ -158,32 +161,59 @@ function serializeDiscriminator(cxt: SerializeCxt): void {
function serializeProperties(cxt: SerializeCxt): void {
const {gen, jsonStr} = cxt
gen.add(jsonStr, str`{`)
const first = gen.let("first", true)
serializeSchemaProperties(cxt, first)
serializeSchemaProperties(cxt)
gen.add(jsonStr, str`}`)
}

function serializeSchemaProperties(cxt: SerializeCxt, first: Name): void {
function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): void {
const {gen, schema, jsonStr, data} = cxt
const {properties, optionalProperties} = schema
for (const key in properties || {}) {
serializeProperty(key, keyValue(key))
const props = keys(properties)
const optProps = keys(optionalProperties)
const allProps = allProperties(props.concat(optProps))
let first = !discriminator
for (const key of props) {
serializeProperty(key, properties[key], keyValue(key))
}
for (const key in optionalProperties || {}) {
for (const key of optProps) {
const value = keyValue(key)
gen.if(and(_`${value} !== undefined`, isOwnProperty(gen, data, key)), () =>
serializeProperty(key, value)
serializeProperty(key, optionalProperties[key], value)
)
}
if (schema.additionalProperties) {
gen.forIn("key", data, (key) =>
gen.if(isAdditional(key, allProps), () =>
serializeKeyValue(cxt, key, {}, gen.let("first", first))
)
)
}

function keys(ps?: SchemaObjectMap): string[] {
return ps ? Object.keys(ps) : []
}

function allProperties(ps: string[]): string[] {
if (discriminator) ps.push(discriminator)
if (new Set(ps).size !== ps.length) {
throw new Error("JTD: properties/optionalProperties/disciminator overlap")
}
return ps
}

function keyValue(key: string): Name {
return gen.const("value", _`${data}${getProperty(key)}`)
}

function serializeProperty(key: string, value: Name): void {
addComma(cxt, first)
function serializeProperty(key: string, propSchema: SchemaObject, value: Name): void {
if (first) first = false
else gen.add(jsonStr, str`,`)
gen.add(jsonStr, str`${JSON.stringify(key)}:`)
serializeCode({...cxt, data: value})
serializeCode({...cxt, schema: propSchema, data: value})
}

function isAdditional(key: Name, ps: string[]): Code | true {
return ps.length ? and(...ps.map((p) => _`${key} !== ${p}`)) : true
}
}

Expand Down Expand Up @@ -245,57 +275,9 @@ function addComma({gen, jsonStr}: SerializeCxt, first: Name): void {
)
}

// eslint-disable-next-line no-control-regex, no-misleading-character-class
const rxEscapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g

const rxEscapableRx = /rxEscapable/g

const escaped: {[K in string]?: string} = {
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
'"': '\\"',
"\\": "\\\\",
}

const escapedRx = /escapedRx/g

function quote(s: string): string {
rxEscapable.lastIndex = 0
return (
'"' +
(rxEscapable.test(s)
? s.replace(rxEscapable, (a) => {
const c = escaped[a]
return typeof c === "string"
? c
: "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4)
})
: s) +
'"'
)
}

function quoteFunc(gen: CodeGen): Name {
// const quoteName = gen.getScopeValue("func", quote)
// if (quoteName) return quoteName
const escapedName = gen.scopeValue("obj", {
ref: escaped,
code: stringify(escaped),
})
const rxEscapableName = gen.scopeValue("obj", {
ref: rxEscapable,
code: new _Code(rxEscapable.toString()),
})
return gen.scopeValue("func", {
ref: quote,
code: new _Code(
quote
.toString()
.replace(rxEscapableRx, rxEscapableName.toString())
.replace(escapedRx, escapedName.toString())
),
code: _`require("ajv/dist/runtime/quote").default`,
})
}
28 changes: 28 additions & 0 deletions lib/runtime/quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// eslint-disable-next-line no-control-regex, no-misleading-character-class
const rxEscapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g

const escaped: {[K in string]?: string} = {
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
'"': '\\"',
"\\": "\\\\",
}

export default function quote(s: string): string {
rxEscapable.lastIndex = 0
return (
'"' +
(rxEscapable.test(s)
? s.replace(rxEscapable, (a) => {
const c = escaped[a]
return typeof c === "string"
? c
: "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4)
})
: s) +
'"'
)
}
4 changes: 2 additions & 2 deletions spec/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("JSON Type Definition", () => {
}
})

describe.skip("serialize", () => {
describe("serialize", () => {
const ajv = new _AjvJTD()

for (const testName in jtdValidationTests) {
Expand All @@ -104,7 +104,7 @@ describe("JSON Type Definition", () => {
describeOnly(testName, () =>
it(`should serialize data`, () => {
const serialize = ajv.compileSerializer(schema)
console.log(serialize.toString())
// console.log(serialize.toString())
assert.deepStrictEqual(JSON.parse(serialize(instance)), instance)
// const opts = ajv instanceof AjvPack ? ajv.ajv.opts : ajv.opts
})
Expand Down