skir-codemirror-plugin 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +126 -0
- package/dist/codemirror/create_editor_state.d.ts +20 -0
- package/dist/codemirror/create_editor_state.d.ts.map +1 -0
- package/dist/codemirror/create_editor_state.js +252 -0
- package/dist/codemirror/create_editor_state.js.map +1 -0
- package/dist/codemirror/enter_key_handler.d.ts +8 -0
- package/dist/codemirror/enter_key_handler.d.ts.map +1 -0
- package/dist/codemirror/enter_key_handler.js +181 -0
- package/dist/codemirror/enter_key_handler.js.map +1 -0
- package/dist/codemirror/json_completion.d.ts +4 -0
- package/dist/codemirror/json_completion.d.ts.map +1 -0
- package/dist/codemirror/json_completion.js +150 -0
- package/dist/codemirror/json_completion.js.map +1 -0
- package/dist/codemirror/json_linter.d.ts +4 -0
- package/dist/codemirror/json_linter.d.ts.map +1 -0
- package/dist/codemirror/json_linter.js +277 -0
- package/dist/codemirror/json_linter.js.map +1 -0
- package/dist/codemirror/json_state.d.ts +16 -0
- package/dist/codemirror/json_state.d.ts.map +1 -0
- package/dist/codemirror/json_state.js +124 -0
- package/dist/codemirror/json_state.js.map +1 -0
- package/dist/codemirror/status_bar.d.ts +3 -0
- package/dist/codemirror/status_bar.d.ts.map +1 -0
- package/dist/codemirror/status_bar.js +123 -0
- package/dist/codemirror/status_bar.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/json/json_parser.d.ts +3 -0
- package/dist/json/json_parser.d.ts.map +1 -0
- package/dist/json/json_parser.js +414 -0
- package/dist/json/json_parser.js.map +1 -0
- package/dist/json/json_parser.test.d.ts +2 -0
- package/dist/json/json_parser.test.d.ts.map +1 -0
- package/dist/json/json_parser.test.js +337 -0
- package/dist/json/json_parser.test.js.map +1 -0
- package/dist/json/schema_validator.d.ts +3 -0
- package/dist/json/schema_validator.d.ts.map +1 -0
- package/dist/json/schema_validator.js +525 -0
- package/dist/json/schema_validator.js.map +1 -0
- package/dist/json/schema_validator.test.d.ts +2 -0
- package/dist/json/schema_validator.test.d.ts.map +1 -0
- package/dist/json/schema_validator.test.js +212 -0
- package/dist/json/schema_validator.test.js.map +1 -0
- package/dist/json/to_json.d.ts +6 -0
- package/dist/json/to_json.d.ts.map +1 -0
- package/dist/json/to_json.js +61 -0
- package/dist/json/to_json.js.map +1 -0
- package/dist/json/to_json.test.d.ts +2 -0
- package/dist/json/to_json.test.d.ts.map +1 -0
- package/dist/json/to_json.test.js +128 -0
- package/dist/json/to_json.test.js.map +1 -0
- package/dist/json/types.d.ts +170 -0
- package/dist/json/types.d.ts.map +1 -0
- package/dist/json/types.js +2 -0
- package/dist/json/types.js.map +1 -0
- package/package.json +85 -0
- package/src/codemirror/create_editor_state.ts +278 -0
- package/src/codemirror/enter_key_handler.ts +232 -0
- package/src/codemirror/json_completion.ts +182 -0
- package/src/codemirror/json_linter.ts +358 -0
- package/src/codemirror/json_state.ts +170 -0
- package/src/codemirror/status_bar.ts +137 -0
- package/src/index.ts +6 -0
- package/src/json/json_parser.test.ts +360 -0
- package/src/json/json_parser.ts +461 -0
- package/src/json/schema_validator.test.ts +230 -0
- package/src/json/schema_validator.ts +558 -0
- package/src/json/to_json.test.ts +150 -0
- package/src/json/to_json.ts +70 -0
- package/src/json/types.ts +254 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { expect } from "buckwheat";
|
|
2
|
+
import { describe, it } from "mocha";
|
|
3
|
+
import { parseJsonValue } from "./json_parser.js";
|
|
4
|
+
import { validateSchema } from "./schema_validator.js";
|
|
5
|
+
function parse(json) {
|
|
6
|
+
const result = parseJsonValue(json);
|
|
7
|
+
if (!result.value) {
|
|
8
|
+
throw new Error(`JSON parse error: ${JSON.stringify(result.errors)}`);
|
|
9
|
+
}
|
|
10
|
+
return result.value;
|
|
11
|
+
}
|
|
12
|
+
describe("schema_validator", () => {
|
|
13
|
+
it("validates primitive int32", () => {
|
|
14
|
+
const schema = {
|
|
15
|
+
type: { kind: "primitive", value: "int32" },
|
|
16
|
+
records: [],
|
|
17
|
+
};
|
|
18
|
+
const result = validateSchema(parse("123"), schema);
|
|
19
|
+
expect(result).toMatch({ errors: [] });
|
|
20
|
+
if (result.hints.length === 0) {
|
|
21
|
+
throw new Error("Expected type hints");
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
it("validates primitive string", () => {
|
|
25
|
+
const schema = {
|
|
26
|
+
type: { kind: "primitive", value: "string" },
|
|
27
|
+
records: [],
|
|
28
|
+
};
|
|
29
|
+
const result = validateSchema(parse('"hello"'), schema);
|
|
30
|
+
expect(result).toMatch({ errors: [] });
|
|
31
|
+
});
|
|
32
|
+
it("reports error for type mismatch (int32 vs string)", () => {
|
|
33
|
+
const schema = {
|
|
34
|
+
type: { kind: "primitive", value: "int32" },
|
|
35
|
+
records: [],
|
|
36
|
+
};
|
|
37
|
+
const result = validateSchema(parse('"hello"'), schema);
|
|
38
|
+
expect(result.errors).toMatch([
|
|
39
|
+
{
|
|
40
|
+
message: "Expected: int32",
|
|
41
|
+
},
|
|
42
|
+
]);
|
|
43
|
+
});
|
|
44
|
+
it("validates array of primitives", () => {
|
|
45
|
+
const schema = {
|
|
46
|
+
type: {
|
|
47
|
+
kind: "array",
|
|
48
|
+
value: { item: { kind: "primitive", value: "int32" } },
|
|
49
|
+
},
|
|
50
|
+
records: [],
|
|
51
|
+
};
|
|
52
|
+
const result = validateSchema(parse("[1, 2, 3]"), schema);
|
|
53
|
+
expect(result).toMatch({ errors: [] });
|
|
54
|
+
});
|
|
55
|
+
it("reports error for invalid array item", () => {
|
|
56
|
+
const schema = {
|
|
57
|
+
type: {
|
|
58
|
+
kind: "array",
|
|
59
|
+
value: { item: { kind: "primitive", value: "int32" } },
|
|
60
|
+
},
|
|
61
|
+
records: [],
|
|
62
|
+
};
|
|
63
|
+
const result = validateSchema(parse('[1, "bad", 3]'), schema);
|
|
64
|
+
expect(result.errors).toMatch([
|
|
65
|
+
{
|
|
66
|
+
message: "Expected: int32",
|
|
67
|
+
segment: {
|
|
68
|
+
start: 4,
|
|
69
|
+
end: 9,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
]);
|
|
73
|
+
});
|
|
74
|
+
it("validates optional field (present)", () => {
|
|
75
|
+
const schema = {
|
|
76
|
+
type: {
|
|
77
|
+
kind: "optional",
|
|
78
|
+
value: { kind: "primitive", value: "int32" },
|
|
79
|
+
},
|
|
80
|
+
records: [],
|
|
81
|
+
};
|
|
82
|
+
const result = validateSchema(parse("123"), schema);
|
|
83
|
+
expect(result).toMatch({ errors: [] });
|
|
84
|
+
});
|
|
85
|
+
it("validates optional field (null)", () => {
|
|
86
|
+
const schema = {
|
|
87
|
+
type: {
|
|
88
|
+
kind: "optional",
|
|
89
|
+
value: { kind: "primitive", value: "int32" },
|
|
90
|
+
},
|
|
91
|
+
records: [],
|
|
92
|
+
};
|
|
93
|
+
const result = validateSchema(parse("null"), schema);
|
|
94
|
+
expect(result).toMatch({ errors: [] });
|
|
95
|
+
});
|
|
96
|
+
it("validates struct", () => {
|
|
97
|
+
const schema = {
|
|
98
|
+
type: { kind: "record", value: "MyStruct" },
|
|
99
|
+
records: [
|
|
100
|
+
{
|
|
101
|
+
kind: "struct",
|
|
102
|
+
id: "MyStruct",
|
|
103
|
+
fields: [
|
|
104
|
+
{
|
|
105
|
+
name: "foo",
|
|
106
|
+
number: 1,
|
|
107
|
+
type: { kind: "primitive", value: "string" },
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "bar",
|
|
111
|
+
number: 2,
|
|
112
|
+
type: { kind: "primitive", value: "int32" },
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
};
|
|
118
|
+
const result = validateSchema(parse('{"foo": "hi", "bar": 123}'), schema);
|
|
119
|
+
expect(result).toMatch({ errors: [] });
|
|
120
|
+
});
|
|
121
|
+
it("reports unknown field in struct", () => {
|
|
122
|
+
const schema = {
|
|
123
|
+
type: { kind: "record", value: "MyStruct" },
|
|
124
|
+
records: [
|
|
125
|
+
{
|
|
126
|
+
kind: "struct",
|
|
127
|
+
id: "MyStruct",
|
|
128
|
+
fields: [],
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
};
|
|
132
|
+
const result = validateSchema(parse('{"unknown": 1}'), schema);
|
|
133
|
+
expect(result.errors).toMatch([
|
|
134
|
+
{
|
|
135
|
+
message: "Unknown field",
|
|
136
|
+
segment: {
|
|
137
|
+
start: 1,
|
|
138
|
+
end: 10,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
it("validates enum (string literal)", () => {
|
|
144
|
+
const schema = {
|
|
145
|
+
type: { kind: "record", value: "MyEnum" },
|
|
146
|
+
records: [
|
|
147
|
+
{
|
|
148
|
+
kind: "enum",
|
|
149
|
+
id: "MyEnum",
|
|
150
|
+
variants: [
|
|
151
|
+
{ name: "First", number: 1 },
|
|
152
|
+
{ name: "Second", number: 2 },
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
};
|
|
157
|
+
const result = validateSchema(parse('"First"'), schema);
|
|
158
|
+
expect(result).toMatch({ errors: [] });
|
|
159
|
+
});
|
|
160
|
+
it("reports unknown enum variant", () => {
|
|
161
|
+
const schema = {
|
|
162
|
+
type: { kind: "record", value: "MyEnum" },
|
|
163
|
+
records: [
|
|
164
|
+
{
|
|
165
|
+
kind: "enum",
|
|
166
|
+
id: "MyEnum",
|
|
167
|
+
variants: [{ name: "First", number: 1 }],
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
};
|
|
171
|
+
const result = validateSchema(parse('"Unknown"'), schema);
|
|
172
|
+
expect(result.errors).toMatch([
|
|
173
|
+
{
|
|
174
|
+
message: "Unknown variant",
|
|
175
|
+
},
|
|
176
|
+
]);
|
|
177
|
+
});
|
|
178
|
+
it("validates enum (object with kind)", () => {
|
|
179
|
+
const schema = {
|
|
180
|
+
type: { kind: "record", value: "MyEnum" },
|
|
181
|
+
records: [
|
|
182
|
+
{
|
|
183
|
+
kind: "enum",
|
|
184
|
+
id: "MyEnum",
|
|
185
|
+
variants: [
|
|
186
|
+
{
|
|
187
|
+
name: "complex",
|
|
188
|
+
number: 1,
|
|
189
|
+
type: { kind: "primitive", value: "int32" },
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
};
|
|
195
|
+
expect(validateSchema(parse('{"kind": "complex", "value": 123}'), schema)).toMatch({ errors: [] });
|
|
196
|
+
expect(validateSchema(parse('{"kind": "complex", "value": "foo"}'), schema)).toMatch({
|
|
197
|
+
errors: [
|
|
198
|
+
{
|
|
199
|
+
message: "Expected: int32",
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
});
|
|
203
|
+
expect(validateSchema(parse('{"kind": "complex"}'), schema)).toMatch({
|
|
204
|
+
errors: [
|
|
205
|
+
{
|
|
206
|
+
message: "Missing: 'value'",
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
//# sourceMappingURL=schema_validator.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema_validator.test.js","sourceRoot":"","sources":["../../src/json/schema_validator.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAGvD,SAAS,KAAK,CAAC,IAAY;IACzB,MAAM,MAAM,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,CAAC,MAAM,CA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AAE,KAAK,EAAE,OAAO,EAAE;yBAC5C;qBACF;iBACF;aACF;SACF,CAAC;QAEF,MAAM,CACJ,cAAc,CAAC,KAAK,CAAC,mCAAmC,CAAC,EAAE,MAAM,CAAC,CACnE,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;QAC1B,MAAM,CACJ,cAAc,CAAC,KAAK,CAAC,qCAAqC,CAAC,EAAE,MAAM,CAAC,CACrE,CAAC,OAAO,CAAC;YACR,MAAM,EAAE;gBACN;oBACE,OAAO,EAAE,iBAAiB;iBAC3B;aACF;SACF,CAAC,CAAC;QACH,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,qBAAqB,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;YACnE,MAAM,EAAE;gBACN;oBACE,OAAO,EAAE,kBAAkB;iBAC5B;aACF;SACF,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { expect } from \"buckwheat\";\nimport { describe, it } from \"mocha\";\nimport { parseJsonValue } from \"./json_parser.js\";\nimport { validateSchema } from \"./schema_validator.js\";\nimport { JsonValue, TypeDefinition } from \"./types.js\";\n\nfunction parse(json: string): JsonValue {\n const result = parseJsonValue(json);\n if (!result.value) {\n throw new Error(`JSON parse error: ${JSON.stringify(result.errors)}`);\n }\n return result.value;\n}\n\ndescribe(\"schema_validator\", () => {\n it(\"validates primitive int32\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"primitive\", value: \"int32\" },\n records: [],\n };\n const result = validateSchema(parse(\"123\"), schema);\n expect(result).toMatch({ errors: [] });\n if (result.hints.length === 0) {\n throw new Error(\"Expected type hints\");\n }\n });\n\n it(\"validates primitive string\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"primitive\", value: \"string\" },\n records: [],\n };\n const result = validateSchema(parse('\"hello\"'), schema);\n expect(result).toMatch({ errors: [] });\n });\n\n it(\"reports error for type mismatch (int32 vs string)\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"primitive\", value: \"int32\" },\n records: [],\n };\n const result = validateSchema(parse('\"hello\"'), schema);\n expect(result.errors).toMatch([\n {\n message: \"Expected: int32\",\n },\n ]);\n });\n\n it(\"validates array of primitives\", () => {\n const schema: TypeDefinition = {\n type: {\n kind: \"array\",\n value: { item: { kind: \"primitive\", value: \"int32\" } },\n },\n records: [],\n };\n const result = validateSchema(parse(\"[1, 2, 3]\"), schema);\n expect(result).toMatch({ errors: [] });\n });\n\n it(\"reports error for invalid array item\", () => {\n const schema: TypeDefinition = {\n type: {\n kind: \"array\",\n value: { item: { kind: \"primitive\", value: \"int32\" } },\n },\n records: [],\n };\n const result = validateSchema(parse('[1, \"bad\", 3]'), schema);\n expect(result.errors).toMatch([\n {\n message: \"Expected: int32\",\n segment: {\n start: 4,\n end: 9,\n },\n },\n ]);\n });\n\n it(\"validates optional field (present)\", () => {\n const schema: TypeDefinition = {\n type: {\n kind: \"optional\",\n value: { kind: \"primitive\", value: \"int32\" },\n },\n records: [],\n };\n const result = validateSchema(parse(\"123\"), schema);\n expect(result).toMatch({ errors: [] });\n });\n\n it(\"validates optional field (null)\", () => {\n const schema: TypeDefinition = {\n type: {\n kind: \"optional\",\n value: { kind: \"primitive\", value: \"int32\" },\n },\n records: [],\n };\n const result = validateSchema(parse(\"null\"), schema);\n expect(result).toMatch({ errors: [] });\n });\n\n it(\"validates struct\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"record\", value: \"MyStruct\" },\n records: [\n {\n kind: \"struct\",\n id: \"MyStruct\",\n fields: [\n {\n name: \"foo\",\n number: 1,\n type: { kind: \"primitive\", value: \"string\" },\n },\n {\n name: \"bar\",\n number: 2,\n type: { kind: \"primitive\", value: \"int32\" },\n },\n ],\n },\n ],\n };\n const result = validateSchema(parse('{\"foo\": \"hi\", \"bar\": 123}'), schema);\n expect(result).toMatch({ errors: [] });\n });\n\n it(\"reports unknown field in struct\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"record\", value: \"MyStruct\" },\n records: [\n {\n kind: \"struct\",\n id: \"MyStruct\",\n fields: [],\n },\n ],\n };\n const result = validateSchema(parse('{\"unknown\": 1}'), schema);\n expect(result.errors).toMatch([\n {\n message: \"Unknown field\",\n segment: {\n start: 1,\n end: 10,\n },\n },\n ]);\n });\n\n it(\"validates enum (string literal)\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"record\", value: \"MyEnum\" },\n records: [\n {\n kind: \"enum\",\n id: \"MyEnum\",\n variants: [\n { name: \"First\", number: 1 },\n { name: \"Second\", number: 2 },\n ],\n },\n ],\n };\n const result = validateSchema(parse('\"First\"'), schema);\n expect(result).toMatch({ errors: [] });\n });\n\n it(\"reports unknown enum variant\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"record\", value: \"MyEnum\" },\n records: [\n {\n kind: \"enum\",\n id: \"MyEnum\",\n variants: [{ name: \"First\", number: 1 }],\n },\n ],\n };\n const result = validateSchema(parse('\"Unknown\"'), schema);\n expect(result.errors).toMatch([\n {\n message: \"Unknown variant\",\n },\n ]);\n });\n\n it(\"validates enum (object with kind)\", () => {\n const schema: TypeDefinition = {\n type: { kind: \"record\", value: \"MyEnum\" },\n records: [\n {\n kind: \"enum\",\n id: \"MyEnum\",\n variants: [\n {\n name: \"complex\",\n number: 1,\n type: { kind: \"primitive\", value: \"int32\" },\n },\n ],\n },\n ],\n };\n\n expect(\n validateSchema(parse('{\"kind\": \"complex\", \"value\": 123}'), schema),\n ).toMatch({ errors: [] });\n expect(\n validateSchema(parse('{\"kind\": \"complex\", \"value\": \"foo\"}'), schema),\n ).toMatch({\n errors: [\n {\n message: \"Expected: int32\",\n },\n ],\n });\n expect(validateSchema(parse('{\"kind\": \"complex\"}'), schema)).toMatch({\n errors: [\n {\n message: \"Missing: 'value'\",\n },\n ],\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Json, JsonValue, RecordDefinition, TypeSignature } from "./types";
|
|
2
|
+
export declare function toJson(value: JsonValue): Json;
|
|
3
|
+
export declare function makeJsonTemplate(type: TypeSignature, idToRecordDef: {
|
|
4
|
+
[id: string]: RecordDefinition;
|
|
5
|
+
}, depth?: "depth"): Json;
|
|
6
|
+
//# sourceMappingURL=to_json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to_json.d.ts","sourceRoot":"","sources":["../../src/json/to_json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAEhF,wBAAgB,MAAM,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAiB7C;AAED,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,aAAa,EACnB,aAAa,EAAE;IAAE,CAAC,EAAE,EAAE,MAAM,GAAG,gBAAgB,CAAA;CAAE,EACjD,KAAK,CAAC,EAAE,OAAO,GACd,IAAI,CA4CN"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export function toJson(value) {
|
|
2
|
+
switch (value.kind) {
|
|
3
|
+
case "literal": {
|
|
4
|
+
return JSON.parse(value.jsonCode);
|
|
5
|
+
}
|
|
6
|
+
case "array": {
|
|
7
|
+
return value.values.map(toJson);
|
|
8
|
+
}
|
|
9
|
+
case "object": {
|
|
10
|
+
return Object.fromEntries(Object.values(value.keyValues).map((keyValue) => [
|
|
11
|
+
keyValue.key,
|
|
12
|
+
toJson(keyValue.value),
|
|
13
|
+
]));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function makeJsonTemplate(type, idToRecordDef, depth) {
|
|
18
|
+
switch (type.kind) {
|
|
19
|
+
case "array": {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
case "optional": {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
case "record": {
|
|
26
|
+
const recordDef = idToRecordDef[type.value];
|
|
27
|
+
if (recordDef.kind === "struct") {
|
|
28
|
+
if (depth) {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
return Object.fromEntries(recordDef.fields.map((field) => [
|
|
32
|
+
field.name,
|
|
33
|
+
makeJsonTemplate(field.type, idToRecordDef, "depth"),
|
|
34
|
+
]));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// Enum
|
|
38
|
+
return "UNKNOWN";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
case "primitive": {
|
|
42
|
+
switch (type.value) {
|
|
43
|
+
case "bool":
|
|
44
|
+
return false;
|
|
45
|
+
case "int32":
|
|
46
|
+
case "float32":
|
|
47
|
+
case "float64":
|
|
48
|
+
return 0;
|
|
49
|
+
case "int64":
|
|
50
|
+
case "hash64":
|
|
51
|
+
return "0";
|
|
52
|
+
case "timestamp":
|
|
53
|
+
return { unix_millis: 0, formatted: "1970-01-01T00:00:00Z" };
|
|
54
|
+
case "string":
|
|
55
|
+
case "bytes":
|
|
56
|
+
return "";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=to_json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to_json.js","sourceRoot":"","sources":["../../src/json/to_json.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,MAAM,CAAC,KAAgB;IACrC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,SAAS,CAAC,CAAC,CAAC;YACf,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACpC,CAAC;QACD,KAAK,OAAO,CAAC,CAAC,CAAC;YACb,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC;QACD,KAAK,QAAQ,CAAC,CAAC,CAAC;YACd,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,MAAM,CAAC,KAAK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;oBACX,OAAO,GAAG,CAAC;gBACb,KAAK,WAAW;oBACd,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,SAAS,EAAE,sBAAsB,EAAE,CAAC;gBAC/D,KAAK,QAAQ,CAAC;gBACd,KAAK,OAAO;oBACV,OAAO,EAAE,CAAC;YACd,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import type { Json, JsonValue, RecordDefinition, TypeSignature } from \"./types\";\n\nexport function toJson(value: JsonValue): Json {\n switch (value.kind) {\n case \"literal\": {\n return JSON.parse(value.jsonCode);\n }\n case \"array\": {\n return value.values.map(toJson);\n }\n case \"object\": {\n return Object.fromEntries(\n Object.values(value.keyValues).map((keyValue) => [\n keyValue.key,\n toJson(keyValue.value),\n ]),\n );\n }\n }\n}\n\nexport function makeJsonTemplate(\n type: TypeSignature,\n idToRecordDef: { [id: string]: RecordDefinition },\n depth?: \"depth\",\n): Json {\n switch (type.kind) {\n case \"array\": {\n return [];\n }\n case \"optional\": {\n return null;\n }\n case \"record\": {\n const recordDef = idToRecordDef[type.value]!;\n if (recordDef.kind === \"struct\") {\n if (depth) {\n return {};\n }\n return Object.fromEntries(\n recordDef.fields.map((field) => [\n field.name,\n makeJsonTemplate(field.type!, idToRecordDef, \"depth\"),\n ]),\n );\n } else {\n // Enum\n return \"UNKNOWN\";\n }\n }\n case \"primitive\": {\n switch (type.value) {\n case \"bool\":\n return false;\n case \"int32\":\n case \"float32\":\n case \"float64\":\n return 0;\n case \"int64\":\n case \"hash64\":\n return \"0\";\n case \"timestamp\":\n return { unix_millis: 0, formatted: \"1970-01-01T00:00:00Z\" };\n case \"string\":\n case \"bytes\":\n return \"\";\n }\n }\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to_json.test.d.ts","sourceRoot":"","sources":["../../src/json/to_json.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { expect } from "buckwheat";
|
|
2
|
+
import { describe, it } from "mocha";
|
|
3
|
+
import { parseJsonValue } from "./json_parser.js";
|
|
4
|
+
import { makeJsonTemplate, toJson } from "./to_json.js";
|
|
5
|
+
function parse(json) {
|
|
6
|
+
const result = parseJsonValue(json);
|
|
7
|
+
if (!result.value) {
|
|
8
|
+
throw new Error(`JSON parse error: ${JSON.stringify(result.errors)}`);
|
|
9
|
+
}
|
|
10
|
+
return result.value;
|
|
11
|
+
}
|
|
12
|
+
describe("to_json", () => {
|
|
13
|
+
describe("toJson", () => {
|
|
14
|
+
it("converts primitives", () => {
|
|
15
|
+
expect(toJson(parse("true"))).toBe(true);
|
|
16
|
+
expect(toJson(parse("false"))).toBe(false);
|
|
17
|
+
expect(toJson(parse("null"))).toBe(null);
|
|
18
|
+
expect(toJson(parse("123"))).toBe(123);
|
|
19
|
+
expect(toJson(parse('"hello"'))).toBe("hello");
|
|
20
|
+
});
|
|
21
|
+
it("converts array", () => {
|
|
22
|
+
expect(toJson(parse("[1, 2, 3]"))).toMatch([1, 2, 3]);
|
|
23
|
+
});
|
|
24
|
+
it("converts object", () => {
|
|
25
|
+
expect(toJson(parse('{"a": 1, "b": "2"}'))).toMatch({ a: 1, b: "2" });
|
|
26
|
+
});
|
|
27
|
+
it("converts nested structure", () => {
|
|
28
|
+
const input = '{"arr": [1, {"x": "y"}], "obj": {"z": null}}';
|
|
29
|
+
expect(toJson(parse(input))).toMatch({
|
|
30
|
+
arr: [1, { x: "y" }],
|
|
31
|
+
obj: { z: null },
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe("makeJsonTemplate", () => {
|
|
36
|
+
const emptyRecords = {};
|
|
37
|
+
it("creates template for array", () => {
|
|
38
|
+
const type = {
|
|
39
|
+
kind: "array",
|
|
40
|
+
value: { item: { kind: "primitive", value: "int32" } },
|
|
41
|
+
};
|
|
42
|
+
expect(makeJsonTemplate(type, emptyRecords)).toMatch([]);
|
|
43
|
+
});
|
|
44
|
+
it("creates template for optional", () => {
|
|
45
|
+
const type = {
|
|
46
|
+
kind: "optional",
|
|
47
|
+
value: { kind: "primitive", value: "int32" },
|
|
48
|
+
};
|
|
49
|
+
expect(makeJsonTemplate(type, emptyRecords)).toBe(null);
|
|
50
|
+
});
|
|
51
|
+
it("creates template for primitives", () => {
|
|
52
|
+
expect(makeJsonTemplate({ kind: "primitive", value: "bool" }, emptyRecords)).toBe(false);
|
|
53
|
+
expect(makeJsonTemplate({ kind: "primitive", value: "int32" }, emptyRecords)).toBe(0);
|
|
54
|
+
expect(makeJsonTemplate({ kind: "primitive", value: "float64" }, emptyRecords)).toBe(0);
|
|
55
|
+
expect(makeJsonTemplate({ kind: "primitive", value: "int64" }, emptyRecords)).toBe("0");
|
|
56
|
+
expect(makeJsonTemplate({ kind: "primitive", value: "string" }, emptyRecords)).toBe("");
|
|
57
|
+
});
|
|
58
|
+
it("creates template for struct", () => {
|
|
59
|
+
const records = {
|
|
60
|
+
MyStruct: {
|
|
61
|
+
kind: "struct",
|
|
62
|
+
id: "MyStruct",
|
|
63
|
+
fields: [
|
|
64
|
+
{
|
|
65
|
+
name: "foo",
|
|
66
|
+
number: 1,
|
|
67
|
+
type: { kind: "primitive", value: "string" },
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: "bar",
|
|
71
|
+
number: 2,
|
|
72
|
+
type: { kind: "primitive", value: "int32" },
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
const type = { kind: "record", value: "MyStruct" };
|
|
78
|
+
expect(makeJsonTemplate(type, records)).toMatch({
|
|
79
|
+
foo: "",
|
|
80
|
+
bar: 0,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
it("creates template for nested struct (depth limit)", () => {
|
|
84
|
+
const records = {
|
|
85
|
+
Parent: {
|
|
86
|
+
kind: "struct",
|
|
87
|
+
id: "Parent",
|
|
88
|
+
fields: [
|
|
89
|
+
{
|
|
90
|
+
name: "child",
|
|
91
|
+
number: 1,
|
|
92
|
+
type: { kind: "record", value: "Child" },
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
Child: {
|
|
97
|
+
kind: "struct",
|
|
98
|
+
id: "Child",
|
|
99
|
+
fields: [
|
|
100
|
+
{
|
|
101
|
+
name: "val",
|
|
102
|
+
number: 1,
|
|
103
|
+
type: { kind: "primitive", value: "int32" },
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
const type = { kind: "record", value: "Parent" };
|
|
109
|
+
// Expect child to be {}, because depth logic in makeJsonTemplate
|
|
110
|
+
// passes "depth" to recursive call for struct fields.
|
|
111
|
+
expect(makeJsonTemplate(type, records)).toMatch({
|
|
112
|
+
child: {},
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
it("creates template for enum", () => {
|
|
116
|
+
const records = {
|
|
117
|
+
MyEnum: {
|
|
118
|
+
kind: "enum",
|
|
119
|
+
id: "MyEnum",
|
|
120
|
+
variants: [],
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
const type = { kind: "record", value: "MyEnum" };
|
|
124
|
+
expect(makeJsonTemplate(type, records)).toBe("UNKNOWN");
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
//# sourceMappingURL=to_json.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to_json.test.js","sourceRoot":"","sources":["../../src/json/to_json.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAGxD,SAAS,KAAK,CAAC,IAAY;IACzB,MAAM,MAAM,GAAG,cAAc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kB,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;YAChE,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC1D,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { expect } from \"buckwheat\";\nimport { describe, it } from \"mocha\";\nimport { parseJsonValue } from \"./json_parser.js\";\nimport { makeJsonTemplate, toJson } from \"./to_json.js\";\nimport { JsonValue, RecordDefinition, TypeSignature } from \"./types.js\";\n\nfunction parse(json: string): JsonValue {\n const result = parseJsonValue(json);\n if (!result.value) {\n throw new Error(`JSON parse error: ${JSON.stringify(result.errors)}`);\n }\n return result.value;\n}\n\ndescribe(\"to_json\", () => {\n describe(\"toJson\", () => {\n it(\"converts primitives\", () => {\n expect(toJson(parse(\"true\"))).toBe(true);\n expect(toJson(parse(\"false\"))).toBe(false);\n expect(toJson(parse(\"null\"))).toBe(null);\n expect(toJson(parse(\"123\"))).toBe(123);\n expect(toJson(parse('\"hello\"'))).toBe(\"hello\");\n });\n\n it(\"converts array\", () => {\n expect(toJson(parse(\"[1, 2, 3]\"))).toMatch([1, 2, 3]);\n });\n\n it(\"converts object\", () => {\n expect(toJson(parse('{\"a\": 1, \"b\": \"2\"}'))).toMatch({ a: 1, b: \"2\" });\n });\n\n it(\"converts nested structure\", () => {\n const input = '{\"arr\": [1, {\"x\": \"y\"}], \"obj\": {\"z\": null}}';\n expect(toJson(parse(input))).toMatch({\n arr: [1, { x: \"y\" }],\n obj: { z: null },\n });\n });\n });\n\n describe(\"makeJsonTemplate\", () => {\n const emptyRecords: { [id: string]: RecordDefinition } = {};\n\n it(\"creates template for array\", () => {\n const type: TypeSignature = {\n kind: \"array\",\n value: { item: { kind: \"primitive\", value: \"int32\" } },\n };\n expect(makeJsonTemplate(type, emptyRecords)).toMatch([]);\n });\n\n it(\"creates template for optional\", () => {\n const type: TypeSignature = {\n kind: \"optional\",\n value: { kind: \"primitive\", value: \"int32\" },\n };\n expect(makeJsonTemplate(type, emptyRecords)).toBe(null);\n });\n\n it(\"creates template for primitives\", () => {\n expect(\n makeJsonTemplate({ kind: \"primitive\", value: \"bool\" }, emptyRecords),\n ).toBe(false);\n expect(\n makeJsonTemplate({ kind: \"primitive\", value: \"int32\" }, emptyRecords),\n ).toBe(0);\n expect(\n makeJsonTemplate({ kind: \"primitive\", value: \"float64\" }, emptyRecords),\n ).toBe(0);\n expect(\n makeJsonTemplate({ kind: \"primitive\", value: \"int64\" }, emptyRecords),\n ).toBe(\"0\");\n expect(\n makeJsonTemplate({ kind: \"primitive\", value: \"string\" }, emptyRecords),\n ).toBe(\"\");\n });\n\n it(\"creates template for struct\", () => {\n const records: { [id: string]: RecordDefinition } = {\n MyStruct: {\n kind: \"struct\",\n id: \"MyStruct\",\n fields: [\n {\n name: \"foo\",\n number: 1,\n type: { kind: \"primitive\", value: \"string\" },\n },\n {\n name: \"bar\",\n number: 2,\n type: { kind: \"primitive\", value: \"int32\" },\n },\n ],\n },\n };\n const type: TypeSignature = { kind: \"record\", value: \"MyStruct\" };\n expect(makeJsonTemplate(type, records)).toMatch({\n foo: \"\",\n bar: 0,\n });\n });\n\n it(\"creates template for nested struct (depth limit)\", () => {\n const records: { [id: string]: RecordDefinition } = {\n Parent: {\n kind: \"struct\",\n id: \"Parent\",\n fields: [\n {\n name: \"child\",\n number: 1,\n type: { kind: \"record\", value: \"Child\" },\n },\n ],\n },\n Child: {\n kind: \"struct\",\n id: \"Child\",\n fields: [\n {\n name: \"val\",\n number: 1,\n type: { kind: \"primitive\", value: \"int32\" },\n },\n ],\n },\n };\n const type: TypeSignature = { kind: \"record\", value: \"Parent\" };\n // Expect child to be {}, because depth logic in makeJsonTemplate\n // passes \"depth\" to recursive call for struct fields.\n expect(makeJsonTemplate(type, records)).toMatch({\n child: {},\n });\n });\n\n it(\"creates template for enum\", () => {\n const records: { [id: string]: RecordDefinition } = {\n MyEnum: {\n kind: \"enum\",\n id: \"MyEnum\",\n variants: [],\n },\n };\n const type: TypeSignature = { kind: \"record\", value: \"MyEnum\" };\n expect(makeJsonTemplate(type, records)).toBe(\"UNKNOWN\");\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
export type Json = null | boolean | number | string | readonly Json[] | Readonly<{
|
|
2
|
+
[name: string]: Json;
|
|
3
|
+
}>;
|
|
4
|
+
export interface JsonError {
|
|
5
|
+
readonly kind: "error";
|
|
6
|
+
readonly segment: Segment;
|
|
7
|
+
readonly message: string;
|
|
8
|
+
}
|
|
9
|
+
export interface JsonParseResult {
|
|
10
|
+
readonly value: JsonValue | undefined;
|
|
11
|
+
readonly errors: readonly JsonError[];
|
|
12
|
+
readonly edits: readonly JsonEdit[];
|
|
13
|
+
}
|
|
14
|
+
export type JsonValue = JsonArray | JsonObject | JsonLiteral;
|
|
15
|
+
export interface JsonArray {
|
|
16
|
+
readonly kind: "array";
|
|
17
|
+
readonly firstToken: Segment;
|
|
18
|
+
readonly segment: Segment;
|
|
19
|
+
readonly values: JsonValue[];
|
|
20
|
+
expectedType?: TypeSignature;
|
|
21
|
+
}
|
|
22
|
+
export interface JsonKey {
|
|
23
|
+
readonly keySegment: Segment;
|
|
24
|
+
readonly key: string;
|
|
25
|
+
}
|
|
26
|
+
export interface JsonKeyValue {
|
|
27
|
+
readonly keySegment: Segment;
|
|
28
|
+
readonly key: string;
|
|
29
|
+
readonly value: JsonValue;
|
|
30
|
+
expectedType?: TypeSignature;
|
|
31
|
+
}
|
|
32
|
+
export interface JsonObject {
|
|
33
|
+
readonly kind: "object";
|
|
34
|
+
readonly firstToken: Segment;
|
|
35
|
+
readonly segment: Segment;
|
|
36
|
+
readonly keyValues: {
|
|
37
|
+
[key: string]: JsonKeyValue;
|
|
38
|
+
};
|
|
39
|
+
readonly allKeys: readonly JsonKey[];
|
|
40
|
+
expectedType?: TypeSignature;
|
|
41
|
+
}
|
|
42
|
+
export interface JsonLiteral {
|
|
43
|
+
readonly kind: "literal";
|
|
44
|
+
readonly firstToken: Segment;
|
|
45
|
+
readonly segment: Segment;
|
|
46
|
+
readonly jsonCode: string;
|
|
47
|
+
readonly type: "boolean" | "null" | "number" | "string";
|
|
48
|
+
expectedType?: TypeSignature;
|
|
49
|
+
}
|
|
50
|
+
export interface Segment {
|
|
51
|
+
readonly start: number;
|
|
52
|
+
readonly end: number;
|
|
53
|
+
}
|
|
54
|
+
export type Path = {
|
|
55
|
+
readonly kind: "root";
|
|
56
|
+
} | {
|
|
57
|
+
readonly kind: "field-value";
|
|
58
|
+
readonly fieldName: string;
|
|
59
|
+
readonly structPath: Path;
|
|
60
|
+
} | {
|
|
61
|
+
readonly kind: "variant-value";
|
|
62
|
+
readonly variantName: string;
|
|
63
|
+
readonly enumPath: Path;
|
|
64
|
+
} | {
|
|
65
|
+
readonly kind: "array-item";
|
|
66
|
+
readonly index: number;
|
|
67
|
+
readonly key: string | null;
|
|
68
|
+
readonly arrayPath: Path;
|
|
69
|
+
};
|
|
70
|
+
export interface JsonValueContext {
|
|
71
|
+
readonly value: JsonValue;
|
|
72
|
+
readonly path: Path;
|
|
73
|
+
}
|
|
74
|
+
export interface JsonEdit {
|
|
75
|
+
readonly segment: Segment;
|
|
76
|
+
readonly replacement: string;
|
|
77
|
+
}
|
|
78
|
+
/** JSON representation of a `TypeDescriptor`. */
|
|
79
|
+
export type TypeDefinition = {
|
|
80
|
+
readonly type: TypeSignature;
|
|
81
|
+
readonly records: readonly RecordDefinition[];
|
|
82
|
+
};
|
|
83
|
+
/** A type in the JSON representation of a `TypeDescriptor`. */
|
|
84
|
+
export type TypeSignature = {
|
|
85
|
+
kind: "optional";
|
|
86
|
+
value: TypeSignature;
|
|
87
|
+
} | ArrayTypeSignature | RecordTypeSignature | {
|
|
88
|
+
kind: "primitive";
|
|
89
|
+
value: PrimitiveType;
|
|
90
|
+
};
|
|
91
|
+
export interface ArrayTypeSignature {
|
|
92
|
+
readonly kind: "array";
|
|
93
|
+
readonly value: {
|
|
94
|
+
readonly item: TypeSignature;
|
|
95
|
+
readonly key_extractor?: string;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export interface RecordTypeSignature {
|
|
99
|
+
readonly kind: "record";
|
|
100
|
+
readonly value: string;
|
|
101
|
+
}
|
|
102
|
+
export type PrimitiveType = "bool" | "int32" | "int64" | "hash64" | "float32" | "float64" | "timestamp" | "string" | "bytes";
|
|
103
|
+
export type RecordDefinition = StructDefinition | EnumDefinition;
|
|
104
|
+
/** Definition of a struct in the JSON representation of a `TypeDescriptor`. */
|
|
105
|
+
export type StructDefinition = {
|
|
106
|
+
readonly kind: "struct";
|
|
107
|
+
readonly id: string;
|
|
108
|
+
readonly doc?: string;
|
|
109
|
+
readonly fields: readonly FieldDefinition[];
|
|
110
|
+
readonly removed_numbers?: readonly number[];
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Definition of a struct field in the JSON representation of a
|
|
114
|
+
* `TypeDescriptor`.
|
|
115
|
+
*/
|
|
116
|
+
export type FieldDefinition = {
|
|
117
|
+
readonly name: string;
|
|
118
|
+
readonly type: TypeSignature;
|
|
119
|
+
readonly number: number;
|
|
120
|
+
readonly doc?: string;
|
|
121
|
+
};
|
|
122
|
+
/** Definition of an enum in the JSON representation of a `TypeDescriptor`. */
|
|
123
|
+
export type EnumDefinition = {
|
|
124
|
+
readonly kind: "enum";
|
|
125
|
+
readonly id: string;
|
|
126
|
+
readonly doc?: string;
|
|
127
|
+
readonly variants: readonly VariantDefinition[];
|
|
128
|
+
readonly removed_numbers?: readonly number[];
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Definition of an enum variant in the JSON representation of a
|
|
132
|
+
* `TypeDescriptor`.
|
|
133
|
+
*/
|
|
134
|
+
export type VariantDefinition = {
|
|
135
|
+
readonly name: string;
|
|
136
|
+
readonly type?: TypeSignature;
|
|
137
|
+
readonly number: number;
|
|
138
|
+
readonly doc?: string;
|
|
139
|
+
};
|
|
140
|
+
export interface ValidationResult {
|
|
141
|
+
readonly errors: JsonError[];
|
|
142
|
+
readonly hints: Hint[];
|
|
143
|
+
readonly rootTypeHint: TypeHint | undefined;
|
|
144
|
+
readonly pathToTypeHint: ReadonlyMap<Path, TypeHint>;
|
|
145
|
+
}
|
|
146
|
+
export interface TypeHint {
|
|
147
|
+
readonly segment: Segment;
|
|
148
|
+
readonly message: string | readonly string[];
|
|
149
|
+
readonly valueContext: JsonValueContext;
|
|
150
|
+
readonly childHints: readonly TypeHint[];
|
|
151
|
+
}
|
|
152
|
+
export type Hint = {
|
|
153
|
+
readonly segment: Segment;
|
|
154
|
+
readonly message: string;
|
|
155
|
+
readonly valueContext?: undefined;
|
|
156
|
+
} | TypeHint;
|
|
157
|
+
export interface MutableTypeHint extends TypeHint {
|
|
158
|
+
readonly childHints: TypeHint[];
|
|
159
|
+
}
|
|
160
|
+
export interface MethodList {
|
|
161
|
+
readonly methods: readonly Method[];
|
|
162
|
+
}
|
|
163
|
+
export interface Method {
|
|
164
|
+
readonly method: string;
|
|
165
|
+
readonly number: number | string;
|
|
166
|
+
readonly request: TypeDefinition;
|
|
167
|
+
readonly response: TypeDefinition;
|
|
168
|
+
readonly doc?: string;
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/json/types.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,IAAI,GACZ,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,SAAS,IAAI,EAAE,GACf,QAAQ,CAAC;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAAC;AAMvC,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;I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