simpler-forms 0.1.0-alpha.0 → 0.1.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 +221 -76
- package/dist/src/casing.d.ts +43 -0
- package/dist/src/casing.js +146 -0
- package/dist/src/casing.js.map +1 -0
- package/dist/src/decorators/index.d.ts +1 -0
- package/dist/src/decorators/index.js +1 -0
- package/dist/src/decorators/index.js.map +1 -1
- package/dist/src/decorators/mapping.d.ts +11 -0
- package/dist/src/decorators/mapping.js +22 -0
- package/dist/src/decorators/mapping.js.map +1 -0
- package/dist/src/decorators/meta.d.ts +13 -3
- package/dist/src/decorators/meta.js +31 -4
- package/dist/src/decorators/meta.js.map +1 -1
- package/dist/src/decorators/shared.d.ts +10 -0
- package/dist/src/decorators/shared.js +8 -0
- package/dist/src/decorators/shared.js.map +1 -1
- package/dist/src/decorators/ui.d.ts +33 -12
- package/dist/src/decorators/ui.js +39 -43
- package/dist/src/decorators/ui.js.map +1 -1
- package/dist/src/emitter.d.ts +8 -0
- package/dist/src/emitter.js +48 -13
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/emitters/block-ui.d.ts +17 -3
- package/dist/src/emitters/block-ui.js +144 -124
- package/dist/src/emitters/block-ui.js.map +1 -1
- package/dist/src/emitters/layout-ui.d.ts +12 -0
- package/dist/src/emitters/layout-ui.js +225 -0
- package/dist/src/emitters/layout-ui.js.map +1 -0
- package/dist/src/emitters/overlay.d.ts +3 -6
- package/dist/src/emitters/overlay.js +3 -86
- package/dist/src/emitters/overlay.js.map +1 -1
- package/dist/src/forms.d.ts +23 -0
- package/dist/src/forms.js +54 -0
- package/dist/src/forms.js.map +1 -0
- package/dist/src/index.d.ts +17 -6
- package/dist/src/index.js +14 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/layout.d.ts +50 -0
- package/dist/src/layout.js +397 -0
- package/dist/src/layout.js.map +1 -0
- package/dist/src/lib.d.ts +485 -39
- package/dist/src/lib.js +216 -16
- package/dist/src/lib.js.map +1 -1
- package/dist/src/linter.js +2 -41
- package/dist/src/linter.js.map +1 -1
- package/dist/src/mapping-from.d.ts +20 -0
- package/dist/src/mapping-from.js +149 -0
- package/dist/src/mapping-from.js.map +1 -0
- package/dist/src/mapping.d.ts +58 -0
- package/dist/src/mapping.js +209 -0
- package/dist/src/mapping.js.map +1 -0
- package/dist/src/model.d.ts +87 -9
- package/dist/src/model.js +65 -10
- package/dist/src/model.js.map +1 -1
- package/dist/src/public.d.ts +19 -3
- package/dist/src/public.js +27 -3
- package/dist/src/public.js.map +1 -1
- package/dist/src/validate.d.ts +2 -1
- package/dist/src/validate.js +181 -59
- package/dist/src/validate.js.map +1 -1
- package/lib/main.tsp +1 -0
- package/lib/mapping.tsp +71 -0
- package/lib/meta.tsp +23 -4
- package/lib/types.tsp +13 -65
- package/lib/ui.tsp +60 -17
- package/package.json +7 -1
- package/src/casing.ts +172 -0
- package/src/decorators/index.ts +1 -0
- package/src/decorators/mapping.ts +57 -0
- package/src/decorators/meta.ts +50 -11
- package/src/decorators/shared.ts +13 -0
- package/src/decorators/ui.ts +81 -59
- package/src/emitter.ts +59 -13
- package/src/emitters/block-ui.ts +156 -140
- package/src/emitters/layout-ui.ts +280 -0
- package/src/emitters/overlay.ts +3 -103
- package/src/forms.ts +82 -0
- package/src/index.ts +14 -3
- package/src/layout.ts +592 -0
- package/src/lib.ts +222 -16
- package/src/linter.ts +1 -42
- package/src/mapping-from.ts +193 -0
- package/src/mapping.ts +351 -0
- package/src/model.ts +192 -24
- package/src/public.ts +50 -5
- package/src/validate.ts +189 -60
- package/dist/src/emitters/override-condition.d.ts +0 -12
- package/dist/src/emitters/override-condition.js +0 -25
- package/dist/src/emitters/override-condition.js.map +0 -1
- package/src/emitters/override-condition.ts +0 -37
package/src/mapping.ts
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EnumMember,
|
|
3
|
+
Model,
|
|
4
|
+
ModelProperty,
|
|
5
|
+
Program,
|
|
6
|
+
Type,
|
|
7
|
+
} from "@typespec/compiler";
|
|
8
|
+
import { isArrayModelType } from "@typespec/compiler";
|
|
9
|
+
import {
|
|
10
|
+
type MappingPathLink,
|
|
11
|
+
modelMappedTo,
|
|
12
|
+
modelMappingPaths,
|
|
13
|
+
modelProperties,
|
|
14
|
+
propMappedTo,
|
|
15
|
+
} from "./model.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* How a linked source property relates to its target:
|
|
19
|
+
*
|
|
20
|
+
* leaf a value, linked to a value
|
|
21
|
+
* object an object, linked to an object; its children are linked within that object
|
|
22
|
+
* array a list, linked to a list; for a list of objects, its children are linked within
|
|
23
|
+
* the target's item
|
|
24
|
+
* group an object with no link of its own whose descendants are linked: flattened, its
|
|
25
|
+
* children landing in the parent's target
|
|
26
|
+
*/
|
|
27
|
+
export type MappingNodeKind = "leaf" | "object" | "array" | "group";
|
|
28
|
+
|
|
29
|
+
export interface MappingNode {
|
|
30
|
+
/** Property names from the mapped model to this one, this one last. */
|
|
31
|
+
sourcePath: string[];
|
|
32
|
+
source: ModelProperty;
|
|
33
|
+
/** What the source corresponds to. A group has none. */
|
|
34
|
+
target?: ModelProperty;
|
|
35
|
+
kind: MappingNodeKind;
|
|
36
|
+
/** The linked properties below an object, a list of objects, or a group, in source order. */
|
|
37
|
+
children: MappingNode[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A model's mapping in one target, resolved through every model it composes. */
|
|
41
|
+
export interface ResolvedMapping {
|
|
42
|
+
mapping: EnumMember;
|
|
43
|
+
source: Model;
|
|
44
|
+
target: Model;
|
|
45
|
+
children: MappingNode[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A link that does not hold, ready to report as a diagnostic. */
|
|
49
|
+
export interface MappingProblem {
|
|
50
|
+
code:
|
|
51
|
+
| "mapping-path-unresolved"
|
|
52
|
+
| "mapping-target-out-of-context"
|
|
53
|
+
| "mapping-type-mismatch"
|
|
54
|
+
| "mapping-cardinality-mismatch"
|
|
55
|
+
| "mapping-target-duplicate";
|
|
56
|
+
target: Model | ModelProperty;
|
|
57
|
+
format: Record<string, string>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* `model`'s mapping in `mapping`, or undefined if `model` has no `@Mapping.to` in it.
|
|
62
|
+
*
|
|
63
|
+
* A property takes the first link that applies:
|
|
64
|
+
*
|
|
65
|
+
* 1. a `@Mapping.path` naming it, from the outermost composing model that has one
|
|
66
|
+
* 2. its own `@Mapping.to`
|
|
67
|
+
*
|
|
68
|
+
* and an object-valued property then resolves its own properties against what it is linked
|
|
69
|
+
* to, so a question linked once at the level its mapping is shared needs nothing more from
|
|
70
|
+
* the forms that compose it. Links that do not hold are left out; `$onValidate` reports them.
|
|
71
|
+
*/
|
|
72
|
+
export function resolveMapping(
|
|
73
|
+
program: Program,
|
|
74
|
+
model: Model,
|
|
75
|
+
mapping: EnumMember,
|
|
76
|
+
): ResolvedMapping | undefined {
|
|
77
|
+
return resolveMappingWithProblems(program, model, mapping)?.resolved;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** `resolveMapping`, with every link that did not hold. */
|
|
81
|
+
export function resolveMappingWithProblems(
|
|
82
|
+
program: Program,
|
|
83
|
+
model: Model,
|
|
84
|
+
mapping: EnumMember,
|
|
85
|
+
): { resolved: ResolvedMapping; problems: MappingProblem[] } | undefined {
|
|
86
|
+
const target = modelMappedTo(program, model, mapping);
|
|
87
|
+
if (!target) return undefined;
|
|
88
|
+
const walk: Walk = {
|
|
89
|
+
program,
|
|
90
|
+
mapping,
|
|
91
|
+
problems: [],
|
|
92
|
+
owned: [],
|
|
93
|
+
consumed: new Set(),
|
|
94
|
+
};
|
|
95
|
+
const children = resolveModel(walk, model, target, [], [], new Map(), [
|
|
96
|
+
model,
|
|
97
|
+
]);
|
|
98
|
+
for (const { link, owner } of walk.owned) {
|
|
99
|
+
if (walk.consumed.has(link)) continue;
|
|
100
|
+
walk.problems.push({
|
|
101
|
+
code: "mapping-path-unresolved",
|
|
102
|
+
target: owner,
|
|
103
|
+
format: {
|
|
104
|
+
path: link.sourcePath,
|
|
105
|
+
name: owner.name || "an anonymous model",
|
|
106
|
+
reason: unresolvedReason(owner, link.sourcePath),
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
resolved: { mapping, source: model, target, children },
|
|
112
|
+
problems: walk.problems,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
interface Walk {
|
|
119
|
+
program: Program;
|
|
120
|
+
mapping: EnumMember;
|
|
121
|
+
problems: MappingProblem[];
|
|
122
|
+
/** Every `@Mapping.path` met so far, with the model that declares it. */
|
|
123
|
+
owned: { link: MappingPathLink; owner: Model }[];
|
|
124
|
+
/** The ones that landed on a property. */
|
|
125
|
+
consumed: Set<MappingPathLink>;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A `@Mapping.path` on its way down: the steps still to take from the current model. */
|
|
129
|
+
interface PendingPath {
|
|
130
|
+
link: MappingPathLink;
|
|
131
|
+
owner: Model;
|
|
132
|
+
steps: string[];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* `model`'s linked properties, each resolved within `context`, the model `model` (or its
|
|
137
|
+
* nearest linked ancestor, for a group) corresponds to.
|
|
138
|
+
*
|
|
139
|
+
* `claimed` is shared by everything that lands in one target object, groups included, so two
|
|
140
|
+
* sources landing on one target property are caught wherever they were declared.
|
|
141
|
+
*/
|
|
142
|
+
function resolveModel(
|
|
143
|
+
walk: Walk,
|
|
144
|
+
model: Model,
|
|
145
|
+
context: Model,
|
|
146
|
+
inherited: PendingPath[],
|
|
147
|
+
prefix: string[],
|
|
148
|
+
claimed: Map<ModelProperty, string>,
|
|
149
|
+
stack: Model[],
|
|
150
|
+
): MappingNode[] {
|
|
151
|
+
const own = modelMappingPaths(walk.program, model, walk.mapping).map(
|
|
152
|
+
(link) => {
|
|
153
|
+
walk.owned.push({ link, owner: model });
|
|
154
|
+
return { link, owner: model, steps: stepsOf(link.sourcePath) };
|
|
155
|
+
},
|
|
156
|
+
);
|
|
157
|
+
// Outermost first, so `find` below prefers the composing model furthest out.
|
|
158
|
+
const pending = [...inherited, ...own];
|
|
159
|
+
const nodes: MappingNode[] = [];
|
|
160
|
+
|
|
161
|
+
for (const source of modelProperties(model)) {
|
|
162
|
+
const here = pending.filter((p) => p.steps[0] === source.name);
|
|
163
|
+
const exact = here.filter((p) => p.steps.length === 1);
|
|
164
|
+
for (const p of exact) walk.consumed.add(p.link);
|
|
165
|
+
const deeper = here
|
|
166
|
+
.filter((p) => p.steps.length > 1)
|
|
167
|
+
.map((p) => ({ ...p, steps: p.steps.slice(1) }));
|
|
168
|
+
const path = exact[0];
|
|
169
|
+
const target =
|
|
170
|
+
path?.link.mappedTo ?? propMappedTo(walk.program, source, walk.mapping);
|
|
171
|
+
const sourcePath = [...prefix, source.name];
|
|
172
|
+
const at = path?.owner ?? source;
|
|
173
|
+
const shape = shapeOf(source.type);
|
|
174
|
+
|
|
175
|
+
if (!target) {
|
|
176
|
+
// Unlinked: flattened into the parent's target if anything below it is linked.
|
|
177
|
+
if (!shape.object || shape.array || stack.includes(shape.object))
|
|
178
|
+
continue;
|
|
179
|
+
const children = resolveModel(
|
|
180
|
+
walk,
|
|
181
|
+
shape.object,
|
|
182
|
+
context,
|
|
183
|
+
deeper,
|
|
184
|
+
sourcePath,
|
|
185
|
+
claimed,
|
|
186
|
+
[...stack, shape.object],
|
|
187
|
+
);
|
|
188
|
+
if (children.length)
|
|
189
|
+
nodes.push({ sourcePath, source, kind: "group", children });
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const node = resolveLink(
|
|
194
|
+
walk,
|
|
195
|
+
source,
|
|
196
|
+
target,
|
|
197
|
+
context,
|
|
198
|
+
deeper,
|
|
199
|
+
sourcePath,
|
|
200
|
+
at,
|
|
201
|
+
stack,
|
|
202
|
+
);
|
|
203
|
+
if (!node) continue;
|
|
204
|
+
const first = claimed.get(target);
|
|
205
|
+
if (first !== undefined) {
|
|
206
|
+
walk.problems.push({
|
|
207
|
+
code: "mapping-target-duplicate",
|
|
208
|
+
target: at,
|
|
209
|
+
format: {
|
|
210
|
+
mapping: walk.mapping.name,
|
|
211
|
+
first,
|
|
212
|
+
second: sourcePath.join("."),
|
|
213
|
+
target: describe(target),
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
claimed.set(target, sourcePath.join("."));
|
|
219
|
+
nodes.push(node);
|
|
220
|
+
}
|
|
221
|
+
return nodes;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function resolveLink(
|
|
225
|
+
walk: Walk,
|
|
226
|
+
source: ModelProperty,
|
|
227
|
+
target: ModelProperty,
|
|
228
|
+
context: Model,
|
|
229
|
+
deeper: PendingPath[],
|
|
230
|
+
sourcePath: string[],
|
|
231
|
+
at: Model | ModelProperty,
|
|
232
|
+
stack: Model[],
|
|
233
|
+
): MappingNode | undefined {
|
|
234
|
+
const fail = (code: MappingProblem["code"], format: Record<string, string>) =>
|
|
235
|
+
void walk.problems.push({
|
|
236
|
+
code,
|
|
237
|
+
target: at,
|
|
238
|
+
format: {
|
|
239
|
+
mapping: walk.mapping.name,
|
|
240
|
+
source: sourcePath.join("."),
|
|
241
|
+
target: describe(target),
|
|
242
|
+
...format,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
if (!isMemberOf(target, context)) {
|
|
247
|
+
fail("mapping-target-out-of-context", {
|
|
248
|
+
context: context.name || "an anonymous model",
|
|
249
|
+
});
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
const from = shapeOf(source.type);
|
|
253
|
+
const to = shapeOf(target.type);
|
|
254
|
+
if (from.array !== to.array) {
|
|
255
|
+
fail("mapping-cardinality-mismatch", {
|
|
256
|
+
sourceShape: from.array ? "a list" : "a single value",
|
|
257
|
+
targetShape: to.array ? "a list" : "a single value",
|
|
258
|
+
});
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
const kind = from.array ? "array" : from.object ? "object" : "leaf";
|
|
262
|
+
if (!from.object) {
|
|
263
|
+
if (to.object) {
|
|
264
|
+
fail("mapping-type-mismatch", {
|
|
265
|
+
reason: "the source is a value and the target is an object",
|
|
266
|
+
});
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
return { sourcePath, source, target, kind, children: [] };
|
|
270
|
+
}
|
|
271
|
+
if (!to.object) {
|
|
272
|
+
fail("mapping-type-mismatch", {
|
|
273
|
+
reason: "the source is an object and the target is a value",
|
|
274
|
+
});
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
const linked = modelMappedTo(walk.program, from.object, walk.mapping);
|
|
278
|
+
if (linked && linked !== to.object) {
|
|
279
|
+
fail("mapping-type-mismatch", {
|
|
280
|
+
reason: `${from.object.name} maps to ${linked.name}, not ${to.object.name}`,
|
|
281
|
+
});
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
const children = stack.includes(from.object)
|
|
285
|
+
? []
|
|
286
|
+
: resolveModel(
|
|
287
|
+
walk,
|
|
288
|
+
from.object,
|
|
289
|
+
to.object,
|
|
290
|
+
deeper,
|
|
291
|
+
sourcePath,
|
|
292
|
+
new Map(),
|
|
293
|
+
[...stack, from.object],
|
|
294
|
+
);
|
|
295
|
+
return { sourcePath, source, target, kind, children };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Whether a type is a list, and the object it holds (directly or as each entry), if any. */
|
|
299
|
+
export function shapeOf(type: Type): { array: boolean; object?: Model } {
|
|
300
|
+
if (type.kind !== "Model") return { array: false };
|
|
301
|
+
if (isArrayModelType(type)) {
|
|
302
|
+
const item = type.indexer!.value;
|
|
303
|
+
return { array: true, object: item.kind === "Model" ? item : undefined };
|
|
304
|
+
}
|
|
305
|
+
return { array: false, object: type };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Whether `target` is one of `context`'s properties. A property `context` has by spread or
|
|
310
|
+
* `is` is a copy, so the copy's source counts too.
|
|
311
|
+
*/
|
|
312
|
+
function isMemberOf(target: ModelProperty, context: Model): boolean {
|
|
313
|
+
return modelProperties(context).some((candidate) => {
|
|
314
|
+
for (
|
|
315
|
+
let p: ModelProperty | undefined = candidate;
|
|
316
|
+
p;
|
|
317
|
+
p = p.sourceProperty
|
|
318
|
+
) {
|
|
319
|
+
if (p === target) return true;
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** A dotted path's property names. A list is stepped through, so `[*]` is optional. */
|
|
326
|
+
const stepsOf = (path: string) =>
|
|
327
|
+
path.split(".").map((step) => step.replace(/\[\*\]$/, ""));
|
|
328
|
+
|
|
329
|
+
const describe = (prop: ModelProperty) =>
|
|
330
|
+
prop.model?.name ? `${prop.model.name}.${prop.name}` : prop.name;
|
|
331
|
+
|
|
332
|
+
/** Why a path that never landed did not: it names nothing, or nothing above it is linked. */
|
|
333
|
+
function unresolvedReason(model: Model, path: string): string {
|
|
334
|
+
let current: Model | undefined = model;
|
|
335
|
+
const steps = stepsOf(path);
|
|
336
|
+
for (const [index, step] of steps.entries()) {
|
|
337
|
+
if (!current) return `${steps.slice(0, index).join(".")} is not an object`;
|
|
338
|
+
const prop: ModelProperty | undefined = modelProperties(current).find(
|
|
339
|
+
(p) => p.name === step,
|
|
340
|
+
);
|
|
341
|
+
if (!prop) {
|
|
342
|
+
const known = modelProperties(current)
|
|
343
|
+
.map((p) => p.name)
|
|
344
|
+
.slice(0, 8)
|
|
345
|
+
.join(", ");
|
|
346
|
+
return `${current.name || "the model"} has no property "${step}" (has ${known})`;
|
|
347
|
+
}
|
|
348
|
+
current = shapeOf(prop.type).object;
|
|
349
|
+
}
|
|
350
|
+
return "a list or a value on the way to it has no link of its own in this mapping, so it has no target to land in";
|
|
351
|
+
}
|
package/src/model.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
getMaxItems,
|
|
15
15
|
getMinItems,
|
|
16
16
|
} from "@typespec/compiler";
|
|
17
|
+
import type { Node } from "@typespec/compiler/ast";
|
|
17
18
|
import { stateKeys } from "./lib.js";
|
|
18
19
|
|
|
19
20
|
interface ConditionBase {
|
|
@@ -24,6 +25,23 @@ export interface EqualsCondition extends ConditionBase {
|
|
|
24
25
|
operator: "equals";
|
|
25
26
|
value: string | number | boolean | null;
|
|
26
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* The negation of `equals`, and the only negated operator.
|
|
30
|
+
*
|
|
31
|
+
* It exists because some source rules are stated against the complement of a value rather
|
|
32
|
+
* than the value -- "active unless the country is the US" -- and expressing those by
|
|
33
|
+
* enumerating every other member of a code list produces a condition no reviewer can check
|
|
34
|
+
* against the source sentence it came from.
|
|
35
|
+
*/
|
|
36
|
+
export interface NotEqualsCondition extends ConditionBase {
|
|
37
|
+
operator: "notEquals";
|
|
38
|
+
value: string | number | boolean | null;
|
|
39
|
+
/**
|
|
40
|
+
* Hold only once the source is answered. An unanswered source is not a different value:
|
|
41
|
+
* "Province applies to a non-US country" does not apply before a country is chosen.
|
|
42
|
+
*/
|
|
43
|
+
requiresAnswer?: boolean;
|
|
44
|
+
}
|
|
27
45
|
export interface InCondition extends ConditionBase {
|
|
28
46
|
operator: "in";
|
|
29
47
|
values: (string | number | boolean | null)[];
|
|
@@ -36,7 +54,11 @@ export interface PresentCondition extends ConditionBase {
|
|
|
36
54
|
operator: "present";
|
|
37
55
|
}
|
|
38
56
|
export type AtomicCondition =
|
|
39
|
-
|
|
57
|
+
| EqualsCondition
|
|
58
|
+
| NotEqualsCondition
|
|
59
|
+
| InCondition
|
|
60
|
+
| CountAtLeastCondition
|
|
61
|
+
| PresentCondition;
|
|
40
62
|
|
|
41
63
|
const conditionSourceModels = new WeakMap<AtomicCondition, Model>();
|
|
42
64
|
|
|
@@ -76,9 +98,7 @@ export interface Block {
|
|
|
76
98
|
responseRole?: ResponseRole;
|
|
77
99
|
label?: string;
|
|
78
100
|
doc?: string;
|
|
79
|
-
sections?: Enum;
|
|
80
101
|
order?: string[];
|
|
81
|
-
overrides: Record<string, Record<string, unknown>>;
|
|
82
102
|
}
|
|
83
103
|
|
|
84
104
|
export type ResponseRole =
|
|
@@ -108,10 +128,10 @@ export function readBlock(
|
|
|
108
128
|
): Block | undefined {
|
|
109
129
|
const q = g(program, stateKeys.questionMeta, model) as
|
|
110
130
|
Record<string, unknown> | undefined;
|
|
111
|
-
const
|
|
131
|
+
const s = g(program, stateKeys.formMeta, model) as
|
|
112
132
|
Record<string, unknown> | undefined;
|
|
113
|
-
if (!q && !
|
|
114
|
-
const meta = (q ??
|
|
133
|
+
if (!q && !s) return undefined;
|
|
134
|
+
const meta = (q ?? s)!;
|
|
115
135
|
return {
|
|
116
136
|
model,
|
|
117
137
|
kind: q ? "question" : "form",
|
|
@@ -126,16 +146,41 @@ export function readBlock(
|
|
|
126
146
|
ResponseRole | undefined,
|
|
127
147
|
label: g(program, stateKeys.label, model) as string | undefined,
|
|
128
148
|
doc: getDoc(program, model),
|
|
129
|
-
sections: g(program, stateKeys.sections, model) as Enum | undefined,
|
|
130
149
|
order: g(program, stateKeys.order, model) as string[] | undefined,
|
|
131
|
-
overrides:
|
|
132
|
-
(g(program, stateKeys.overrides, model) as Record<
|
|
133
|
-
string,
|
|
134
|
-
Record<string, unknown>
|
|
135
|
-
>) ?? {},
|
|
136
150
|
};
|
|
137
151
|
}
|
|
138
152
|
|
|
153
|
+
/** A form's identity, from `@Meta.form`. */
|
|
154
|
+
export interface FormMeta {
|
|
155
|
+
id: string;
|
|
156
|
+
formName: string;
|
|
157
|
+
revision?: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** `@Meta.form` on a model, if it has one. */
|
|
161
|
+
export const modelFormMeta = (p: Program, model: Model) =>
|
|
162
|
+
g(p, stateKeys.formMeta, model) as FormMeta | undefined;
|
|
163
|
+
|
|
164
|
+
/** Every model with `@Meta.form`, in the order the decorators ran. */
|
|
165
|
+
export const allFormMetas = (p: Program) =>
|
|
166
|
+
p.stateMap(stateKeys.formMeta) as Map<Model, FormMeta>;
|
|
167
|
+
|
|
168
|
+
/** Every `@UI.layout` model, with the model it presents. */
|
|
169
|
+
export const allLayouts = (p: Program) =>
|
|
170
|
+
p.stateMap(stateKeys.layout) as Map<Model, Model>;
|
|
171
|
+
|
|
172
|
+
/** A `@UI.field` on a layout entry: the field it lays out, and the reference's syntax. */
|
|
173
|
+
export interface LayoutFieldRef {
|
|
174
|
+
field: ModelProperty;
|
|
175
|
+
node: Node;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export const entryField = (p: Program, prop: ModelProperty) =>
|
|
179
|
+
g(p, stateKeys.layoutField, prop) as LayoutFieldRef | undefined;
|
|
180
|
+
|
|
181
|
+
export const propFlatten = (p: Program, prop: ModelProperty) =>
|
|
182
|
+
g(p, stateKeys.flatten, prop) === true;
|
|
183
|
+
|
|
139
184
|
/** All blocks in the program, in declaration order. */
|
|
140
185
|
export function allBlocks(program: Program): Block[] {
|
|
141
186
|
const out: Block[] = [];
|
|
@@ -153,17 +198,62 @@ export function allBlocks(program: Program): Block[] {
|
|
|
153
198
|
return out;
|
|
154
199
|
}
|
|
155
200
|
|
|
201
|
+
/** A property's label: its own `@UI.label`, else the one its scalar, model or enum type declares. */
|
|
156
202
|
export const propLabel = (p: Program, prop: ModelProperty) =>
|
|
157
|
-
g(p, stateKeys.label, prop)
|
|
203
|
+
(g(p, stateKeys.label, prop) ??
|
|
204
|
+
(prop.type.kind === "Scalar" ||
|
|
205
|
+
prop.type.kind === "Model" ||
|
|
206
|
+
prop.type.kind === "Enum"
|
|
207
|
+
? g(p, stateKeys.label, prop.type)
|
|
208
|
+
: undefined)) as string | undefined;
|
|
158
209
|
export const propHelpText = (p: Program, prop: ModelProperty) =>
|
|
159
210
|
g(p, stateKeys.helpText, prop) as string | undefined;
|
|
160
|
-
|
|
161
|
-
|
|
211
|
+
/** A property's widget: its own `@UI.widget`, else its type's, or a base scalar's. */
|
|
212
|
+
export const propWidget = (p: Program, prop: ModelProperty) => {
|
|
213
|
+
const own = g(p, stateKeys.widget, prop) as string | undefined;
|
|
214
|
+
if (own) return own;
|
|
215
|
+
if (prop.type.kind === "Model")
|
|
216
|
+
return g(p, stateKeys.widget, prop.type) as string | undefined;
|
|
217
|
+
for (
|
|
218
|
+
let s = prop.type.kind === "Scalar" ? prop.type : undefined;
|
|
219
|
+
s;
|
|
220
|
+
s = s.baseScalar
|
|
221
|
+
) {
|
|
222
|
+
const widget = g(p, stateKeys.widget, s) as string | undefined;
|
|
223
|
+
if (widget) return widget;
|
|
224
|
+
}
|
|
225
|
+
return undefined;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/** Every enum a namespace declares with `@Meta.tags`: the tags the program catalogues by. */
|
|
229
|
+
export const declaredTagSets = (p: Program) =>
|
|
230
|
+
new Set(p.stateMap(stateKeys.tagSets).values() as Iterable<Enum>);
|
|
231
|
+
|
|
232
|
+
/** Each question or scalar given a `@Meta.tag`, with the enum members it named. */
|
|
233
|
+
export const tagMembers = (p: Program) =>
|
|
234
|
+
p.stateMap(stateKeys.tagMembers) as Map<Model | Scalar, EnumMember[]>;
|
|
235
|
+
|
|
236
|
+
/** Every enum a namespace declares with `@Meta.entities`: what questions are asked about. */
|
|
237
|
+
export const declaredEntitySets = (p: Program) =>
|
|
238
|
+
new Set(p.stateMap(stateKeys.entitySets).values() as Iterable<Enum>);
|
|
239
|
+
|
|
240
|
+
/** Each question that names an entity, with the enum member it named. */
|
|
241
|
+
export const entityMembers = (p: Program) =>
|
|
242
|
+
p.stateMap(stateKeys.entityMember) as Map<Model | Scalar, EnumMember>;
|
|
243
|
+
|
|
244
|
+
/** Every enum a namespace declares with `@UI.widgets`: the widgets the program provides. */
|
|
245
|
+
export const declaredWidgetSets = (p: Program) =>
|
|
246
|
+
new Set(p.stateMap(stateKeys.widgets).values() as Iterable<Enum>);
|
|
247
|
+
|
|
248
|
+
/** Each property given a `@UI.widget`, with the enum member it named. */
|
|
249
|
+
export const widgetMembers = (p: Program) =>
|
|
250
|
+
p.stateMap(stateKeys.widgetMember) as Map<
|
|
251
|
+
ModelProperty | Scalar | Model,
|
|
252
|
+
EnumMember
|
|
253
|
+
>;
|
|
162
254
|
export const propEncodedCheckboxGroup = (p: Program, prop: ModelProperty) =>
|
|
163
255
|
g(p, stateKeys.encodedCheckboxGroup, prop) as
|
|
164
256
|
Record<string, unknown> | undefined;
|
|
165
|
-
export const propSection = (p: Program, prop: ModelProperty) =>
|
|
166
|
-
g(p, stateKeys.section, prop) as { name: string; label?: string } | undefined;
|
|
167
257
|
export const propReadOnly = (p: Program, prop: ModelProperty) =>
|
|
168
258
|
g(p, stateKeys.readOnly, prop) === true;
|
|
169
259
|
export const propResponseRole = (p: Program, prop: ModelProperty) =>
|
|
@@ -184,6 +274,90 @@ export const propRequiredWhen = (p: Program, prop: ModelProperty) =>
|
|
|
184
274
|
(g(p, stateKeys.requiredWhen, prop) as Condition[]) ?? [];
|
|
185
275
|
export const propNotBefore = (p: Program, prop: ModelProperty) =>
|
|
186
276
|
g(p, stateKeys.notBefore, prop) as ModelProperty | undefined;
|
|
277
|
+
|
|
278
|
+
// --- mapping ----------------------------------------------------------------
|
|
279
|
+
|
|
280
|
+
/** One `@Mapping.to`: in `mapping`, the decorated type corresponds to `mappedTo`. */
|
|
281
|
+
export interface MappingLink {
|
|
282
|
+
mapping: EnumMember;
|
|
283
|
+
mappedTo: Model | ModelProperty;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** One `@Mapping.path`: in `mapping`, the dotted source path corresponds to `mappedTo`. */
|
|
287
|
+
export interface MappingPathLink {
|
|
288
|
+
mapping: EnumMember;
|
|
289
|
+
sourcePath: string;
|
|
290
|
+
mappedTo: ModelProperty;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** One `@Mapping.from`: in `mapping`, the decorated target takes its value from `source`. */
|
|
294
|
+
export interface MappingFromLink {
|
|
295
|
+
mapping: EnumMember;
|
|
296
|
+
source: Model | ModelProperty;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Every `@Mapping.from` on each target model and property, in every mapping. */
|
|
300
|
+
export const allMappingFromLinks = (p: Program) =>
|
|
301
|
+
p.stateMap(stateKeys.mappedFrom) as Map<
|
|
302
|
+
Model | ModelProperty,
|
|
303
|
+
MappingFromLink[]
|
|
304
|
+
>;
|
|
305
|
+
|
|
306
|
+
/** The fields a target property is filled from in `mapping`, one per form that fills it. */
|
|
307
|
+
export const propMappedFrom = (
|
|
308
|
+
p: Program,
|
|
309
|
+
prop: ModelProperty,
|
|
310
|
+
mapping: EnumMember,
|
|
311
|
+
) =>
|
|
312
|
+
((g(p, stateKeys.mappedFrom, prop) as MappingFromLink[] | undefined) ?? [])
|
|
313
|
+
.filter(
|
|
314
|
+
(link) =>
|
|
315
|
+
link.mapping === mapping && link.source.kind === "ModelProperty",
|
|
316
|
+
)
|
|
317
|
+
.map((link) => link.source as ModelProperty);
|
|
318
|
+
|
|
319
|
+
/** Every enum a namespace declares with `@Mapping.targets`: the mappings the program produces. */
|
|
320
|
+
export const declaredMappingSets = (p: Program) =>
|
|
321
|
+
new Set(p.stateMap(stateKeys.mappingTargets).values() as Iterable<Enum>);
|
|
322
|
+
|
|
323
|
+
/** Every member of a declared mapping set, in declaration order. */
|
|
324
|
+
export const mappingTargets = (p: Program): EnumMember[] =>
|
|
325
|
+
[...declaredMappingSets(p)].flatMap((e) => [...e.members.values()]);
|
|
326
|
+
|
|
327
|
+
/** Every `@Mapping.to` on each model and property, in every mapping. */
|
|
328
|
+
export const allMappingLinks = (p: Program) =>
|
|
329
|
+
p.stateMap(stateKeys.mappedTo) as Map<Model | ModelProperty, MappingLink[]>;
|
|
330
|
+
|
|
331
|
+
/** Every `@Mapping.path` on each model, in every mapping. */
|
|
332
|
+
export const allMappingPathLinks = (p: Program) =>
|
|
333
|
+
p.stateMap(stateKeys.mappingPaths) as Map<Model, MappingPathLink[]>;
|
|
334
|
+
|
|
335
|
+
/** The model a model corresponds to in `mapping`; the first, if it is linked twice. */
|
|
336
|
+
export const modelMappedTo = (p: Program, model: Model, mapping: EnumMember) =>
|
|
337
|
+
((g(p, stateKeys.mappedTo, model) as MappingLink[] | undefined) ?? []).find(
|
|
338
|
+
(link) => link.mapping === mapping && link.mappedTo.kind === "Model",
|
|
339
|
+
)?.mappedTo as Model | undefined;
|
|
340
|
+
|
|
341
|
+
/** The property a property corresponds to in `mapping`; the first, if it is linked twice. */
|
|
342
|
+
export const propMappedTo = (
|
|
343
|
+
p: Program,
|
|
344
|
+
prop: ModelProperty,
|
|
345
|
+
mapping: EnumMember,
|
|
346
|
+
) =>
|
|
347
|
+
((g(p, stateKeys.mappedTo, prop) as MappingLink[] | undefined) ?? []).find(
|
|
348
|
+
(link) =>
|
|
349
|
+
link.mapping === mapping && link.mappedTo.kind === "ModelProperty",
|
|
350
|
+
)?.mappedTo as ModelProperty | undefined;
|
|
351
|
+
|
|
352
|
+
/** A model's `@Mapping.path` links in `mapping`. */
|
|
353
|
+
export const modelMappingPaths = (
|
|
354
|
+
p: Program,
|
|
355
|
+
model: Model,
|
|
356
|
+
mapping: EnumMember,
|
|
357
|
+
) =>
|
|
358
|
+
(
|
|
359
|
+
(g(p, stateKeys.mappingPaths, model) as MappingPathLink[] | undefined) ?? []
|
|
360
|
+
).filter((link) => link.mapping === mapping);
|
|
187
361
|
export const propValidationConstraints = (p: Program, prop: ModelProperty) =>
|
|
188
362
|
(g(p, stateKeys.validationConstraints, prop) as
|
|
189
363
|
Record<string, unknown> | undefined) ?? {};
|
|
@@ -270,12 +444,6 @@ export const modelLabel = (p: Program, model: Model) =>
|
|
|
270
444
|
export const modelOrder = (p: Program, model: Model) =>
|
|
271
445
|
g(p, stateKeys.order, model) as string[] | undefined;
|
|
272
446
|
|
|
273
|
-
export const propOverrides = (p: Program, prop: ModelProperty) =>
|
|
274
|
-
(g(p, stateKeys.overrides, prop) as Record<
|
|
275
|
-
string,
|
|
276
|
-
Record<string, unknown>
|
|
277
|
-
>) ?? {};
|
|
278
|
-
|
|
279
447
|
/** Own and inherited properties in declaration order, with the derived declaration winning. */
|
|
280
448
|
export function modelProperties(model: Model): ModelProperty[] {
|
|
281
449
|
const chain: Model[] = [];
|
|
@@ -298,7 +466,7 @@ export function modelProperties(model: Model): ModelProperty[] {
|
|
|
298
466
|
export function orderedProps(program: Program, block: Block): ModelProperty[] {
|
|
299
467
|
if (block.model.kind !== "Model") return [];
|
|
300
468
|
// A form may extend a question to preserve flat response fields with explicit semantic
|
|
301
|
-
// lineage. Its inherited fields remain
|
|
469
|
+
// lineage. Its inherited fields remain its members for presentation.
|
|
302
470
|
// Question artifacts themselves keep their local surface and compose their base through
|
|
303
471
|
// JSON Schema `allOf`, so walking inherited properties there would duplicate that surface.
|
|
304
472
|
const candidates =
|