@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,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Union shape accessors.
|
|
3
|
+
*
|
|
4
|
+
* Lists the alternatives a shape admits values from, and picks the branches a stored value, a filter operand or a
|
|
5
|
+
* retrieval placeholder is matched against. A caller routing an input over a shape thus handles polymorphic and plain
|
|
6
|
+
* shapes alike.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { type Lazy, type Optional } from "@grafctory/core";
|
|
11
|
+
import { type Shape } from "../value/index.js";
|
|
12
|
+
/**
|
|
13
|
+
* Resolves the branches a shape admits values from.
|
|
14
|
+
*
|
|
15
|
+
* Flattens a union to its branches, in the order they were stated, and takes any other shape to the single branch it
|
|
16
|
+
* is, so that a caller routing a value over a shape needs not tell a polymorphic one from a plain one. Branches are
|
|
17
|
+
* resolved, a nested union flattened into the enclosing one, so that a caller routes each alternative without
|
|
18
|
+
* flattening again.
|
|
19
|
+
*
|
|
20
|
+
* Resolution also holds the union to coherence: a member name declared by several of the resources its branches
|
|
21
|
+
* describe or link to must denote the same property throughout, so that a caller may resolve it once rather than per
|
|
22
|
+
* branch. The declarations must agree on the member kind, on the `forward` and `reverse` predicates the name is
|
|
23
|
+
* mapped to, and on the `captive` and `foreign` flags; they may differ in range, cardinality and value domain, which
|
|
24
|
+
* merge into the range a path crossing the union reaches.
|
|
25
|
+
*
|
|
26
|
+
* @param shape The shape to enumerate, possibly deferred to break definition cycles
|
|
27
|
+
*
|
|
28
|
+
* @returns The branches `shape` admits values from, in the order they were stated, each resolved and merged where it
|
|
29
|
+
* describes a resource
|
|
30
|
+
*
|
|
31
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where a deferred definition reaches itself with no link to
|
|
32
|
+
* break the cycle, or where the branches declare a shared member name inconsistently
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://grafctory.github.io/qest/documents/model.Model_Design.html Model Design § 3.2 Union Constraints}
|
|
35
|
+
*/
|
|
36
|
+
export declare function getShapeBranches(shape: Lazy<Shape>): readonly Shape[];
|
|
37
|
+
/**
|
|
38
|
+
* Picks the single branch a data value belongs to.
|
|
39
|
+
*
|
|
40
|
+
* Routes a value being stored to the one branch admitting it against every constraint, so that a caller ingesting a
|
|
41
|
+
* value against a polymorphic shape settles it on a definite branch. A value admitted by several branches is ambiguous
|
|
42
|
+
* and one admitted by none unsatisfiable; both yield nothing rather than a branch chosen by guesswork.
|
|
43
|
+
*
|
|
44
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
45
|
+
*
|
|
46
|
+
* @param state The value to route
|
|
47
|
+
* @param branches The branches to choose among
|
|
48
|
+
*
|
|
49
|
+
* @returns The sole branch `state` belongs to, or `undefined` where it belongs to none or to several
|
|
50
|
+
*
|
|
51
|
+
* @see [Unions — Design](./index.md)
|
|
52
|
+
*/
|
|
53
|
+
export declare function getStateBranch<B extends Shape>(state: unknown, branches: readonly B[]): Optional<B>;
|
|
54
|
+
/**
|
|
55
|
+
* Picks the single branch a relational bound filters against.
|
|
56
|
+
*
|
|
57
|
+
* Routes a `<`, `>`, `<=` or `>=` operand to the one branch it filters, so that a caller may type the bound by the
|
|
58
|
+
* branch it resolves to. A bound need not be a legal element value, so it is matched on the syntactic discriminators
|
|
59
|
+
* alone and the union is expected to be literally disjoint; the match stays exactly one, as a conversion commits to a
|
|
60
|
+
* single branch.
|
|
61
|
+
*
|
|
62
|
+
* A localised branch takes a plain string bound, compared against the strings its tags carry. A plain string bound over
|
|
63
|
+
* a string branch and a localised one therefore fits both and routes to neither. Links and embedded resources have no
|
|
64
|
+
* order and take no bound.
|
|
65
|
+
*
|
|
66
|
+
* Routing applies the same rule as template validation, so every bound in a validated template routes to a branch.
|
|
67
|
+
*
|
|
68
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
69
|
+
*
|
|
70
|
+
* @param bound The bound to route
|
|
71
|
+
* @param branches The branches to choose among
|
|
72
|
+
*
|
|
73
|
+
* @returns The sole branch `bound` filters against, or `undefined` where it filters none or several
|
|
74
|
+
*
|
|
75
|
+
* @see [Unions — Design](./index.md)
|
|
76
|
+
*/
|
|
77
|
+
export declare function getBoundBranch<B extends Shape>(bound: unknown, branches: readonly B[]): Optional<B>;
|
|
78
|
+
/**
|
|
79
|
+
* Picks the single branch a set-matching option is tested against.
|
|
80
|
+
*
|
|
81
|
+
* Routes a `?`, `!` or `+` operand to the one branch it is compared with, so that a caller may type the option by that
|
|
82
|
+
* branch. An option is tested by equality and need not be a legal element value. It is matched on its kind alone,
|
|
83
|
+
* ignoring any lexical pattern, so the union is expected to be kind-disjoint. The match stays exactly one, as a
|
|
84
|
+
* conversion commits to a single branch.
|
|
85
|
+
*
|
|
86
|
+
* A localised branch takes either a plain string option, matched against the strings of every tag, or a map grouping
|
|
87
|
+
* options by language tag. A `null` option has no kind and fits every branch, so it routes to a branch only over a
|
|
88
|
+
* single-branch shape.
|
|
89
|
+
*
|
|
90
|
+
* Routing applies the same rule as template validation, so every option in a validated template routes to a branch.
|
|
91
|
+
* Stored values route through {@link getStateBranch} instead, which never assigns a plain string to a localised branch.
|
|
92
|
+
*
|
|
93
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
94
|
+
*
|
|
95
|
+
* @param option The option to route, stated singly rather than as the set a filter lists
|
|
96
|
+
* @param branches The branches to choose among
|
|
97
|
+
*
|
|
98
|
+
* @returns The sole branch `option` is tested against, or `undefined` where it fits none or several
|
|
99
|
+
*
|
|
100
|
+
* @see [Unions — Design](./index.md)
|
|
101
|
+
*/
|
|
102
|
+
export declare function getOptionBranch<B extends Shape>(option: unknown, branches: readonly B[]): Optional<B>;
|
|
103
|
+
/**
|
|
104
|
+
* Picks every branch a retrieval placeholder fits.
|
|
105
|
+
*
|
|
106
|
+
* Routes a placeholder to all branches it may draw from, so that a caller retrieving against a polymorphic shape need
|
|
107
|
+
* not know which branch a value was stored on. A placeholder carries no value to tell branches apart, so it may span
|
|
108
|
+
* several and retrieve each; only one fitting no branch at all is unsatisfiable.
|
|
109
|
+
*
|
|
110
|
+
* Routing goes by form. The atomic placeholder asks for the value as it stands, so it fits every branch coming back as
|
|
111
|
+
* one: a literal, a link, as the identifier naming its target, and a localised map, coalesced under the request's
|
|
112
|
+
* language priority; an embedded resource states no identifier to come back as, so it is reached through a template
|
|
113
|
+
* alone. A nested template fits the branches naming a resource, by the members it asks for, so that a link is reached
|
|
114
|
+
* either way it may be asked for. A template states what to bring back rather than what is held, so it is held to the
|
|
115
|
+
* members the resource declares but not to their presence: leaving one out routes the template all the same. A map of
|
|
116
|
+
* tag ranges fits the localised branches alone.
|
|
117
|
+
*
|
|
118
|
+
* A template may span several resource branches. Each branch admitting at least one of the members asked for is
|
|
119
|
+
* picked, and answers the members it admits. A member several branches declare may take a different shape in each,
|
|
120
|
+
* and what is asked for it need fit only one of them. The template is rejected only if some member it asks for fits no
|
|
121
|
+
* branch.
|
|
122
|
+
*
|
|
123
|
+
* A tag range may also be a member name, so an object is read one way only. It is a template if each of its keys names
|
|
124
|
+
* a member of a resource branch, reached directly or through a link, and a map of tag ranges otherwise. A template
|
|
125
|
+
* never reaches the localised branches, even if the resource branches reject it.
|
|
126
|
+
*
|
|
127
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
128
|
+
*
|
|
129
|
+
* @param model The placeholder to route
|
|
130
|
+
* @param branches The branches to choose among
|
|
131
|
+
*
|
|
132
|
+
* @returns Every branch `model` fits, or `undefined` where it fits none
|
|
133
|
+
*
|
|
134
|
+
* @see [Unions — Design](./index.md)
|
|
135
|
+
*/
|
|
136
|
+
export declare function getModelBranches<B extends Shape>(model: unknown, branches: readonly B[]): Optional<readonly B[]>;
|
|
137
|
+
//# sourceMappingURL=accessors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accessors.d.ts","sourceRoot":"","sources":["../../src/union/accessors.ts"],"names":[],"mappings":"AAgBA;;;;;;;;GAQG;AAEH,OAAO,EAAY,KAAK,IAAI,EAAO,KAAK,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAO1E,OAAO,EAAS,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAgBtD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,SAAS,KAAK,EAAE,CAQrE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAMnG;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAMnG;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAMrG;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAoChH"}
|
|
@@ -0,0 +1,253 @@
|
|
|
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
|
+
* Union shape accessors.
|
|
18
|
+
*
|
|
19
|
+
* Lists the alternatives a shape admits values from, and picks the branches a stored value, a filter operand or a
|
|
20
|
+
* retrieval placeholder is matched against. A caller routing an input over a shape thus handles polymorphic and plain
|
|
21
|
+
* shapes alike.
|
|
22
|
+
*
|
|
23
|
+
* @module
|
|
24
|
+
*/
|
|
25
|
+
import { isObject, opt } from "@grafctory/core";
|
|
26
|
+
import { isAtomic } from "@grafctory/qest/model";
|
|
27
|
+
import { getShapeTarget } from "../reference/index.js";
|
|
28
|
+
import { validateTemplate } from "../resource/validator.js";
|
|
29
|
+
import { reject } from "../value/assembler.js";
|
|
30
|
+
import { eager } from "../value/index.js";
|
|
31
|
+
import { validateShape } from "../value/validator.js";
|
|
32
|
+
import { checkBound, checkOption, isTemplateModel, matching } from "./validator.js";
|
|
33
|
+
/**
|
|
34
|
+
* The branches of each union already resolved and found coherent.
|
|
35
|
+
*
|
|
36
|
+
* Keyed by the union stating them, so that coherence is checked once per union rather than on every reach.
|
|
37
|
+
*/
|
|
38
|
+
const unions = new WeakMap();
|
|
39
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
40
|
+
/**
|
|
41
|
+
* Resolves the branches a shape admits values from.
|
|
42
|
+
*
|
|
43
|
+
* Flattens a union to its branches, in the order they were stated, and takes any other shape to the single branch it
|
|
44
|
+
* is, so that a caller routing a value over a shape needs not tell a polymorphic one from a plain one. Branches are
|
|
45
|
+
* resolved, a nested union flattened into the enclosing one, so that a caller routes each alternative without
|
|
46
|
+
* flattening again.
|
|
47
|
+
*
|
|
48
|
+
* Resolution also holds the union to coherence: a member name declared by several of the resources its branches
|
|
49
|
+
* describe or link to must denote the same property throughout, so that a caller may resolve it once rather than per
|
|
50
|
+
* branch. The declarations must agree on the member kind, on the `forward` and `reverse` predicates the name is
|
|
51
|
+
* mapped to, and on the `captive` and `foreign` flags; they may differ in range, cardinality and value domain, which
|
|
52
|
+
* merge into the range a path crossing the union reaches.
|
|
53
|
+
*
|
|
54
|
+
* @param shape The shape to enumerate, possibly deferred to break definition cycles
|
|
55
|
+
*
|
|
56
|
+
* @returns The branches `shape` admits values from, in the order they were stated, each resolved and merged where it
|
|
57
|
+
* describes a resource
|
|
58
|
+
*
|
|
59
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where a deferred definition reaches itself with no link to
|
|
60
|
+
* break the cycle, or where the branches declare a shared member name inconsistently
|
|
61
|
+
*
|
|
62
|
+
* @see {@link https://grafctory.github.io/qest/documents/model.Model_Design.html Model Design § 3.2 Union Constraints}
|
|
63
|
+
*/
|
|
64
|
+
export function getShapeBranches(shape) {
|
|
65
|
+
const resolved = eager(shape);
|
|
66
|
+
return resolved.kind === "union"
|
|
67
|
+
? unions.get(resolved) ?? settle(resolved)
|
|
68
|
+
: [resolved];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Picks the single branch a data value belongs to.
|
|
72
|
+
*
|
|
73
|
+
* Routes a value being stored to the one branch admitting it against every constraint, so that a caller ingesting a
|
|
74
|
+
* value against a polymorphic shape settles it on a definite branch. A value admitted by several branches is ambiguous
|
|
75
|
+
* and one admitted by none unsatisfiable; both yield nothing rather than a branch chosen by guesswork.
|
|
76
|
+
*
|
|
77
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
78
|
+
*
|
|
79
|
+
* @param state The value to route
|
|
80
|
+
* @param branches The branches to choose among
|
|
81
|
+
*
|
|
82
|
+
* @returns The sole branch `state` belongs to, or `undefined` where it belongs to none or to several
|
|
83
|
+
*
|
|
84
|
+
* @see [Unions — Design](./index.md)
|
|
85
|
+
*/
|
|
86
|
+
export function getStateBranch(state, branches) {
|
|
87
|
+
const matched = matching(state, branches, "state");
|
|
88
|
+
return matched.length === 1 ? matched[0] : undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Picks the single branch a relational bound filters against.
|
|
92
|
+
*
|
|
93
|
+
* Routes a `<`, `>`, `<=` or `>=` operand to the one branch it filters, so that a caller may type the bound by the
|
|
94
|
+
* branch it resolves to. A bound need not be a legal element value, so it is matched on the syntactic discriminators
|
|
95
|
+
* alone and the union is expected to be literally disjoint; the match stays exactly one, as a conversion commits to a
|
|
96
|
+
* single branch.
|
|
97
|
+
*
|
|
98
|
+
* A localised branch takes a plain string bound, compared against the strings its tags carry. A plain string bound over
|
|
99
|
+
* a string branch and a localised one therefore fits both and routes to neither. Links and embedded resources have no
|
|
100
|
+
* order and take no bound.
|
|
101
|
+
*
|
|
102
|
+
* Routing applies the same rule as template validation, so every bound in a validated template routes to a branch.
|
|
103
|
+
*
|
|
104
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
105
|
+
*
|
|
106
|
+
* @param bound The bound to route
|
|
107
|
+
* @param branches The branches to choose among
|
|
108
|
+
*
|
|
109
|
+
* @returns The sole branch `bound` filters against, or `undefined` where it filters none or several
|
|
110
|
+
*
|
|
111
|
+
* @see [Unions — Design](./index.md)
|
|
112
|
+
*/
|
|
113
|
+
export function getBoundBranch(bound, branches) {
|
|
114
|
+
const matched = branches.filter(branch => checkBound(bound, branch) === undefined);
|
|
115
|
+
return matched.length === 1 ? matched[0] : undefined;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Picks the single branch a set-matching option is tested against.
|
|
119
|
+
*
|
|
120
|
+
* Routes a `?`, `!` or `+` operand to the one branch it is compared with, so that a caller may type the option by that
|
|
121
|
+
* branch. An option is tested by equality and need not be a legal element value. It is matched on its kind alone,
|
|
122
|
+
* ignoring any lexical pattern, so the union is expected to be kind-disjoint. The match stays exactly one, as a
|
|
123
|
+
* conversion commits to a single branch.
|
|
124
|
+
*
|
|
125
|
+
* A localised branch takes either a plain string option, matched against the strings of every tag, or a map grouping
|
|
126
|
+
* options by language tag. A `null` option has no kind and fits every branch, so it routes to a branch only over a
|
|
127
|
+
* single-branch shape.
|
|
128
|
+
*
|
|
129
|
+
* Routing applies the same rule as template validation, so every option in a validated template routes to a branch.
|
|
130
|
+
* Stored values route through {@link getStateBranch} instead, which never assigns a plain string to a localised branch.
|
|
131
|
+
*
|
|
132
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
133
|
+
*
|
|
134
|
+
* @param option The option to route, stated singly rather than as the set a filter lists
|
|
135
|
+
* @param branches The branches to choose among
|
|
136
|
+
*
|
|
137
|
+
* @returns The sole branch `option` is tested against, or `undefined` where it fits none or several
|
|
138
|
+
*
|
|
139
|
+
* @see [Unions — Design](./index.md)
|
|
140
|
+
*/
|
|
141
|
+
export function getOptionBranch(option, branches) {
|
|
142
|
+
const matched = branches.filter(branch => checkOption(option, branch) === undefined);
|
|
143
|
+
return matched.length === 1 ? matched[0] : undefined;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Picks every branch a retrieval placeholder fits.
|
|
147
|
+
*
|
|
148
|
+
* Routes a placeholder to all branches it may draw from, so that a caller retrieving against a polymorphic shape need
|
|
149
|
+
* not know which branch a value was stored on. A placeholder carries no value to tell branches apart, so it may span
|
|
150
|
+
* several and retrieve each; only one fitting no branch at all is unsatisfiable.
|
|
151
|
+
*
|
|
152
|
+
* Routing goes by form. The atomic placeholder asks for the value as it stands, so it fits every branch coming back as
|
|
153
|
+
* one: a literal, a link, as the identifier naming its target, and a localised map, coalesced under the request's
|
|
154
|
+
* language priority; an embedded resource states no identifier to come back as, so it is reached through a template
|
|
155
|
+
* alone. A nested template fits the branches naming a resource, by the members it asks for, so that a link is reached
|
|
156
|
+
* either way it may be asked for. A template states what to bring back rather than what is held, so it is held to the
|
|
157
|
+
* members the resource declares but not to their presence: leaving one out routes the template all the same. A map of
|
|
158
|
+
* tag ranges fits the localised branches alone.
|
|
159
|
+
*
|
|
160
|
+
* A template may span several resource branches. Each branch admitting at least one of the members asked for is
|
|
161
|
+
* picked, and answers the members it admits. A member several branches declare may take a different shape in each,
|
|
162
|
+
* and what is asked for it need fit only one of them. The template is rejected only if some member it asks for fits no
|
|
163
|
+
* branch.
|
|
164
|
+
*
|
|
165
|
+
* A tag range may also be a member name, so an object is read one way only. It is a template if each of its keys names
|
|
166
|
+
* a member of a resource branch, reached directly or through a link, and a map of tag ranges otherwise. A template
|
|
167
|
+
* never reaches the localised branches, even if the resource branches reject it.
|
|
168
|
+
*
|
|
169
|
+
* @typeParam B The branch type, carried through from the branches supplied
|
|
170
|
+
*
|
|
171
|
+
* @param model The placeholder to route
|
|
172
|
+
* @param branches The branches to choose among
|
|
173
|
+
*
|
|
174
|
+
* @returns Every branch `model` fits, or `undefined` where it fits none
|
|
175
|
+
*
|
|
176
|
+
* @see [Unions — Design](./index.md)
|
|
177
|
+
*/
|
|
178
|
+
export function getModelBranches(model, branches) {
|
|
179
|
+
const template = isTemplateModel(model, branches);
|
|
180
|
+
const members = isObject(model) ? Object.entries(model) : [];
|
|
181
|
+
const matched = branches.filter(branch => {
|
|
182
|
+
const target = getShapeTarget(branch);
|
|
183
|
+
return isAtomic(model)
|
|
184
|
+
// an embedded resource states no identifier to come back as, so it is reached through a template alone
|
|
185
|
+
? branch.kind !== "resource" && validateShape([model], branch, { scope: "model" }) === undefined
|
|
186
|
+
// a template crosses a link, standing for the resource it points at rather than for the link itself, and
|
|
187
|
+
// may span several resources, each answering the members whose placeholders it admits
|
|
188
|
+
: target !== undefined ? template && members.some(([name, asked]) => answers(target, name, asked))
|
|
189
|
+
// an object read as a template never asks for tag ranges
|
|
190
|
+
: (branch.kind !== "dictionary" || !template)
|
|
191
|
+
&& validateShape([model], branch, { scope: "model" }) === undefined;
|
|
192
|
+
});
|
|
193
|
+
// a member several resources declare may take a different shape in each, so its placeholder is held to fit one of
|
|
194
|
+
// them rather than all
|
|
195
|
+
const answered = !template || members.every(([name, asked]) => matched.some(branch => opt(getShapeTarget(branch), target => answers(target, name, asked), false)));
|
|
196
|
+
return answered && matched.length > 0 ? matched : undefined;
|
|
197
|
+
}
|
|
198
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
199
|
+
/**
|
|
200
|
+
* Tells whether a resource admits what a template asks for one of its members.
|
|
201
|
+
*/
|
|
202
|
+
function answers(target, name, asked) {
|
|
203
|
+
return validateTemplate([{ [name]: asked }], target) === undefined;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Resolves the branches of a union and holds them to coherence.
|
|
207
|
+
*
|
|
208
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the branches declare a shared member name
|
|
209
|
+
* inconsistently
|
|
210
|
+
*/
|
|
211
|
+
function settle(union) {
|
|
212
|
+
const branches = union.branches.flatMap(branch => getShapeBranches(branch));
|
|
213
|
+
reject("incoherent union shape", checkCoherence(branches));
|
|
214
|
+
unions.set(union, branches);
|
|
215
|
+
return branches;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Checks that the member names shared by the resources a set of branches describe or link to denote the same property.
|
|
219
|
+
*
|
|
220
|
+
* @returns A trace of the attributes each shared name is declared with inconsistently, or `undefined` where every
|
|
221
|
+
* shared name agrees throughout
|
|
222
|
+
*/
|
|
223
|
+
function checkCoherence(branches) {
|
|
224
|
+
const declarations = branches
|
|
225
|
+
.map(branch => getShapeTarget(branch))
|
|
226
|
+
.filter(target => target !== undefined)
|
|
227
|
+
.flatMap(target => Object.entries(target.members));
|
|
228
|
+
const names = [...new Set(declarations.map(([name]) => name))];
|
|
229
|
+
const issues = names.flatMap(name => {
|
|
230
|
+
const [first, ...rest] = declarations
|
|
231
|
+
.filter(([declared]) => declared === name)
|
|
232
|
+
.map(([, member]) => identity(member));
|
|
233
|
+
const conflicts = first
|
|
234
|
+
.filter(([, value], index) => rest.some(other => other[index][1] !== value))
|
|
235
|
+
.map(([attribute]) => `inconsistent <${attribute}> across branches`);
|
|
236
|
+
return conflicts.length === 0 ? [] : [{ [name]: conflicts }];
|
|
237
|
+
});
|
|
238
|
+
return issues.length === 0 ? undefined : issues;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Lists the attributes fixing the property a member denotes, in a fixed order, unstated flags taken as `false`.
|
|
242
|
+
*/
|
|
243
|
+
function identity(member) {
|
|
244
|
+
const property = member.kind === "property" ? member : undefined;
|
|
245
|
+
return [
|
|
246
|
+
["kind", member.kind],
|
|
247
|
+
["forward", property?.forward],
|
|
248
|
+
["reverse", property?.reverse],
|
|
249
|
+
["captive", property?.captive ?? false],
|
|
250
|
+
["foreign", property?.foreign ?? false]
|
|
251
|
+
];
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=accessors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accessors.js","sourceRoot":"","sources":["../../src/union/accessors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;GAQG;AAEH,OAAO,EAAE,QAAQ,EAAa,GAAG,EAAiB,MAAM,iBAAiB,CAAC;AAE1E,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAc,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAGpF;;;;GAIG;AACH,MAAM,MAAM,GAAG,IAAI,OAAO,EAAgC,CAAC;AAG3D,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAkB;IAElD,MAAM,Q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AM,CAAC,IAAI,CAAC;QACrB,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,CAAC;QAC9B,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,CAAC;QAC9B,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,IAAI,KAAK,CAAC;QACvC,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,IAAI,KAAK,CAAC;KACvC,CAAC;AAEH,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Union shape assembly.
|
|
3
|
+
*
|
|
4
|
+
* Builds the union the factory states into the form its consumers read, flattening a nested union into the enclosing
|
|
5
|
+
* one, and combines it with the one it overrides: an extension narrows exactly when each of its branches claims one
|
|
6
|
+
* inherited branch of its own.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { type Optional } from "@grafctory/core";
|
|
11
|
+
import { type Trace } from "@grafctory/core/trace";
|
|
12
|
+
import type { UnionBranches, UnionShape } from "./index.js";
|
|
13
|
+
/**
|
|
14
|
+
* Assembles a union shape.
|
|
15
|
+
*
|
|
16
|
+
* Backs the factory the {@link union!} module exposes. A branch that is itself a union is replaced by the alternatives
|
|
17
|
+
* it holds, so that a union states a flat list of alternatives however it was assembled; any other branch is kept as
|
|
18
|
+
* it was stated, deferred or not, so that a cycle among definitions is broken by whichever branch defers. Order is
|
|
19
|
+
* retained throughout, a spliced branch taking the place its union held.
|
|
20
|
+
*
|
|
21
|
+
* @typeParam B The alternatives a value may be drawn from
|
|
22
|
+
*
|
|
23
|
+
* @param branches The shapes a value may be drawn from
|
|
24
|
+
*
|
|
25
|
+
* @returns An immutable shape admitting the values any of the branches admits
|
|
26
|
+
*/
|
|
27
|
+
export declare function assemble<B extends UnionBranches>(branches: B): UnionShape<B>;
|
|
28
|
+
/**
|
|
29
|
+
* Reports whether a union narrows an inherited one.
|
|
30
|
+
*
|
|
31
|
+
* Tests the override relation without building the merged union, so that an incompatible extension is told apart from
|
|
32
|
+
* a legitimate refinement before either is committed to: a union narrows the inherited one where each of its branches
|
|
33
|
+
* narrows exactly one inherited branch and no two of them claim the same one. Inherited branches left unclaimed are
|
|
34
|
+
* dropped, so an extending shape may shed alternatives but never add them.
|
|
35
|
+
*
|
|
36
|
+
* @param target The overriding union
|
|
37
|
+
* @param source The inherited union
|
|
38
|
+
*
|
|
39
|
+
* @returns A trace of the obstacles to the override, or `undefined` where `target` narrows `source`
|
|
40
|
+
*/
|
|
41
|
+
export declare function narrowsUnion(target: UnionShape, source: UnionShape): Optional<Trace>;
|
|
42
|
+
/**
|
|
43
|
+
* Merges a union with an inherited one.
|
|
44
|
+
*
|
|
45
|
+
* Yields the single shape an extending member is validated against: each claimed inherited branch merged with the
|
|
46
|
+
* branch claiming it, in the order the inherited union stated them, with the unclaimed ones dropped.
|
|
47
|
+
*
|
|
48
|
+
* @param target The overriding union
|
|
49
|
+
* @param source The inherited union
|
|
50
|
+
*
|
|
51
|
+
* @returns An immutable shape admitting the values both `target` and `source` admit
|
|
52
|
+
*
|
|
53
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where `target` doesn't narrow `source`
|
|
54
|
+
*/
|
|
55
|
+
export declare function mergeUnion(target: UnionShape, source: UnionShape): UnionShape;
|
|
56
|
+
//# sourceMappingURL=assembler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assembler.d.ts","sourceRoot":"","sources":["../../src/union/assembler.ts"],"names":[],"mappings":"AAgBA;;;;;;;;GAQG;AAEH,OAAO,EAAc,KAAK,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE5D,OAAO,EAAS,KAAK,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAG1D,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG5D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,aAAa,EAAE,QAAQ,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAgB5E;AAKD;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,QAAQ,CAAC,KAAK,CAAC,CAIpF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,UAAU,CAuB7E"}
|
|
@@ -0,0 +1,140 @@
|
|
|
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
|
+
* Union shape assembly.
|
|
18
|
+
*
|
|
19
|
+
* Builds the union the factory states into the form its consumers read, flattening a nested union into the enclosing
|
|
20
|
+
* one, and combines it with the one it overrides: an extension narrows exactly when each of its branches claims one
|
|
21
|
+
* inherited branch of its own.
|
|
22
|
+
*
|
|
23
|
+
* @module
|
|
24
|
+
*/
|
|
25
|
+
import { isFunction } from "@grafctory/core";
|
|
26
|
+
import { immutable } from "@grafctory/core/values";
|
|
27
|
+
import { array } from "@grafctory/core/trace";
|
|
28
|
+
import { mergeShape, narrowsShape, reject } from "../value/assembler.js";
|
|
29
|
+
import { getShapeBranches } from "./accessors.js";
|
|
30
|
+
/**
|
|
31
|
+
* Assembles a union shape.
|
|
32
|
+
*
|
|
33
|
+
* Backs the factory the {@link union!} module exposes. A branch that is itself a union is replaced by the alternatives
|
|
34
|
+
* it holds, so that a union states a flat list of alternatives however it was assembled; any other branch is kept as
|
|
35
|
+
* it was stated, deferred or not, so that a cycle among definitions is broken by whichever branch defers. Order is
|
|
36
|
+
* retained throughout, a spliced branch taking the place its union held.
|
|
37
|
+
*
|
|
38
|
+
* @typeParam B The alternatives a value may be drawn from
|
|
39
|
+
*
|
|
40
|
+
* @param branches The shapes a value may be drawn from
|
|
41
|
+
*
|
|
42
|
+
* @returns An immutable shape admitting the values any of the branches admits
|
|
43
|
+
*/
|
|
44
|
+
export function assemble(branches) {
|
|
45
|
+
// a nested union is itself built here, so it is already flat and splicing one level keeps the branches flat
|
|
46
|
+
const flattened = branches.flatMap(branch => !isFunction(branch) && branch.kind === "union" ? branch.branches : [branch]);
|
|
47
|
+
return immutable({
|
|
48
|
+
kind: "union",
|
|
49
|
+
branches: flattened
|
|
50
|
+
}); // ;(cast) a spliced branch admits exactly the values the union holding it admitted
|
|
51
|
+
}
|
|
52
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
53
|
+
/**
|
|
54
|
+
* Reports whether a union narrows an inherited one.
|
|
55
|
+
*
|
|
56
|
+
* Tests the override relation without building the merged union, so that an incompatible extension is told apart from
|
|
57
|
+
* a legitimate refinement before either is committed to: a union narrows the inherited one where each of its branches
|
|
58
|
+
* narrows exactly one inherited branch and no two of them claim the same one. Inherited branches left unclaimed are
|
|
59
|
+
* dropped, so an extending shape may shed alternatives but never add them.
|
|
60
|
+
*
|
|
61
|
+
* @param target The overriding union
|
|
62
|
+
* @param source The inherited union
|
|
63
|
+
*
|
|
64
|
+
* @returns A trace of the obstacles to the override, or `undefined` where `target` narrows `source`
|
|
65
|
+
*/
|
|
66
|
+
export function narrowsUnion(target, source) {
|
|
67
|
+
return checkClaims(pair(target, source));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Merges a union with an inherited one.
|
|
71
|
+
*
|
|
72
|
+
* Yields the single shape an extending member is validated against: each claimed inherited branch merged with the
|
|
73
|
+
* branch claiming it, in the order the inherited union stated them, with the unclaimed ones dropped.
|
|
74
|
+
*
|
|
75
|
+
* @param target The overriding union
|
|
76
|
+
* @param source The inherited union
|
|
77
|
+
*
|
|
78
|
+
* @returns An immutable shape admitting the values both `target` and `source` admit
|
|
79
|
+
*
|
|
80
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where `target` doesn't narrow `source`
|
|
81
|
+
*/
|
|
82
|
+
export function mergeUnion(target, source) {
|
|
83
|
+
const pairs = pair(target, source);
|
|
84
|
+
reject("incompatible union shape override", checkClaims(pairs));
|
|
85
|
+
const overriding = getShapeBranches(target);
|
|
86
|
+
const inherited = getShapeBranches(source);
|
|
87
|
+
// the branch claiming each inherited one, keyed by the position the inherited union states it at
|
|
88
|
+
const claimants = new Map(pairs.map(([base], index) => [base, index]));
|
|
89
|
+
// merged branches keep the inherited order; unclaimed inherited branches are dropped
|
|
90
|
+
return assemble(inherited.flatMap((base, index) => {
|
|
91
|
+
const claimant = claimants.get(index);
|
|
92
|
+
return claimant === undefined ? [] : [mergeShape(overriding[claimant], base)];
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
96
|
+
/**
|
|
97
|
+
* Pairs each overriding branch with the inherited branches it claims.
|
|
98
|
+
*
|
|
99
|
+
* Pairs by trial narrowing: a branch claims an inherited one where {@link narrowsShape} reports no obstacle.
|
|
100
|
+
*
|
|
101
|
+
* @param target The overriding union
|
|
102
|
+
* @param source The inherited union
|
|
103
|
+
*
|
|
104
|
+
* @returns For each overriding branch, in the order `target` states them, the positions of the inherited branches it
|
|
105
|
+
* claims
|
|
106
|
+
*/
|
|
107
|
+
function pair(target, source) {
|
|
108
|
+
const inherited = getShapeBranches(source);
|
|
109
|
+
// a branch restated unchanged, as where the same union reaches a shape along two inheritance paths, claims its own
|
|
110
|
+
// copy, never competing for the wider ones it narrows
|
|
111
|
+
return getShapeBranches(target).map(branch => {
|
|
112
|
+
const restated = inherited.indexOf(branch);
|
|
113
|
+
return restated >= 0 ? [restated]
|
|
114
|
+
: inherited.flatMap((base, index) => narrowsShape(branch, base) === undefined ? [index] : []);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Checks a pairing for the obstacles that stand against an override.
|
|
119
|
+
*
|
|
120
|
+
* Every branch must claim exactly one inherited branch, and no inherited branch may be claimed twice, so the pairing is
|
|
121
|
+
* a partial injection from the inherited branches to the overriding ones and the inherited branches left over are
|
|
122
|
+
* dropped by the caller.
|
|
123
|
+
*
|
|
124
|
+
* @param pairs The inherited branches each overriding branch claims
|
|
125
|
+
*
|
|
126
|
+
* @returns A trace of the obstacles to the pairing, or `undefined` where it is a partial injection
|
|
127
|
+
*/
|
|
128
|
+
function checkClaims(pairs) {
|
|
129
|
+
const exactly = array((bases) => bases.length === 0 ? ["branch narrows no inherited branch"]
|
|
130
|
+
: bases.length > 1 ? ["branch narrows several inherited branches"]
|
|
131
|
+
: undefined)(pairs);
|
|
132
|
+
return exactly ?? injective(pairs.map(bases => bases[0]));
|
|
133
|
+
/**
|
|
134
|
+
* Reports the inherited branches claimed by more than one overriding branch.
|
|
135
|
+
*/
|
|
136
|
+
function injective(claimed) {
|
|
137
|
+
return array((taken) => taken ? ["branch narrows an inherited branch already claimed"] : undefined)(claimed.map((base, index) => claimed.indexOf(base) !== index));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=assembler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assembler.js","sourceRoot":"","sources":["../../src/union/assembler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAiB,MAAM,iBAAiB,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,w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