simpler-forms 0.1.0 → 0.2.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 +79 -0
- package/dist/src/decorators/mapping.d.ts +7 -1
- package/dist/src/decorators/mapping.js +103 -3
- package/dist/src/decorators/mapping.js.map +1 -1
- package/dist/src/decorators/meta.d.ts +3 -3
- package/dist/src/decorators/meta.js.map +1 -1
- package/dist/src/decorators/shared.d.ts +2 -2
- package/dist/src/decorators/shared.js.map +1 -1
- package/dist/src/emitter.js +7 -3
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/emitters/overlay.d.ts +7 -2
- package/dist/src/emitters/overlay.js +27 -19
- package/dist/src/emitters/overlay.js.map +1 -1
- package/dist/src/index.d.ts +8 -3
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/lib.d.ts +104 -4
- package/dist/src/lib.js +43 -0
- package/dist/src/lib.js.map +1 -1
- package/dist/src/linter.js +6 -2
- package/dist/src/linter.js.map +1 -1
- package/dist/src/mapping-from.d.ts +2 -1
- package/dist/src/mapping-from.js +30 -10
- package/dist/src/mapping-from.js.map +1 -1
- package/dist/src/mapping.d.ts +7 -0
- package/dist/src/mapping.js.map +1 -1
- package/dist/src/model.d.ts +76 -8
- package/dist/src/model.js +43 -2
- package/dist/src/model.js.map +1 -1
- package/dist/src/public.d.ts +1 -1
- package/dist/src/public.js +1 -1
- package/dist/src/public.js.map +1 -1
- package/dist/src/validate.js +29 -1
- package/dist/src/validate.js.map +1 -1
- package/lib/mapping.tsp +102 -0
- package/lib/meta.tsp +3 -3
- package/lib/types.tsp +13 -0
- package/package.json +11 -11
- package/src/decorators/mapping.ts +152 -9
- package/src/decorators/meta.ts +7 -3
- package/src/decorators/shared.ts +6 -1
- package/src/emitter.ts +17 -3
- package/src/emitters/overlay.ts +40 -13
- package/src/index.ts +5 -0
- package/src/lib.ts +43 -0
- package/src/linter.ts +16 -7
- package/src/mapping-from.ts +33 -10
- package/src/mapping.ts +7 -0
- package/src/model.ts +119 -11
- package/src/public.ts +10 -0
- package/src/validate.ts +33 -1
package/src/lib.ts
CHANGED
|
@@ -160,6 +160,32 @@ export const $lib = createTypeSpecLibrary({
|
|
|
160
160
|
default: paramMessage`${"name"} is linked in mapping ${"mapping"} from ${"enumName"}, which no namespace declares with @Mapping.targets, so no emitter is known to produce it. Declare the program's mappings once, e.g. @Mapping.targets(${"enumName"}).`,
|
|
161
161
|
},
|
|
162
162
|
},
|
|
163
|
+
"mapping-transform-undeclared": {
|
|
164
|
+
severity: "error",
|
|
165
|
+
messages: {
|
|
166
|
+
default: paramMessage`${"name"} is transformed with ${"transform"} from ${"enumName"}, which no namespace declares with @Mapping.transforms, so no emitter is known to implement it. Declare the program's transformations once, e.g. @Mapping.transforms(${"enumName"}).`,
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
"mapping-match-and-transform": {
|
|
170
|
+
severity: "error",
|
|
171
|
+
messages: {
|
|
172
|
+
default: paramMessage`${"name"} has both @Mapping.match and @Mapping.transform. A property changes its value one way: spell a lookup out with @Mapping.match, or name a transformation.`,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
"mapping-when-value": {
|
|
176
|
+
severity: "error",
|
|
177
|
+
messages: {
|
|
178
|
+
exists: paramMessage`@Mapping.when on ${"name"} tests whether ${"source"} is answered, so it takes no value.`,
|
|
179
|
+
in: paramMessage`@Mapping.when on ${"name"} tests whether ${"source"} is one of a list's entries, so its value is a list.`,
|
|
180
|
+
default: paramMessage`@Mapping.when on ${"name"} compares ${"source"} with ${"operator"}, so it needs a value.`,
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
"mapping-when-source": {
|
|
184
|
+
severity: "error",
|
|
185
|
+
messages: {
|
|
186
|
+
default: paramMessage`@Mapping.when on ${"name"} names a model as its source: name a field of the form, or one item of a list field with Item.`,
|
|
187
|
+
},
|
|
188
|
+
},
|
|
163
189
|
"mapping-duplicate": {
|
|
164
190
|
severity: "error",
|
|
165
191
|
messages: {
|
|
@@ -208,6 +234,18 @@ export const $lib = createTypeSpecLibrary({
|
|
|
208
234
|
default: paramMessage`In mapping ${"mapping"}, ${"target"} is ${"targetShape"} but ${"source"}, which fills it, is ${"sourceShape"}.`,
|
|
209
235
|
},
|
|
210
236
|
},
|
|
237
|
+
"mapping-from-select-not-a-list": {
|
|
238
|
+
severity: "error",
|
|
239
|
+
messages: {
|
|
240
|
+
default: paramMessage`In mapping ${"mapping"}, ${"target"} reads items of ${"source"} with Item or Slice, but ${"source"} is not a list.`,
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
"mapping-from-select-on-model": {
|
|
244
|
+
severity: "error",
|
|
245
|
+
messages: {
|
|
246
|
+
default: paramMessage`In mapping ${"mapping"}, ${"target"} is a target model, filled from a form: Item and Slice name items of a list field, for a target property.`,
|
|
247
|
+
},
|
|
248
|
+
},
|
|
211
249
|
"mapping-from-wrapper": {
|
|
212
250
|
severity: "warning",
|
|
213
251
|
messages: {
|
|
@@ -395,6 +433,11 @@ export const $lib = createTypeSpecLibrary({
|
|
|
395
433
|
mappedTo: {},
|
|
396
434
|
mappingPaths: {},
|
|
397
435
|
mappedFrom: {},
|
|
436
|
+
mappingTransformSets: {},
|
|
437
|
+
mappingTransform: {},
|
|
438
|
+
mappingMatch: {},
|
|
439
|
+
mappingRequired: {},
|
|
440
|
+
mappingWhen: {},
|
|
398
441
|
},
|
|
399
442
|
} as const);
|
|
400
443
|
|
package/src/linter.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Model, ModelProperty, Scalar } from "@typespec/compiler";
|
|
1
|
+
import type { Enum, Model, ModelProperty, Scalar } from "@typespec/compiler";
|
|
2
2
|
import {
|
|
3
3
|
createRule,
|
|
4
4
|
defineCodeFix,
|
|
@@ -47,12 +47,14 @@ const orphanQuestion = createRule({
|
|
|
47
47
|
// property, by extending it, or by being reached through a model that is not itself
|
|
48
48
|
// a question -- and all three have to count, or the rule reports a question that a
|
|
49
49
|
// form does ask.
|
|
50
|
-
const seen = new Set<
|
|
51
|
-
const reach = (type:
|
|
50
|
+
const seen = new Set<Held>();
|
|
51
|
+
const reach = (type: Held, viaComposition: boolean): void => {
|
|
52
52
|
const block = byType.get(type);
|
|
53
53
|
if (block && viaComposition) referenced.add(block.id);
|
|
54
54
|
if (seen.has(type)) return;
|
|
55
55
|
seen.add(type);
|
|
56
|
+
// A code list holds nothing further.
|
|
57
|
+
if (type.kind === "Enum") return;
|
|
56
58
|
// Extending a question composes it, for a scalar as much as for a model: naming a
|
|
57
59
|
// shape does not make the shape unasked.
|
|
58
60
|
const base = type.kind === "Model" ? type.baseModel : type.baseScalar;
|
|
@@ -91,16 +93,23 @@ const orphanQuestion = createRule({
|
|
|
91
93
|
* three codes, so the *list* is what the form asks -- and the entries can be a question too.
|
|
92
94
|
* Returning only the entries reports the list as composed by nothing.
|
|
93
95
|
*/
|
|
94
|
-
function held(prop: ModelProperty):
|
|
96
|
+
function held(prop: ModelProperty): Held[] {
|
|
95
97
|
const type = prop.type;
|
|
96
|
-
if (type.kind === "Scalar") return [type];
|
|
98
|
+
if (type.kind === "Scalar" || type.kind === "Enum") return [type];
|
|
97
99
|
if (type.kind !== "Model") return [];
|
|
98
|
-
const out:
|
|
100
|
+
const out: Held[] = [type];
|
|
99
101
|
const item = type.indexer?.value;
|
|
100
|
-
if (
|
|
102
|
+
if (
|
|
103
|
+
item &&
|
|
104
|
+
(item.kind === "Model" || item.kind === "Scalar" || item.kind === "Enum")
|
|
105
|
+
)
|
|
106
|
+
out.push(item);
|
|
101
107
|
return out;
|
|
102
108
|
}
|
|
103
109
|
|
|
110
|
+
/** What a question can be: an object, a single value, or a code list. */
|
|
111
|
+
type Held = Model | Scalar | Enum;
|
|
112
|
+
|
|
104
113
|
const questionDocs = createRule({
|
|
105
114
|
name: "require-question-docs",
|
|
106
115
|
severity: "warning",
|
package/src/mapping-from.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { type MappingNode, type ResolvedMapping, shapeOf } from "./mapping.js";
|
|
|
10
10
|
import {
|
|
11
11
|
allMappingFromLinks,
|
|
12
12
|
modelProperties,
|
|
13
|
-
|
|
13
|
+
propMappedFromLinks,
|
|
14
14
|
} from "./model.js";
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -23,7 +23,8 @@ import {
|
|
|
23
23
|
* found below a field with no element of its own lands in a group. A target property with
|
|
24
24
|
* no `@Mapping.from` whose own properties are filled is a wrapper element, which a tree keyed
|
|
25
25
|
* by the form's properties cannot hold: it is left out, with a `mapping-from-wrapper` problem
|
|
26
|
-
* for an emitter whose output cannot hold it either to report.
|
|
26
|
+
* for an emitter whose output cannot hold it either to report. A field named with `Item` or
|
|
27
|
+
* `Slice` fills its target with some of its items: the node's `select`.
|
|
27
28
|
*/
|
|
28
29
|
export function resolveMappingFrom(
|
|
29
30
|
program: Program,
|
|
@@ -43,8 +44,8 @@ export function resolveMappingFrom(
|
|
|
43
44
|
const nodes: MappingNode[] = [];
|
|
44
45
|
for (const element of modelProperties(into)) {
|
|
45
46
|
const to = shapeOf(element.type);
|
|
46
|
-
const
|
|
47
|
-
if (!
|
|
47
|
+
const links = propMappedFromLinks(program, element, mapping);
|
|
48
|
+
if (!links.length) {
|
|
48
49
|
if (!to.object || to.array || stack.includes(to.object)) continue;
|
|
49
50
|
// Structure: its own elements are filled from the same field as its parent.
|
|
50
51
|
const inner = walk(to.object, from, prefix, [...stack, to.object]);
|
|
@@ -62,10 +63,10 @@ export function resolveMappingFrom(
|
|
|
62
63
|
);
|
|
63
64
|
continue;
|
|
64
65
|
}
|
|
65
|
-
const
|
|
66
|
-
.map((
|
|
67
|
-
.find((
|
|
68
|
-
if (!
|
|
66
|
+
const found = links
|
|
67
|
+
.map((link) => ({ link, path: pathTo(from, link.source) }))
|
|
68
|
+
.find((candidate) => candidate.path !== undefined);
|
|
69
|
+
if (!found) {
|
|
69
70
|
problems.push(
|
|
70
71
|
createDiagnostic({
|
|
71
72
|
code: "mapping-from-unresolved",
|
|
@@ -73,16 +74,37 @@ export function resolveMappingFrom(
|
|
|
73
74
|
format: {
|
|
74
75
|
mapping: mapping.name,
|
|
75
76
|
target: describe(element),
|
|
76
|
-
source:
|
|
77
|
+
source: links.map((link) => describe(link.source)).join(" or "),
|
|
77
78
|
context: from.name || "an anonymous model",
|
|
78
79
|
},
|
|
79
80
|
}),
|
|
80
81
|
);
|
|
81
82
|
continue;
|
|
82
83
|
}
|
|
84
|
+
const { link } = found;
|
|
85
|
+
const path = found.path!;
|
|
83
86
|
const source = path.at(-1)!;
|
|
84
87
|
const sourcePath = [...prefix, ...path.map((p) => p.name)];
|
|
85
|
-
const
|
|
88
|
+
const listed = shapeOf(source.type);
|
|
89
|
+
if (link.select && !listed.array) {
|
|
90
|
+
problems.push(
|
|
91
|
+
createDiagnostic({
|
|
92
|
+
code: "mapping-from-select-not-a-list",
|
|
93
|
+
target: element,
|
|
94
|
+
format: {
|
|
95
|
+
mapping: mapping.name,
|
|
96
|
+
target: describe(element),
|
|
97
|
+
source: sourcePath.join("."),
|
|
98
|
+
},
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
// One item of a list is a single value, or a single object; a slice is still a list.
|
|
104
|
+
const shape =
|
|
105
|
+
link.select && "index" in link.select
|
|
106
|
+
? { array: false, object: listed.object }
|
|
107
|
+
: listed;
|
|
86
108
|
if (shape.array !== to.array) {
|
|
87
109
|
problems.push(
|
|
88
110
|
createDiagnostic({
|
|
@@ -109,6 +131,7 @@ export function resolveMappingFrom(
|
|
|
109
131
|
target: element,
|
|
110
132
|
kind: shape.array ? "array" : shape.object ? "object" : "leaf",
|
|
111
133
|
children,
|
|
134
|
+
...(link.select ? { select: link.select } : {}),
|
|
112
135
|
});
|
|
113
136
|
}
|
|
114
137
|
return nodes;
|
package/src/mapping.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
import { isArrayModelType } from "@typespec/compiler";
|
|
9
9
|
import {
|
|
10
10
|
type MappingPathLink,
|
|
11
|
+
type MappingSelection,
|
|
11
12
|
modelMappedTo,
|
|
12
13
|
modelMappingPaths,
|
|
13
14
|
modelProperties,
|
|
@@ -35,6 +36,12 @@ export interface MappingNode {
|
|
|
35
36
|
kind: MappingNodeKind;
|
|
36
37
|
/** The linked properties below an object, a list of objects, or a group, in source order. */
|
|
37
38
|
children: MappingNode[];
|
|
39
|
+
/**
|
|
40
|
+
* Which items of the list at `sourcePath` fill the target, when its `@Mapping.from` names
|
|
41
|
+
* them with `Item` or `Slice`. `kind` describes what's selected: one item of a list of
|
|
42
|
+
* objects is an `object`.
|
|
43
|
+
*/
|
|
44
|
+
select?: MappingSelection;
|
|
38
45
|
}
|
|
39
46
|
|
|
40
47
|
/** A model's mapping in one target, resolved through every model it composes. */
|
package/src/model.ts
CHANGED
|
@@ -85,8 +85,11 @@ export type Condition = AtomicCondition | AnyCondition;
|
|
|
85
85
|
|
|
86
86
|
/** Everything the emitters need about one block, read out of decorator state. */
|
|
87
87
|
export interface Block {
|
|
88
|
-
/**
|
|
89
|
-
|
|
88
|
+
/**
|
|
89
|
+
* A Model for object-valued blocks, a Scalar for single-value questions, and an Enum for a
|
|
90
|
+
* code list published as a question of its own.
|
|
91
|
+
*/
|
|
92
|
+
model: Model | Scalar | Enum;
|
|
90
93
|
kind: "question" | "form";
|
|
91
94
|
/** True when the block is a single value rather than an object. */
|
|
92
95
|
scalar: boolean;
|
|
@@ -124,7 +127,7 @@ function enumName(value: unknown): string | undefined {
|
|
|
124
127
|
|
|
125
128
|
export function readBlock(
|
|
126
129
|
program: Program,
|
|
127
|
-
model: Model | Scalar,
|
|
130
|
+
model: Model | Scalar | Enum,
|
|
128
131
|
): Block | undefined {
|
|
129
132
|
const q = g(program, stateKeys.questionMeta, model) as
|
|
130
133
|
Record<string, unknown> | undefined;
|
|
@@ -135,7 +138,7 @@ export function readBlock(
|
|
|
135
138
|
return {
|
|
136
139
|
model,
|
|
137
140
|
kind: q ? "question" : "form",
|
|
138
|
-
scalar: model.kind
|
|
141
|
+
scalar: model.kind !== "Model",
|
|
139
142
|
id: String(meta.id),
|
|
140
143
|
meta,
|
|
141
144
|
classification: (enumName(meta.classification) ??
|
|
@@ -184,9 +187,13 @@ export const propFlatten = (p: Program, prop: ModelProperty) =>
|
|
|
184
187
|
/** All blocks in the program, in declaration order. */
|
|
185
188
|
export function allBlocks(program: Program): Block[] {
|
|
186
189
|
const out: Block[] = [];
|
|
187
|
-
const seen = new Set<Model | Scalar>();
|
|
190
|
+
const seen = new Set<Model | Scalar | Enum>();
|
|
188
191
|
const walk = (ns: any): void => {
|
|
189
|
-
for (const m of [
|
|
192
|
+
for (const m of [
|
|
193
|
+
...ns.models.values(),
|
|
194
|
+
...ns.scalars.values(),
|
|
195
|
+
...ns.enums.values(),
|
|
196
|
+
]) {
|
|
190
197
|
if (seen.has(m)) continue;
|
|
191
198
|
seen.add(m);
|
|
192
199
|
const b = readBlock(program, m);
|
|
@@ -231,7 +238,7 @@ export const declaredTagSets = (p: Program) =>
|
|
|
231
238
|
|
|
232
239
|
/** Each question or scalar given a `@Meta.tag`, with the enum members it named. */
|
|
233
240
|
export const tagMembers = (p: Program) =>
|
|
234
|
-
p.stateMap(stateKeys.tagMembers) as Map<Model | Scalar, EnumMember[]>;
|
|
241
|
+
p.stateMap(stateKeys.tagMembers) as Map<Model | Scalar | Enum, EnumMember[]>;
|
|
235
242
|
|
|
236
243
|
/** Every enum a namespace declares with `@Meta.entities`: what questions are asked about. */
|
|
237
244
|
export const declaredEntitySets = (p: Program) =>
|
|
@@ -239,7 +246,7 @@ export const declaredEntitySets = (p: Program) =>
|
|
|
239
246
|
|
|
240
247
|
/** Each question that names an entity, with the enum member it named. */
|
|
241
248
|
export const entityMembers = (p: Program) =>
|
|
242
|
-
p.stateMap(stateKeys.entityMember) as Map<Model | Scalar, EnumMember>;
|
|
249
|
+
p.stateMap(stateKeys.entityMember) as Map<Model | Scalar | Enum, EnumMember>;
|
|
243
250
|
|
|
244
251
|
/** Every enum a namespace declares with `@UI.widgets`: the widgets the program provides. */
|
|
245
252
|
export const declaredWidgetSets = (p: Program) =>
|
|
@@ -258,9 +265,9 @@ export const propReadOnly = (p: Program, prop: ModelProperty) =>
|
|
|
258
265
|
g(p, stateKeys.readOnly, prop) === true;
|
|
259
266
|
export const propResponseRole = (p: Program, prop: ModelProperty) =>
|
|
260
267
|
g(p, stateKeys.responseRole, prop) as ResponseRole | undefined;
|
|
261
|
-
export const typeResponseRole = (p: Program, type: Model | Scalar) =>
|
|
268
|
+
export const typeResponseRole = (p: Program, type: Model | Scalar | Enum) =>
|
|
262
269
|
g(p, stateKeys.responseRole, type) as ResponseRole | undefined;
|
|
263
|
-
export const typeTags = (p: Program, type: Model | Scalar) =>
|
|
270
|
+
export const typeTags = (p: Program, type: Model | Scalar | Enum) =>
|
|
264
271
|
(g(p, stateKeys.tags, type) as string[]) ?? [];
|
|
265
272
|
export const propReadOnlyWhen = (p: Program, prop: ModelProperty) =>
|
|
266
273
|
(g(p, stateKeys.readOnlyWhen, prop) as Condition[]) ?? [];
|
|
@@ -294,8 +301,18 @@ export interface MappingPathLink {
|
|
|
294
301
|
export interface MappingFromLink {
|
|
295
302
|
mapping: EnumMember;
|
|
296
303
|
source: Model | ModelProperty;
|
|
304
|
+
/** Which items of the list field `source` the link reads, when it names one with `Item` or `Slice`. */
|
|
305
|
+
select?: MappingSelection;
|
|
297
306
|
}
|
|
298
307
|
|
|
308
|
+
/**
|
|
309
|
+
* Which items of a list field a `@Mapping.from` link reads: one by `index` (`Mapping.Item`),
|
|
310
|
+
* or those from `start` to `stop`, excluded (`Mapping.Slice`). A negative number counts back
|
|
311
|
+
* from the end.
|
|
312
|
+
*/
|
|
313
|
+
export type MappingSelection =
|
|
314
|
+
{ index: number } | { start: number; stop?: number };
|
|
315
|
+
|
|
299
316
|
/** Every `@Mapping.from` on each target model and property, in every mapping. */
|
|
300
317
|
export const allMappingFromLinks = (p: Program) =>
|
|
301
318
|
p.stateMap(stateKeys.mappedFrom) as Map<
|
|
@@ -316,10 +333,96 @@ export const propMappedFrom = (
|
|
|
316
333
|
)
|
|
317
334
|
.map((link) => link.source as ModelProperty);
|
|
318
335
|
|
|
336
|
+
/**
|
|
337
|
+
* The links a target property is filled from in `mapping`, one per form that fills it: each
|
|
338
|
+
* field, and which of its items the link reads, if it names them with `Item` or `Slice`.
|
|
339
|
+
*/
|
|
340
|
+
export const propMappedFromLinks = (
|
|
341
|
+
p: Program,
|
|
342
|
+
prop: ModelProperty,
|
|
343
|
+
mapping: EnumMember,
|
|
344
|
+
) =>
|
|
345
|
+
(
|
|
346
|
+
(g(p, stateKeys.mappedFrom, prop) as MappingFromLink[] | undefined) ?? []
|
|
347
|
+
).filter(
|
|
348
|
+
(link) => link.mapping === mapping && link.source.kind === "ModelProperty",
|
|
349
|
+
) as (MappingFromLink & { source: ModelProperty })[];
|
|
350
|
+
|
|
319
351
|
/** Every enum a namespace declares with `@Mapping.targets`: the mappings the program produces. */
|
|
320
352
|
export const declaredMappingSets = (p: Program) =>
|
|
321
353
|
new Set(p.stateMap(stateKeys.mappingTargets).values() as Iterable<Enum>);
|
|
322
354
|
|
|
355
|
+
/** Every enum a namespace declares with `@Mapping.transforms`: the transformations emitters implement. */
|
|
356
|
+
export const declaredTransformSets = (p: Program) =>
|
|
357
|
+
new Set(
|
|
358
|
+
p.stateMap(stateKeys.mappingTransformSets).values() as Iterable<Enum>,
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
/** A target property's `@Mapping.transform`: the transformation, by the name an emitter implements it under. */
|
|
362
|
+
export interface MappingTransform {
|
|
363
|
+
/** The member's value, or its name when it has none. */
|
|
364
|
+
name: string;
|
|
365
|
+
member: EnumMember;
|
|
366
|
+
params?: Record<string, unknown>;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Every target property with a `@Mapping.transform`. */
|
|
370
|
+
export const allTransformedProps = (p: Program) => [
|
|
371
|
+
...(p.stateMap(stateKeys.mappingTransform).keys() as Iterable<ModelProperty>),
|
|
372
|
+
];
|
|
373
|
+
|
|
374
|
+
/** The transformation `@Mapping.transform` gives a target property, if any. */
|
|
375
|
+
export function propTransform(
|
|
376
|
+
p: Program,
|
|
377
|
+
prop: ModelProperty,
|
|
378
|
+
): MappingTransform | undefined {
|
|
379
|
+
const stored = g(p, stateKeys.mappingTransform, prop) as
|
|
380
|
+
{ member: EnumMember; params?: Record<string, unknown> } | undefined;
|
|
381
|
+
if (!stored) return undefined;
|
|
382
|
+
const { member, params } = stored;
|
|
383
|
+
const name = typeof member.value === "string" ? member.value : member.name;
|
|
384
|
+
return { name, member, ...(params ? { params } : {}) };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* A target property's `@Mapping.match`: the value each listed value is written as, and what
|
|
389
|
+
* an unlisted one is written as, a fixed `value` or a `field`'s, if anything.
|
|
390
|
+
*/
|
|
391
|
+
export interface MappingMatch {
|
|
392
|
+
cases: Record<string, string | number | boolean>;
|
|
393
|
+
default?: { value: string | number | boolean } | { field: ModelProperty };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** The lookup `@Mapping.match` gives a target property, if any. */
|
|
397
|
+
export const propMatch = (p: Program, prop: ModelProperty) =>
|
|
398
|
+
g(p, stateKeys.mappingMatch, prop) as MappingMatch | undefined;
|
|
399
|
+
|
|
400
|
+
/** Whether `@Mapping.required` writes a target property even when its value is empty. */
|
|
401
|
+
export const propMappingRequired = (p: Program, prop: ModelProperty) =>
|
|
402
|
+
p.stateSet(stateKeys.mappingRequired).has(prop);
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* One of a target property's `@Mapping.when` conditions: the answer at `source` (or the
|
|
406
|
+
* item `select` picks of it) compared with `value` by `operator`, a member name of
|
|
407
|
+
* `SimplerForms.Operator`.
|
|
408
|
+
*/
|
|
409
|
+
export interface MappingCondition {
|
|
410
|
+
source: ModelProperty;
|
|
411
|
+
select?: MappingSelection;
|
|
412
|
+
operator: "equals" | "notEquals" | "in" | "contains" | "exists";
|
|
413
|
+
value?: unknown;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* The conditions `@Mapping.when` gives a target property, all of which must hold, in the
|
|
418
|
+
* order they're written. Decorators run from the bottom up, so they're stored reversed.
|
|
419
|
+
*/
|
|
420
|
+
export const propMappingConditions = (p: Program, prop: ModelProperty) =>
|
|
421
|
+
[
|
|
422
|
+
...((g(p, stateKeys.mappingWhen, prop) as MappingCondition[] | undefined) ??
|
|
423
|
+
[]),
|
|
424
|
+
].reverse();
|
|
425
|
+
|
|
323
426
|
/** Every member of a declared mapping set, in declaration order. */
|
|
324
427
|
export const mappingTargets = (p: Program): EnumMember[] =>
|
|
325
428
|
[...declaredMappingSets(p)].flatMap((e) => [...e.members.values()]);
|
|
@@ -493,8 +596,13 @@ export function orderedProps(program: Program, block: Block): ModelProperty[] {
|
|
|
493
596
|
*/
|
|
494
597
|
export function blockAncestry(
|
|
495
598
|
program: Program,
|
|
496
|
-
model: Model | Scalar,
|
|
599
|
+
model: Model | Scalar | Enum,
|
|
497
600
|
): string[] {
|
|
601
|
+
// An enum extends nothing: its ancestry is itself.
|
|
602
|
+
if (model.kind === "Enum") {
|
|
603
|
+
const block = readBlock(program, model);
|
|
604
|
+
return block ? [block.id] : [];
|
|
605
|
+
}
|
|
498
606
|
const out: string[] = [];
|
|
499
607
|
let current: Model | Scalar | undefined = model;
|
|
500
608
|
while (current) {
|
package/src/public.ts
CHANGED
|
@@ -112,9 +112,19 @@ export {
|
|
|
112
112
|
propMappedTo,
|
|
113
113
|
modelMappingPaths,
|
|
114
114
|
propMappedFrom,
|
|
115
|
+
propMappedFromLinks,
|
|
116
|
+
propTransform,
|
|
117
|
+
propMatch,
|
|
118
|
+
propMappingRequired,
|
|
119
|
+
propMappingConditions,
|
|
120
|
+
declaredTransformSets,
|
|
115
121
|
type MappingLink,
|
|
116
122
|
type MappingPathLink,
|
|
117
123
|
type MappingFromLink,
|
|
124
|
+
type MappingSelection,
|
|
125
|
+
type MappingTransform,
|
|
126
|
+
type MappingMatch,
|
|
127
|
+
type MappingCondition,
|
|
118
128
|
} from "./model.js";
|
|
119
129
|
export {
|
|
120
130
|
/** A model's mapping in one target, resolved through every model it composes. */
|
package/src/validate.ts
CHANGED
|
@@ -41,6 +41,10 @@ import {
|
|
|
41
41
|
entityMembers,
|
|
42
42
|
allMappingLinks,
|
|
43
43
|
allMappingPathLinks,
|
|
44
|
+
allTransformedProps,
|
|
45
|
+
declaredTransformSets,
|
|
46
|
+
propTransform,
|
|
47
|
+
propMatch,
|
|
44
48
|
declaredMappingSets,
|
|
45
49
|
allLayouts,
|
|
46
50
|
allMappingFromLinks,
|
|
@@ -383,6 +387,27 @@ function checkMappings(program: Program): void {
|
|
|
383
387
|
for (const [model, paths] of allMappingPathLinks(program))
|
|
384
388
|
for (const { mapping } of paths) undeclared(model, mapping);
|
|
385
389
|
|
|
390
|
+
const transforms = declaredTransformSets(program);
|
|
391
|
+
for (const prop of allTransformedProps(program)) {
|
|
392
|
+
const { member } = propTransform(program, prop)!;
|
|
393
|
+
if (propMatch(program, prop))
|
|
394
|
+
reportDiagnostic(program, {
|
|
395
|
+
code: "mapping-match-and-transform",
|
|
396
|
+
target: prop,
|
|
397
|
+
format: { name: prop.name },
|
|
398
|
+
});
|
|
399
|
+
if (transforms.has(member.enum)) continue;
|
|
400
|
+
reportDiagnostic(program, {
|
|
401
|
+
code: "mapping-transform-undeclared",
|
|
402
|
+
target: prop,
|
|
403
|
+
format: {
|
|
404
|
+
name: prop.name,
|
|
405
|
+
transform: member.name,
|
|
406
|
+
enumName: member.enum.name,
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
386
411
|
const roots: [Model, EnumMember][] = [];
|
|
387
412
|
for (const [target, list] of links) {
|
|
388
413
|
const seen = new Set<EnumMember>();
|
|
@@ -434,6 +459,13 @@ function checkMappings(program: Program): void {
|
|
|
434
459
|
for (const [target, links] of allMappingFromLinks(program)) {
|
|
435
460
|
for (const { mapping } of links) undeclared(target, mapping);
|
|
436
461
|
if (target.kind !== "Model") continue;
|
|
462
|
+
for (const { mapping, select } of links)
|
|
463
|
+
if (select)
|
|
464
|
+
reportDiagnostic(program, {
|
|
465
|
+
code: "mapping-from-select-on-model",
|
|
466
|
+
target,
|
|
467
|
+
format: { mapping: mapping.name, target: target.name },
|
|
468
|
+
});
|
|
437
469
|
for (const { mapping, source } of links) {
|
|
438
470
|
if (source.kind !== "Model") continue;
|
|
439
471
|
for (const problem of resolveMappingFrom(program, source, mapping)
|
|
@@ -754,7 +786,7 @@ function checkDuplicateIds(program: Program, blocks: Block[]): void {
|
|
|
754
786
|
}
|
|
755
787
|
}
|
|
756
788
|
|
|
757
|
-
const name = (type: Model | Scalar) => type.name || "an anonymous model";
|
|
789
|
+
const name = (type: Model | Scalar | Enum) => type.name || "an anonymous model";
|
|
758
790
|
|
|
759
791
|
// ---------------------------------------------------------------------------
|
|
760
792
|
// conditions
|