@fncts/schema 0.0.11 → 0.0.13
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/ASTAnnotation.d.ts +9 -9
- package/Parser.d.ts +2 -2
- package/Schema/api.d.ts +2 -2
- package/Schema/derivations.d.ts +6 -2
- package/Schema.d.ts +8 -8
- package/Show.d.ts +8 -0
- package/_cjs/AST.cjs +43 -102
- package/_cjs/AST.cjs.map +1 -1
- package/_cjs/ASTAnnotation.cjs +22 -42
- package/_cjs/ASTAnnotation.cjs.map +1 -1
- package/_cjs/ASTAnnotationMap.cjs +2 -2
- package/_cjs/ASTAnnotationMap.cjs.map +1 -1
- package/_cjs/Gen.cjs +3 -4
- package/_cjs/Gen.cjs.map +1 -1
- package/_cjs/InvalidInterpretationError.cjs +1 -2
- package/_cjs/InvalidInterpretationError.cjs.map +1 -1
- package/_cjs/ParseError.cjs +4 -5
- package/_cjs/ParseError.cjs.map +1 -1
- package/_cjs/ParseResult.cjs +2 -2
- package/_cjs/Parser/api.cjs +2 -2
- package/_cjs/Parser/api.cjs.map +1 -1
- package/_cjs/Parser/definition.cjs +2 -2
- package/_cjs/Parser/interpreter.cjs +2 -2
- package/_cjs/Parser/interpreter.cjs.map +1 -1
- package/_cjs/Parser.cjs +8 -8
- package/_cjs/Parser.cjs.map +1 -1
- package/_cjs/Schema/api/conc.cjs +4 -5
- package/_cjs/Schema/api/conc.cjs.map +1 -1
- package/_cjs/Schema/api/either.cjs +4 -6
- package/_cjs/Schema/api/either.cjs.map +1 -1
- package/_cjs/Schema/api/hashMap.cjs +4 -6
- package/_cjs/Schema/api/hashMap.cjs.map +1 -1
- package/_cjs/Schema/api/hashSet.cjs +7 -7
- package/_cjs/Schema/api/hashSet.cjs.map +1 -1
- package/_cjs/Schema/api/immutableArray.cjs +4 -6
- package/_cjs/Schema/api/immutableArray.cjs.map +1 -1
- package/_cjs/Schema/api/list.cjs +4 -6
- package/_cjs/Schema/api/list.cjs.map +1 -1
- package/_cjs/Schema/api/maybe.cjs +4 -6
- package/_cjs/Schema/api/maybe.cjs.map +1 -1
- package/_cjs/Schema/api.cjs +29 -56
- package/_cjs/Schema/api.cjs.map +1 -1
- package/_cjs/Schema/definition.cjs +5 -8
- package/_cjs/Schema/definition.cjs.map +1 -1
- package/_cjs/Schema/derivations.cjs +22 -8
- package/_cjs/Schema/derivations.cjs.map +1 -1
- package/_cjs/Schema.cjs +32 -32
- package/_cjs/Schema.cjs.map +1 -1
- package/_cjs/Show.cjs +146 -0
- package/_cjs/Show.cjs.map +1 -0
- package/_cjs/utils.cjs +2 -2
- package/_cjs/utils.cjs.map +1 -1
- package/_mjs/AST.mjs +5 -28
- package/_mjs/AST.mjs.map +1 -1
- package/_mjs/ASTAnnotation.mjs.map +1 -1
- package/_mjs/ASTAnnotationMap.mjs +1 -2
- package/_mjs/ASTAnnotationMap.mjs.map +1 -1
- package/_mjs/Gen.mjs +1 -2
- package/_mjs/Gen.mjs.map +1 -1
- package/_mjs/ParseError.mjs +1 -1
- package/_mjs/ParseError.mjs.map +1 -1
- package/_mjs/Parser/interpreter.mjs.map +1 -1
- package/_mjs/Parser.mjs +3 -2
- package/_mjs/Parser.mjs.map +1 -1
- package/_mjs/Schema/api/conc.mjs +1 -1
- package/_mjs/Schema/api/conc.mjs.map +1 -1
- package/_mjs/Schema/api/either.mjs.map +1 -1
- package/_mjs/Schema/api/hashMap.mjs.map +1 -1
- package/_mjs/Schema/api/hashSet.mjs.map +1 -1
- package/_mjs/Schema/api/immutableArray.mjs.map +1 -1
- package/_mjs/Schema/api/list.mjs.map +1 -1
- package/_mjs/Schema/api/maybe.mjs.map +1 -1
- package/_mjs/Schema/api.mjs.map +1 -1
- package/_mjs/Schema/derivations.mjs +19 -6
- package/_mjs/Schema/derivations.mjs.map +1 -1
- package/_mjs/Schema.mjs +10 -8
- package/_mjs/Schema.mjs.map +1 -1
- package/_mjs/Show.mjs +138 -0
- package/_mjs/Show.mjs.map +1 -0
- package/_mjs/utils.mjs.map +1 -1
- package/_src/AST.ts +24 -6
- package/_src/ASTAnnotation.ts +5 -1
- package/_src/Gen.ts +0 -1
- package/_src/ParseError.ts +13 -3
- package/_src/Parser.ts +3 -2
- package/_src/Schema/api/conc.ts +1 -1
- package/_src/Schema/derivations.ts +19 -3
- package/_src/Schema.ts +10 -8
- package/_src/Show.ts +169 -0
- package/_src/global.ts +5 -1
- package/global.d.ts +5 -1
- package/package.json +3 -3
|
@@ -66,11 +66,13 @@ export function deriveStruct<A extends Record<string, any>>(
|
|
|
66
66
|
): Schema<A> {
|
|
67
67
|
const maybeFieldsKeys: Vector<PropertyKey> = ownKeys(maybeFields);
|
|
68
68
|
let propertySignatures = ownKeys(requiredFields).map((key) =>
|
|
69
|
+
// @ts-expect-error
|
|
69
70
|
AST.createPropertySignature(key, requiredFields[key]!.ast, false, true),
|
|
70
71
|
);
|
|
71
72
|
|
|
72
73
|
if (optionalFields) {
|
|
73
74
|
propertySignatures = propertySignatures.concat(
|
|
75
|
+
// @ts-expect-error
|
|
74
76
|
ownKeys(optionalFields).map((key) => AST.createPropertySignature(key, optionalFields[key]!.ast, true, true)),
|
|
75
77
|
);
|
|
76
78
|
}
|
|
@@ -89,6 +91,7 @@ export function deriveStruct<A extends Record<string, any>>(
|
|
|
89
91
|
maybeFieldsKeys.map((key) =>
|
|
90
92
|
AST.createPropertySignature(
|
|
91
93
|
key,
|
|
94
|
+
// @ts-expect-error
|
|
92
95
|
AST.createUnion(Vector(AST.undefinedKeyword, AST.createLiteral(null), maybeFields![key]!.ast)),
|
|
93
96
|
true,
|
|
94
97
|
true,
|
|
@@ -103,7 +106,8 @@ export function deriveStruct<A extends Record<string, any>>(
|
|
|
103
106
|
AST.createTypeLiteral(
|
|
104
107
|
propertySignatures.concat(
|
|
105
108
|
maybeFieldsKeys.map((key) =>
|
|
106
|
-
|
|
109
|
+
// @ts-expect-error
|
|
110
|
+
AST.createPropertySignature(key, Schema.maybe(maybeFields![key]!).ast, false, true),
|
|
107
111
|
),
|
|
108
112
|
),
|
|
109
113
|
Vector.empty(),
|
|
@@ -158,10 +162,9 @@ export function deriveLazy<A>(f: (_: Schema<A>) => Schema<A>): Schema<A> {
|
|
|
158
162
|
}
|
|
159
163
|
|
|
160
164
|
/**
|
|
161
|
-
* @tsplus derive fncts.schema.Schema[fncts.Array]<_> 10
|
|
162
165
|
* @tsplus derive fncts.schema.Schema[fncts.ReadonlyArray]<_> 10
|
|
163
166
|
*/
|
|
164
|
-
export function
|
|
167
|
+
export function deriveReadonlyArray<A extends ReadonlyArray<any>>(
|
|
165
168
|
...[element]: [A] extends [ReadonlyArray<infer _A>]
|
|
166
169
|
? Check<Check.IsEqual<A, ReadonlyArray<_A>>> extends Check.True
|
|
167
170
|
? [element: Schema<_A>]
|
|
@@ -171,6 +174,19 @@ export function deriveArray<A extends ReadonlyArray<any>>(
|
|
|
171
174
|
return Schema.array(element) as unknown as Schema<A>;
|
|
172
175
|
}
|
|
173
176
|
|
|
177
|
+
/**
|
|
178
|
+
* @tsplus derive fncts.schema.Schema[fncts.Array]<_> 10
|
|
179
|
+
*/
|
|
180
|
+
export function deriveArray<A extends Array<any>>(
|
|
181
|
+
...[element]: [A] extends [Array<infer _A>]
|
|
182
|
+
? Check<Check.IsEqual<A, Array<_A>>> extends Check.True
|
|
183
|
+
? [element: Schema<_A>]
|
|
184
|
+
: never
|
|
185
|
+
: never
|
|
186
|
+
): Schema<A> {
|
|
187
|
+
return Schema.mutableArray(element) as unknown as Schema<A>;
|
|
188
|
+
}
|
|
189
|
+
|
|
174
190
|
/**
|
|
175
191
|
* @tsplus derive fncts.schema.Schema<_> 15
|
|
176
192
|
*/
|
package/_src/Schema.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
/* eslint-disable simple-import-sort/exports */
|
|
1
2
|
// codegen:start { preset: barrel, include: ./Schema/*.ts }
|
|
2
|
-
export * from "./Schema/api.js";
|
|
3
|
-
export * from "./Schema/definition.js";
|
|
4
3
|
export * from "./Schema/derivations.js";
|
|
4
|
+
export * from "./Schema/definition.js";
|
|
5
|
+
export * from "./Schema/api.js";
|
|
5
6
|
// codegen:end
|
|
6
7
|
|
|
8
|
+
/* eslint-disable simple-import-sort/exports */
|
|
7
9
|
// codegen:start { preset: barrel, include: ./Schema/api/*.ts }
|
|
8
|
-
export * from "./Schema/api/conc.js";
|
|
9
|
-
export * from "./Schema/api/either.js";
|
|
10
|
-
export * from "./Schema/api/hashMap.js";
|
|
11
|
-
export * from "./Schema/api/hashSet.js";
|
|
12
|
-
export * from "./Schema/api/immutableArray.js";
|
|
13
|
-
export * from "./Schema/api/list.js";
|
|
14
10
|
export * from "./Schema/api/maybe.js";
|
|
11
|
+
export * from "./Schema/api/list.js";
|
|
12
|
+
export * from "./Schema/api/immutableArray.js";
|
|
13
|
+
export * from "./Schema/api/hashSet.js";
|
|
14
|
+
export * from "./Schema/api/hashMap.js";
|
|
15
|
+
export * from "./Schema/api/either.js";
|
|
16
|
+
export * from "./Schema/api/conc.js";
|
|
15
17
|
// codegen:end
|
|
16
18
|
|
|
17
19
|
export type {} from "./Parser.js";
|
package/_src/Show.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { TemplateLiteral, TemplateLiteralSpan } from "@fncts/schema/AST";
|
|
2
|
+
|
|
3
|
+
import { ASTTag } from "@fncts/schema/AST";
|
|
4
|
+
import { ASTAnnotation } from "@fncts/schema/ASTAnnotation";
|
|
5
|
+
import { memoize } from "@fncts/schema/utils";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @tsplus getter fncts.schema.Schema show
|
|
9
|
+
*/
|
|
10
|
+
export function show<A>(self: Schema<A>): string {
|
|
11
|
+
const ev = go(self.ast);
|
|
12
|
+
return ev.run;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const go = memoize(function go(ast: AST): Eval<string> {
|
|
16
|
+
AST.concrete(ast);
|
|
17
|
+
switch (ast._tag) {
|
|
18
|
+
case ASTTag.Declaration: {
|
|
19
|
+
return ast.annotations.get(ASTAnnotation.Identifier).match(
|
|
20
|
+
() => Eval.now("Unknown Type"),
|
|
21
|
+
(id) => {
|
|
22
|
+
return ast.typeParameters
|
|
23
|
+
.traverse(Eval.Applicative)(go)
|
|
24
|
+
.map((ts) => {
|
|
25
|
+
if (ts.length <= 0) {
|
|
26
|
+
return id;
|
|
27
|
+
} else {
|
|
28
|
+
return `${id}<${ts.join(", ")}>`;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
case ASTTag.Literal: {
|
|
35
|
+
if (ast.literal === null) {
|
|
36
|
+
return Eval.now("null");
|
|
37
|
+
} else {
|
|
38
|
+
return Eval.now(ast.literal.toString());
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
case ASTTag.UniqueSymbol:
|
|
42
|
+
return Eval.now(ast.symbol.toString());
|
|
43
|
+
case ASTTag.UndefinedKeyword:
|
|
44
|
+
return Eval.now("undefined");
|
|
45
|
+
case ASTTag.VoidKeyword:
|
|
46
|
+
return Eval.now("void");
|
|
47
|
+
case ASTTag.NeverKeyword:
|
|
48
|
+
return Eval.now("never");
|
|
49
|
+
case ASTTag.UnknownKeyword:
|
|
50
|
+
return Eval.now("unknown");
|
|
51
|
+
case ASTTag.AnyKeyword:
|
|
52
|
+
return Eval.now("any");
|
|
53
|
+
case ASTTag.StringKeyword:
|
|
54
|
+
return Eval.now("string");
|
|
55
|
+
case ASTTag.NumberKeyword:
|
|
56
|
+
return Eval.now("number");
|
|
57
|
+
case ASTTag.BooleanKeyword:
|
|
58
|
+
return Eval.now("boolean");
|
|
59
|
+
case ASTTag.BigIntKeyword:
|
|
60
|
+
return Eval.now("bigint");
|
|
61
|
+
case ASTTag.SymbolKeyword:
|
|
62
|
+
return Eval.now("symbol");
|
|
63
|
+
case ASTTag.ObjectKeyword:
|
|
64
|
+
return Eval.now("object");
|
|
65
|
+
case ASTTag.TemplateLiteral:
|
|
66
|
+
return Eval.now("`" + formatTemplateLiteral(ast) + "`");
|
|
67
|
+
case ASTTag.Tuple:
|
|
68
|
+
return Do((Δ) => {
|
|
69
|
+
const elements = Δ(ast.elements.map((element) => element.type).traverse(Eval.Applicative)(go));
|
|
70
|
+
const restElements = Δ(
|
|
71
|
+
ast.rest.match(
|
|
72
|
+
() => Eval.now(Vector.empty<string>()),
|
|
73
|
+
(rest) => rest.traverse(Eval.Applicative)(go),
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return Δ(
|
|
78
|
+
Eval(() => {
|
|
79
|
+
if (elements.length === 0 && restElements.length === 1) {
|
|
80
|
+
if (ast.isReadonly) {
|
|
81
|
+
return `ReadonlyArray<${restElements[0]}>`;
|
|
82
|
+
} else {
|
|
83
|
+
return `Array<${restElements[0]}>`;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const prefix = (ast.isReadonly ? "readonly " : "") + "[" + elements.join(", ");
|
|
88
|
+
const middle = restElements.length > 0 ? ", " : "";
|
|
89
|
+
const suffix = restElements.map((s) => `...${s}`).join(", ") + "]";
|
|
90
|
+
return prefix + middle + suffix;
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
case ASTTag.TypeLiteral:
|
|
95
|
+
return Do((Δ) => {
|
|
96
|
+
const propertySignatures = Δ(ast.propertySignatures.traverse(Eval.Applicative)((ps) => go(ps.type)));
|
|
97
|
+
const indexSignatures = Δ(
|
|
98
|
+
ast.indexSignatures.traverse(Eval.Applicative)((is) => go(is.parameter).zip(go(is.type))),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const required: Array<[PropertyKey, string]> = [];
|
|
102
|
+
const optional: Array<[PropertyKey, string]> = [];
|
|
103
|
+
|
|
104
|
+
ast.propertySignatures.forEachWithIndex((i, ps) => {
|
|
105
|
+
const name = ps.name;
|
|
106
|
+
if (!ps.isOptional) {
|
|
107
|
+
required.push([name, propertySignatures[i]!]);
|
|
108
|
+
} else {
|
|
109
|
+
optional.push([name, propertySignatures[i]!]);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const prefix = "{";
|
|
114
|
+
const properties = required
|
|
115
|
+
.concat(optional)
|
|
116
|
+
.sort(([k1], [k2]) => k1.toLocaleString().localeCompare(k2.toLocaleString()))
|
|
117
|
+
.map(([propertyKey, type]) => `${String(propertyKey)}: ${type}`)
|
|
118
|
+
.join(", ");
|
|
119
|
+
const index = indexSignatures.map(([param, type]) => `[x: ${param}]: ${type}`).join(", ");
|
|
120
|
+
const suffix = "}";
|
|
121
|
+
|
|
122
|
+
return prefix + " " + properties + (index.length === 0 ? "" : ", ") + index + " " + suffix;
|
|
123
|
+
});
|
|
124
|
+
case ASTTag.Union:
|
|
125
|
+
return ast.types
|
|
126
|
+
.traverse(Eval.Applicative)(go)
|
|
127
|
+
.map((ts) => ts.join(" | "));
|
|
128
|
+
case ASTTag.Lazy: {
|
|
129
|
+
const f = () => go(ast.getAST());
|
|
130
|
+
const get = memoize<typeof f, Eval<string>>(f);
|
|
131
|
+
return Eval.defer(() => get(f));
|
|
132
|
+
}
|
|
133
|
+
case ASTTag.Enum: {
|
|
134
|
+
return Eval.now(ast.enums.map(([name]) => name).join(" | "));
|
|
135
|
+
}
|
|
136
|
+
case ASTTag.Refinement: {
|
|
137
|
+
return ast.annotations.get(ASTAnnotation.Identifier).match(
|
|
138
|
+
() => go(ast.from).map((from) => `Refined<${from}>`),
|
|
139
|
+
(id) => Eval.now(id),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
case ASTTag.Transform:
|
|
143
|
+
return go(ast.to);
|
|
144
|
+
case ASTTag.Validation: {
|
|
145
|
+
return go(ast.from).map((from) => {
|
|
146
|
+
const validationNames = ast.validation.map((v) => v.name).join(" & ");
|
|
147
|
+
|
|
148
|
+
if (validationNames.length <= 0) {
|
|
149
|
+
return from;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return `${from} & ${validationNames}`;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
function formatTemplateLiteralSpan(span: TemplateLiteralSpan): string {
|
|
159
|
+
switch (span.type._tag) {
|
|
160
|
+
case ASTTag.StringKeyword:
|
|
161
|
+
return "${string}";
|
|
162
|
+
case ASTTag.NumberKeyword:
|
|
163
|
+
return "${number}";
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function formatTemplateLiteral(ast: TemplateLiteral): string {
|
|
168
|
+
return ast.head + ast.spans.map((span) => formatTemplateLiteralSpan(span) + span.literal).join("");
|
|
169
|
+
}
|
package/_src/global.ts
CHANGED
|
@@ -47,6 +47,10 @@ import { Parser } from "@fncts/schema/Parser/definition";
|
|
|
47
47
|
* @tsplus global
|
|
48
48
|
*/
|
|
49
49
|
import { ParseResult } from "@fncts/schema/ParseResult";
|
|
50
|
+
/**
|
|
51
|
+
* @tsplus global
|
|
52
|
+
*/
|
|
53
|
+
import {} from "@fncts/schema/Schema";
|
|
50
54
|
/**
|
|
51
55
|
* @tsplus global
|
|
52
56
|
*/
|
|
@@ -54,7 +58,7 @@ import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
|
|
|
54
58
|
/**
|
|
55
59
|
* @tsplus global
|
|
56
60
|
*/
|
|
57
|
-
import {} from "@fncts/schema/
|
|
61
|
+
import {} from "@fncts/schema/Show";
|
|
58
62
|
/**
|
|
59
63
|
* @tsplus global
|
|
60
64
|
*/
|
package/global.d.ts
CHANGED
|
@@ -46,6 +46,10 @@ import { Parser } from "@fncts/schema/Parser/definition";
|
|
|
46
46
|
* @tsplus global
|
|
47
47
|
*/
|
|
48
48
|
import { ParseResult } from "@fncts/schema/ParseResult";
|
|
49
|
+
/**
|
|
50
|
+
* @tsplus global
|
|
51
|
+
*/
|
|
52
|
+
import {} from "@fncts/schema/Schema";
|
|
49
53
|
/**
|
|
50
54
|
* @tsplus global
|
|
51
55
|
*/
|
|
@@ -53,7 +57,7 @@ import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
|
|
|
53
57
|
/**
|
|
54
58
|
* @tsplus global
|
|
55
59
|
*/
|
|
56
|
-
import {} from "@fncts/schema/
|
|
60
|
+
import {} from "@fncts/schema/Show";
|
|
57
61
|
/**
|
|
58
62
|
* @tsplus global
|
|
59
63
|
*/
|
package/package.json
CHANGED