@grafctory/blue 0.0.0-stage → 0.15.1
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/LICENSE +201 -0
- package/README.md +509 -2
- package/dist/boolean/assembler.d.ts +53 -0
- package/dist/boolean/assembler.d.ts.map +1 -0
- package/dist/boolean/assembler.js +78 -0
- package/dist/boolean/assembler.js.map +1 -0
- package/dist/boolean/index.d.ts +86 -0
- package/dist/boolean/index.d.ts.map +1 -0
- package/dist/boolean/index.js +99 -0
- package/dist/boolean/index.js.map +1 -0
- package/dist/boolean/validator.d.ts +30 -0
- package/dist/boolean/validator.d.ts.map +1 -0
- package/dist/boolean/validator.js +68 -0
- package/dist/boolean/validator.js.map +1 -0
- package/dist/dictionary/assembler.d.ts +70 -0
- package/dist/dictionary/assembler.d.ts.map +1 -0
- package/dist/dictionary/assembler.js +154 -0
- package/dist/dictionary/assembler.js.map +1 -0
- package/dist/dictionary/index.d.ts +211 -0
- package/dist/dictionary/index.d.ts.map +1 -0
- package/dist/dictionary/index.js +43 -0
- package/dist/dictionary/index.js.map +1 -0
- package/dist/dictionary/inference.d.ts +42 -0
- package/dist/dictionary/inference.d.ts.map +1 -0
- package/dist/dictionary/inference.js +17 -0
- package/dist/dictionary/inference.js.map +1 -0
- package/dist/dictionary/validator.d.ts +34 -0
- package/dist/dictionary/validator.d.ts.map +1 -0
- package/dist/dictionary/validator.js +91 -0
- package/dist/dictionary/validator.js.map +1 -0
- package/dist/index.core.d.ts +33 -0
- package/dist/index.core.d.ts.map +1 -0
- package/dist/index.core.js +149 -0
- package/dist/index.core.js.map +1 -0
- package/dist/index.d.ts +333 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +378 -0
- package/dist/index.js.map +1 -0
- package/dist/number/assembler.d.ts +70 -0
- package/dist/number/assembler.d.ts.map +1 -0
- package/dist/number/assembler.js +188 -0
- package/dist/number/assembler.js.map +1 -0
- package/dist/number/index.d.ts +329 -0
- package/dist/number/index.d.ts.map +1 -0
- package/dist/number/index.js +312 -0
- package/dist/number/index.js.map +1 -0
- package/dist/number/validator.d.ts +31 -0
- package/dist/number/validator.d.ts.map +1 -0
- package/dist/number/validator.js +57 -0
- package/dist/number/validator.js.map +1 -0
- package/dist/reference/accessors.d.ts +29 -0
- package/dist/reference/accessors.d.ts.map +1 -0
- package/dist/reference/accessors.js +38 -0
- package/dist/reference/accessors.js.map +1 -0
- package/dist/reference/assembler.d.ts +63 -0
- package/dist/reference/assembler.d.ts.map +1 -0
- package/dist/reference/assembler.js +127 -0
- package/dist/reference/assembler.js.map +1 -0
- package/dist/reference/index.d.ts +108 -0
- package/dist/reference/index.d.ts.map +1 -0
- package/dist/reference/index.js +40 -0
- package/dist/reference/index.js.map +1 -0
- package/dist/reference/validator.d.ts +33 -0
- package/dist/reference/validator.d.ts.map +1 -0
- package/dist/reference/validator.js +56 -0
- package/dist/reference/validator.js.map +1 -0
- package/dist/resource/accessors.d.ts +140 -0
- package/dist/resource/accessors.d.ts.map +1 -0
- package/dist/resource/accessors.js +173 -0
- package/dist/resource/accessors.js.map +1 -0
- package/dist/resource/assembler.d.ts +265 -0
- package/dist/resource/assembler.d.ts.map +1 -0
- package/dist/resource/assembler.js +816 -0
- package/dist/resource/assembler.js.map +1 -0
- package/dist/resource/index.d.ts +898 -0
- package/dist/resource/index.d.ts.map +1 -0
- package/dist/resource/index.js +160 -0
- package/dist/resource/index.js.map +1 -0
- package/dist/resource/inference.d.ts +310 -0
- package/dist/resource/inference.d.ts.map +1 -0
- package/dist/resource/inference.js +17 -0
- package/dist/resource/inference.js.map +1 -0
- package/dist/resource/validator.d.ts +158 -0
- package/dist/resource/validator.d.ts.map +1 -0
- package/dist/resource/validator.js +1031 -0
- package/dist/resource/validator.js.map +1 -0
- package/dist/string/assembler.d.ts +69 -0
- package/dist/string/assembler.d.ts.map +1 -0
- package/dist/string/assembler.js +143 -0
- package/dist/string/assembler.js.map +1 -0
- package/dist/string/index.d.ts +431 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +528 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/validator.d.ts +30 -0
- package/dist/string/validator.d.ts.map +1 -0
- package/dist/string/validator.js +69 -0
- package/dist/string/validator.js.map +1 -0
- package/dist/union/accessors.d.ts +137 -0
- package/dist/union/accessors.d.ts.map +1 -0
- package/dist/union/accessors.js +253 -0
- package/dist/union/accessors.js.map +1 -0
- package/dist/union/assembler.d.ts +56 -0
- package/dist/union/assembler.d.ts.map +1 -0
- package/dist/union/assembler.js +140 -0
- package/dist/union/assembler.js.map +1 -0
- package/dist/union/index.d.ts +172 -0
- package/dist/union/index.d.ts.map +1 -0
- package/dist/union/index.js +45 -0
- package/dist/union/index.js.map +1 -0
- package/dist/union/inference.d.ts +30 -0
- package/dist/union/inference.d.ts.map +1 -0
- package/dist/union/inference.js +17 -0
- package/dist/union/inference.js.map +1 -0
- package/dist/union/validator.d.ts +94 -0
- package/dist/union/validator.d.ts.map +1 -0
- package/dist/union/validator.js +171 -0
- package/dist/union/validator.js.map +1 -0
- package/dist/value/accessors.d.ts +151 -0
- package/dist/value/accessors.d.ts.map +1 -0
- package/dist/value/accessors.js +468 -0
- package/dist/value/accessors.js.map +1 -0
- package/dist/value/assembler.d.ts +129 -0
- package/dist/value/assembler.d.ts.map +1 -0
- package/dist/value/assembler.js +248 -0
- package/dist/value/assembler.js.map +1 -0
- package/dist/value/index.d.ts +309 -0
- package/dist/value/index.d.ts.map +1 -0
- package/dist/value/index.js +26 -0
- package/dist/value/index.js.map +1 -0
- package/dist/value/inference.d.ts +292 -0
- package/dist/value/inference.d.ts.map +1 -0
- package/dist/value/inference.js +17 -0
- package/dist/value/inference.js.map +1 -0
- package/dist/value/validator.d.ts +76 -0
- package/dist/value/validator.d.ts.map +1 -0
- package/dist/value/validator.js +111 -0
- package/dist/value/validator.js.map +1 -0
- package/package.json +95 -4
|
@@ -0,0 +1,816 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Resource shape assembly.
|
|
18
|
+
*
|
|
19
|
+
* Builds the shape a factory states into the merged form its consumers read: the shapes extended contribute their
|
|
20
|
+
* members and constraints, each declared member is held to the one it overrides, and the result is frozen, so that a
|
|
21
|
+
* shape admitting no resource at all is rejected as it is built rather than when a resource is first matched against
|
|
22
|
+
* it.
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
import { assert, isFunction, isNumber, isObject, isString, opt as fold } from "@grafctory/core";
|
|
27
|
+
import { unique, union } from "@grafctory/core/arrays";
|
|
28
|
+
import { base, isIRI } from "@grafctory/core/resource";
|
|
29
|
+
import { dedent, tidy } from "@grafctory/core/strings";
|
|
30
|
+
import { equals, immutable, seal } from "@grafctory/core/values";
|
|
31
|
+
import { all, fail, test } from "@grafctory/core/trace";
|
|
32
|
+
import { eager } from "../value/index.js";
|
|
33
|
+
import { mergeShape, narrowsShape, reject } from "../value/assembler.js";
|
|
34
|
+
import { assemble as assembleUnion } from "../union/assembler.js";
|
|
35
|
+
/**
|
|
36
|
+
* The seal marking a shape as already merged.
|
|
37
|
+
*/
|
|
38
|
+
const Flattened = Symbol("flattened");
|
|
39
|
+
/**
|
|
40
|
+
* The form an identifier pattern is stated in.
|
|
41
|
+
*/
|
|
42
|
+
const PatternFormat = new RegExp("^"
|
|
43
|
+
+ "(?:[a-zA-Z][a-zA-Z0-9+.-]*://[^/]+)?" // optional scheme://authority
|
|
44
|
+
+ "/(?:" // root path, followed by either
|
|
45
|
+
+ "(?:[^/{}*]+|\\{\\w+})(?:/(?:[^/{}*]+|\\{\\w+}))*(?:/\\*|/)?" // segments with {name} placeholders, closed by /* or /
|
|
46
|
+
+ "|\\*" // or a wildcard on the root path
|
|
47
|
+
+ ")?"
|
|
48
|
+
+ "$");
|
|
49
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
50
|
+
/**
|
|
51
|
+
* Assembles a resource shape.
|
|
52
|
+
*
|
|
53
|
+
* Backs the factory the {@link resource!} module exposes: reads the extended shapes, the constraints and the members
|
|
54
|
+
* off the arguments as they were stated, resolves each member's predicate against the effective space, and merges the
|
|
55
|
+
* inherited definitions in, so that the shape a caller holds states everything its resources carry.
|
|
56
|
+
*
|
|
57
|
+
* @param args The extended shapes, followed by the constraints, the members, or both, each optional; a lone declaration
|
|
58
|
+
* is read as members where it lists members, and as constraints otherwise
|
|
59
|
+
*
|
|
60
|
+
* @returns An immutable shape admitting the resources the members and constraints bound
|
|
61
|
+
*
|
|
62
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated declarations are inconsistent with the
|
|
63
|
+
* inherited ones
|
|
64
|
+
* @throws {@link !TypeError TypeError} Where a lone declaration lists neither members nor constraints, of two
|
|
65
|
+
* declarations the last lists something other than members or the first something other than constraints, or
|
|
66
|
+
* the constraints state an identifier pattern that is not a well-formed absolute or root-relative path template
|
|
67
|
+
*/
|
|
68
|
+
export function assemble(args) {
|
|
69
|
+
const parents = args.filter(isParent);
|
|
70
|
+
const declarations = args.filter(isDeclaration);
|
|
71
|
+
const [constraints = {}, members = {}] = declarations.length === 1 && !isMembers(declarations[0])
|
|
72
|
+
? [declarations[0]]
|
|
73
|
+
: [declarations.at(-2), declarations.at(-1)];
|
|
74
|
+
if (!isMembers(members)) {
|
|
75
|
+
throw new TypeError(`malformed member declarations <${JSON.stringify(members)}>`);
|
|
76
|
+
}
|
|
77
|
+
if (!isConstraints(constraints)) {
|
|
78
|
+
throw new TypeError(`malformed shape constraints <${JSON.stringify(constraints)}>`);
|
|
79
|
+
}
|
|
80
|
+
if (constraints.pattern !== undefined && !isPattern(constraints.pattern)) {
|
|
81
|
+
throw new TypeError(`malformed pattern <${constraints.pattern}>`);
|
|
82
|
+
}
|
|
83
|
+
const { name, description, ...stated } = constraints;
|
|
84
|
+
const space = locate(parents, constraints.space);
|
|
85
|
+
return flatten({
|
|
86
|
+
kind: "resource",
|
|
87
|
+
...stated,
|
|
88
|
+
...name !== undefined && { name: localize(name, tidy) },
|
|
89
|
+
...description !== undefined && { description: localize(description, dedent) },
|
|
90
|
+
classes: [], // computed as the shape is flattened
|
|
91
|
+
parents,
|
|
92
|
+
members: resolve(inherit(members, parents), space)
|
|
93
|
+
});
|
|
94
|
+
/**
|
|
95
|
+
* Checks whether an argument states an extended shape.
|
|
96
|
+
*/
|
|
97
|
+
function isParent(argument) {
|
|
98
|
+
return isFunction(argument) || isObject(argument) && argument.kind === "resource";
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Checks whether an argument states members or constraints rather than an extended shape.
|
|
102
|
+
*/
|
|
103
|
+
function isDeclaration(argument) {
|
|
104
|
+
return !isParent(argument);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Checks whether a declaration lists members.
|
|
108
|
+
*/
|
|
109
|
+
function isMembers(declaration) {
|
|
110
|
+
return isObject(declaration) && Object.values(declaration).every(isMember);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Checks whether a declaration lists constraints.
|
|
114
|
+
*/
|
|
115
|
+
function isConstraints(declaration) {
|
|
116
|
+
return isObject(declaration) && declaration.kind === undefined && !Object.values(declaration).some(isMember);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Checks whether a value states a member.
|
|
120
|
+
*/
|
|
121
|
+
function isMember(value) {
|
|
122
|
+
return isObject(value)
|
|
123
|
+
&& (value.kind === "id" || value.kind === "type" || value.kind === "property");
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Expands a label to its localised form, normalising the layout of its content.
|
|
127
|
+
*
|
|
128
|
+
* @param label The stated label, localised or taken as English content where stated as a bare string
|
|
129
|
+
* @param normalise The layout normalisation applied to every entry
|
|
130
|
+
*
|
|
131
|
+
* @returns `label` as a localised dictionary, keyed by `en` where stated as a bare string
|
|
132
|
+
*/
|
|
133
|
+
function localize(label, normalise) {
|
|
134
|
+
return isString(label) ? {
|
|
135
|
+
en: normalise(label)
|
|
136
|
+
} : Object.fromEntries(Object.entries(label).map(([tag, content]) => [tag, isString(content) ? normalise(content) : content.map(normalise)]));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Identifies the space member names are resolved against.
|
|
140
|
+
*
|
|
141
|
+
* @param parents The extended shapes the space may be inherited from
|
|
142
|
+
* @param space The space the shape states, if any
|
|
143
|
+
*
|
|
144
|
+
* @returns The effective space: the stated one, else the inherited one, else the default
|
|
145
|
+
*/
|
|
146
|
+
function locate(parents, space) {
|
|
147
|
+
// a conflict among the extended shapes is reported by checkParents against the merged ones
|
|
148
|
+
const [first] = parents;
|
|
149
|
+
return space ?? (first === undefined ? undefined : eager(first).space) ?? base;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Completes members with the predicates they inherit.
|
|
153
|
+
*
|
|
154
|
+
* A member stating neither `forward` nor `reverse` takes the mapping of the one it overrides, so that an override
|
|
155
|
+
* restates the range alone and keeps pointing at the inherited predicate.
|
|
156
|
+
*
|
|
157
|
+
* @param members The members stated in the shape's own right
|
|
158
|
+
* @param parents The extended shapes the mappings may be inherited from
|
|
159
|
+
*
|
|
160
|
+
* @returns The members carrying the predicates they inherit
|
|
161
|
+
*/
|
|
162
|
+
function inherit(members, parents) {
|
|
163
|
+
const inherited = parents.map(parent => eager(parent).members);
|
|
164
|
+
return Object.fromEntries(Object.entries(members).map(([name, member]) => {
|
|
165
|
+
const base = inherited.flatMap(source => source[name] ?? []).at(0);
|
|
166
|
+
return member.kind === "property"
|
|
167
|
+
&& member.forward === undefined && member.reverse === undefined
|
|
168
|
+
&& base?.kind === "property"
|
|
169
|
+
&& (base.forward !== undefined || base.reverse !== undefined)
|
|
170
|
+
? [name, declare({
|
|
171
|
+
...member,
|
|
172
|
+
...base.forward !== undefined && { forward: base.forward },
|
|
173
|
+
...base.reverse !== undefined && { reverse: base.reverse }
|
|
174
|
+
})]
|
|
175
|
+
: [name, member];
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Resolves the predicates and labels the members state.
|
|
180
|
+
*
|
|
181
|
+
* A member stating neither `forward` nor `reverse` is mapped to its own name resolved against the effective space.
|
|
182
|
+
*
|
|
183
|
+
* @param members The members to resolve
|
|
184
|
+
* @param space The space member names are resolved against
|
|
185
|
+
*
|
|
186
|
+
* @returns The members with their predicates resolved to IRIs and their labels localised
|
|
187
|
+
*/
|
|
188
|
+
function resolve(members, space) {
|
|
189
|
+
return Object.fromEntries(Object.entries(members).map(([name, member]) => {
|
|
190
|
+
if (member.kind !== "property") {
|
|
191
|
+
return [name, member];
|
|
192
|
+
}
|
|
193
|
+
const forward = isString(member.forward) ? assert(member.forward, isIRI)
|
|
194
|
+
: member.forward !== undefined ? assert(member.forward[name], isIRI)
|
|
195
|
+
: undefined;
|
|
196
|
+
const reverse = isString(member.reverse) ? assert(member.reverse, isIRI)
|
|
197
|
+
: member.reverse !== undefined ? assert(member.reverse[name], isIRI)
|
|
198
|
+
: undefined;
|
|
199
|
+
return [name, declare({
|
|
200
|
+
...member,
|
|
201
|
+
...member.name !== undefined && { name: localize(member.name, tidy) },
|
|
202
|
+
...member.description !== undefined && { description: localize(member.description, dedent) },
|
|
203
|
+
forward: forward === undefined && reverse === undefined ? assert(space[name], isIRI) : forward,
|
|
204
|
+
reverse
|
|
205
|
+
})];
|
|
206
|
+
}));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Declares a resource member.
|
|
211
|
+
*
|
|
212
|
+
* Backs the member factories the {@link resource!} module exposes, fixing what they share: the cardinality bounds are
|
|
213
|
+
* checked as they are stated, and the member is frozen as it is declared, so that a shape carrying it states the same
|
|
214
|
+
* member however many shapes it reaches.
|
|
215
|
+
*
|
|
216
|
+
* @typeParam M The member the calling factory states
|
|
217
|
+
*
|
|
218
|
+
* @param member The member to declare
|
|
219
|
+
*
|
|
220
|
+
* @returns An immutable member as stated
|
|
221
|
+
*
|
|
222
|
+
* @throws {@link !TypeError TypeError} Where the member states a negative bound, or bounds admitting no value at all
|
|
223
|
+
*/
|
|
224
|
+
export function declare(member) {
|
|
225
|
+
const { range } = member;
|
|
226
|
+
const { minCount, maxCount } = isObject(range) ? range : {};
|
|
227
|
+
if (isNumber(minCount) && minCount < 0) {
|
|
228
|
+
throw new TypeError(`negative minCount <${minCount}>`);
|
|
229
|
+
}
|
|
230
|
+
if (isNumber(maxCount) && maxCount < 0) {
|
|
231
|
+
throw new TypeError(`negative maxCount <${maxCount}>`);
|
|
232
|
+
}
|
|
233
|
+
if (isNumber(minCount) && isNumber(maxCount) && minCount > maxCount) {
|
|
234
|
+
throw new TypeError(`inconsistent bounds <${minCount}> > <${maxCount}>`);
|
|
235
|
+
}
|
|
236
|
+
return immutable(member); // ;(cast) the factory signatures fix the member each one states
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Merges the inheritance chain of a resource shape into a single shape.
|
|
240
|
+
*
|
|
241
|
+
* The merged shape states every member and constraint its resources are held to. A resource the shape admits is an
|
|
242
|
+
* instance of every shape extended: a member several of them state takes the narrowest definition, whatever order
|
|
243
|
+
* they are listed in, and a member the shape declares must narrow each inherited definition. Merging a shape already
|
|
244
|
+
* merged returns it unchanged at no cost, so every shape extending it may merge it again freely.
|
|
245
|
+
*
|
|
246
|
+
* @param shape The shape to merge
|
|
247
|
+
*
|
|
248
|
+
* @returns An immutable shape stating the inherited definitions alongside its own
|
|
249
|
+
*
|
|
250
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated declarations are inconsistent with the
|
|
251
|
+
* inherited ones or with each other, where the extended shapes conflict on a field or member the shape leaves
|
|
252
|
+
* unstated, or where a member admitting literals states a reverse mapping, as a literal can't be a subject
|
|
253
|
+
*/
|
|
254
|
+
export function flatten(shape) {
|
|
255
|
+
if (seal(shape, Flattened) === null) {
|
|
256
|
+
return shape;
|
|
257
|
+
}
|
|
258
|
+
const parents = shape.parents.map(parent => eager(parent));
|
|
259
|
+
// a resource the shape admits is an instance of every shape extended, so the shape is held to each of them on
|
|
260
|
+
// its own, and the conflicts they leave for it to settle are reported before the members are merged, so that a
|
|
261
|
+
// member unrelated shapes state surfaces as such rather than as a failed override
|
|
262
|
+
reject("incompatible resource shape override", all(...parents.map(parent => () => narrowsResource(shape, parent)))(undefined));
|
|
263
|
+
reject("incompatible merged shape", checkParents(shape, parents));
|
|
264
|
+
// the constraints are merged in list order with the members left out, which are merged apart, as every shape
|
|
265
|
+
// extended states them rather than as the first listed does, whose definition would otherwise override the rest
|
|
266
|
+
const merged = immutable({
|
|
267
|
+
...mergeResource({ ...shape, members: {} }, parents.map(parent => ({ ...parent, members: {} })).reduce(mergeResource, { kind: "resource", classes: [], parents: [], members: {} })),
|
|
268
|
+
members: mergeMembers(shape, parents)
|
|
269
|
+
});
|
|
270
|
+
reject("incompatible merged shape", all(() => checkSingletons(Object.values(merged.members)), () => checkPredicates(merged), () => checkReverse(merged)
|
|
271
|
+
// checkId is left to validateResource, which alone can tell an embedded resource from an expanded target
|
|
272
|
+
)(undefined));
|
|
273
|
+
// the class each extended shape states, followed by the ones it inherits in turn, so a caller tests a resource
|
|
274
|
+
// against a supertype without walking the chain itself
|
|
275
|
+
const classes = unique(parents.flatMap(parent => [
|
|
276
|
+
...parent.class === undefined ? [] : [parent.class],
|
|
277
|
+
...parent.classes
|
|
278
|
+
]));
|
|
279
|
+
// the shapes a member reaches are merged in turn, so a caller walking a range holds a merged shape throughout
|
|
280
|
+
return seal(immutable({
|
|
281
|
+
...merged,
|
|
282
|
+
classes,
|
|
283
|
+
members: Object.fromEntries(Object.entries(merged.members).map(([name, member]) => member.kind === "property"
|
|
284
|
+
? [name, { ...member, range: { ...member.range, shape: descend(member.range.shape) } }]
|
|
285
|
+
: [name, member]))
|
|
286
|
+
}), Flattened, null);
|
|
287
|
+
/**
|
|
288
|
+
* Merges the inheritance chain of the shapes a range reaches.
|
|
289
|
+
*
|
|
290
|
+
* @param range The range to descend into, possibly deferred to break definition cycles
|
|
291
|
+
*
|
|
292
|
+
* @returns The range with the resource shapes it reaches merged
|
|
293
|
+
*/
|
|
294
|
+
function descend(range) {
|
|
295
|
+
if (isFunction(range)) {
|
|
296
|
+
return range;
|
|
297
|
+
} // a deferred range is merged where it resolves
|
|
298
|
+
switch (range.kind) {
|
|
299
|
+
case "resource":
|
|
300
|
+
return flatten(range);
|
|
301
|
+
case "reference":
|
|
302
|
+
// a deferred target is carried as stated: resolving it merges the shape it states, as for any
|
|
303
|
+
// deferred range, so a wrapper of its own would add an indirection merging nothing further
|
|
304
|
+
return { ...range, target: isFunction(range.target) ? range.target : flatten(range.target) };
|
|
305
|
+
case "union":
|
|
306
|
+
// rebuilt through the factory, so the branches stay flat however descending reshapes them
|
|
307
|
+
return assembleUnion(range.branches.map(branch => descend(branch)));
|
|
308
|
+
default:
|
|
309
|
+
return range;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Merges the members of a resource shape with the ones the shapes it extends state.
|
|
315
|
+
*
|
|
316
|
+
* Returns the members an extending resource is validated against. A declared member is merged over the definition of
|
|
317
|
+
* each extended shape stating it. An undeclared member takes the narrowest inherited definition, merged over the
|
|
318
|
+
* others, whatever order the extended shapes are listed in, so the result never depends on declaration order.
|
|
319
|
+
*
|
|
320
|
+
* @param shape The extending shape
|
|
321
|
+
* @param parents The extended shapes, already merged and {@link checkParents | checked} for unsettled conflicts
|
|
322
|
+
*
|
|
323
|
+
* @returns The merged members, each admitting the values every definition stated for it admits
|
|
324
|
+
*
|
|
325
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where a declared member doesn't narrow an inherited definition
|
|
326
|
+
*/
|
|
327
|
+
function mergeMembers(shape, parents) {
|
|
328
|
+
return Object.fromEntries(union([
|
|
329
|
+
Object.keys(shape.members),
|
|
330
|
+
...parents.map(parent => Object.keys(parent.members))
|
|
331
|
+
]).map(name => {
|
|
332
|
+
const declared = shape.members[name];
|
|
333
|
+
const inherited = parents.flatMap(parent => {
|
|
334
|
+
const member = parent.members[name];
|
|
335
|
+
return member === undefined ? [] : [member];
|
|
336
|
+
});
|
|
337
|
+
return [name, declared !== undefined ? inherited.reduce(mergeMember, declared) : inherit(inherited)];
|
|
338
|
+
}));
|
|
339
|
+
/**
|
|
340
|
+
* Merges the definitions the extended shapes state for a member the extending shape leaves undeclared.
|
|
341
|
+
*
|
|
342
|
+
* @param stated The definitions stated for the member, at least one as the member is named after them
|
|
343
|
+
*
|
|
344
|
+
* @returns The narrowest definition merged over the others
|
|
345
|
+
*/
|
|
346
|
+
function inherit(stated) {
|
|
347
|
+
const [first, ...rest] = stated;
|
|
348
|
+
const settled = narrowest(first, ...rest);
|
|
349
|
+
// the definition settled on is merged over each other one once and never over itself, as a union reached
|
|
350
|
+
// along two paths is told restated as it stands by the branches it shares, which a merge no longer carries
|
|
351
|
+
return stated.filter(member => member !== settled).reduce(mergeMember, settled);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
355
|
+
/**
|
|
356
|
+
* Checks whether a string is a well-formed identifier {@link ResourceConstraints.pattern | pattern}.
|
|
357
|
+
*
|
|
358
|
+
* @param value The string to check
|
|
359
|
+
*
|
|
360
|
+
* @returns `true` where `value` is an absolute or root-relative path of literal segments and `{name}` placeholders,
|
|
361
|
+
* optionally closed by `/` or by a `/*` wildcard; the wildcard may also stand alone on the root path
|
|
362
|
+
*/
|
|
363
|
+
export function isPattern(value) {
|
|
364
|
+
return PatternFormat.test(value);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Checks a set of resource constraints for internal consistency.
|
|
368
|
+
*
|
|
369
|
+
* Reports the contradictions that would leave a shape admitting no resource at all, so that a shape is rejected as it
|
|
370
|
+
* is built rather than when a resource is first matched against it.
|
|
371
|
+
*
|
|
372
|
+
* @param constraints The constraints to check
|
|
373
|
+
*
|
|
374
|
+
* @returns A trace of the inconsistencies found, or `undefined` where the constraints admit at least one resource
|
|
375
|
+
*/
|
|
376
|
+
export function checkResource(constraints) {
|
|
377
|
+
return test(({ in: allowed, hasValue }) => {
|
|
378
|
+
return hasValue === undefined || allowed === undefined || hasValue.every(v => allowed.includes(v)) || [
|
|
379
|
+
`{hasValue/in} required values <${hasValue.filter(v => !allowed.includes(v))}> not in allowed set`
|
|
380
|
+
];
|
|
381
|
+
})(constraints);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Checks a pair of cardinality bounds for internal consistency.
|
|
385
|
+
*
|
|
386
|
+
* Reports bounds that cross, leaving a member admitting no value at all, so that a contradiction between what is
|
|
387
|
+
* required and what is admitted surfaces as the member is settled rather than when a resource is first matched against
|
|
388
|
+
* it. An unstated bound leaves that end unbounded and crosses nothing.
|
|
389
|
+
*
|
|
390
|
+
* @param bounds The bounds to check
|
|
391
|
+
*
|
|
392
|
+
* @returns A trace of the inconsistency found, or `undefined` where the bounds admit at least one value
|
|
393
|
+
*/
|
|
394
|
+
export function checkBounds(bounds) {
|
|
395
|
+
return test(({ minCount, maxCount }) => {
|
|
396
|
+
return minCount === undefined || maxCount === undefined || minCount <= maxCount || [
|
|
397
|
+
`{minCount/maxCount} inconsistent bounds <${minCount}> > <${maxCount}>`
|
|
398
|
+
];
|
|
399
|
+
})(bounds);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Checks the extended shapes for conflicts the extending shape leaves unsettled.
|
|
403
|
+
*
|
|
404
|
+
* Reports the inherited fields the extended shapes disagree on and the extending shape states no value for, so that a
|
|
405
|
+
* shape reaching the same field along two paths never resolves it by declaration order. The fields held to this rule
|
|
406
|
+
* are `space` on the resource and `hidden`, `name` and `description` on each of its properties, even where the
|
|
407
|
+
* extending shape redeclares the property; markers, which take `hidden` from their own declaration, are exempt.
|
|
408
|
+
*
|
|
409
|
+
* Also reports each undeclared member stated by several extended shapes if no definition narrows all the others.
|
|
410
|
+
* Such a member is rejected rather than settled by declaration order. The trace is keyed by the member name and lists
|
|
411
|
+
* why the closest definition fails to narrow the others, so a disagreement on a field is reported under that field.
|
|
412
|
+
*
|
|
413
|
+
* @param shape The extending shape
|
|
414
|
+
* @param parents The extended shapes, already merged
|
|
415
|
+
*
|
|
416
|
+
* @returns A trace of the conflicts found, or `undefined` where the extended shapes agree on every inherited field
|
|
417
|
+
*/
|
|
418
|
+
export function checkParents(shape, parents) {
|
|
419
|
+
if (parents.length < 2) {
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
const space = shape.space !== undefined ? undefined
|
|
423
|
+
: conflict(parents.map(parent => parent.space?.[""]));
|
|
424
|
+
return all(space !== undefined
|
|
425
|
+
&& fail([`{space} conflicting inherited values <${space[0]}> vs <${space[1]}> without an override`]), ...unique(parents.flatMap(parent => Object.keys(parent.members))).map(name => {
|
|
426
|
+
const stated = parents.flatMap(parent => {
|
|
427
|
+
const member = parent.members[name];
|
|
428
|
+
return member === undefined ? [] : [member];
|
|
429
|
+
});
|
|
430
|
+
// a marker is taken whole from the most derived declaration, so its `hidden` is settled before this check
|
|
431
|
+
const properties = stated.flatMap(member => member.kind === "property" ? [member] : []);
|
|
432
|
+
const hidden = properties.length > 1 && shape.members[name]?.hidden === undefined
|
|
433
|
+
? conflict(properties.map(member => member.hidden))
|
|
434
|
+
: undefined;
|
|
435
|
+
const unsettled = stated.length > 1 && shape.members[name] === undefined
|
|
436
|
+
? conflicting(stated)
|
|
437
|
+
: undefined;
|
|
438
|
+
const annotations = disagreeing(shape.members[name], properties);
|
|
439
|
+
return all(hidden !== undefined
|
|
440
|
+
&& fail([`{hidden} conflicting inherited values <${hidden[0]}> vs <${hidden[1]}> for <${name}> without an override`]), unsettled !== undefined && fail([{ [name]: unsettled }]), annotations.length > 0 && fail([{ [name]: annotations }]));
|
|
441
|
+
}))(undefined);
|
|
442
|
+
/**
|
|
443
|
+
* Resolves the first pair of inherited values that disagree.
|
|
444
|
+
*/
|
|
445
|
+
function conflict(values) {
|
|
446
|
+
const [first] = values;
|
|
447
|
+
const other = values.findIndex(value => value !== first);
|
|
448
|
+
return other < 0 ? undefined : [first, values[other]];
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Identifies the annotations the extended shapes state differently for a property. Each one the extending shape
|
|
452
|
+
* leaves unstated is an obstacle requiring an override, whether or not the extending shape redeclares the property.
|
|
453
|
+
*
|
|
454
|
+
* @param declared The member the extending shape declares under the same name, if any
|
|
455
|
+
* @param properties The definitions the extended shapes state for the property
|
|
456
|
+
*
|
|
457
|
+
* @returns The obstacles found, one for each annotation left unsettled
|
|
458
|
+
*/
|
|
459
|
+
function disagreeing(declared, properties) {
|
|
460
|
+
const fields = ["name", "description"];
|
|
461
|
+
return fields.flatMap(field => {
|
|
462
|
+
const own = declared?.kind === "property" ? declared[field] : undefined;
|
|
463
|
+
const values = properties.flatMap(property => property[field] === undefined ? [] : [property[field]]);
|
|
464
|
+
return own === undefined && values.some(value => !equals(value, values[0]))
|
|
465
|
+
? [`{${field}} conflicting inherited values without an override`]
|
|
466
|
+
: [];
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Identifies why the closest of the definitions stated for a member fails to narrow the others.
|
|
471
|
+
*
|
|
472
|
+
* @param stated The definitions stated for the member, at least two
|
|
473
|
+
*
|
|
474
|
+
* @returns A trace of the first obstacle found, or `undefined` where the closest definition narrows every other
|
|
475
|
+
*/
|
|
476
|
+
function conflicting(stated) {
|
|
477
|
+
const [first, ...rest] = stated;
|
|
478
|
+
const closest = narrowest(first, ...rest);
|
|
479
|
+
return stated.map(member => narrowsMember(closest, member)).find(trace => trace !== undefined);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Checks that a shape states at most one identifier and one type.
|
|
484
|
+
*
|
|
485
|
+
* > [!IMPORTANT]
|
|
486
|
+
* > Members must be collapsed by name before they are counted: a marker reaching a shape under one name along several
|
|
487
|
+
* > inheritance paths, or redeclared over an inherited one, is a single member rather than a duplicate. Only markers
|
|
488
|
+
* > of the same kind held under *distinct* names are duplicates.
|
|
489
|
+
*
|
|
490
|
+
* @param members The merged members to check, collapsed by name
|
|
491
|
+
*
|
|
492
|
+
* @returns A trace of the duplicates found, or `undefined` where the shape states at most one of each
|
|
493
|
+
*/
|
|
494
|
+
export function checkSingletons(members) {
|
|
495
|
+
return all(...["id", "type"].map(kind => () => {
|
|
496
|
+
const declared = members.filter(member => member.kind === kind).length;
|
|
497
|
+
return declared <= 1 ? undefined : [`{${kind}} duplicate member (<${declared}> found)`];
|
|
498
|
+
}))(undefined);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Checks that no two members of a shape map to the same predicate.
|
|
502
|
+
*
|
|
503
|
+
* Forward and reverse mappings are counted apart, so the same IRI may serve both without conflict, but no two members
|
|
504
|
+
* may share a forward mapping, and no two a reverse one.
|
|
505
|
+
*
|
|
506
|
+
* @param shape The merged shape to check
|
|
507
|
+
*
|
|
508
|
+
* @returns A trace of the duplicates found, or `undefined` where every member maps to a predicate of its own
|
|
509
|
+
*/
|
|
510
|
+
export function checkPredicates(shape) {
|
|
511
|
+
const properties = Object.entries(shape.members)
|
|
512
|
+
.flatMap(([name, member]) => member.kind === "property" ? [[name, member]] : []);
|
|
513
|
+
return all(...[...duplicates("forward"), ...duplicates("reverse")].map(message => fail([message])))(undefined);
|
|
514
|
+
/**
|
|
515
|
+
* Lists the members sharing a mapping with one stated before them.
|
|
516
|
+
*
|
|
517
|
+
* @param mapping The mapping to inspect
|
|
518
|
+
*
|
|
519
|
+
* @returns One message per member sharing `mapping` with an earlier one
|
|
520
|
+
*/
|
|
521
|
+
function duplicates(mapping) {
|
|
522
|
+
return properties
|
|
523
|
+
.flatMap(([name, member], index) => {
|
|
524
|
+
const iri = member[mapping];
|
|
525
|
+
const earlier = iri === undefined ? undefined : properties
|
|
526
|
+
.slice(0, index)
|
|
527
|
+
.find(([, source]) => source[mapping] === iri);
|
|
528
|
+
return earlier === undefined ? []
|
|
529
|
+
: [`{${mapping}} duplicate predicate <${iri}> on <${name}> already used by <${earlier[0]}>`];
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Checks that every reverse mapping of a shape ranges over resources alone.
|
|
535
|
+
*
|
|
536
|
+
* A reverse mapping makes each value the subject of the statement it writes, and a literal can't be a subject. A
|
|
537
|
+
* member stating a reverse mapping must therefore range over references, embedded resources, or unions of them. The
|
|
538
|
+
* check runs as the shape is built, so consumers never receive values they can't encode. A range or union branch
|
|
539
|
+
* deferred to break a definition cycle is exempt.
|
|
540
|
+
*
|
|
541
|
+
* @param shape The merged shape to check
|
|
542
|
+
*
|
|
543
|
+
* @returns A trace naming each member that maps a literal range in reverse, or `undefined` if every reverse mapping
|
|
544
|
+
* ranges over resources alone
|
|
545
|
+
*/
|
|
546
|
+
export function checkReverse(shape) {
|
|
547
|
+
return all(...Object.entries(shape.members).map(([name, member]) => member.kind === "property" && member.reverse !== undefined && literal(member.range.shape)
|
|
548
|
+
&& fail([`{reverse} unexpected literal range on <${name}>`])))(undefined);
|
|
549
|
+
/**
|
|
550
|
+
* Checks whether a range admits literal values, treating a deferred range as admitting none.
|
|
551
|
+
*/
|
|
552
|
+
function literal(range) {
|
|
553
|
+
return !isFunction(range) && (range.kind === "union"
|
|
554
|
+
? range.branches.some(literal)
|
|
555
|
+
: range.kind !== "reference" && range.kind !== "resource");
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Reports whether a resource shape narrows an inherited one.
|
|
560
|
+
*
|
|
561
|
+
* Tests the override relation without building the merged shape, so that an incompatible extension is told apart from
|
|
562
|
+
* a legitimate refinement before either is committed to: a shape narrows the inherited one where its identifier
|
|
563
|
+
* `pattern` stays compatible, it adds no identifier the inherited shape omits, requires every identifier it requires,
|
|
564
|
+
* and every member it redeclares keeps its kind and narrows the one it overrides.
|
|
565
|
+
*
|
|
566
|
+
* The class a resource belongs to is settled apart, where a {@link value!Shape | shape} narrows another: a shape
|
|
567
|
+
* extending another is free to state a class of its own, while a shape standing in for another as a member range must
|
|
568
|
+
* belong to every class that one declares.
|
|
569
|
+
*
|
|
570
|
+
* @param target The overriding shape
|
|
571
|
+
* @param source The inherited shape
|
|
572
|
+
*
|
|
573
|
+
* @returns A trace of the obstacles to the override, or `undefined` where `target` narrows `source`
|
|
574
|
+
*/
|
|
575
|
+
export function narrowsResource(target, source) {
|
|
576
|
+
const { in: allowed, hasValue: required } = source;
|
|
577
|
+
return all(test(({ pattern }) => {
|
|
578
|
+
return pattern === undefined || source.pattern === undefined || pattern === source.pattern
|
|
579
|
+
|| source.pattern.endsWith("/*") && pattern.startsWith(source.pattern.slice(0, -1))
|
|
580
|
+
|| [`{pattern} incompatible identifier patterns <${pattern}> and <${source.pattern}>`];
|
|
581
|
+
}), test(({ in: values }) => {
|
|
582
|
+
return values === undefined || allowed === undefined || values.every(v => allowed.includes(v)) || [
|
|
583
|
+
`{in} unexpected values [${values.filter(v => !allowed.includes(v))}]`
|
|
584
|
+
];
|
|
585
|
+
}), test(({ hasValue }) => {
|
|
586
|
+
return hasValue === undefined || required === undefined || required.every(v => hasValue.includes(v)) || [
|
|
587
|
+
`{hasValue} missing required values [${required.filter(v => !hasValue.includes(v))}]`
|
|
588
|
+
];
|
|
589
|
+
}),
|
|
590
|
+
// per shared member: the kind is kept, and a property narrows the one it overrides
|
|
591
|
+
...Object.keys(target.members).filter(name => Object.hasOwn(source.members, name)).map(name => () => fold(narrowsMember(target.members[name], source.members[name]), trace => [{ [name]: trace }])),
|
|
592
|
+
// post-merge constraint consistency, over the identifiers alone: merging the members here would throw on
|
|
593
|
+
// the very incompatibility this relation is asked to report
|
|
594
|
+
() => checkResource(identifiers(target, source)))(target);
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Reports whether a member narrows an inherited one.
|
|
598
|
+
*
|
|
599
|
+
* Tests the override relation without building the merged member. A member narrows the inherited one if both state
|
|
600
|
+
* the same `forward` and `reverse` mappings, `foreign` and `captive` have the same value wherever both sides state
|
|
601
|
+
* them, and its range narrows the inherited one, bound by bound and shape by shape. A child may take ownership of a
|
|
602
|
+
* member its parent left bare. It may also hide, label or describe the member regardless of what its parent states,
|
|
603
|
+
* as these facets restrict no value the member admits and the merged member takes the ones the child states.
|
|
604
|
+
*
|
|
605
|
+
* Mappings are compared as stated, unstated included: an override leaving them unstated must first take the inherited
|
|
606
|
+
* ones, as the {@link resource!resource | resource} factory does before testing the relation.
|
|
607
|
+
*
|
|
608
|
+
* @param target The overriding member
|
|
609
|
+
* @param source The inherited member
|
|
610
|
+
*
|
|
611
|
+
* @returns A trace of the obstacles to the override, or `undefined` where `target` narrows `source`
|
|
612
|
+
*/
|
|
613
|
+
export function narrowsProperty(target, source) {
|
|
614
|
+
return all(
|
|
615
|
+
// a facet stated on one side alone is agreed on; one stated on both sides must be stated alike, save for
|
|
616
|
+
// `hidden`, `name` and `description`, which an override may replace as they restrict no value admitted
|
|
617
|
+
test(({ foreign }) => {
|
|
618
|
+
return foreign === undefined || source.foreign === undefined || foreign === source.foreign || [
|
|
619
|
+
`{foreign} conflicting <foreign> override`
|
|
620
|
+
];
|
|
621
|
+
}), test(({ captive }) => {
|
|
622
|
+
return captive === undefined || source.captive === undefined || captive === source.captive || [
|
|
623
|
+
`{captive} conflicting <captive> override`
|
|
624
|
+
];
|
|
625
|
+
}),
|
|
626
|
+
// a mapping fixes the data the member denotes, so both sides state the same one, unstated included; the
|
|
627
|
+
// factory copies the inherited mappings onto a member stating none before the relation is tested
|
|
628
|
+
test(({ forward }) => {
|
|
629
|
+
return forward === source.forward || [
|
|
630
|
+
`{forward} mismatched <forward> mappings <${forward}> vs <${source.forward}>`
|
|
631
|
+
];
|
|
632
|
+
}), test(({ reverse }) => {
|
|
633
|
+
return reverse === source.reverse || [
|
|
634
|
+
`{reverse} mismatched <reverse> mappings <${reverse}> vs <${source.reverse}>`
|
|
635
|
+
];
|
|
636
|
+
}),
|
|
637
|
+
// an unstated bound leaves that end unbounded rather than unsaid, so it is read as the widest one
|
|
638
|
+
test(({ range: { minCount } }) => {
|
|
639
|
+
return (minCount ?? 0) >= (source.range.minCount ?? 0) || [
|
|
640
|
+
`{minCount} widened limit <${minCount}> beyond <${source.range.minCount}>`
|
|
641
|
+
];
|
|
642
|
+
}), test(({ range: { maxCount } }) => {
|
|
643
|
+
return (maxCount ?? Infinity) <= (source.range.maxCount ?? Infinity) || [
|
|
644
|
+
`{maxCount} widened limit <${maxCount}> beyond <${source.range.maxCount}>`
|
|
645
|
+
];
|
|
646
|
+
}), () => narrowsShape(eager(target.range.shape), eager(source.range.shape)),
|
|
647
|
+
// the bounds the merged member carries, which each may narrow on its own and still cross
|
|
648
|
+
() => checkBounds({
|
|
649
|
+
minCount: target.range.minCount ?? source.range.minCount,
|
|
650
|
+
maxCount: target.range.maxCount ?? source.range.maxCount
|
|
651
|
+
}))(target);
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Reports whether a member of any kind narrows an inherited one.
|
|
655
|
+
*
|
|
656
|
+
* Extends the {@link narrowsProperty | property override relation} to markers. A member narrows the inherited one if
|
|
657
|
+
* it keeps its kind and, for a property, narrows the inherited property. A marker narrows any marker of its kind,
|
|
658
|
+
* whatever their `hidden` flags, as the most derived declaration is taken whole.
|
|
659
|
+
*
|
|
660
|
+
* @param target The overriding member
|
|
661
|
+
* @param source The inherited member
|
|
662
|
+
*
|
|
663
|
+
* @returns A trace of the obstacles to the override, or `undefined` where `target` narrows `source`
|
|
664
|
+
*/
|
|
665
|
+
export function narrowsMember(target, source) {
|
|
666
|
+
return target.kind !== source.kind ? [`mismatched member kinds <${target.kind}> vs <${source.kind}>`]
|
|
667
|
+
: target.kind === "property" && source.kind === "property" ? narrowsProperty(target, source)
|
|
668
|
+
: undefined;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Selects the narrowest of the definitions several shapes state for a member.
|
|
672
|
+
*
|
|
673
|
+
* Returns the definition narrowing every other, whatever order the definitions are listed in: the one every value of
|
|
674
|
+
* the member must satisfy. Among definitions narrowing each other, as markers of the same kind do, the first listed
|
|
675
|
+
* is returned. If no definition narrows every other, the result fails to narrow at least one of them, and callers
|
|
676
|
+
* must check it with {@link narrowsMember} before relying on it, reporting or rejecting the conflict it reveals.
|
|
677
|
+
*
|
|
678
|
+
* @param first The first definition stated for the member
|
|
679
|
+
* @param rest The other definitions stated for the member
|
|
680
|
+
*
|
|
681
|
+
* @returns The narrowest of the definitions stated
|
|
682
|
+
*/
|
|
683
|
+
export function narrowest(first, ...rest) {
|
|
684
|
+
// a definition narrowing every other narrows whichever one is held as the narrowest so far and takes its place,
|
|
685
|
+
// which no later definition takes back unless it narrows it in turn; one narrowing it no more than it is narrowed
|
|
686
|
+
// by it, as a marker is by every marker of its kind, leaves the one listed first in place
|
|
687
|
+
return rest.reduce((narrowest, member) => narrowsMember(member, narrowest) === undefined && narrowsMember(narrowest, member) !== undefined
|
|
688
|
+
? member
|
|
689
|
+
: narrowest, first);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Merges a resource shape with an inherited one.
|
|
693
|
+
*
|
|
694
|
+
* Returns the single shape an extending resource is validated against, combining the inherited constraints with the
|
|
695
|
+
* overriding ones: the admitted identifiers intersect, the required ones accumulate, and members declared on both
|
|
696
|
+
* sides are merged one by one, a marker being taken whole from the overriding shape. A shape merged with itself is
|
|
697
|
+
* returned as it stands, so a shape inherited along several paths keeps its reference identity.
|
|
698
|
+
*
|
|
699
|
+
* @param target The overriding shape
|
|
700
|
+
* @param source The inherited shape
|
|
701
|
+
*
|
|
702
|
+
* @returns An immutable shape admitting the resources both `target` and `source` admit
|
|
703
|
+
*
|
|
704
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where `target` doesn't narrow `source`
|
|
705
|
+
*/
|
|
706
|
+
export function mergeResource(target, source) {
|
|
707
|
+
reject("incompatible resource shape override", narrowsResource(target, source));
|
|
708
|
+
// a shape reached along several paths is merged with itself and kept as stated, keeping its identity for consumers
|
|
709
|
+
return target === source ? target : immutable({
|
|
710
|
+
kind: target.kind,
|
|
711
|
+
name: target.name,
|
|
712
|
+
description: target.description,
|
|
713
|
+
space: target.space ?? source.space,
|
|
714
|
+
class: target.class,
|
|
715
|
+
classes: target.classes,
|
|
716
|
+
parents: target.parents,
|
|
717
|
+
// structural: pattern — the narrower of the two, equal unless the inherited one ends in a wildcard
|
|
718
|
+
pattern: target.pattern ?? source.pattern,
|
|
719
|
+
...identifiers(target, source),
|
|
720
|
+
// conjunctive: validators — union, a check reaching the shape along several paths stated once
|
|
721
|
+
validators: target.validators !== undefined && source.validators !== undefined
|
|
722
|
+
? union([target.validators, source.validators])
|
|
723
|
+
: target.validators ?? source.validators,
|
|
724
|
+
// conjunctive: members — union, each member stated on both sides merged
|
|
725
|
+
members: Object.fromEntries(union([
|
|
726
|
+
Object.keys(target.members),
|
|
727
|
+
Object.keys(source.members)
|
|
728
|
+
]).map(name => {
|
|
729
|
+
const declared = target.members[name];
|
|
730
|
+
const inherited = source.members[name];
|
|
731
|
+
return declared === undefined || inherited === undefined
|
|
732
|
+
? [name, declared ?? inherited]
|
|
733
|
+
: [name, mergeMember(declared, inherited)];
|
|
734
|
+
}))
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Merges a member with an inherited one.
|
|
739
|
+
*
|
|
740
|
+
* Returns the single member an extending resource is validated against: a range taking the tighter of the two
|
|
741
|
+
* cardinality bounds over the merged shape, the mappings both members share, and every other facet from whichever
|
|
742
|
+
* member states it. The overriding `hidden`, `name` and `description` take precedence; any other facet stated on both
|
|
743
|
+
* sides is the same, as the {@link narrowsProperty | override relation} requires.
|
|
744
|
+
*
|
|
745
|
+
* @param target The overriding member
|
|
746
|
+
* @param source The inherited member
|
|
747
|
+
*
|
|
748
|
+
* @returns An immutable member admitting the values both `target` and `source` admit
|
|
749
|
+
*
|
|
750
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where `target` doesn't narrow `source`
|
|
751
|
+
*/
|
|
752
|
+
export function mergeProperty(target, source) {
|
|
753
|
+
reject("incompatible member override", narrowsProperty(target, source));
|
|
754
|
+
const hidden = target.hidden ?? source.hidden;
|
|
755
|
+
const name = target.name ?? source.name;
|
|
756
|
+
const description = target.description ?? source.description;
|
|
757
|
+
const foreign = target.foreign ?? source.foreign;
|
|
758
|
+
const captive = target.captive ?? source.captive;
|
|
759
|
+
return immutable({
|
|
760
|
+
kind: target.kind,
|
|
761
|
+
...hidden !== undefined && { hidden },
|
|
762
|
+
...name !== undefined && { name },
|
|
763
|
+
...description !== undefined && { description },
|
|
764
|
+
...foreign !== undefined && { foreign },
|
|
765
|
+
...captive !== undefined && { captive },
|
|
766
|
+
// the mappings are stated alike on both sides, as narrowsProperty checked
|
|
767
|
+
...source.forward !== undefined && { forward: source.forward },
|
|
768
|
+
...source.reverse !== undefined && { reverse: source.reverse },
|
|
769
|
+
range: {
|
|
770
|
+
minCount: target.range.minCount ?? source.range.minCount,
|
|
771
|
+
maxCount: target.range.maxCount ?? source.range.maxCount,
|
|
772
|
+
shape: mergeShape(eager(target.range.shape), eager(source.range.shape))
|
|
773
|
+
}
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Merges a member of any kind with an inherited one.
|
|
778
|
+
*
|
|
779
|
+
* Returns the single member an extending resource is validated against: a {@link mergeProperty | merged property}
|
|
780
|
+
* where both are properties, and the overriding marker taken whole, `hidden` included, where both name a resource.
|
|
781
|
+
*
|
|
782
|
+
* @param target The overriding member
|
|
783
|
+
* @param source The inherited member
|
|
784
|
+
*
|
|
785
|
+
* @returns An immutable member admitting the values both `target` and `source` admit
|
|
786
|
+
*
|
|
787
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where `target` doesn't narrow `source`
|
|
788
|
+
*/
|
|
789
|
+
export function mergeMember(target, source) {
|
|
790
|
+
reject("incompatible member override", narrowsMember(target, source));
|
|
791
|
+
return target.kind === "property" && source.kind === "property"
|
|
792
|
+
? mergeProperty(target, source)
|
|
793
|
+
: target; // a marker is taken whole, `hidden` included, and narrowsMember kept the kind
|
|
794
|
+
}
|
|
795
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
796
|
+
/**
|
|
797
|
+
* Combines the identifier constraints of an overriding shape with the inherited ones.
|
|
798
|
+
*
|
|
799
|
+
* @param target The overriding shape
|
|
800
|
+
* @param source The inherited shape
|
|
801
|
+
*
|
|
802
|
+
* @returns The merged identifier constraints, as they stand before they are checked for consistency
|
|
803
|
+
*/
|
|
804
|
+
function identifiers(target, source) {
|
|
805
|
+
const { in: allowed, hasValue: required } = source;
|
|
806
|
+
return {
|
|
807
|
+
// conjunctive: in — intersection; hasValue — union
|
|
808
|
+
in: target.in !== undefined && allowed !== undefined
|
|
809
|
+
? target.in.filter(v => allowed.includes(v))
|
|
810
|
+
: target.in ?? allowed,
|
|
811
|
+
hasValue: target.hasValue !== undefined && required !== undefined
|
|
812
|
+
? union([target.hasValue, required])
|
|
813
|
+
: target.hasValue ?? required
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
//# sourceMappingURL=assembler.js.map
|