@nmnmcc/ability 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -14
- package/dist/Condition.d.ts +17 -7
- package/dist/Condition.d.ts.map +1 -1
- package/dist/Condition.js +2 -1
- package/dist/Condition.js.map +1 -1
- package/dist/Limits.d.ts +3 -1
- package/dist/Limits.d.ts.map +1 -1
- package/dist/Limits.js +3 -1
- package/dist/Limits.js.map +1 -1
- package/dist/Policy.d.ts +2 -6
- package/dist/Policy.d.ts.map +1 -1
- package/dist/Policy.js.map +1 -1
- package/dist/internal/algebra.js +23 -13
- package/dist/internal/algebra.js.map +1 -1
- package/dist/internal/analysis.js +4 -2
- package/dist/internal/analysis.js.map +1 -1
- package/dist/internal/bind.js +28 -19
- package/dist/internal/bind.js.map +1 -1
- package/dist/internal/catalog.d.ts +9 -0
- package/dist/internal/catalog.d.ts.map +1 -1
- package/dist/internal/catalog.js +37 -22
- package/dist/internal/catalog.js.map +1 -1
- package/dist/internal/decide.js +2 -1
- package/dist/internal/decide.js.map +1 -1
- package/dist/internal/place.js +19 -0
- package/dist/internal/place.js.map +1 -0
- package/dist/internal/policy-json.js +39 -39
- package/dist/internal/policy-json.js.map +1 -1
- package/dist/internal/rule.d.ts +20 -22
- package/dist/internal/rule.d.ts.map +1 -1
- package/dist/internal/rule.js +32 -4
- package/dist/internal/rule.js.map +1 -1
- package/package.json +1 -1
- package/src/Condition.ts +46 -25
- package/src/Limits.ts +3 -1
- package/src/Policy.ts +1 -7
- package/src/internal/algebra.ts +30 -13
- package/src/internal/analysis.ts +5 -0
- package/src/internal/bind.ts +66 -27
- package/src/internal/catalog.ts +78 -30
- package/src/internal/decide.ts +3 -0
- package/src/internal/place.ts +32 -0
- package/src/internal/policy-json.ts +49 -29
- package/src/internal/rule.ts +55 -11
package/src/internal/algebra.ts
CHANGED
|
@@ -259,8 +259,8 @@ const testUnder = (
|
|
|
259
259
|
})
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
/** Tests a condition with no predicate on a value. */
|
|
263
|
-
|
|
262
|
+
/** Tests a simple condition with no predicate on a value. */
|
|
263
|
+
const testSync = (node: Node, input: Input): boolean => {
|
|
264
264
|
switch (node._tag) {
|
|
265
265
|
case "True":
|
|
266
266
|
return true
|
|
@@ -283,11 +283,11 @@ export const evaluateSync = (node: Node, input: Input): boolean => {
|
|
|
283
283
|
case "Under":
|
|
284
284
|
return testUnder(node.prefix, input)
|
|
285
285
|
case "And":
|
|
286
|
-
return Arr.every(node.conditions, (child) =>
|
|
286
|
+
return Arr.every(node.conditions, (child) => testSync(child, input))
|
|
287
287
|
case "Or":
|
|
288
|
-
return Arr.some(node.conditions, (child) =>
|
|
288
|
+
return Arr.some(node.conditions, (child) => testSync(child, input))
|
|
289
289
|
case "Not":
|
|
290
|
-
return !
|
|
290
|
+
return !testSync(node.condition, input)
|
|
291
291
|
case "Predicate":
|
|
292
292
|
// The types stop this. A predicate needs `evaluate`.
|
|
293
293
|
throw new Error(
|
|
@@ -297,38 +297,55 @@ export const evaluateSync = (node: Node, input: Input): boolean => {
|
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
/**
|
|
300
|
-
* Tests a condition on a value. The children of `And` and `Or` run in
|
|
301
|
-
* and they stop at the first child that gives the answer.
|
|
300
|
+
* Tests a simple condition on a value. The children of `And` and `Or` run in
|
|
301
|
+
* order, and they stop at the first child that gives the answer.
|
|
302
302
|
*/
|
|
303
|
-
|
|
303
|
+
const test = (
|
|
304
304
|
node: Node,
|
|
305
305
|
input: Input,
|
|
306
306
|
): Effect.Effect<boolean, unknown, unknown> => {
|
|
307
307
|
if (!node.opaque) {
|
|
308
|
-
return Effect.sync(() =>
|
|
308
|
+
return Effect.sync(() => testSync(node, input))
|
|
309
309
|
}
|
|
310
310
|
switch (node._tag) {
|
|
311
311
|
case "Predicate":
|
|
312
312
|
return Nodes.runOf(node)(input.value, input.principal)
|
|
313
313
|
case "Not":
|
|
314
|
-
return Effect.map(
|
|
314
|
+
return Effect.map(test(node.condition, input), (value) => !value)
|
|
315
315
|
case "And":
|
|
316
316
|
return Effect.map(
|
|
317
317
|
Effect.findFirst(node.conditions, (child) =>
|
|
318
|
-
Effect.map(
|
|
318
|
+
Effect.map(test(child, input), (value) => !value),
|
|
319
319
|
),
|
|
320
320
|
Option.isNone,
|
|
321
321
|
)
|
|
322
322
|
case "Or":
|
|
323
323
|
return Effect.map(
|
|
324
|
-
Effect.findFirst(node.conditions, (child) =>
|
|
324
|
+
Effect.findFirst(node.conditions, (child) => test(child, input)),
|
|
325
325
|
Option.isSome,
|
|
326
326
|
)
|
|
327
327
|
default:
|
|
328
|
-
return Effect.sync(() =>
|
|
328
|
+
return Effect.sync(() => testSync(node, input))
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
331
|
|
|
332
|
+
/**
|
|
333
|
+
* Tests a condition with no predicate on a value. The test runs on the
|
|
334
|
+
* simple form of the condition with the principal in it, as a residual does.
|
|
335
|
+
* So a test reads only the fields that the residual reads: a read of a field
|
|
336
|
+
* that is not plain data is a defect, and a field that the answer does not
|
|
337
|
+
* need, as in `x AND False`, is not read.
|
|
338
|
+
*/
|
|
339
|
+
export const evaluateSync = (node: Node, input: Input): boolean =>
|
|
340
|
+
testSync(partial(node, input), input)
|
|
341
|
+
|
|
342
|
+
/** Tests a condition on a value, as {@link evaluateSync} does. */
|
|
343
|
+
export const evaluate = (
|
|
344
|
+
node: Node,
|
|
345
|
+
input: Input,
|
|
346
|
+
): Effect.Effect<boolean, unknown, unknown> =>
|
|
347
|
+
Effect.suspend(() => test(partial(node, input), input))
|
|
348
|
+
|
|
332
349
|
// The principal and the path
|
|
333
350
|
|
|
334
351
|
/**
|
package/src/internal/analysis.ts
CHANGED
|
@@ -85,6 +85,8 @@ interface ResidualContext {
|
|
|
85
85
|
}
|
|
86
86
|
/** The resource of the kind, for the fields that a rule may grant. */
|
|
87
87
|
readonly descriptor: Resource.Descriptor
|
|
88
|
+
/** The test of `RuleData.fitsOf` for the catalog, for this request. */
|
|
89
|
+
readonly fits: (pattern: string) => boolean
|
|
88
90
|
/**
|
|
89
91
|
* The condition of each rule, with the principal in it. They are made
|
|
90
92
|
* first, from a flat list of the rules, so the walk does not go down the
|
|
@@ -241,6 +243,7 @@ interface PredicateContext {
|
|
|
241
243
|
readonly kind: string
|
|
242
244
|
readonly field: string | undefined
|
|
243
245
|
readonly descriptor: Resource.Descriptor
|
|
246
|
+
readonly fits: (pattern: string) => boolean
|
|
244
247
|
}
|
|
245
248
|
|
|
246
249
|
/** Tests if a rule that the request may use has a predicate. */
|
|
@@ -345,6 +348,7 @@ const scopedResiduals = (policy: Any, request: ScopedRequest): Scoped => {
|
|
|
345
348
|
field: request.field,
|
|
346
349
|
input,
|
|
347
350
|
descriptor,
|
|
351
|
+
fits: RuleData.fitsOf(descriptor.catalog),
|
|
348
352
|
}
|
|
349
353
|
const context: ResidualContext = {
|
|
350
354
|
...target,
|
|
@@ -378,6 +382,7 @@ const scopedResiduals = (policy: Any, request: ScopedRequest): Scoped => {
|
|
|
378
382
|
path: descriptor.path,
|
|
379
383
|
},
|
|
380
384
|
descriptor,
|
|
385
|
+
fits: RuleData.fitsOf(descriptor.catalog),
|
|
381
386
|
trace: undefined,
|
|
382
387
|
},
|
|
383
388
|
[],
|
package/src/internal/bind.ts
CHANGED
|
@@ -16,6 +16,7 @@ import * as Catalog from "./catalog.ts"
|
|
|
16
16
|
import * as Limits from "./limits.ts"
|
|
17
17
|
import * as Nodes from "./nodes.ts"
|
|
18
18
|
import * as Patterns from "./patterns.ts"
|
|
19
|
+
import * as Place from "./place.ts"
|
|
19
20
|
import { withSuggestion } from "./suggest.ts"
|
|
20
21
|
|
|
21
22
|
/** One problem of a rule or a policy, with its path. */
|
|
@@ -176,6 +177,36 @@ const fieldIssue = (
|
|
|
176
177
|
]
|
|
177
178
|
}
|
|
178
179
|
|
|
180
|
+
/**
|
|
181
|
+
* The issues of a field that a condition reads: a field that is not a field
|
|
182
|
+
* of every resource of the rule, or a field that is not plain data in one of
|
|
183
|
+
* them. A read of a value that is not plain data, as a getter of a class, is
|
|
184
|
+
* a defect in a decision. A residual may leave the read out, so the residual
|
|
185
|
+
* would say "yes" where the decision fails. So the binding does not take
|
|
186
|
+
* such a read: see `Catalog.Field.plain`.
|
|
187
|
+
*/
|
|
188
|
+
const readIssues = (
|
|
189
|
+
context: Context,
|
|
190
|
+
field: string,
|
|
191
|
+
at: ReadonlyArray<PropertyKey>,
|
|
192
|
+
): ReadonlyArray<Issue> => {
|
|
193
|
+
const exists = fieldIssue(context, field, at)
|
|
194
|
+
if (exists.length > 0) {
|
|
195
|
+
return exists
|
|
196
|
+
}
|
|
197
|
+
return Arr.some(
|
|
198
|
+
context.named,
|
|
199
|
+
(descriptor) => !(Catalog.lookup(descriptor.catalog, field)?.plain ?? true),
|
|
200
|
+
)
|
|
201
|
+
? [
|
|
202
|
+
{
|
|
203
|
+
path: at,
|
|
204
|
+
issue: `A condition may not read the field "${field}". A value above it may be an object that does not have it as a data field, as a class with a getter, a date or a list. Use a predicate for it.`,
|
|
205
|
+
},
|
|
206
|
+
]
|
|
207
|
+
: []
|
|
208
|
+
}
|
|
209
|
+
|
|
179
210
|
/**
|
|
180
211
|
* The issues of a field pattern: a pattern that does not parse, or that
|
|
181
212
|
* matches no field of the resources of the rule, as a pattern with a mistake
|
|
@@ -190,24 +221,29 @@ const patternIssues = (
|
|
|
190
221
|
Result.match(Patterns.parse(pattern), {
|
|
191
222
|
onFailure: (issue) => [{ path: at, issue }],
|
|
192
223
|
onSuccess: (parsed) => {
|
|
193
|
-
// A rule with no known resource has an issue for that already.
|
|
224
|
+
// A rule with no known resource has an issue for that already. The
|
|
225
|
+
// pattern is checked on every kind, not only up to the first that it
|
|
226
|
+
// fits: a decision on a kind does this check, so the limit of the
|
|
227
|
+
// work below is a limit of the work of a decision too.
|
|
194
228
|
const fits =
|
|
195
229
|
known.length === 0 ||
|
|
196
|
-
Arr.
|
|
230
|
+
Arr.reduce(known, false, (before, descriptor) => {
|
|
197
231
|
const key = `${descriptor.kind}\u0000${pattern}`
|
|
198
|
-
return
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
232
|
+
return (
|
|
233
|
+
Option.getOrElse(MutableHashMap.get(memo.fits, key), () => {
|
|
234
|
+
// Each check of a pattern reads the fields of a catalog. The
|
|
235
|
+
// work of one binding has a limit, so very many patterns stop
|
|
236
|
+
// with one issue, and the policy does not load.
|
|
237
|
+
memo.work.steps +=
|
|
238
|
+
descriptor.catalog.fields.length * Math.max(parsed.length, 1)
|
|
239
|
+
if (memo.work.steps > Limits.searchSteps) {
|
|
240
|
+
return true
|
|
241
|
+
}
|
|
242
|
+
const found = Patterns.fitsCatalog(parsed, descriptor.catalog)
|
|
243
|
+
MutableHashMap.set(memo.fits, key, found)
|
|
244
|
+
return found
|
|
245
|
+
}) || before
|
|
246
|
+
)
|
|
211
247
|
})
|
|
212
248
|
if (memo.work.steps > Limits.searchSteps && !memo.work.noted) {
|
|
213
249
|
memo.work.noted = true
|
|
@@ -471,7 +507,7 @@ const conditionIssues = (
|
|
|
471
507
|
const here = (issues: ReadonlyArray<Issue>) => under(context.memo, at, issues)
|
|
472
508
|
switch (node._tag) {
|
|
473
509
|
case "Compare": {
|
|
474
|
-
const exists =
|
|
510
|
+
const exists = readIssues(context, node.field, ["field"])
|
|
475
511
|
return here(
|
|
476
512
|
exists.length > 0
|
|
477
513
|
? exists
|
|
@@ -485,7 +521,7 @@ const conditionIssues = (
|
|
|
485
521
|
)
|
|
486
522
|
}
|
|
487
523
|
case "In": {
|
|
488
|
-
const exists =
|
|
524
|
+
const exists = readIssues(context, node.field, ["field"])
|
|
489
525
|
return here(
|
|
490
526
|
exists.length > 0
|
|
491
527
|
? exists
|
|
@@ -498,7 +534,7 @@ const conditionIssues = (
|
|
|
498
534
|
)
|
|
499
535
|
}
|
|
500
536
|
case "Exists":
|
|
501
|
-
return here(
|
|
537
|
+
return here(readIssues(context, node.field, ["field"]))
|
|
502
538
|
case "Under":
|
|
503
539
|
return here(underIssues(context, node.prefix, []))
|
|
504
540
|
case "And":
|
|
@@ -630,15 +666,18 @@ export const treeIssues = (
|
|
|
630
666
|
// The rules come out of the tree first, and each one is checked after.
|
|
631
667
|
// A check of a condition goes down its levels, so a check inside the walk
|
|
632
668
|
// of the tree would go down the levels of both.
|
|
669
|
+
// The place of each rule shares the path of its parent, and the full path
|
|
670
|
+
// is made only when its rule is checked. A deep tree with very many rules
|
|
671
|
+
// would need very much memory for a full copy of the path of each rule.
|
|
633
672
|
const rulesOf = (
|
|
634
673
|
node: TreeNode,
|
|
635
|
-
|
|
674
|
+
place: Place.Place | undefined,
|
|
636
675
|
level: number,
|
|
637
|
-
): ReadonlyArray<Issue | readonly [RuleData,
|
|
676
|
+
): ReadonlyArray<Issue | readonly [RuleData, Place.Place]> => {
|
|
638
677
|
if (level > Limits.policyDepth) {
|
|
639
678
|
return [
|
|
640
679
|
{
|
|
641
|
-
path,
|
|
680
|
+
path: Place.pathOf(place),
|
|
642
681
|
issue: `A policy tree has more than ${String(Limits.policyDepth)} levels. The most is ${String(Limits.policyDepth)}.`,
|
|
643
682
|
},
|
|
644
683
|
]
|
|
@@ -647,25 +686,25 @@ export const treeIssues = (
|
|
|
647
686
|
case "Rules":
|
|
648
687
|
return Arr.map(
|
|
649
688
|
node.rules,
|
|
650
|
-
(rule, index) => [rule,
|
|
689
|
+
(rule, index) => [rule, Place.at(place, "rules", index)] as const,
|
|
651
690
|
)
|
|
652
691
|
case "All":
|
|
653
692
|
case "Any":
|
|
654
693
|
case "FirstOf":
|
|
655
694
|
return Arr.flatMap(node.policies, (child, index) =>
|
|
656
|
-
rulesOf(child,
|
|
695
|
+
rulesOf(child, Place.at(place, "policies", index), level + 1),
|
|
657
696
|
)
|
|
658
697
|
case "Ceiling":
|
|
659
698
|
return [
|
|
660
|
-
...rulesOf(node.cap,
|
|
661
|
-
...rulesOf(node.policy,
|
|
699
|
+
...rulesOf(node.cap, Place.at(place, "cap"), level + 1),
|
|
700
|
+
...rulesOf(node.policy, Place.at(place, "policy"), level + 1),
|
|
662
701
|
]
|
|
663
702
|
}
|
|
664
703
|
}
|
|
665
704
|
const memo = makeMemo()
|
|
666
|
-
return Arr.flatMap(rulesOf(root,
|
|
705
|
+
return Arr.flatMap(rulesOf(root, undefined, 1), (found) =>
|
|
667
706
|
Array.isArray(found)
|
|
668
|
-
? ruleIssues(domain, found[0], found[1], memo)
|
|
707
|
+
? ruleIssues(domain, found[0], Place.pathOf(found[1]), memo)
|
|
669
708
|
: [found as Issue],
|
|
670
709
|
)
|
|
671
710
|
}
|
package/src/internal/catalog.ts
CHANGED
|
@@ -129,6 +129,15 @@ export interface Field {
|
|
|
129
129
|
readonly listed: boolean
|
|
130
130
|
/** The field may hold parts that the Schema does not name, as a record may. */
|
|
131
131
|
readonly open: boolean
|
|
132
|
+
/**
|
|
133
|
+
* A read of the field finds only plain data. It is `false` when a value
|
|
134
|
+
* above the field may be an object that does not have it as a data field:
|
|
135
|
+
* a class without the field, which may have a getter of that name, a
|
|
136
|
+
* value such as a date or a list, or any object, as of `Schema.Unknown`.
|
|
137
|
+
* A decode or a `make` copies a struct or a record into a plain object,
|
|
138
|
+
* so a struct without the field gives `null` for it.
|
|
139
|
+
*/
|
|
140
|
+
readonly plain: boolean
|
|
132
141
|
}
|
|
133
142
|
|
|
134
143
|
/** A place in the tree of a catalog, with the places below it. */
|
|
@@ -185,8 +194,8 @@ const classStruct = (
|
|
|
185
194
|
}
|
|
186
195
|
|
|
187
196
|
/**
|
|
188
|
-
* Gives the Schemas that a value at one place may fit: it opens unions
|
|
189
|
-
* `Schema.suspend
|
|
197
|
+
* Gives the Schemas that a value at one place may fit: it opens unions and
|
|
198
|
+
* `Schema.suspend`. A class stays a class: see {@link structOf}. The open suspend nodes are in a native `Set`,
|
|
190
199
|
* because the walk must find a node by the node itself: `Equal` compares
|
|
191
200
|
* Schema nodes by structure, and a recursive Schema has no end. A suspend
|
|
192
201
|
* node that gives itself again, with no step on the path, gives nothing.
|
|
@@ -202,15 +211,19 @@ const alternatives = (
|
|
|
202
211
|
return open.has(ast)
|
|
203
212
|
? []
|
|
204
213
|
: alternatives(ast.thunk(), new Set([...open, ast]))
|
|
205
|
-
case "Declaration": {
|
|
206
|
-
const inner = classStruct(ast)
|
|
207
|
-
return inner === undefined ? [ast] : alternatives(inner, open)
|
|
208
|
-
}
|
|
209
214
|
default:
|
|
210
215
|
return [ast]
|
|
211
216
|
}
|
|
212
217
|
}
|
|
213
218
|
|
|
219
|
+
/** The struct of a Schema: a struct, or the struct of a class. */
|
|
220
|
+
const structOf = (ast: SchemaAST.AST): SchemaAST.Objects | undefined =>
|
|
221
|
+
ast._tag === "Objects"
|
|
222
|
+
? ast
|
|
223
|
+
: ast._tag === "Declaration"
|
|
224
|
+
? classStruct(ast)
|
|
225
|
+
: undefined
|
|
226
|
+
|
|
214
227
|
const hasCheck = (
|
|
215
228
|
checks: ReadonlyArray<SchemaAST.Check<never>>,
|
|
216
229
|
id: string,
|
|
@@ -223,8 +236,10 @@ const hasCheck = (
|
|
|
223
236
|
)
|
|
224
237
|
|
|
225
238
|
/** A struct with named fields, or with the keys of a record. */
|
|
226
|
-
const isStruct = (
|
|
227
|
-
ast.
|
|
239
|
+
const isStruct = (
|
|
240
|
+
ast: SchemaAST.Objects | undefined,
|
|
241
|
+
): ast is SchemaAST.Objects =>
|
|
242
|
+
ast !== undefined &&
|
|
228
243
|
(ast.propertySignatures.length > 0 || ast.indexSignatures.length > 0)
|
|
229
244
|
|
|
230
245
|
/** The kinds of the values of a Schema that is not a struct. */
|
|
@@ -326,6 +341,8 @@ interface Place {
|
|
|
326
341
|
readonly missing: boolean
|
|
327
342
|
/** The value may be anything, as an extra field of a struct may be. */
|
|
328
343
|
readonly top: boolean
|
|
344
|
+
/** A read here finds only plain data: see {@link Field.plain}. */
|
|
345
|
+
readonly plain: boolean
|
|
329
346
|
}
|
|
330
347
|
|
|
331
348
|
const textOf = (path: ReadonlyArray<Segment>): string =>
|
|
@@ -333,16 +350,27 @@ const textOf = (path: ReadonlyArray<Segment>): string =>
|
|
|
333
350
|
typeof segment === "string" ? segment : "*",
|
|
334
351
|
).join(".")
|
|
335
352
|
|
|
336
|
-
/**
|
|
337
|
-
|
|
353
|
+
/**
|
|
354
|
+
* The name of a property as a part of a path. A key of an object is a string,
|
|
355
|
+
* so a number key, as from `Schema.Record(Schema.Literal(1), ...)`, has the
|
|
356
|
+
* name `"1"`. A symbol key is not a field.
|
|
357
|
+
*/
|
|
358
|
+
const nameOf = (signature: SchemaAST.PropertySignature): string | undefined =>
|
|
359
|
+
typeof signature.name === "symbol" ? undefined : String(signature.name)
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The properties of a struct by name. A walk makes it one time for each
|
|
363
|
+
* struct of a place, so a struct with very many fields does not take a
|
|
364
|
+
* search for each name.
|
|
365
|
+
*/
|
|
366
|
+
const propertiesOf = (
|
|
338
367
|
struct: SchemaAST.Objects,
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
),
|
|
368
|
+
): HashMap.HashMap<string, SchemaAST.PropertySignature> =>
|
|
369
|
+
HashMap.fromIterable(
|
|
370
|
+
Arr.flatMap(struct.propertySignatures, (signature) => {
|
|
371
|
+
const name = nameOf(signature)
|
|
372
|
+
return name === undefined ? [] : [[name, signature] as const]
|
|
373
|
+
}),
|
|
346
374
|
)
|
|
347
375
|
|
|
348
376
|
/** The names of the fields of the structs, in the order that they first come. */
|
|
@@ -352,8 +380,8 @@ const namesOf = (
|
|
|
352
380
|
Arr.dedupe(
|
|
353
381
|
Arr.flatMap(structs, (struct) =>
|
|
354
382
|
Arr.flatMap(struct.propertySignatures, (signature) => {
|
|
355
|
-
const name = signature
|
|
356
|
-
if (
|
|
383
|
+
const name = nameOf(signature)
|
|
384
|
+
if (name === undefined) {
|
|
357
385
|
return []
|
|
358
386
|
}
|
|
359
387
|
// A dot in a path is the step into a nested field. A field name with
|
|
@@ -377,10 +405,22 @@ const namesOf = (
|
|
|
377
405
|
* very many fields before the walk is done.
|
|
378
406
|
*/
|
|
379
407
|
const branchOf = (place: Place, count: { size: number }): Branch => {
|
|
380
|
-
const structs = Arr.filter(place.schemas, isStruct)
|
|
381
|
-
|
|
408
|
+
const structs = Arr.filter(Arr.map(place.schemas, structOf), isStruct)
|
|
409
|
+
// The structs of classes. A value of a class is not copied into a plain
|
|
410
|
+
// object, so it keeps its getters and methods.
|
|
411
|
+
const classes = Arr.filter(
|
|
412
|
+
Arr.map(
|
|
413
|
+
Arr.filter(place.schemas, (schema) => schema._tag === "Declaration"),
|
|
414
|
+
structOf,
|
|
415
|
+
),
|
|
416
|
+
isStruct,
|
|
417
|
+
)
|
|
418
|
+
const values = Arr.filter(
|
|
419
|
+
place.schemas,
|
|
420
|
+
(schema) => !isStruct(structOf(schema)),
|
|
421
|
+
)
|
|
382
422
|
const valueKinds = Arr.reduce(values, noKinds, (kinds, schema) =>
|
|
383
|
-
joinKinds(kinds, kindsOf(schema)),
|
|
423
|
+
joinKinds(kinds, kindsOf(structOf(schema) ?? schema)),
|
|
384
424
|
)
|
|
385
425
|
const kinds = place.top
|
|
386
426
|
? anyKinds
|
|
@@ -412,6 +452,7 @@ const branchOf = (place: Place, count: { size: number }): Branch => {
|
|
|
412
452
|
structs.length > 0 ||
|
|
413
453
|
valueKinds.object ||
|
|
414
454
|
place.top,
|
|
455
|
+
plain: place.plain,
|
|
415
456
|
}
|
|
416
457
|
if (field !== undefined) {
|
|
417
458
|
count.size += 1
|
|
@@ -434,11 +475,12 @@ const branchOf = (place: Place, count: { size: number }): Branch => {
|
|
|
434
475
|
keyBranch: undefined,
|
|
435
476
|
}
|
|
436
477
|
}
|
|
478
|
+
const structProperties = Arr.map(structs, propertiesOf)
|
|
479
|
+
const classProperties = Arr.map(classes, propertiesOf)
|
|
437
480
|
const children = Arr.map(namesOf(structs), (name) => {
|
|
438
|
-
const properties = Arr.flatMap(
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
})
|
|
481
|
+
const properties = Arr.flatMap(structProperties, (named) =>
|
|
482
|
+
Option.toArray(HashMap.get(named, name)),
|
|
483
|
+
)
|
|
442
484
|
const below: Place = {
|
|
443
485
|
path: [...place.path, name],
|
|
444
486
|
schemas: Arr.flatMap(properties, (property) =>
|
|
@@ -454,10 +496,11 @@ const branchOf = (place: Place, count: { size: number }): Branch => {
|
|
|
454
496
|
// with any value: only a decode takes extra fields away. A value
|
|
455
497
|
// that may be any object, as one of `Schema.Unknown` may, may have
|
|
456
498
|
// the name with any value too.
|
|
457
|
-
top:
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
499
|
+
top: place.top || valueKinds.object || properties.length < structs.length,
|
|
500
|
+
plain:
|
|
501
|
+
place.plain &&
|
|
502
|
+
!valueKinds.object &&
|
|
503
|
+
Arr.every(classProperties, (named) => HashMap.has(named, name)),
|
|
461
504
|
}
|
|
462
505
|
return [name, branchOf(below, count)] as const
|
|
463
506
|
})
|
|
@@ -479,6 +522,10 @@ const branchOf = (place: Place, count: { size: number }): Branch => {
|
|
|
479
522
|
// A record may not have a given key.
|
|
480
523
|
missing: true,
|
|
481
524
|
top: place.top || valueKinds.object || records.length < structs.length,
|
|
525
|
+
plain:
|
|
526
|
+
place.plain &&
|
|
527
|
+
!valueKinds.object &&
|
|
528
|
+
Arr.every(classes, (struct) => struct.indexSignatures.length > 0),
|
|
482
529
|
},
|
|
483
530
|
count,
|
|
484
531
|
)
|
|
@@ -502,6 +549,7 @@ export const catalogOf = (ast: SchemaAST.AST): Catalog => {
|
|
|
502
549
|
schemas: alternatives(ast, new Set()),
|
|
503
550
|
missing: false,
|
|
504
551
|
top: false,
|
|
552
|
+
plain: true,
|
|
505
553
|
},
|
|
506
554
|
{ size: 0 },
|
|
507
555
|
)
|
package/src/internal/decide.ts
CHANGED
|
@@ -37,6 +37,8 @@ interface DecideContext {
|
|
|
37
37
|
readonly input: Condition.Input
|
|
38
38
|
/** The resource of the kind, for the fields that a rule may grant. */
|
|
39
39
|
readonly descriptor: Resource.Descriptor
|
|
40
|
+
/** The test of `RuleData.fitsOf` for the catalog, for this decision. */
|
|
41
|
+
readonly fits: (pattern: string) => boolean
|
|
40
42
|
/** The rules that were tested, for {@link explain}. */
|
|
41
43
|
readonly trace: Ref.Ref<Chunk.Chunk<Step>> | undefined
|
|
42
44
|
}
|
|
@@ -282,6 +284,7 @@ const makeContext = (
|
|
|
282
284
|
path: descriptor.path,
|
|
283
285
|
},
|
|
284
286
|
descriptor,
|
|
287
|
+
fits: RuleData.fitsOf(descriptor.catalog),
|
|
285
288
|
trace,
|
|
286
289
|
}
|
|
287
290
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The place of a part of an input, as a path that shares the path of its
|
|
3
|
+
* parent. A deep tree with very many rules would need very much memory for a
|
|
4
|
+
* full copy of the path of each rule, so a place keeps only its own parts and
|
|
5
|
+
* its parent. The full path is made only for a problem, in one walk up.
|
|
6
|
+
*/
|
|
7
|
+
import * as Arr from "effect/Array"
|
|
8
|
+
import * as Option from "effect/Option"
|
|
9
|
+
|
|
10
|
+
/** A place: its own parts of the path, after the path of its parent. */
|
|
11
|
+
export interface Place {
|
|
12
|
+
readonly parent: Place | undefined
|
|
13
|
+
readonly parts: ReadonlyArray<PropertyKey>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The place below `parent` with more parts. */
|
|
17
|
+
export const at = (
|
|
18
|
+
parent: Place | undefined,
|
|
19
|
+
...parts: ReadonlyArray<PropertyKey>
|
|
20
|
+
): Place => ({ parent, parts })
|
|
21
|
+
|
|
22
|
+
/** The full path of a place. The root, with no place, has the empty path. */
|
|
23
|
+
export const pathOf = (place: Place | undefined): ReadonlyArray<PropertyKey> =>
|
|
24
|
+
Arr.flatten(
|
|
25
|
+
Arr.reverse(
|
|
26
|
+
Arr.unfold(place, (current) =>
|
|
27
|
+
current === undefined
|
|
28
|
+
? Option.none()
|
|
29
|
+
: Option.some([current.parts, current.parent] as const),
|
|
30
|
+
),
|
|
31
|
+
),
|
|
32
|
+
)
|