@fncts/schema 0.0.12 → 0.0.14
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/ParseFailure.d.ts +18 -0
- package/ParseResult.d.ts +2 -1
- package/Schema/derivations.d.ts +6 -2
- package/Show.d.ts +8 -0
- package/_cjs/Gen.cjs +1 -2
- package/_cjs/Gen.cjs.map +1 -1
- package/_cjs/ParseFailure.cjs +28 -0
- package/_cjs/ParseFailure.cjs.map +1 -0
- package/_cjs/ParseResult.cjs +4 -3
- package/_cjs/ParseResult.cjs.map +1 -1
- package/_cjs/Parser/api.cjs +2 -2
- package/_cjs/Parser/api.cjs.map +1 -1
- package/_cjs/Parser/interpreter.cjs +8 -8
- package/_cjs/Parser/interpreter.cjs.map +1 -1
- package/_cjs/Schema/api/conc.cjs +1 -1
- package/_cjs/Schema/api/conc.cjs.map +1 -1
- package/_cjs/Schema/api/hashMap.cjs +1 -1
- package/_cjs/Schema/api/hashMap.cjs.map +1 -1
- package/_cjs/Schema/api/immutableArray.cjs +1 -1
- package/_cjs/Schema/api/immutableArray.cjs.map +1 -1
- package/_cjs/Schema/api/list.cjs +1 -1
- package/_cjs/Schema/api/list.cjs.map +1 -1
- package/_cjs/Schema/derivations.cjs +9 -3
- package/_cjs/Schema/derivations.cjs.map +1 -1
- package/_cjs/Show.cjs +146 -0
- package/_cjs/Show.cjs.map +1 -0
- package/_mjs/Gen.mjs +1 -2
- package/_mjs/Gen.mjs.map +1 -1
- package/_mjs/ParseFailure.mjs +20 -0
- package/_mjs/ParseFailure.mjs.map +1 -0
- package/_mjs/ParseResult.mjs +4 -3
- package/_mjs/ParseResult.mjs.map +1 -1
- package/_mjs/Parser/api.mjs +2 -2
- package/_mjs/Parser/api.mjs.map +1 -1
- package/_mjs/Parser/interpreter.mjs +8 -8
- package/_mjs/Parser/interpreter.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/hashMap.mjs +1 -1
- package/_mjs/Schema/api/hashMap.mjs.map +1 -1
- package/_mjs/Schema/api/immutableArray.mjs +1 -1
- package/_mjs/Schema/api/immutableArray.mjs.map +1 -1
- package/_mjs/Schema/api/list.mjs +1 -1
- package/_mjs/Schema/api/list.mjs.map +1 -1
- package/_mjs/Schema/derivations.mjs +8 -3
- package/_mjs/Schema/derivations.mjs.map +1 -1
- package/_mjs/Show.mjs +138 -0
- package/_mjs/Show.mjs.map +1 -0
- package/_src/Gen.ts +0 -1
- package/_src/ParseFailure.ts +18 -0
- package/_src/ParseResult.ts +3 -3
- package/_src/Parser/api.ts +2 -2
- package/_src/Parser/interpreter.ts +8 -8
- package/_src/Schema/api/conc.ts +1 -1
- package/_src/Schema/api/hashMap.ts +1 -1
- package/_src/Schema/api/immutableArray.ts +1 -1
- package/_src/Schema/api/list.ts +1 -1
- package/_src/Schema/derivations.ts +15 -3
- package/_src/Show.ts +169 -0
- package/_src/global.ts +8 -0
- package/global.d.ts +8 -0
- package/package.json +3 -3
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
|
@@ -39,6 +39,10 @@ import { ASTAnnotationMap } from "@fncts/schema/ASTAnnotationMap";
|
|
|
39
39
|
* @tsplus global
|
|
40
40
|
*/
|
|
41
41
|
import { ParseError } from "@fncts/schema/ParseError";
|
|
42
|
+
/**
|
|
43
|
+
* @tsplus global
|
|
44
|
+
*/
|
|
45
|
+
import { ParseFailure } from "@fncts/schema/ParseFailure";
|
|
42
46
|
/**
|
|
43
47
|
* @tsplus global
|
|
44
48
|
*/
|
|
@@ -55,6 +59,10 @@ import {} from "@fncts/schema/Schema";
|
|
|
55
59
|
* @tsplus global
|
|
56
60
|
*/
|
|
57
61
|
import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
|
|
62
|
+
/**
|
|
63
|
+
* @tsplus global
|
|
64
|
+
*/
|
|
65
|
+
import {} from "@fncts/schema/Show";
|
|
58
66
|
/**
|
|
59
67
|
* @tsplus global
|
|
60
68
|
*/
|
package/global.d.ts
CHANGED
|
@@ -38,6 +38,10 @@ import { ASTAnnotationMap } from "@fncts/schema/ASTAnnotationMap";
|
|
|
38
38
|
* @tsplus global
|
|
39
39
|
*/
|
|
40
40
|
import { ParseError } from "@fncts/schema/ParseError";
|
|
41
|
+
/**
|
|
42
|
+
* @tsplus global
|
|
43
|
+
*/
|
|
44
|
+
import { ParseFailure } from "@fncts/schema/ParseFailure";
|
|
41
45
|
/**
|
|
42
46
|
* @tsplus global
|
|
43
47
|
*/
|
|
@@ -54,6 +58,10 @@ import {} from "@fncts/schema/Schema";
|
|
|
54
58
|
* @tsplus global
|
|
55
59
|
*/
|
|
56
60
|
import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
|
|
61
|
+
/**
|
|
62
|
+
* @tsplus global
|
|
63
|
+
*/
|
|
64
|
+
import {} from "@fncts/schema/Show";
|
|
57
65
|
/**
|
|
58
66
|
* @tsplus global
|
|
59
67
|
*/
|
package/package.json
CHANGED