@effect-dynamodb/schema 1.39.0 → 1.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Aggregate.d.ts +11 -4
- package/dist/Aggregate.d.ts.map +1 -1
- package/dist/Aggregate.js +12 -2
- package/dist/Aggregate.js.map +1 -1
- package/dist/DynamoModel.d.ts +48 -7
- package/dist/DynamoModel.d.ts.map +1 -1
- package/dist/DynamoModel.js +34 -16
- package/dist/DynamoModel.js.map +1 -1
- package/dist/DynamoSchema.d.ts +25 -0
- package/dist/DynamoSchema.d.ts.map +1 -1
- package/dist/DynamoSchema.js.map +1 -1
- package/dist/Entity.d.ts +66 -11
- package/dist/Entity.d.ts.map +1 -1
- package/dist/Entity.js +261 -34
- package/dist/Entity.js.map +1 -1
- package/dist/Errors.d.ts +61 -4
- package/dist/Errors.d.ts.map +1 -1
- package/dist/Errors.js +83 -6
- package/dist/Errors.js.map +1 -1
- package/dist/KeyComposer.d.ts +196 -5
- package/dist/KeyComposer.d.ts.map +1 -1
- package/dist/KeyComposer.js +490 -54
- package/dist/KeyComposer.js.map +1 -1
- package/dist/VectorIndex.d.ts.map +1 -1
- package/dist/VectorIndex.js +5 -4
- package/dist/VectorIndex.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/AggregateEdges.d.ts +3 -3
- package/dist/internal/AggregateSchemas.d.ts +306 -14
- package/dist/internal/AggregateSchemas.d.ts.map +1 -1
- package/dist/internal/AggregateSchemas.js +733 -54
- package/dist/internal/AggregateSchemas.js.map +1 -1
- package/dist/internal/AggregateTypes.d.ts +396 -36
- package/dist/internal/AggregateTypes.d.ts.map +1 -1
- package/dist/internal/CompositeCodec.d.ts +298 -8
- package/dist/internal/CompositeCodec.d.ts.map +1 -1
- package/dist/internal/CompositeCodec.js +940 -34
- package/dist/internal/CompositeCodec.js.map +1 -1
- package/dist/internal/EffectInstances.d.ts +87 -0
- package/dist/internal/EffectInstances.d.ts.map +1 -0
- package/dist/internal/EffectInstances.js +662 -0
- package/dist/internal/EffectInstances.js.map +1 -0
- package/dist/internal/EntityPacked.d.ts.map +1 -1
- package/dist/internal/EntityPacked.js +5 -4
- package/dist/internal/EntityPacked.js.map +1 -1
- package/dist/internal/EntitySchemas.d.ts +239 -30
- package/dist/internal/EntitySchemas.d.ts.map +1 -1
- package/dist/internal/EntitySchemas.js +1600 -197
- package/dist/internal/EntitySchemas.js.map +1 -1
- package/dist/internal/EntityTypes.d.ts +114 -25
- package/dist/internal/EntityTypes.d.ts.map +1 -1
- package/dist/internal/FailingAsBefore.d.ts +25 -0
- package/dist/internal/FailingAsBefore.d.ts.map +1 -0
- package/dist/internal/FailingAsBefore.js +42 -0
- package/dist/internal/FailingAsBefore.js.map +1 -0
- package/dist/internal/InstanceReads.d.ts +40 -0
- package/dist/internal/InstanceReads.d.ts.map +1 -0
- package/dist/internal/InstanceReads.js +183 -0
- package/dist/internal/InstanceReads.js.map +1 -0
- package/dist/internal/KeyReadings.d.ts +30 -0
- package/dist/internal/KeyReadings.d.ts.map +1 -0
- package/dist/internal/KeyReadings.js +99 -0
- package/dist/internal/KeyReadings.js.map +1 -0
- package/dist/internal/KeyText136.d.ts +4 -2
- package/dist/internal/KeyText136.d.ts.map +1 -1
- package/dist/internal/KeyText136.js +46 -13
- package/dist/internal/KeyText136.js.map +1 -1
- package/dist/internal/LostValues.d.ts +50 -0
- package/dist/internal/LostValues.d.ts.map +1 -0
- package/dist/internal/LostValues.js +465 -0
- package/dist/internal/LostValues.js.map +1 -0
- package/dist/internal/NumericKey.d.ts +21 -2
- package/dist/internal/NumericKey.d.ts.map +1 -1
- package/dist/internal/NumericKey.js +19 -1
- package/dist/internal/NumericKey.js.map +1 -1
- package/dist/internal/ObjectKeyText.d.ts +29 -0
- package/dist/internal/ObjectKeyText.d.ts.map +1 -0
- package/dist/internal/ObjectKeyText.js +50 -0
- package/dist/internal/ObjectKeyText.js.map +1 -0
- package/dist/internal/OwnNames.d.ts +205 -0
- package/dist/internal/OwnNames.d.ts.map +1 -0
- package/dist/internal/OwnNames.js +415 -0
- package/dist/internal/OwnNames.js.map +1 -0
- package/dist/internal/PackedCodec.d.ts +7 -1
- package/dist/internal/PackedCodec.d.ts.map +1 -1
- package/dist/internal/PackedCodec.js +83 -21
- package/dist/internal/PackedCodec.js.map +1 -1
- package/dist/internal/RestoredKeyText.d.ts +10 -0
- package/dist/internal/RestoredKeyText.d.ts.map +1 -0
- package/dist/internal/RestoredKeyText.js +50 -0
- package/dist/internal/RestoredKeyText.js.map +1 -0
- package/dist/internal/SchemaAccessors.d.ts +280 -1
- package/dist/internal/SchemaAccessors.d.ts.map +1 -1
- package/dist/internal/SchemaAccessors.js +1717 -1
- package/dist/internal/SchemaAccessors.js.map +1 -1
- package/dist/internal/SchemaIssue.d.ts +21 -0
- package/dist/internal/SchemaIssue.d.ts.map +1 -0
- package/dist/internal/SchemaIssue.js +34 -0
- package/dist/internal/SchemaIssue.js.map +1 -0
- package/dist/internal/SharedKeyAttribute.d.ts +60 -6
- package/dist/internal/SharedKeyAttribute.d.ts.map +1 -1
- package/dist/internal/SharedKeyAttribute.js +129 -13
- package/dist/internal/SharedKeyAttribute.js.map +1 -1
- package/dist/internal/SparseKey.d.ts +46 -0
- package/dist/internal/SparseKey.d.ts.map +1 -0
- package/dist/internal/SparseKey.js +69 -0
- package/dist/internal/SparseKey.js.map +1 -0
- package/dist/internal/StoredForm.d.ts +139 -0
- package/dist/internal/StoredForm.d.ts.map +1 -0
- package/dist/internal/StoredForm.js +503 -0
- package/dist/internal/StoredForm.js.map +1 -0
- package/dist/internal/ZOrderCurve.d.ts.map +1 -1
- package/dist/internal/ZOrderCurve.js +2 -1
- package/dist/internal/ZOrderCurve.js.map +1 -1
- package/dist/internal/ZoneKeyText.d.ts +55 -0
- package/dist/internal/ZoneKeyText.d.ts.map +1 -0
- package/dist/internal/ZoneKeyText.js +264 -0
- package/dist/internal/ZoneKeyText.js.map +1 -0
- package/package.json +4 -4
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
*
|
|
13
13
|
* Not part of the public API.
|
|
14
14
|
*/
|
|
15
|
-
import { SchemaAST } from "effect";
|
|
15
|
+
import { Exit, Schema, SchemaAST } from "effect";
|
|
16
|
+
import { effectDeclarationOf } from "./EffectInstances.js";
|
|
16
17
|
/**
|
|
17
18
|
* Read `.fields` from a `Schema.Struct` or `Schema.Class`.
|
|
18
19
|
*
|
|
@@ -81,6 +82,28 @@ export const extractArrayElement = (fieldSchema) => {
|
|
|
81
82
|
}
|
|
82
83
|
return undefined;
|
|
83
84
|
};
|
|
85
|
+
/**
|
|
86
|
+
* The value schema of a keyed collection a custom `decompose` takes apart: `Schema.Record(K, T)`,
|
|
87
|
+
* under `optional` / `optionalKey` too. Returns `T`.
|
|
88
|
+
*/
|
|
89
|
+
export const extractKeyedValue = (fieldSchema) => {
|
|
90
|
+
const keyed = (schema) => getSchemaProp(schema, "key") === undefined ? undefined : getSchemaProp(schema, "value");
|
|
91
|
+
if (SchemaAST.isOptional(fieldSchema.ast)) {
|
|
92
|
+
const inner = getSchemaProp(fieldSchema, "schema");
|
|
93
|
+
if (inner === undefined)
|
|
94
|
+
return undefined;
|
|
95
|
+
const members = getSchemaMembers(inner);
|
|
96
|
+
if (members === undefined)
|
|
97
|
+
return keyed(inner);
|
|
98
|
+
for (const member of members) {
|
|
99
|
+
const value = keyed(member);
|
|
100
|
+
if (value !== undefined)
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
return keyed(fieldSchema);
|
|
106
|
+
};
|
|
84
107
|
/** Read the typed `.value` element schema off an `Array`/`NonEmptyArray` schema. */
|
|
85
108
|
const arrayValue = (schema) => getSchemaProp(schema, "value");
|
|
86
109
|
/** Read the typed `.members` array off a `Union` schema, if present. */
|
|
@@ -209,4 +232,1697 @@ export const numericTypeWithStringEncoding = (schema) => {
|
|
|
209
232
|
const encodedAst = SchemaAST.toEncoded(schema.ast);
|
|
210
233
|
return ((SchemaAST.isNumber(typeAst) || SchemaAST.isBigInt(typeAst)) && SchemaAST.isString(encodedAst));
|
|
211
234
|
};
|
|
235
|
+
/** The `representation` id Effect gives `Schema.ReadonlySet`, the one type stored as a set. */
|
|
236
|
+
const READONLY_SET = "effect/schema/ReadonlySet";
|
|
237
|
+
/** The `representation` id Effect gives a declaration of its own. A custom one has none. */
|
|
238
|
+
const representationIdOf = (ast) => {
|
|
239
|
+
const representation = ast.annotations?.representation;
|
|
240
|
+
const id = representation?.id;
|
|
241
|
+
return typeof id === "string" ? id : undefined;
|
|
242
|
+
};
|
|
243
|
+
/** Whether an AST is the declaration Effect represents under this id. */
|
|
244
|
+
const isDeclarationOf = (ast, id) => ast._tag === "Declaration" && representationIdOf(ast) === id;
|
|
245
|
+
/** Whether an AST is a `Schema.ReadonlySet`, by the id Effect gives its representation. */
|
|
246
|
+
export const isSetDeclaration = (ast) => isDeclarationOf(ast, READONLY_SET);
|
|
247
|
+
const NO_LONGER_DECODES = "it's stored as an empty map, which no longer decodes";
|
|
248
|
+
/**
|
|
249
|
+
* Effect's declarations that no owner can read back, by the `representation` id Effect gives them,
|
|
250
|
+
* each with a replacement measured on entities, aggregates, streams and outboxes.
|
|
251
|
+
*/
|
|
252
|
+
const NEVER_READ_BACK = {
|
|
253
|
+
"effect/schema/ReadonlyMap": {
|
|
254
|
+
name: "Schema.ReadonlyMap",
|
|
255
|
+
why: "it's stored as a map, a map reads back as a plain object, and the row no longer decodes",
|
|
256
|
+
use: "Schema.Record, which is stored the same way and reads back",
|
|
257
|
+
},
|
|
258
|
+
"effect/schema/HashMap": {
|
|
259
|
+
name: "Schema.HashMap",
|
|
260
|
+
why: "a non-empty one can't be written, and an empty one is stored as its internals, which no longer decode",
|
|
261
|
+
use: "Schema.Record",
|
|
262
|
+
},
|
|
263
|
+
"effect/schema/Chunk": {
|
|
264
|
+
name: "Schema.Chunk",
|
|
265
|
+
why: "writing one overflows the stack, so nothing is ever stored",
|
|
266
|
+
use: "Schema.Array",
|
|
267
|
+
},
|
|
268
|
+
"effect/schema/URL": {
|
|
269
|
+
name: "Schema.URL",
|
|
270
|
+
why: "it's stored as a map of its parts, which no longer decodes",
|
|
271
|
+
use: "Schema.URLFromString",
|
|
272
|
+
},
|
|
273
|
+
"effect/schema/URLSearchParams": {
|
|
274
|
+
name: "Schema.URLSearchParams",
|
|
275
|
+
why: "it's stored as a map of its size alone, which no longer decodes",
|
|
276
|
+
use: "Schema.String, holding the parameters as text",
|
|
277
|
+
},
|
|
278
|
+
"effect/schema/RegExp": {
|
|
279
|
+
name: "Schema.RegExp",
|
|
280
|
+
why: NO_LONGER_DECODES,
|
|
281
|
+
use: "Schema.String, holding the pattern as text",
|
|
282
|
+
},
|
|
283
|
+
"effect/schema/FormData": {
|
|
284
|
+
name: "Schema.FormData",
|
|
285
|
+
why: NO_LONGER_DECODES,
|
|
286
|
+
use: "Schema.Record",
|
|
287
|
+
},
|
|
288
|
+
"effect/http/Headers": {
|
|
289
|
+
name: "Schema.Headers",
|
|
290
|
+
why: "it's stored as a map, a map reads back as a plain object, and the row no longer decodes",
|
|
291
|
+
use: "Schema.Record",
|
|
292
|
+
},
|
|
293
|
+
"effect/schema/File": {
|
|
294
|
+
name: "Schema.File",
|
|
295
|
+
why: "it's stored as bytes without its name or type, which no longer decode",
|
|
296
|
+
use: "Schema.Uint8Array",
|
|
297
|
+
},
|
|
298
|
+
"effect/schema/ByteSize": {
|
|
299
|
+
name: "Schema.ByteSize",
|
|
300
|
+
why: "it's stored as a number, which no longer decodes",
|
|
301
|
+
use: "Schema.ByteSizeFromString or Schema.ByteSizeFromNumber",
|
|
302
|
+
},
|
|
303
|
+
"effect/schema/Error": {
|
|
304
|
+
name: "Schema.ErrorInstance",
|
|
305
|
+
why: NO_LONGER_DECODES,
|
|
306
|
+
use: "Schema.Defect()",
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
const DATE = "effect/schema/Date";
|
|
310
|
+
const REDACTED = "effect/schema/Redacted";
|
|
311
|
+
/** What an aggregate's date encoder stores as a string. */
|
|
312
|
+
const DATE_DOMAINS = new Set([
|
|
313
|
+
DATE,
|
|
314
|
+
"effect/schema/DateTimeUtc",
|
|
315
|
+
"effect/schema/DateTimeZoned",
|
|
316
|
+
]);
|
|
317
|
+
const PLAIN_DATE = {
|
|
318
|
+
name: "Schema.Date",
|
|
319
|
+
why: `${NO_LONGER_DECODES}. An entity or an aggregate stores one as a string only where it reaches it, which isn't under another declaration, a Schema.suspend, a transformation or a decoding default on a container`,
|
|
320
|
+
use: "Schema.DateFromString or Schema.DateFromMillis",
|
|
321
|
+
};
|
|
322
|
+
const TRANSFORMED_DATE = {
|
|
323
|
+
name: "a transformation over Schema.Date, such as Schema.DateTimeUtcFromDate",
|
|
324
|
+
why: "its stored form no longer decodes",
|
|
325
|
+
use: "Schema.DateTimeUtcFromString or Schema.DateTimeUtcFromMillis",
|
|
326
|
+
};
|
|
327
|
+
const PLAIN_REDACTED = {
|
|
328
|
+
name: "Schema.Redacted",
|
|
329
|
+
why: "its value isn't stored, and reading it fails. An entity or an aggregate stores the value only where it reaches it, which isn't under another declaration, a Schema.suspend, a transformation or a decoding default on a container",
|
|
330
|
+
use: "Schema.RedactedFromValue",
|
|
331
|
+
};
|
|
332
|
+
const BIGINT_BESIDE_A_NUMBER = {
|
|
333
|
+
name: "a union of Schema.BigInt and something else stored as a number",
|
|
334
|
+
why: "5n and 5 are stored the same, so a read can't tell which one was written",
|
|
335
|
+
use: "Schema.BigIntFromString",
|
|
336
|
+
};
|
|
337
|
+
const BIGINT_LITERAL = {
|
|
338
|
+
name: "a bigint Schema.Literal",
|
|
339
|
+
why: "it's stored as a number, which reads back as a number and no longer decodes",
|
|
340
|
+
use: "a string or a number literal",
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* Whether a field reached this node through a transformation. A decoding default isn't one, and the
|
|
344
|
+
* owner's substitution sees through it. A transformation is one even when it ends in the type it
|
|
345
|
+
* started from, since the owner stores and reads what's beneath it.
|
|
346
|
+
*/
|
|
347
|
+
const isTransformed = (walk) => walk.declared !== undefined && walk.beneath !== "default";
|
|
348
|
+
/** Whether this node, on its own, is something this owner can't read back. */
|
|
349
|
+
const unreadableAt = (ast, walk) => {
|
|
350
|
+
const stored = walk.by !== "schema" && !walk.unreached && !walk.writtenAsIs;
|
|
351
|
+
switch (ast._tag) {
|
|
352
|
+
case "Literal":
|
|
353
|
+
return typeof ast.literal === "bigint" ? BIGINT_LITERAL : undefined;
|
|
354
|
+
case "Declaration": {
|
|
355
|
+
const id = representationIdOf(ast) ?? "";
|
|
356
|
+
if (id === DATE) {
|
|
357
|
+
if (isTransformed(walk))
|
|
358
|
+
return TRANSFORMED_DATE;
|
|
359
|
+
return stored ? undefined : PLAIN_DATE;
|
|
360
|
+
}
|
|
361
|
+
if (id === REDACTED) {
|
|
362
|
+
// A step ending in a redacted value is substituted whole. A step onto anything else, such as
|
|
363
|
+
// `Schema.OptionFromOptional`, writes what's beneath it as declared.
|
|
364
|
+
const overAnother = isTransformed(walk) && representationIdOf(walk.declared) !== REDACTED;
|
|
365
|
+
return stored && !overAnother ? undefined : PLAIN_REDACTED;
|
|
366
|
+
}
|
|
367
|
+
return NEVER_READ_BACK[id];
|
|
368
|
+
}
|
|
369
|
+
default:
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
/** What a suspended node stands for, and the suspensions on the way to it. `undefined` is a cycle. */
|
|
374
|
+
const forced = (ast, seen) => {
|
|
375
|
+
if (!SchemaAST.isSuspend(ast))
|
|
376
|
+
return { node: ast, seen };
|
|
377
|
+
return seen.has(ast) ? undefined : forced(ast.thunk(), new Set(seen).add(ast));
|
|
378
|
+
};
|
|
379
|
+
const JSON_VALUES = new Set([
|
|
380
|
+
"effect/schema/Json",
|
|
381
|
+
"effect/schema/MutableJson",
|
|
382
|
+
]);
|
|
383
|
+
/** A number may be what it takes: a custom declaration doesn't say, and the rest take any value. */
|
|
384
|
+
const takesAnything = (ast) => SchemaAST.isUnknown(ast) ||
|
|
385
|
+
SchemaAST.isAny(ast) ||
|
|
386
|
+
(SchemaAST.isDeclaration(ast) &&
|
|
387
|
+
(representationIdOf(ast) === undefined || JSON_VALUES.has(representationIdOf(ast))));
|
|
388
|
+
const isNumeric = (ast) => SchemaAST.isNumber(ast) ||
|
|
389
|
+
SchemaAST.isEnum(ast) ||
|
|
390
|
+
(SchemaAST.isLiteral(ast) && typeof ast.literal === "number");
|
|
391
|
+
/** The kind of attribute a node takes, with a bigint's being a number's. */
|
|
392
|
+
const storedKind = (ast) => SchemaAST.isLiteral(ast)
|
|
393
|
+
? typeof ast.literal
|
|
394
|
+
: isNumeric(ast) || SchemaAST.isBigInt(ast)
|
|
395
|
+
? "number"
|
|
396
|
+
: SchemaAST.isString(ast) || SchemaAST.isTemplateLiteral(ast)
|
|
397
|
+
? "string"
|
|
398
|
+
: SchemaAST.isBoolean(ast)
|
|
399
|
+
? "boolean"
|
|
400
|
+
: ast._tag;
|
|
401
|
+
/** Whether a struct's map is one a struct takes: it has every field the struct requires. */
|
|
402
|
+
const hasFieldsOf = (writer, taker) => taker.propertySignatures.every((theirs) => SchemaAST.isOptional(theirs.type) ||
|
|
403
|
+
writer.indexSignatures.length > 0 ||
|
|
404
|
+
writer.propertySignatures.some((mine) => mine.name === theirs.name));
|
|
405
|
+
const storedMap = (tag, marker, fields) => Schema.Struct({
|
|
406
|
+
_op: Schema.String,
|
|
407
|
+
_tag: Schema.Literal(tag),
|
|
408
|
+
[marker]: Schema.Struct({}),
|
|
409
|
+
...Object.fromEntries(Object.entries(fields).map(([name, ast]) => [name, Schema.make(ast)])),
|
|
410
|
+
}).ast;
|
|
411
|
+
/**
|
|
412
|
+
* The maps the marshaller makes of a declaration, where one holds a value in it. A struct takes a
|
|
413
|
+
* stored value by its fields, so these say which structs take one of these for their own.
|
|
414
|
+
*/
|
|
415
|
+
const STORED_MAPS = {
|
|
416
|
+
"effect/schema/Option": ([value]) => [
|
|
417
|
+
storedMap("Some", "~effect/Option", { value: value }),
|
|
418
|
+
storedMap("None", "~effect/Option", {}),
|
|
419
|
+
],
|
|
420
|
+
"effect/schema/Result": ([success, failure]) => [
|
|
421
|
+
storedMap("Success", "~effect/Result", { success: success }),
|
|
422
|
+
storedMap("Failure", "~effect/Result", { failure: failure }),
|
|
423
|
+
],
|
|
424
|
+
};
|
|
425
|
+
const storedMapsOf = (ast) => STORED_MAPS[representationIdOf(ast) ?? ""]?.(ast.typeParameters);
|
|
426
|
+
/**
|
|
427
|
+
* Whether a struct declares a marker. Each of Effect's declarations looks for its marker or a class,
|
|
428
|
+
* and none takes any other map or a primitive (measured, each of them), so a struct's map is one of
|
|
429
|
+
* theirs only with a marker in it.
|
|
430
|
+
*/
|
|
431
|
+
const declaresMarker = (ast) => ast.propertySignatures.some((property) => typeof property.name === "string" && property.name.startsWith("~effect/"));
|
|
432
|
+
/**
|
|
433
|
+
* Whether a stored value could be one both a declaration of Effect's and this take. What a struct
|
|
434
|
+
* takes of one depends on the map it's stored as. Two declarations, or a struct beside a declaration
|
|
435
|
+
* whose map isn't known, count.
|
|
436
|
+
*/
|
|
437
|
+
const declarationCouldBe = (declaration, other, seen) => {
|
|
438
|
+
if (SchemaAST.isDeclaration(other))
|
|
439
|
+
return true;
|
|
440
|
+
if (!SchemaAST.isObjects(other))
|
|
441
|
+
return false;
|
|
442
|
+
const maps = storedMapsOf(declaration);
|
|
443
|
+
return (maps === undefined ||
|
|
444
|
+
maps.some((map) => hasFieldsOf(map, other) && couldBeBoth(map, other, seen)));
|
|
445
|
+
};
|
|
446
|
+
/** Whether a stored value could be one both of these take. What can't be told apart counts. */
|
|
447
|
+
const couldBeBoth = (a, b, seen) => {
|
|
448
|
+
const left = forced(a, seen);
|
|
449
|
+
const right = forced(b, left?.seen ?? seen);
|
|
450
|
+
if (left === undefined || right === undefined)
|
|
451
|
+
return true;
|
|
452
|
+
const [l, r, within] = [left.node, right.node, right.seen];
|
|
453
|
+
if (SchemaAST.isUnion(l))
|
|
454
|
+
return l.types.some((type) => couldBeBoth(type, r, within));
|
|
455
|
+
if (SchemaAST.isUnion(r))
|
|
456
|
+
return r.types.some((type) => couldBeBoth(l, type, within));
|
|
457
|
+
if (takesAnything(l) || takesAnything(r))
|
|
458
|
+
return true;
|
|
459
|
+
if (SchemaAST.isDeclaration(l))
|
|
460
|
+
return declarationCouldBe(l, r, within);
|
|
461
|
+
if (SchemaAST.isDeclaration(r))
|
|
462
|
+
return declarationCouldBe(r, l, within);
|
|
463
|
+
if (SchemaAST.isLiteral(l) && SchemaAST.isLiteral(r))
|
|
464
|
+
return l.literal === r.literal;
|
|
465
|
+
if (SchemaAST.isObjects(l) && SchemaAST.isObjects(r)) {
|
|
466
|
+
// A record takes a field its values could be, and a key it doesn't cover says nothing.
|
|
467
|
+
const fitsRecord = (fields, record) => fields.propertySignatures.every((property) => SchemaAST.isOptional(property.type) ||
|
|
468
|
+
record.propertySignatures.some((named) => named.name === property.name) ||
|
|
469
|
+
record.indexSignatures.every((index) => !SchemaAST.isString(index.parameter) ||
|
|
470
|
+
couldBeBoth(property.type, index.type, within)));
|
|
471
|
+
return (fitsRecord(l, r) &&
|
|
472
|
+
fitsRecord(r, l) &&
|
|
473
|
+
l.propertySignatures.every((property) => {
|
|
474
|
+
const other = r.propertySignatures.find((candidate) => candidate.name === property.name);
|
|
475
|
+
return (other === undefined ||
|
|
476
|
+
(SchemaAST.isOptional(property.type) && SchemaAST.isOptional(other.type)) ||
|
|
477
|
+
couldBeBoth(property.type, other.type, within));
|
|
478
|
+
}));
|
|
479
|
+
}
|
|
480
|
+
return storedKind(l) === storedKind(r);
|
|
481
|
+
};
|
|
482
|
+
/** Whether a stored side holds a plain bigint anywhere. */
|
|
483
|
+
export const holdsPlainBigint = (ast, seen = new Set()) => {
|
|
484
|
+
const found = forced(ast, seen);
|
|
485
|
+
if (found === undefined)
|
|
486
|
+
return false;
|
|
487
|
+
const { node } = found;
|
|
488
|
+
const within = (asts) => asts.some((part) => holdsPlainBigint(part, found.seen));
|
|
489
|
+
switch (node._tag) {
|
|
490
|
+
case "BigInt":
|
|
491
|
+
return true;
|
|
492
|
+
case "Declaration":
|
|
493
|
+
return within(node.typeParameters);
|
|
494
|
+
case "Union":
|
|
495
|
+
return within(node.types);
|
|
496
|
+
case "Arrays":
|
|
497
|
+
return within([...node.elements, ...node.rest]);
|
|
498
|
+
case "Objects":
|
|
499
|
+
return within([
|
|
500
|
+
...node.propertySignatures.map((property) => property.type),
|
|
501
|
+
...node.indexSignatures.map((signature) => signature.type),
|
|
502
|
+
]);
|
|
503
|
+
default:
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
/**
|
|
508
|
+
* Whether a number can end up on the wrong side of a plain bigint, between two members of a union.
|
|
509
|
+
* The bigint is lifted where it sits and a union tries its members in order, so whichever of the two
|
|
510
|
+
* comes `first` takes what the other stored, if it has what the first requires: a bigint's number
|
|
511
|
+
* kept as a number, or another member's number lifted to a bigint. Two structs apart on a field, as
|
|
512
|
+
* tagged events are, can't. A value lost to an earlier member that has no place for it isn't this:
|
|
513
|
+
* a union of anything does that. What can't be told apart counts.
|
|
514
|
+
*/
|
|
515
|
+
const confusesItsBigint = (holder, other, first, seen) => {
|
|
516
|
+
const left = forced(holder, seen);
|
|
517
|
+
const right = forced(other, left?.seen ?? seen);
|
|
518
|
+
if (left === undefined || right === undefined)
|
|
519
|
+
return false;
|
|
520
|
+
const [l, r, within] = [left.node, right.node, right.seen];
|
|
521
|
+
const confused = (mine, theirs) => confusesItsBigint(mine, theirs, first, within);
|
|
522
|
+
if (SchemaAST.isUnion(l))
|
|
523
|
+
return l.types.some((type) => confused(type, r));
|
|
524
|
+
if (SchemaAST.isUnion(r))
|
|
525
|
+
return r.types.some((type) => confused(l, type));
|
|
526
|
+
if (SchemaAST.isBigInt(l))
|
|
527
|
+
return isNumeric(r) || takesAnything(r);
|
|
528
|
+
if (takesAnything(r))
|
|
529
|
+
return !(first && SchemaAST.isDeclaration(l)) && holdsPlainBigint(l);
|
|
530
|
+
if (SchemaAST.isArrays(l)) {
|
|
531
|
+
if (!SchemaAST.isArrays(r))
|
|
532
|
+
return false;
|
|
533
|
+
const fixed = Math.min(l.elements.length, r.elements.length);
|
|
534
|
+
const parts = (ast) => [...ast.elements, ...ast.rest];
|
|
535
|
+
return (l.elements
|
|
536
|
+
.slice(0, fixed)
|
|
537
|
+
.every((element, i) => couldBeBoth(element, r.elements[i], new Set())) &&
|
|
538
|
+
parts(l).some((mine, i) => (i < fixed ? [r.elements[i]] : parts(r)).some((theirs) => confused(mine, theirs))));
|
|
539
|
+
}
|
|
540
|
+
if (SchemaAST.isObjects(l)) {
|
|
541
|
+
if (SchemaAST.isDeclaration(r)) {
|
|
542
|
+
if (!first)
|
|
543
|
+
return declaresMarker(l) && holdsPlainBigint(l);
|
|
544
|
+
return storedMapsOf(r)?.some((map) => confused(l, map)) ?? false;
|
|
545
|
+
}
|
|
546
|
+
if (!SchemaAST.isObjects(r))
|
|
547
|
+
return false;
|
|
548
|
+
if (!(first ? hasFieldsOf(r, l) : hasFieldsOf(l, r)) || !couldBeBoth(l, r, new Set())) {
|
|
549
|
+
return false;
|
|
550
|
+
}
|
|
551
|
+
const records = r.indexSignatures.map((signature) => signature.type);
|
|
552
|
+
return (l.propertySignatures.some((mine) => {
|
|
553
|
+
const named = r.propertySignatures.find((theirs) => theirs.name === mine.name);
|
|
554
|
+
return (named === undefined ? records : [named.type]).some((theirs) => confused(mine.type, theirs));
|
|
555
|
+
}) ||
|
|
556
|
+
l.indexSignatures.some((signature) => [...r.propertySignatures.map((theirs) => theirs.type), ...records].some((theirs) => confused(signature.type, theirs))));
|
|
557
|
+
}
|
|
558
|
+
if (SchemaAST.isDeclaration(l)) {
|
|
559
|
+
if (SchemaAST.isObjects(r)) {
|
|
560
|
+
if (first)
|
|
561
|
+
return declaresMarker(r) && holdsPlainBigint(l);
|
|
562
|
+
if (isSetDeclaration(l))
|
|
563
|
+
return false;
|
|
564
|
+
const maps = storedMapsOf(l);
|
|
565
|
+
return maps === undefined ? holdsPlainBigint(l) : maps.some((map) => confused(map, r));
|
|
566
|
+
}
|
|
567
|
+
return (SchemaAST.isDeclaration(r) &&
|
|
568
|
+
representationIdOf(l) === representationIdOf(r) &&
|
|
569
|
+
l.typeParameters.some((parameter, i) => r.typeParameters[i] !== undefined && confused(parameter, r.typeParameters[i])));
|
|
570
|
+
}
|
|
571
|
+
return false;
|
|
572
|
+
};
|
|
573
|
+
/**
|
|
574
|
+
* Whether a union's members can confuse a bigint with a number. A `oneOf` tries every member, so
|
|
575
|
+
* neither comes first and a value two of them take fails the read.
|
|
576
|
+
*/
|
|
577
|
+
const hasBigintBesideANumber = (union) => {
|
|
578
|
+
const members = union.types.map((member) => SchemaAST.toEncoded(member));
|
|
579
|
+
return members.some((holder, at) => members.some((other, position) => position !== at &&
|
|
580
|
+
(union.options?.mode === "oneOf"
|
|
581
|
+
? confusesItsBigint(holder, other, true, new Set()) ||
|
|
582
|
+
confusesItsBigint(holder, other, false, new Set())
|
|
583
|
+
: confusesItsBigint(holder, other, at < position, new Set()))));
|
|
584
|
+
};
|
|
585
|
+
/** A `Schema.Class`'s own encoding step, onto the struct of its fields, which are walked. */
|
|
586
|
+
const isClassStep = (ast, to) => SchemaAST.isDeclaration(ast) && ast.typeParameters[0] === to;
|
|
587
|
+
/** A union of X and undefined, and nothing else, which is what `Schema.optional(X)` makes of X. */
|
|
588
|
+
const isOptionalOfOne = (ast) => ast.types.filter((member) => !SchemaAST.isUndefined(member)).length === 1;
|
|
589
|
+
/** Whether a stored side has a type's shape: the same kind of node, with the same fields or elements. */
|
|
590
|
+
const sameShape = (type, stored) => {
|
|
591
|
+
if (type._tag !== stored._tag || representationIdOf(type) !== representationIdOf(stored)) {
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
if (SchemaAST.isArrays(type) && SchemaAST.isArrays(stored))
|
|
595
|
+
return sameElements(type, stored);
|
|
596
|
+
if (!SchemaAST.isObjects(type) || !SchemaAST.isObjects(stored))
|
|
597
|
+
return true;
|
|
598
|
+
const names = new Set(stored.propertySignatures.map((property) => property.name));
|
|
599
|
+
return (type.propertySignatures.length === names.size &&
|
|
600
|
+
type.propertySignatures.every((property) => names.has(property.name)));
|
|
601
|
+
};
|
|
602
|
+
/** Whether two lists hold the same things, which is what makes a step between them invisible. */
|
|
603
|
+
const sameElements = (type, stored) => {
|
|
604
|
+
const same = (a, b) => sameShape(SchemaAST.toType(a), SchemaAST.toType(b));
|
|
605
|
+
return (type.elements.length === stored.elements.length &&
|
|
606
|
+
type.rest.length === stored.rest.length &&
|
|
607
|
+
type.elements.every((element, at) => same(element, stored.elements[at])) &&
|
|
608
|
+
type.rest.every((element, at) => same(element, stored.rest[at])));
|
|
609
|
+
};
|
|
610
|
+
/**
|
|
611
|
+
* Whether a step onto `to` is a decoding default: the stored side may be absent, it's the same type
|
|
612
|
+
* on both sides, where `Schema.OptionFromOptionalKey` is a different one, and nothing else changes
|
|
613
|
+
* it on the way, as a transformation beneath the default would.
|
|
614
|
+
*/
|
|
615
|
+
export const isDecodingDefault = (ast, to) => {
|
|
616
|
+
if (to.context?.isOptional !== true)
|
|
617
|
+
return false;
|
|
618
|
+
const steps = ast.encoding ?? [];
|
|
619
|
+
const type = steps.length === 2 ? steps[0].to : ast;
|
|
620
|
+
if (steps.length > 2 || (steps.length === 2 && !isClassStep(ast, type)))
|
|
621
|
+
return false;
|
|
622
|
+
const sides = SchemaAST.isUnion(to) && isOptionalOfOne(to) ? [to, ...to.types] : [to];
|
|
623
|
+
return sides.some((side) => sameShape(type, side));
|
|
624
|
+
};
|
|
625
|
+
/** The list a step's stored side is, if it's one: `Schema.UndefinedOr(Schema.Array(X))` is that array. */
|
|
626
|
+
export const storedList = (to) => {
|
|
627
|
+
const side = SchemaAST.isUnion(to) && isOptionalOfOne(to)
|
|
628
|
+
? to.types.find((member) => !SchemaAST.isUndefined(member))
|
|
629
|
+
: to;
|
|
630
|
+
return side !== undefined && SchemaAST.isArrays(side) ? side : undefined;
|
|
631
|
+
};
|
|
632
|
+
/** The encoding step onto `to`, unless a class's own. */
|
|
633
|
+
const encodingStep = (ast, to, walk) => {
|
|
634
|
+
if (isClassStep(ast, to) || walk.beneath === "transformation")
|
|
635
|
+
return walk.beneath;
|
|
636
|
+
if (isDecodingDefault(ast, to))
|
|
637
|
+
return "default";
|
|
638
|
+
if (walk.by === "schema" || !SchemaAST.isArrays(ast))
|
|
639
|
+
return "transformation";
|
|
640
|
+
const list = storedList(to);
|
|
641
|
+
return list !== undefined && sameElements(ast, list) ? "list" : "transformation";
|
|
642
|
+
};
|
|
643
|
+
/** What the children of a container beneath an encoding are. A list stays seen through. */
|
|
644
|
+
const unreachedBy = (walk, list) => {
|
|
645
|
+
if (walk.beneath === undefined || (list && walk.beneath !== "transformation"))
|
|
646
|
+
return {};
|
|
647
|
+
return walk.beneath === "default" ? { writtenAsIs: true } : { unreached: true };
|
|
648
|
+
};
|
|
649
|
+
/** A plain date or nothing, as an aggregate's date encoder finds it in an attribute's union. */
|
|
650
|
+
const isNullablePlainDate = (ast) => SchemaAST.isUnion(ast) &&
|
|
651
|
+
ast.types.every((member) => SchemaAST.isNull(member) ||
|
|
652
|
+
SchemaAST.isUndefined(member) ||
|
|
653
|
+
(member.encoding === undefined && DATE_DOMAINS.has(representationIdOf(member))));
|
|
654
|
+
/**
|
|
655
|
+
* A transformation to a date on an aggregate: its date encoder stores the domain value as a string,
|
|
656
|
+
* whatever the transformation is over, except under a union or another declaration. It finds one
|
|
657
|
+
* under a decoding default too, in an attribute of the aggregate's own and not deeper.
|
|
658
|
+
*/
|
|
659
|
+
const storedByDateEncoder = (ast, to, path, walk) => {
|
|
660
|
+
if (walk.by !== "aggregate" || walk.unreached || walk.inUnion)
|
|
661
|
+
return false;
|
|
662
|
+
return (DATE_DOMAINS.has(representationIdOf(SchemaAST.toType(ast))) ||
|
|
663
|
+
(to.context?.isOptional === true && !/[.[]/.test(path) && isNullablePlainDate(ast)));
|
|
664
|
+
};
|
|
665
|
+
/**
|
|
666
|
+
* The first thing in this AST that can't be read back, and its path. A cycle through
|
|
667
|
+
* `Schema.suspend` ends where it starts, and a path segment says what kind of value it went through.
|
|
668
|
+
*/
|
|
669
|
+
const firstUnreadable = (ast, path, seen, walk) => {
|
|
670
|
+
const firstIn = (asts, at, next) => {
|
|
671
|
+
for (const part of asts) {
|
|
672
|
+
const found = firstUnreadable(part, at, seen, next);
|
|
673
|
+
if (found !== undefined)
|
|
674
|
+
return found;
|
|
675
|
+
}
|
|
676
|
+
return undefined;
|
|
677
|
+
};
|
|
678
|
+
const child = (at, list = false) => ({
|
|
679
|
+
...walk,
|
|
680
|
+
...unreachedBy(walk, list),
|
|
681
|
+
declared: undefined,
|
|
682
|
+
edge: walk.manyEdges.has(at),
|
|
683
|
+
});
|
|
684
|
+
const encoding = ast.encoding;
|
|
685
|
+
if (encoding !== undefined) {
|
|
686
|
+
const to = encoding[encoding.length - 1].to;
|
|
687
|
+
if (walk.declared === undefined && storedByDateEncoder(ast, to, path, walk))
|
|
688
|
+
return undefined;
|
|
689
|
+
const beneath = encodingStep(ast, to, walk);
|
|
690
|
+
return firstUnreadable(to, path, seen, {
|
|
691
|
+
...walk,
|
|
692
|
+
declared: SchemaAST.toType(ast),
|
|
693
|
+
beneath,
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
if (!walk.edge) {
|
|
697
|
+
const what = unreadableAt(ast, walk);
|
|
698
|
+
if (what !== undefined)
|
|
699
|
+
return { at: path, what };
|
|
700
|
+
}
|
|
701
|
+
switch (ast._tag) {
|
|
702
|
+
case "Declaration":
|
|
703
|
+
return firstIn(ast.typeParameters, path, walk.edge
|
|
704
|
+
? { ...walk, declared: undefined, edge: false }
|
|
705
|
+
: { ...walk, declared: undefined, unreached: true });
|
|
706
|
+
case "Suspend": {
|
|
707
|
+
if (seen.has(ast))
|
|
708
|
+
return undefined;
|
|
709
|
+
seen.add(ast);
|
|
710
|
+
return firstUnreadable(ast.thunk(), path, seen, { ...walk, unreached: true });
|
|
711
|
+
}
|
|
712
|
+
case "Union": {
|
|
713
|
+
// Where the substitution reaches a union, every member gets its try as declared and only a
|
|
714
|
+
// number none of them takes is lifted. Anywhere else the lift is on the bigint itself.
|
|
715
|
+
const lifted = walk.by === "schema" || walk.unreached || walk.beneath === "transformation";
|
|
716
|
+
if (lifted && hasBigintBesideANumber(ast)) {
|
|
717
|
+
return { at: path, what: BIGINT_BESIDE_A_NUMBER };
|
|
718
|
+
}
|
|
719
|
+
const members = {
|
|
720
|
+
...walk,
|
|
721
|
+
// The union `Schema.optional(X)` makes under a default on X, which is X to the substitution.
|
|
722
|
+
...(isOptionalOfOne(ast) && walk.declared?._tag !== "Union"
|
|
723
|
+
? {}
|
|
724
|
+
: unreachedBy(walk, false)),
|
|
725
|
+
inUnion: walk.inUnion || ast.context?.isOptional !== true,
|
|
726
|
+
};
|
|
727
|
+
if (ast.context?.isOptional !== true)
|
|
728
|
+
return firstIn(ast.types, path, members);
|
|
729
|
+
// `Schema.optionalKey` over several members: the first is written in its stored form, and the
|
|
730
|
+
// rest as they're declared, unless an aggregate's date encoder finds them in its own attribute.
|
|
731
|
+
const defined = ast.types.filter((member) => !SchemaAST.isUndefined(member));
|
|
732
|
+
const encoded = walk.by === "aggregate" && !/[.[]/.test(path) && isNullablePlainDate(ast);
|
|
733
|
+
return (firstIn(defined.slice(0, 1), path, members) ??
|
|
734
|
+
firstIn(defined.slice(1), path, encoded ? members : { ...members, writtenAsIs: true }));
|
|
735
|
+
}
|
|
736
|
+
case "Arrays":
|
|
737
|
+
return firstIn([...ast.elements, ...ast.rest], `${path}[]`, child(`${path}[]`, ast.elements.length === 0));
|
|
738
|
+
case "Objects": {
|
|
739
|
+
for (const property of ast.propertySignatures) {
|
|
740
|
+
const name = String(property.name);
|
|
741
|
+
const at = path === "" ? name : `${path}.${name}`;
|
|
742
|
+
const found = firstUnreadable(property.type, at, seen, child(at));
|
|
743
|
+
if (found !== undefined)
|
|
744
|
+
return found;
|
|
745
|
+
}
|
|
746
|
+
return firstIn(ast.indexSignatures.map((signature) => signature.type), `${path}[key]`, child(`${path}[key]`));
|
|
747
|
+
}
|
|
748
|
+
default:
|
|
749
|
+
return undefined;
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
/**
|
|
753
|
+
* `[EDD-9125]`: a model whose stored side holds something its owner can't read back, at any depth.
|
|
754
|
+
* Either the write fails in a way nothing can recover, or it succeeds and the row no longer decodes,
|
|
755
|
+
* so a definition holding one can't work and fails at load. The stored side is what's read: a map
|
|
756
|
+
* decoded from a record, or under `Schema.toCodecJson`, is stored as that and reads back, and the
|
|
757
|
+
* collection a `many` edge is declared as is rows, of which only the rows are stored.
|
|
758
|
+
*
|
|
759
|
+
* Only Effect's own declarations can be judged here. A `Schema.instanceOf` and a duck-typed
|
|
760
|
+
* `Schema.declare` have the same AST, and one reads back where the other doesn't, so those are
|
|
761
|
+
* judged by value at write ({@link holdsCustomDeclaration}, `EDD-9126`).
|
|
762
|
+
*/
|
|
763
|
+
export const refuseUnreadable = (owner, by, model, options) => {
|
|
764
|
+
const found = firstUnreadable(model.ast, "", new Set(), {
|
|
765
|
+
by,
|
|
766
|
+
manyEdges: options?.manyEdges ?? new Set(),
|
|
767
|
+
declared: undefined,
|
|
768
|
+
unreached: false,
|
|
769
|
+
writtenAsIs: false,
|
|
770
|
+
beneath: undefined,
|
|
771
|
+
inUnion: false,
|
|
772
|
+
edge: false,
|
|
773
|
+
});
|
|
774
|
+
if (found === undefined)
|
|
775
|
+
return;
|
|
776
|
+
const { at, what } = found;
|
|
777
|
+
throw new Error(`[EDD-9125] ${owner}: ${at === "" ? (options?.whole ?? "the model") : `field "${at}"`} is ` +
|
|
778
|
+
`${what.name.startsWith("Schema.") ? "a " : ""}${what.name}, which can't be read back: ` +
|
|
779
|
+
`${what.why}. Use ${what.use}.`);
|
|
780
|
+
};
|
|
781
|
+
/**
|
|
782
|
+
* Whether a stored side holds a declaration Effect didn't make, such as a `Schema.instanceOf` or a
|
|
783
|
+
* duck-typed `Schema.declare`. Only a value can say whether one reads back (`internal/RoundTrip.ts`
|
|
784
|
+
* in `effect-dynamodb`), and this says which schemas have such values. A `Schema.Class`, and a struct
|
|
785
|
+
* decoded to a class, have an encoding: what's stored is the struct, and only what's in it counts.
|
|
786
|
+
* Effect's own address and DNS record declarations carry no `representation` id, and read back as
|
|
787
|
+
* the instances they were (`internal/InstanceReads.ts`), so they're not custom: recognised by their
|
|
788
|
+
* parser, never by a name, so a `Schema.declare` of your own under the same identifier still is.
|
|
789
|
+
*/
|
|
790
|
+
export const holdsCustomDeclaration = (ast, seen = new Set()) => {
|
|
791
|
+
const encoding = ast.encoding;
|
|
792
|
+
if (encoding !== undefined) {
|
|
793
|
+
return holdsCustomDeclaration(encoding[encoding.length - 1].to, seen);
|
|
794
|
+
}
|
|
795
|
+
const within = (asts) => asts.some((part) => holdsCustomDeclaration(part, seen));
|
|
796
|
+
switch (ast._tag) {
|
|
797
|
+
case "Declaration":
|
|
798
|
+
return ((representationIdOf(ast) === undefined && effectDeclarationOf(ast) === undefined) ||
|
|
799
|
+
within(ast.typeParameters));
|
|
800
|
+
case "Suspend": {
|
|
801
|
+
if (seen.has(ast))
|
|
802
|
+
return false;
|
|
803
|
+
seen.add(ast);
|
|
804
|
+
return holdsCustomDeclaration(ast.thunk(), seen);
|
|
805
|
+
}
|
|
806
|
+
case "Union":
|
|
807
|
+
return within(ast.types);
|
|
808
|
+
case "Arrays":
|
|
809
|
+
return within([...ast.elements, ...ast.rest]);
|
|
810
|
+
case "Objects":
|
|
811
|
+
return within([
|
|
812
|
+
...ast.propertySignatures.map((property) => property.type),
|
|
813
|
+
...ast.indexSignatures.map((signature) => signature.type),
|
|
814
|
+
]);
|
|
815
|
+
default:
|
|
816
|
+
return false;
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
/**
|
|
820
|
+
* Whether a stored side holds a position no schema types (`Schema.Unknown`, `Schema.Any`), at any
|
|
821
|
+
* depth: what's written there is the caller's value as given, so only a value can say whether
|
|
822
|
+
* DynamoDB stores it (`internal/LostValues.ts`, `EDD-9161`). A declaration's type parameters count,
|
|
823
|
+
* as `Schema.Option(Schema.Unknown)` holds its value as given.
|
|
824
|
+
*/
|
|
825
|
+
export const holdsUntyped = (ast, seen = new Set()) => {
|
|
826
|
+
const encoding = ast.encoding;
|
|
827
|
+
if (encoding !== undefined)
|
|
828
|
+
return holdsUntyped(encoding[encoding.length - 1].to, seen);
|
|
829
|
+
const within = (asts) => asts.some((part) => holdsUntyped(part, seen));
|
|
830
|
+
switch (ast._tag) {
|
|
831
|
+
case "Unknown":
|
|
832
|
+
case "Any":
|
|
833
|
+
return true;
|
|
834
|
+
case "Declaration":
|
|
835
|
+
return within(ast.typeParameters);
|
|
836
|
+
case "Suspend": {
|
|
837
|
+
if (seen.has(ast))
|
|
838
|
+
return false;
|
|
839
|
+
seen.add(ast);
|
|
840
|
+
return holdsUntyped(ast.thunk(), seen);
|
|
841
|
+
}
|
|
842
|
+
case "Union":
|
|
843
|
+
return within(ast.types);
|
|
844
|
+
case "Arrays":
|
|
845
|
+
return within([...ast.elements, ...ast.rest]);
|
|
846
|
+
case "Objects":
|
|
847
|
+
return within([
|
|
848
|
+
...ast.propertySignatures.map((property) => property.type),
|
|
849
|
+
...ast.indexSignatures.map((signature) => signature.type),
|
|
850
|
+
]);
|
|
851
|
+
default:
|
|
852
|
+
return false;
|
|
853
|
+
}
|
|
854
|
+
};
|
|
855
|
+
/**
|
|
856
|
+
* The value on a stored side that DynamoDB can't store, if there is one. With
|
|
857
|
+
* both in one union, `undefined` is what an absent attribute means.
|
|
858
|
+
*/
|
|
859
|
+
const valueWithNoStoredForm = (ast, seen = new Set()) => {
|
|
860
|
+
switch (ast._tag) {
|
|
861
|
+
case "Undefined":
|
|
862
|
+
case "Void":
|
|
863
|
+
case "Unknown":
|
|
864
|
+
case "Any":
|
|
865
|
+
return { stored: undefined };
|
|
866
|
+
case "Declaration":
|
|
867
|
+
return isSetDeclaration(ast) ? { stored: new Set() } : undefined;
|
|
868
|
+
case "Suspend": {
|
|
869
|
+
if (seen.has(ast))
|
|
870
|
+
return undefined;
|
|
871
|
+
seen.add(ast);
|
|
872
|
+
return valueWithNoStoredForm(ast.thunk(), seen);
|
|
873
|
+
}
|
|
874
|
+
case "Union": {
|
|
875
|
+
const found = ast.types.flatMap((member) => valueWithNoStoredForm(member, seen) ?? []);
|
|
876
|
+
return found.find((value) => value.stored === undefined) ?? found[0];
|
|
877
|
+
}
|
|
878
|
+
default:
|
|
879
|
+
return undefined;
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
/**
|
|
883
|
+
* Whether a value of this type can be `undefined` and be stored as one: an `undefined` that
|
|
884
|
+
* encodes to something else has a stored form.
|
|
885
|
+
*/
|
|
886
|
+
export const holdsUndefined = (ast) => [SchemaAST.toType(ast), SchemaAST.toEncoded(ast)].every((side) => {
|
|
887
|
+
const found = valueWithNoStoredForm(side);
|
|
888
|
+
return found !== undefined && found.stored === undefined;
|
|
889
|
+
});
|
|
890
|
+
/**
|
|
891
|
+
* The value a field's absent attribute stands for, from its schema alone: DynamoDB can't
|
|
892
|
+
* store `undefined` or an empty set, so on a field whose stored side includes one the value
|
|
893
|
+
* is an absent attribute. `undefined` when the rule doesn't apply: the field's key is
|
|
894
|
+
* optional, so absent reads as absent (or as its decoding default), or its stored side holds
|
|
895
|
+
* no such value. Nothing the schema runs is run, so a `.check()` that refuses the value isn't
|
|
896
|
+
* seen here: {@link absentReading} asks it, at a write.
|
|
897
|
+
*/
|
|
898
|
+
export const absentStored = (field) => {
|
|
899
|
+
const encoded = SchemaAST.toEncoded(field.ast);
|
|
900
|
+
return SchemaAST.isOptional(encoded) ? undefined : valueWithNoStoredForm(encoded);
|
|
901
|
+
};
|
|
902
|
+
/**
|
|
903
|
+
* The one rule for values with no stored form. DynamoDB can't store `undefined`
|
|
904
|
+
* or an empty set, so on a field whose stored side includes one the value is an
|
|
905
|
+
* absent attribute, and an absent attribute reads back as that value. Returns
|
|
906
|
+
* what it reads back as, or `undefined` when the rule doesn't apply: the field's
|
|
907
|
+
* key is optional, so absent reads as absent (or as its decoding default); or
|
|
908
|
+
* its schema refuses the value, as a `.check()` for a non-empty set does, so no
|
|
909
|
+
* row may be without it.
|
|
910
|
+
*
|
|
911
|
+
* The check decodes for real, running the field's checks and transformations on the value,
|
|
912
|
+
* so it's asked by the write that leaves the attribute out and never while an entity is
|
|
913
|
+
* defined ({@link absentStored} answers there). A transformation that can't run
|
|
914
|
+
* synchronously is taken at its word, reading back as the stored value.
|
|
915
|
+
*/
|
|
916
|
+
export const absentReading = (field) => {
|
|
917
|
+
const found = absentStored(field);
|
|
918
|
+
if (found === undefined)
|
|
919
|
+
return undefined;
|
|
920
|
+
const decoded = Schema.decodeUnknownExit(field)(found.stored);
|
|
921
|
+
if (Exit.hasFails(decoded))
|
|
922
|
+
return undefined;
|
|
923
|
+
return { stored: found.stored, value: Exit.isSuccess(decoded) ? decoded.value : found.stored };
|
|
924
|
+
};
|
|
925
|
+
/**
|
|
926
|
+
* Whether a step may make something of a key the stored row is without. A getter that only
|
|
927
|
+
* maps the value it's given (`passthrough`, `transform`, `transformEffect`) leaves an absent
|
|
928
|
+
* key absent. Anything else may act on the absence: a getter handed the `Option`
|
|
929
|
+
* (`SchemaGetter.withDefault` beneath every `Schema.withDecodingDefault*`, a step onto an
|
|
930
|
+
* `Option` as `Schema.OptionFromOptional`'s is, `SchemaGetter.required`), a middleware
|
|
931
|
+
* (`Schema.middlewareDecoding`, `Schema.catchDecoding`), which is handed the read of the
|
|
932
|
+
* missing key and can fill it or fail it, and any kind of step this doesn't know.
|
|
933
|
+
*/
|
|
934
|
+
const actsOnAbsentKey = (link) => {
|
|
935
|
+
const transformation = link.transformation;
|
|
936
|
+
if (transformation._tag !== "Transformation")
|
|
937
|
+
return true;
|
|
938
|
+
const step = transformation.decode?._tag;
|
|
939
|
+
return !(step === "Passthrough" || step === "Transform" || step === "TransformEffect");
|
|
940
|
+
};
|
|
941
|
+
/**
|
|
942
|
+
* Whether the model's code may take part in reading a missing key through a node: a step
|
|
943
|
+
* that may act on the absence ({@link actsOnAbsentKey}), anywhere on the way from the most
|
|
944
|
+
* stored side up. A suspension is taken to: what it suspends isn't looked into here, so
|
|
945
|
+
* only reading the absence says.
|
|
946
|
+
*/
|
|
947
|
+
const mayReadMissingKey = (ast) => SchemaAST.isSuspend(ast) ||
|
|
948
|
+
(ast.encoding ?? []).some((link) => actsOnAbsentKey(link) || mayReadMissingKey(link.to));
|
|
949
|
+
/**
|
|
950
|
+
* Whether reading through a node runs nothing but Effect's own parsing: no check, no
|
|
951
|
+
* transformation and no declaration of the model's own, anywhere in it. A check on the stored
|
|
952
|
+
* side of a flipped schema (`encodingChecks`) runs on a read too.
|
|
953
|
+
*/
|
|
954
|
+
const runsNoCode = (ast, seen = new Set()) => {
|
|
955
|
+
if (seen.has(ast))
|
|
956
|
+
return true;
|
|
957
|
+
seen.add(ast);
|
|
958
|
+
if (ast.checks !== undefined ||
|
|
959
|
+
ast.encoding !== undefined ||
|
|
960
|
+
ast.encodingChecks !== undefined) {
|
|
961
|
+
return false;
|
|
962
|
+
}
|
|
963
|
+
const all = (asts) => asts.every((part) => runsNoCode(part, seen));
|
|
964
|
+
switch (ast._tag) {
|
|
965
|
+
case "Declaration":
|
|
966
|
+
return representationIdOf(ast) !== undefined && all(ast.typeParameters);
|
|
967
|
+
case "Suspend":
|
|
968
|
+
return runsNoCode(ast.thunk(), seen);
|
|
969
|
+
case "Union":
|
|
970
|
+
return all(ast.types);
|
|
971
|
+
case "Arrays":
|
|
972
|
+
return all([...ast.elements, ...ast.rest]);
|
|
973
|
+
case "Objects":
|
|
974
|
+
return all([
|
|
975
|
+
...ast.propertySignatures.map((property) => property.type),
|
|
976
|
+
...ast.indexSignatures.flatMap((signature) => [signature.parameter, signature.type]),
|
|
977
|
+
]);
|
|
978
|
+
default:
|
|
979
|
+
return true;
|
|
980
|
+
}
|
|
981
|
+
};
|
|
982
|
+
export const lackDecision = (field) => {
|
|
983
|
+
if (mayReadMissingKey(field.ast))
|
|
984
|
+
return "decode";
|
|
985
|
+
if (SchemaAST.isOptional(field.ast))
|
|
986
|
+
return "allow";
|
|
987
|
+
if (absentStored(field) === undefined)
|
|
988
|
+
return "refuse";
|
|
989
|
+
return runsNoCode(field.ast) ? "allow" : "decode";
|
|
990
|
+
};
|
|
991
|
+
/**
|
|
992
|
+
* Whether a row without this field's key reads, by reading one: a decoding default, an
|
|
993
|
+
* absent-key step or a middleware runs, and the field's checks see what that makes. Asked
|
|
994
|
+
* by a write that leaves the key out of a row, once, and never while an entity is defined.
|
|
995
|
+
* A default that can't run synchronously is taken at its word.
|
|
996
|
+
*/
|
|
997
|
+
const readsMissingKey = (field) => {
|
|
998
|
+
const probe = Schema.Struct({ field });
|
|
999
|
+
return !Exit.hasFails(Schema.decodeUnknownExit(probe)({}));
|
|
1000
|
+
};
|
|
1001
|
+
/**
|
|
1002
|
+
* Whether the field's key is one a stored row may leave out: optional fields
|
|
1003
|
+
* (`Schema.optional`, `Schema.optionalKey`, the `OptionFromOptional` family) and
|
|
1004
|
+
* a decoding default, whose value a read fills in.
|
|
1005
|
+
*
|
|
1006
|
+
* The schema answers where nothing it runs takes part: the missing key reaches the read
|
|
1007
|
+
* side as missing, so the read side's key says. Where the model's code may take part in
|
|
1008
|
+
* reading the missing key ({@link mayReadMissingKey}), the key is read for real, so this
|
|
1009
|
+
* belongs to a write and is never asked while an entity is defined.
|
|
1010
|
+
*/
|
|
1011
|
+
export const rowOmits = (field) => mayReadMissingKey(field.ast) ? readsMissingKey(field) : SchemaAST.isOptional(field.ast);
|
|
1012
|
+
/**
|
|
1013
|
+
* Whether what's stored for this can leave out a value that reads back all the same: a struct's
|
|
1014
|
+
* field ({@link absentStored}), or the set an `Option` holds as its value. An `undefined` there
|
|
1015
|
+
* reads as what it is without anything being done about it.
|
|
1016
|
+
*/
|
|
1017
|
+
export const holdsAbsentValue = (ast, seen = new Set()) => {
|
|
1018
|
+
const found = forced(ast, seen);
|
|
1019
|
+
if (found === undefined)
|
|
1020
|
+
return false;
|
|
1021
|
+
const { node } = found;
|
|
1022
|
+
const reading = (part) => absentStored(Schema.make(part));
|
|
1023
|
+
const within = (asts) => asts.some((part) => holdsAbsentValue(part, found.seen));
|
|
1024
|
+
switch (node._tag) {
|
|
1025
|
+
case "Declaration":
|
|
1026
|
+
return (node.typeParameters.some((part) => reading(part)?.stored !== undefined) ||
|
|
1027
|
+
within(node.typeParameters));
|
|
1028
|
+
case "Union":
|
|
1029
|
+
return within(node.types);
|
|
1030
|
+
case "Arrays":
|
|
1031
|
+
return within([...node.elements, ...node.rest]);
|
|
1032
|
+
case "Objects": {
|
|
1033
|
+
const fields = node.propertySignatures.map((property) => property.type);
|
|
1034
|
+
return (fields.some((part) => reading(part) !== undefined) ||
|
|
1035
|
+
within([...fields, ...node.indexSignatures.map((signature) => signature.type)]));
|
|
1036
|
+
}
|
|
1037
|
+
default:
|
|
1038
|
+
return false;
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
/**
|
|
1042
|
+
* Whether a stored row can be without this field and still be read: its key may
|
|
1043
|
+
* be left out ({@link rowOmits}), or an absent attribute is one of its values
|
|
1044
|
+
* ({@link absentReading}). A required field cannot, nor can a `NullOr`, whose key
|
|
1045
|
+
* is required even though its value may be empty.
|
|
1046
|
+
*
|
|
1047
|
+
* Reads the absence where the model's code takes part in it ({@link lackDecision}), so
|
|
1048
|
+
* this belongs to a write and is never asked while an entity is defined.
|
|
1049
|
+
*/
|
|
1050
|
+
export const rowCanLack = (field) => rowOmits(field) || absentReading(field) !== undefined;
|
|
1051
|
+
const keyRenamesFound = new WeakMap();
|
|
1052
|
+
/**
|
|
1053
|
+
* The key renames of a struct `Schema.encodeKeys` built, seen by its shape alone: the struct's one
|
|
1054
|
+
* encoding step is a transformation of two plain functions, to a bare struct of as many
|
|
1055
|
+
* properties, whose `i`-th property holds `SchemaAST.toEncoded` of the struct's `i`-th (the very
|
|
1056
|
+
* node `encodeKeys` stores, optionality included), under the name it's stored by. `encodeKeys`
|
|
1057
|
+
* builds the stored struct property by property in the struct's own order, so the `i`-th stored
|
|
1058
|
+
* name is the `i`-th property's; a name JavaScript orders first as an array index (`"0"`) could
|
|
1059
|
+
* break that order, so a struct naming one isn't taken.
|
|
1060
|
+
*
|
|
1061
|
+
* Nothing the model wrote is run: the step's functions are never called. A struct of any other
|
|
1062
|
+
* shape (a transformation that defaults, drops or adds a key, stores a key optional the struct
|
|
1063
|
+
* requires, or anything else) is `undefined`, a node whose stored names aren't known, so a path
|
|
1064
|
+
* below it is sent as written and a removal below it isn't judged (#238). A transformation of
|
|
1065
|
+
* exactly that shape is read as the rename it has the shape of: a stored property is then the
|
|
1066
|
+
* encoded form of its own, so a removal of a key the stored struct requires leaves a row no read
|
|
1067
|
+
* decodes, whatever the functions do.
|
|
1068
|
+
*
|
|
1069
|
+
* @internal
|
|
1070
|
+
*/
|
|
1071
|
+
export const keyRenamesOf = (ast) => {
|
|
1072
|
+
const found = keyRenamesFound.get(ast);
|
|
1073
|
+
if (found !== undefined)
|
|
1074
|
+
return found ?? undefined;
|
|
1075
|
+
const renames = declaredKeyRenames(ast);
|
|
1076
|
+
keyRenamesFound.set(ast, renames ?? null);
|
|
1077
|
+
return renames;
|
|
1078
|
+
};
|
|
1079
|
+
/** A name JavaScript lists before the others, as it does an array index. */
|
|
1080
|
+
const INDEX_NAME = /^(?:0|[1-9]\d*)$/;
|
|
1081
|
+
const declaredKeyRenames = (ast) => {
|
|
1082
|
+
const [link, ...more] = ast.encoding ?? [];
|
|
1083
|
+
if (link === undefined || more.length > 0 || ast.indexSignatures.length > 0)
|
|
1084
|
+
return undefined;
|
|
1085
|
+
const to = link.to;
|
|
1086
|
+
if (!SchemaAST.isObjects(to) || !bare(to) || to.indexSignatures.length > 0)
|
|
1087
|
+
return undefined;
|
|
1088
|
+
const step = link.transformation;
|
|
1089
|
+
if (step._tag !== "Transformation" ||
|
|
1090
|
+
step.encode?._tag !== "Transform" ||
|
|
1091
|
+
step.decode?._tag !== "Transform") {
|
|
1092
|
+
return undefined;
|
|
1093
|
+
}
|
|
1094
|
+
const own = ast.propertySignatures;
|
|
1095
|
+
const stored = to.propertySignatures;
|
|
1096
|
+
if (own.length === 0 || own.length !== stored.length)
|
|
1097
|
+
return undefined;
|
|
1098
|
+
const storedName = new Map();
|
|
1099
|
+
const storedNames = new Set();
|
|
1100
|
+
for (const [i, property] of own.entries()) {
|
|
1101
|
+
const held = stored[i];
|
|
1102
|
+
if (typeof property.name !== "string" || typeof held.name !== "string")
|
|
1103
|
+
return undefined;
|
|
1104
|
+
if (INDEX_NAME.test(property.name) || INDEX_NAME.test(held.name))
|
|
1105
|
+
return undefined;
|
|
1106
|
+
if (held.type !== SchemaAST.toEncoded(property.type))
|
|
1107
|
+
return undefined;
|
|
1108
|
+
if (storedNames.has(held.name))
|
|
1109
|
+
return undefined;
|
|
1110
|
+
storedNames.add(held.name);
|
|
1111
|
+
storedName.set(property.name, held.name);
|
|
1112
|
+
}
|
|
1113
|
+
return {
|
|
1114
|
+
storedName,
|
|
1115
|
+
holding: new Map(own.map((property) => [storedName.get(property.name), property])),
|
|
1116
|
+
};
|
|
1117
|
+
};
|
|
1118
|
+
const NEUTRAL = { _tag: "Neutral" };
|
|
1119
|
+
const UNKNOWN = { _tag: "Unknown" };
|
|
1120
|
+
/** The nodes a value read through `ast` is read by: each member of a union, what a suspension holds. */
|
|
1121
|
+
const namingNodes = (ast, into, seen) => {
|
|
1122
|
+
if (seen.has(ast))
|
|
1123
|
+
return;
|
|
1124
|
+
seen.add(ast);
|
|
1125
|
+
if (SchemaAST.isUnion(ast) && ast.encoding === undefined) {
|
|
1126
|
+
for (const member of ast.types)
|
|
1127
|
+
namingNodes(member, into, seen);
|
|
1128
|
+
}
|
|
1129
|
+
else if (SchemaAST.isSuspend(ast) && ast.encoding === undefined) {
|
|
1130
|
+
namingNodes(ast.thunk(), into, seen);
|
|
1131
|
+
}
|
|
1132
|
+
else {
|
|
1133
|
+
into.push(ast);
|
|
1134
|
+
}
|
|
1135
|
+
};
|
|
1136
|
+
const namingStep = (node, segment) => {
|
|
1137
|
+
if (SchemaAST.isDeclaration(node)) {
|
|
1138
|
+
// A `Schema.Class`: stored as the struct it's encoded to.
|
|
1139
|
+
const [link, ...more] = node.encoding ?? [];
|
|
1140
|
+
const step = link?.transformation;
|
|
1141
|
+
if (link === undefined ||
|
|
1142
|
+
more.length > 0 ||
|
|
1143
|
+
!SchemaAST.isObjects(link.to) ||
|
|
1144
|
+
step?._tag !== "Transformation" ||
|
|
1145
|
+
step.decode?._tag !== "Transform") {
|
|
1146
|
+
return UNKNOWN;
|
|
1147
|
+
}
|
|
1148
|
+
return namingStep(link.to, segment);
|
|
1149
|
+
}
|
|
1150
|
+
if (SchemaAST.isObjects(node)) {
|
|
1151
|
+
if (node.encoding === undefined) {
|
|
1152
|
+
if (typeof segment !== "string")
|
|
1153
|
+
return NEUTRAL;
|
|
1154
|
+
const property = node.propertySignatures.find((signature) => signature.name === segment);
|
|
1155
|
+
if (property !== undefined)
|
|
1156
|
+
return { _tag: "Named", stored: segment, next: [property.type] };
|
|
1157
|
+
const reading = [];
|
|
1158
|
+
for (const signature of node.indexSignatures) {
|
|
1159
|
+
if (!signatureKey(signature.parameter))
|
|
1160
|
+
return UNKNOWN;
|
|
1161
|
+
if (keyMatches(signature.parameter, segment))
|
|
1162
|
+
reading.push(signature.type);
|
|
1163
|
+
}
|
|
1164
|
+
return reading.length > 0 ? { _tag: "Named", stored: segment, next: reading } : NEUTRAL;
|
|
1165
|
+
}
|
|
1166
|
+
const renames = keyRenamesOf(node);
|
|
1167
|
+
if (renames === undefined)
|
|
1168
|
+
return UNKNOWN;
|
|
1169
|
+
if (typeof segment !== "string")
|
|
1170
|
+
return NEUTRAL;
|
|
1171
|
+
// A stored name is sent as it is, even one that's also another property's own name.
|
|
1172
|
+
const held = renames.holding.get(segment);
|
|
1173
|
+
if (held !== undefined)
|
|
1174
|
+
return { _tag: "Named", stored: segment, next: [held.type] };
|
|
1175
|
+
const stored = renames.storedName.get(segment);
|
|
1176
|
+
if (stored === undefined)
|
|
1177
|
+
return NEUTRAL;
|
|
1178
|
+
const property = node.propertySignatures.find((signature) => signature.name === segment);
|
|
1179
|
+
return { _tag: "Named", stored, next: [property.type] };
|
|
1180
|
+
}
|
|
1181
|
+
if (SchemaAST.isArrays(node)) {
|
|
1182
|
+
if (node.encoding !== undefined)
|
|
1183
|
+
return UNKNOWN;
|
|
1184
|
+
if (typeof segment !== "number")
|
|
1185
|
+
return NEUTRAL;
|
|
1186
|
+
if (node.rest.length > 1)
|
|
1187
|
+
return UNKNOWN;
|
|
1188
|
+
const element = node.elements[segment] ?? node.rest[0];
|
|
1189
|
+
return element === undefined ? NEUTRAL : { _tag: "Named", stored: segment, next: [element] };
|
|
1190
|
+
}
|
|
1191
|
+
return SCALARS.has(node._tag) || node._tag === "Never" ? NEUTRAL : UNKNOWN;
|
|
1192
|
+
};
|
|
1193
|
+
/** The struct a node is stored as, where it renames its keys: a struct's own, a `Schema.Class`'s. */
|
|
1194
|
+
const renamingStruct = (node) => {
|
|
1195
|
+
if (SchemaAST.isDeclaration(node)) {
|
|
1196
|
+
const to = node.encoding?.[0]?.to;
|
|
1197
|
+
return to !== undefined && renamingStruct(to);
|
|
1198
|
+
}
|
|
1199
|
+
return SchemaAST.isObjects(node) && node.encoding !== undefined;
|
|
1200
|
+
};
|
|
1201
|
+
/** Every name a node takes a path segment by: its properties' own names and the names they're stored under. */
|
|
1202
|
+
const namesTaken = (node) => {
|
|
1203
|
+
if (SchemaAST.isDeclaration(node)) {
|
|
1204
|
+
const to = node.encoding?.[0]?.to;
|
|
1205
|
+
return to === undefined ? [] : namesTaken(to);
|
|
1206
|
+
}
|
|
1207
|
+
if (!SchemaAST.isObjects(node))
|
|
1208
|
+
return [];
|
|
1209
|
+
const own = node.propertySignatures.flatMap((property) => typeof property.name === "string" ? [property.name] : []);
|
|
1210
|
+
const renames = node.encoding === undefined ? undefined : keyRenamesOf(node);
|
|
1211
|
+
return renames === undefined ? own : [...own, ...renames.holding.keys()];
|
|
1212
|
+
};
|
|
1213
|
+
/**
|
|
1214
|
+
* Whether the nodes a value at one place may be read by (a union's members) name every path
|
|
1215
|
+
* segment alike (#238): each name any of them takes is stored under one name by every one that
|
|
1216
|
+
* takes it, and each stored name is reached by the same names in every one that stores it. Where
|
|
1217
|
+
* none renames its keys, they do. Where they don't (members storing one property under different
|
|
1218
|
+
* names, or one name holding a different property in each), no one name is right for every row,
|
|
1219
|
+
* so a path through them is named as written, as 1.40.0 named it.
|
|
1220
|
+
*
|
|
1221
|
+
* @internal
|
|
1222
|
+
*/
|
|
1223
|
+
export const unionNamedExactly = (nodes) => {
|
|
1224
|
+
if (nodes.length < 2 || !nodes.some(renamingStruct))
|
|
1225
|
+
return true;
|
|
1226
|
+
const names = new Set(nodes.flatMap(namesTaken));
|
|
1227
|
+
const reaching = nodes.map(() => new Map());
|
|
1228
|
+
for (const name of names) {
|
|
1229
|
+
let stored;
|
|
1230
|
+
for (const [i, node] of nodes.entries()) {
|
|
1231
|
+
const step = namingStep(node, name);
|
|
1232
|
+
if (step._tag === "Unknown")
|
|
1233
|
+
return false;
|
|
1234
|
+
if (step._tag === "Neutral")
|
|
1235
|
+
continue;
|
|
1236
|
+
if (stored !== undefined && stored !== step.stored)
|
|
1237
|
+
return false;
|
|
1238
|
+
stored = step.stored;
|
|
1239
|
+
const by = reaching[i].get(step.stored);
|
|
1240
|
+
if (by === undefined)
|
|
1241
|
+
reaching[i].set(step.stored, [name]);
|
|
1242
|
+
else
|
|
1243
|
+
by.push(name);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
const reachedBy = new Map();
|
|
1247
|
+
for (const reached of reaching) {
|
|
1248
|
+
for (const [stored, by] of reached) {
|
|
1249
|
+
const key = JSON.stringify(by);
|
|
1250
|
+
const seen = reachedBy.get(stored);
|
|
1251
|
+
if (seen !== undefined && seen !== key)
|
|
1252
|
+
return false;
|
|
1253
|
+
reachedBy.set(stored, key);
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
return true;
|
|
1257
|
+
};
|
|
1258
|
+
/** {@link unionNamedExactly} for a union's members as schemas: each read by its own members, in turn. */
|
|
1259
|
+
export const membersNamedExactly = (members) => {
|
|
1260
|
+
const nodes = [];
|
|
1261
|
+
const seen = new Set();
|
|
1262
|
+
for (const member of members)
|
|
1263
|
+
namingNodes(member.ast, nodes, seen);
|
|
1264
|
+
return unionNamedExactly(nodes);
|
|
1265
|
+
};
|
|
1266
|
+
/**
|
|
1267
|
+
* The path below a field as it's stored (#238): each segment that is a property's own name, in a
|
|
1268
|
+
* struct that renames its keys as it's stored (`Schema.encodeKeys`), becomes the name it's stored
|
|
1269
|
+
* under, so a path names what `put` stored and a read takes. This is the one place that says how
|
|
1270
|
+
* a path below a field is named, for an update's path operations, a condition, a filter and a
|
|
1271
|
+
* projection alike.
|
|
1272
|
+
*
|
|
1273
|
+
* A segment that is a name a property is stored under is kept as it is, even where it's also
|
|
1274
|
+
* another property's own name: `encodeKeys({ a: "b", b: "a" })` stores property `b` under `a`, and
|
|
1275
|
+
* the segment `a` names what's stored under `a`, property `b`, as 1.40.0 named it. Every name that
|
|
1276
|
+
* reached a stored attribute before is unchanged.
|
|
1277
|
+
*
|
|
1278
|
+
* The walk follows unions (every member that holds the segment must name it alike), suspensions,
|
|
1279
|
+
* `Schema.Class` structs, records and lists. From a node it doesn't know the stored names of (any
|
|
1280
|
+
* other transformation, a declaration, `Schema.Unknown`), and from a union whose members don't
|
|
1281
|
+
* name every segment alike ({@link unionNamedExactly}), the rest of the path is kept as it is, as
|
|
1282
|
+
* it was before.
|
|
1283
|
+
*/
|
|
1284
|
+
export const storedSegments = (field, segments) => {
|
|
1285
|
+
let ways = [field.ast];
|
|
1286
|
+
const out = [];
|
|
1287
|
+
for (const [i, segment] of segments.entries()) {
|
|
1288
|
+
const nodes = [];
|
|
1289
|
+
const seen = new Set();
|
|
1290
|
+
for (const way of ways)
|
|
1291
|
+
namingNodes(way, nodes, seen);
|
|
1292
|
+
if (!unionNamedExactly(nodes))
|
|
1293
|
+
return [...out, ...segments.slice(i)];
|
|
1294
|
+
let stored;
|
|
1295
|
+
const next = [];
|
|
1296
|
+
for (const node of nodes) {
|
|
1297
|
+
const step = namingStep(node, segment);
|
|
1298
|
+
if (step._tag === "Neutral")
|
|
1299
|
+
continue;
|
|
1300
|
+
if (step._tag === "Unknown" || (stored !== undefined && stored !== step.stored)) {
|
|
1301
|
+
return [...out, ...segments.slice(i)];
|
|
1302
|
+
}
|
|
1303
|
+
stored = step.stored;
|
|
1304
|
+
next.push(...step.next);
|
|
1305
|
+
}
|
|
1306
|
+
if (stored === undefined)
|
|
1307
|
+
return [...out, ...segments.slice(i)];
|
|
1308
|
+
out.push(stored);
|
|
1309
|
+
ways = next;
|
|
1310
|
+
}
|
|
1311
|
+
return out;
|
|
1312
|
+
};
|
|
1313
|
+
/**
|
|
1314
|
+
* The segments below a path's first, as a model of these fields stores them (#238): through the
|
|
1315
|
+
* schema of the field the first segment names ({@link storedSegments}), as written below a name
|
|
1316
|
+
* that is no field. The one way a model's path below its field is named: an entity's (beside its
|
|
1317
|
+
* sparse maps' entries) and an aggregate's root rows' alike.
|
|
1318
|
+
*/
|
|
1319
|
+
export const storedBelowFields = (fields) => (segments) => {
|
|
1320
|
+
const head = segments[0];
|
|
1321
|
+
const rest = segments.slice(1);
|
|
1322
|
+
return rest.length > 0 &&
|
|
1323
|
+
typeof head === "string" &&
|
|
1324
|
+
fields !== undefined &&
|
|
1325
|
+
Object.hasOwn(fields, head)
|
|
1326
|
+
? storedSegments(fields[head], rest)
|
|
1327
|
+
: rest;
|
|
1328
|
+
};
|
|
1329
|
+
/**
|
|
1330
|
+
* A struct that renames its keys as it's stored, as the guard models it (#238): a struct of the
|
|
1331
|
+
* same properties under their stored names. Reading one is reading the stored struct, then the
|
|
1332
|
+
* properties renamed, then the struct, which reads a property as this does under its stored name.
|
|
1333
|
+
*/
|
|
1334
|
+
const renamedViews = new WeakMap();
|
|
1335
|
+
/** The views {@link renamedStruct} built: a way through one is one 1.40.0 didn't judge. */
|
|
1336
|
+
const isRenamedView = new WeakSet();
|
|
1337
|
+
const renamedStruct = (ast) => {
|
|
1338
|
+
const found = renamedViews.get(ast);
|
|
1339
|
+
if (found !== undefined)
|
|
1340
|
+
return found ?? undefined;
|
|
1341
|
+
const renames = ast.checks === undefined &&
|
|
1342
|
+
ast.encodingChecks === undefined
|
|
1343
|
+
? keyRenamesOf(ast)
|
|
1344
|
+
: undefined;
|
|
1345
|
+
const view = renames === undefined
|
|
1346
|
+
? undefined
|
|
1347
|
+
: new SchemaAST.Objects(ast.propertySignatures.map((property) => new SchemaAST.PropertySignature(renames.storedName.get(property.name), property.type)), []);
|
|
1348
|
+
if (view !== undefined)
|
|
1349
|
+
isRenamedView.add(view);
|
|
1350
|
+
renamedViews.set(ast, view ?? null);
|
|
1351
|
+
return view;
|
|
1352
|
+
};
|
|
1353
|
+
const SCALARS = new Set([
|
|
1354
|
+
"Null",
|
|
1355
|
+
"Undefined",
|
|
1356
|
+
"String",
|
|
1357
|
+
"Number",
|
|
1358
|
+
"Boolean",
|
|
1359
|
+
"BigInt",
|
|
1360
|
+
"Symbol",
|
|
1361
|
+
"Literal",
|
|
1362
|
+
"UniqueSymbol",
|
|
1363
|
+
"Enum",
|
|
1364
|
+
"TemplateLiteral",
|
|
1365
|
+
]);
|
|
1366
|
+
/** Whether a node carries nothing of its own a read runs: no transformation, no check. */
|
|
1367
|
+
const bare = (ast) => ast.checks === undefined &&
|
|
1368
|
+
ast.encoding === undefined &&
|
|
1369
|
+
ast.encodingChecks === undefined;
|
|
1370
|
+
/**
|
|
1371
|
+
* Whether an index signature's key is one the guard reads keys by: plain `Schema.String`, or a
|
|
1372
|
+
* template literal whose parts are plain strings and string literals. Effect matches a key
|
|
1373
|
+
* against either as it decodes it, so {@link keyMatches} asks Effect.
|
|
1374
|
+
*/
|
|
1375
|
+
const signatureKey = (parameter) => {
|
|
1376
|
+
if (!bare(parameter))
|
|
1377
|
+
return false;
|
|
1378
|
+
if (parameter._tag === "String")
|
|
1379
|
+
return true;
|
|
1380
|
+
if (parameter._tag !== "TemplateLiteral")
|
|
1381
|
+
return false;
|
|
1382
|
+
return parameter.parts.every((part) => bare(part) &&
|
|
1383
|
+
(part._tag === "String" || (part._tag === "Literal" && typeof part.literal === "string")));
|
|
1384
|
+
};
|
|
1385
|
+
/** Whether a record reads `key` through a signature keyed by `parameter` ({@link signatureKey}). */
|
|
1386
|
+
const keyMatches = (parameter, key) => parameter._tag === "String" || Schema.is(Schema.make(parameter))(key);
|
|
1387
|
+
/** What the guard models a node as ({@link Modelled}), or `undefined` for one it doesn't. */
|
|
1388
|
+
const modelled = (ast) => {
|
|
1389
|
+
if (SchemaAST.isDeclaration(ast))
|
|
1390
|
+
return classStruct(ast);
|
|
1391
|
+
if (SchemaAST.isObjects(ast) && ast.encoding !== undefined) {
|
|
1392
|
+
const view = renamedStruct(ast);
|
|
1393
|
+
return view === undefined ? undefined : { _tag: "Struct", ast: view };
|
|
1394
|
+
}
|
|
1395
|
+
if (!bare(ast))
|
|
1396
|
+
return undefined;
|
|
1397
|
+
switch (ast._tag) {
|
|
1398
|
+
case "Objects": {
|
|
1399
|
+
if (ast.indexSignatures.length === 0) {
|
|
1400
|
+
return ast.propertySignatures.length > 0 ? { _tag: "Struct", ast } : undefined;
|
|
1401
|
+
}
|
|
1402
|
+
return ast.propertySignatures.length === 0 &&
|
|
1403
|
+
ast.indexSignatures.every((signature) => signatureKey(signature.parameter))
|
|
1404
|
+
? { _tag: "Record", signatures: ast.indexSignatures }
|
|
1405
|
+
: undefined;
|
|
1406
|
+
}
|
|
1407
|
+
case "Arrays":
|
|
1408
|
+
return ast.rest.length <= 1 ? { _tag: "List", ast } : undefined;
|
|
1409
|
+
case "Union":
|
|
1410
|
+
return { _tag: "Union", members: ast.types };
|
|
1411
|
+
case "Suspend":
|
|
1412
|
+
return { _tag: "Suspend", suspended: ast.thunk() };
|
|
1413
|
+
default:
|
|
1414
|
+
return SCALARS.has(ast._tag) ? { _tag: "Scalar" } : undefined;
|
|
1415
|
+
}
|
|
1416
|
+
};
|
|
1417
|
+
/**
|
|
1418
|
+
* A `Schema.Class`, as its struct: a declaration read by reading a struct and making an instance
|
|
1419
|
+
* of what that gives, with one step that only maps the value, and no check of its own.
|
|
1420
|
+
*/
|
|
1421
|
+
const classStruct = (ast) => {
|
|
1422
|
+
const [link, ...more] = ast.encoding ?? [];
|
|
1423
|
+
if (link === undefined || more.length > 0)
|
|
1424
|
+
return undefined;
|
|
1425
|
+
if (ast.checks !== undefined || ast.encodingChecks !== undefined)
|
|
1426
|
+
return undefined;
|
|
1427
|
+
const transformation = link.transformation;
|
|
1428
|
+
if (transformation._tag !== "Transformation" || transformation.decode?._tag !== "Transform") {
|
|
1429
|
+
return undefined;
|
|
1430
|
+
}
|
|
1431
|
+
const struct = modelled(link.to);
|
|
1432
|
+
return struct?._tag === "Struct" ? struct : undefined;
|
|
1433
|
+
};
|
|
1434
|
+
/**
|
|
1435
|
+
* The annotation on a field a read takes an absent attribute of as the value that absence stands
|
|
1436
|
+
* for (a `Schema.UndefinedOr`'s `undefined`, `EntitySchemas`' `readingAbsent`): the field as it reads
|
|
1437
|
+
* a stored value. The default the read adds runs only where the attribute is absent, so a value
|
|
1438
|
+
* written there is read as this field alone.
|
|
1439
|
+
*/
|
|
1440
|
+
export const ReadsAbsentAs = Symbol.for("effect-dynamodb/ReadsAbsentAs");
|
|
1441
|
+
/** A node as it reads a stored value: under the read's default for an absent attribute, the field. */
|
|
1442
|
+
const asStored = (ast) => {
|
|
1443
|
+
const field = SchemaAST.resolve(ast)?.[ReadsAbsentAs];
|
|
1444
|
+
return typeof field === "object" && field !== null ? field : ast;
|
|
1445
|
+
};
|
|
1446
|
+
/** Whether 1.40.0 modelled a record: one index signature, keyed by plain `Schema.String`. */
|
|
1447
|
+
const recordOf140 = (signatures) => signatures.length === 1 && signatures[0].parameter._tag === "String";
|
|
1448
|
+
const waysOf = (ast, segments, ways, structs = [], seen = new Set(), since140) => {
|
|
1449
|
+
const node = seen.has(ast) ? undefined : modelled(ast);
|
|
1450
|
+
const mark = since140 === undefined ? {} : { since140 };
|
|
1451
|
+
if (node === undefined) {
|
|
1452
|
+
ways.push({ end: { _tag: "Unknown" }, structs, ...mark });
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
if (node._tag === "Union" || node._tag === "Suspend") {
|
|
1456
|
+
const within = new Set(seen).add(ast);
|
|
1457
|
+
for (const member of node._tag === "Union" ? node.members : [node.suspended]) {
|
|
1458
|
+
waysOf(member, segments, ways, structs, within, since140);
|
|
1459
|
+
}
|
|
1460
|
+
return;
|
|
1461
|
+
}
|
|
1462
|
+
const here = [...structs, node._tag === "Struct" ? node.ast : undefined];
|
|
1463
|
+
const [head, ...rest] = segments;
|
|
1464
|
+
if (head === undefined) {
|
|
1465
|
+
ways.push({ end: { _tag: "At", node }, structs: here, ...mark });
|
|
1466
|
+
return;
|
|
1467
|
+
}
|
|
1468
|
+
if (node._tag === "Struct" && typeof head === "string") {
|
|
1469
|
+
// A struct renaming its keys as it's stored, which 1.40.0 didn't model (#238).
|
|
1470
|
+
const struct = isRenamedView.has(node.ast) ? true : since140;
|
|
1471
|
+
const property = node.ast.propertySignatures.find((signature) => signature.name === head);
|
|
1472
|
+
if (property === undefined) {
|
|
1473
|
+
ways.push({
|
|
1474
|
+
end: { _tag: "Unread" },
|
|
1475
|
+
structs: here,
|
|
1476
|
+
...(struct === undefined ? {} : { since140: struct }),
|
|
1477
|
+
});
|
|
1478
|
+
}
|
|
1479
|
+
else
|
|
1480
|
+
waysOf(property.type, rest, ways, here, new Set(), struct);
|
|
1481
|
+
}
|
|
1482
|
+
else if (node._tag === "Record" && typeof head === "string") {
|
|
1483
|
+
// A key every matching signature reads: each is a way, and a value there is one all of them
|
|
1484
|
+
// read, so a removal any of them refuses leaves a row the record refuses. Judging them as
|
|
1485
|
+
// alternatives asks more than that, never less.
|
|
1486
|
+
const record = recordOf140(node.signatures) ? since140 : true;
|
|
1487
|
+
const reading = node.signatures.filter((signature) => keyMatches(signature.parameter, head));
|
|
1488
|
+
if (reading.length === 0) {
|
|
1489
|
+
ways.push({
|
|
1490
|
+
end: { _tag: "Unread" },
|
|
1491
|
+
structs: here,
|
|
1492
|
+
...(record === undefined ? {} : { since140: record }),
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
for (const signature of reading)
|
|
1496
|
+
waysOf(signature.type, rest, ways, here, new Set(), record);
|
|
1497
|
+
}
|
|
1498
|
+
else if (node._tag === "List" && typeof head === "number") {
|
|
1499
|
+
const element = node.ast.elements[head] ?? node.ast.rest[0];
|
|
1500
|
+
if (element !== undefined)
|
|
1501
|
+
waysOf(element, rest, ways, here, new Set(), since140);
|
|
1502
|
+
}
|
|
1503
|
+
};
|
|
1504
|
+
const domainOf = (ast, seen = new Set()) => {
|
|
1505
|
+
if (seen.has(ast))
|
|
1506
|
+
return "any";
|
|
1507
|
+
const of = (...entries) => new Map(entries);
|
|
1508
|
+
if (SchemaAST.isDeclaration(ast))
|
|
1509
|
+
return classStruct(ast) === undefined ? "any" : of(["map", "any"]);
|
|
1510
|
+
if (ast.encoding !== undefined)
|
|
1511
|
+
return "any";
|
|
1512
|
+
switch (ast._tag) {
|
|
1513
|
+
case "String":
|
|
1514
|
+
case "TemplateLiteral":
|
|
1515
|
+
return of(["string", "any"]);
|
|
1516
|
+
case "Number":
|
|
1517
|
+
return of(["number", "any"]);
|
|
1518
|
+
case "Boolean":
|
|
1519
|
+
return of(["boolean", "any"]);
|
|
1520
|
+
case "Null":
|
|
1521
|
+
return of(["null", "any"]);
|
|
1522
|
+
case "Undefined":
|
|
1523
|
+
return of(["undefined", "any"]);
|
|
1524
|
+
case "BigInt":
|
|
1525
|
+
return of(["number", "any"], ["bigint", "any"]);
|
|
1526
|
+
case "Never":
|
|
1527
|
+
return of();
|
|
1528
|
+
case "Literal": {
|
|
1529
|
+
const literal = ast.literal;
|
|
1530
|
+
if (typeof literal === "bigint")
|
|
1531
|
+
return of(["number", "any"], ["bigint", "any"]);
|
|
1532
|
+
return of([typeof literal, new Set([literal])]);
|
|
1533
|
+
}
|
|
1534
|
+
case "Enum": {
|
|
1535
|
+
const values = new Map();
|
|
1536
|
+
for (const [, value] of ast.enums) {
|
|
1537
|
+
const kind = typeof value;
|
|
1538
|
+
values.set(kind, (values.get(kind) ?? new Set()).add(value));
|
|
1539
|
+
}
|
|
1540
|
+
return values;
|
|
1541
|
+
}
|
|
1542
|
+
case "Arrays":
|
|
1543
|
+
return of(["list", "any"]);
|
|
1544
|
+
case "Objects":
|
|
1545
|
+
return ast.propertySignatures.length > 0 || ast.indexSignatures.length > 0
|
|
1546
|
+
? of(["map", "any"])
|
|
1547
|
+
: "any";
|
|
1548
|
+
case "Union": {
|
|
1549
|
+
const within = new Set(seen).add(ast);
|
|
1550
|
+
const merged = new Map();
|
|
1551
|
+
for (const member of ast.types) {
|
|
1552
|
+
const domain = domainOf(member, within);
|
|
1553
|
+
if (domain === "any")
|
|
1554
|
+
return "any";
|
|
1555
|
+
for (const [kind, values] of domain) {
|
|
1556
|
+
const held = merged.get(kind);
|
|
1557
|
+
if (held === "any" || values === "any")
|
|
1558
|
+
merged.set(kind, "any");
|
|
1559
|
+
else
|
|
1560
|
+
merged.set(kind, new Set([...(held ?? []), ...values]));
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
return merged;
|
|
1564
|
+
}
|
|
1565
|
+
case "Suspend":
|
|
1566
|
+
return domainOf(ast.thunk(), new Set(seen).add(ast));
|
|
1567
|
+
default:
|
|
1568
|
+
return "any";
|
|
1569
|
+
}
|
|
1570
|
+
};
|
|
1571
|
+
/** Whether no stored value two nodes read can be the same ({@link domainOf}). */
|
|
1572
|
+
const disjoint = (a, b) => {
|
|
1573
|
+
const left = domainOf(a);
|
|
1574
|
+
const right = domainOf(b);
|
|
1575
|
+
if (left === "any" || right === "any")
|
|
1576
|
+
return false;
|
|
1577
|
+
for (const [kind, values] of left) {
|
|
1578
|
+
const other = right.get(kind);
|
|
1579
|
+
if (other === undefined)
|
|
1580
|
+
continue;
|
|
1581
|
+
if (values === "any" || other === "any")
|
|
1582
|
+
return false;
|
|
1583
|
+
for (const value of values)
|
|
1584
|
+
if (other.has(value))
|
|
1585
|
+
return false;
|
|
1586
|
+
}
|
|
1587
|
+
return true;
|
|
1588
|
+
};
|
|
1589
|
+
/**
|
|
1590
|
+
* Whether a struct on `target`'s way refuses every map a value on `source`'s way leaves beside
|
|
1591
|
+
* the path: at some depth both ways pass a struct declaring a property other than the path's,
|
|
1592
|
+
* of types no stored value can be both of ({@link disjoint}), where either `source`'s value always
|
|
1593
|
+
* holds it or `target`'s can't read a map without it ({@link rowCanLack}). A tag (`kind: "a"`
|
|
1594
|
+
* against `kind: "b"`) is one. A removal changes nothing beside the path, so such a map is still
|
|
1595
|
+
* there afterwards.
|
|
1596
|
+
*/
|
|
1597
|
+
const besideDisjoint = (source, target, segments) => target.structs.some((struct, depth) => {
|
|
1598
|
+
const from = source.structs[depth];
|
|
1599
|
+
if (struct === undefined || from === undefined)
|
|
1600
|
+
return false;
|
|
1601
|
+
return struct.propertySignatures.some((property) => {
|
|
1602
|
+
if (property.name === segments[depth])
|
|
1603
|
+
return false;
|
|
1604
|
+
const held = from.propertySignatures.find((signature) => signature.name === property.name);
|
|
1605
|
+
return (held !== undefined &&
|
|
1606
|
+
disjoint(held.type, property.type) &&
|
|
1607
|
+
(!rowCanLack(Schema.make(held.type)) ||
|
|
1608
|
+
!rowCanLack(Schema.make(property.type))));
|
|
1609
|
+
});
|
|
1610
|
+
});
|
|
1611
|
+
/**
|
|
1612
|
+
* Removing a property `name` from the maps at the ends of `ways`. A struct requires it when a row
|
|
1613
|
+
* can't be without it ({@link rowCanLack}, asked of the property as of a field). A record never
|
|
1614
|
+
* does, and a struct without it holds no such value, so removing it leaves the value as it was.
|
|
1615
|
+
* Lists and scalars hold no property, so a value of theirs that held one isn't one this reads.
|
|
1616
|
+
* Every way that holds the path is also one that reads what's left, so a struct requiring the
|
|
1617
|
+
* property on every such way is a refusal by each of them.
|
|
1618
|
+
*/
|
|
1619
|
+
const propertyRemoval = (ways, name) => {
|
|
1620
|
+
if (ways.some(({ end }) => end._tag === "Unknown"))
|
|
1621
|
+
return "unknown";
|
|
1622
|
+
let held = false;
|
|
1623
|
+
for (const { end } of ways) {
|
|
1624
|
+
if (end._tag === "Unread")
|
|
1625
|
+
return "readable";
|
|
1626
|
+
if (end._tag !== "At")
|
|
1627
|
+
continue;
|
|
1628
|
+
const { node } = end;
|
|
1629
|
+
if (node._tag === "Record")
|
|
1630
|
+
return "readable";
|
|
1631
|
+
if (node._tag !== "Struct")
|
|
1632
|
+
continue;
|
|
1633
|
+
const property = node.ast.propertySignatures.find((signature) => signature.name === name);
|
|
1634
|
+
if (property === undefined || rowCanLack(Schema.make(property.type))) {
|
|
1635
|
+
return "readable";
|
|
1636
|
+
}
|
|
1637
|
+
held = true;
|
|
1638
|
+
}
|
|
1639
|
+
return held ? "required" : "unheld";
|
|
1640
|
+
};
|
|
1641
|
+
/** The lengths a list accepts: from its required positions to all of them, or on with a rest. */
|
|
1642
|
+
const lengthsOf = (list) => ({
|
|
1643
|
+
min: list.elements.filter((element) => !SchemaAST.isOptional(element)).length,
|
|
1644
|
+
max: list.rest.length > 0 ? Number.POSITIVE_INFINITY : list.elements.length,
|
|
1645
|
+
});
|
|
1646
|
+
/** The element a list reads at `position`, or `undefined` past its last. */
|
|
1647
|
+
const elementAt = (list, position) => list.elements[position] ?? list.rest[0];
|
|
1648
|
+
/**
|
|
1649
|
+
* Whether `target`'s list refuses every list one shorter that removing `position` leaves of a
|
|
1650
|
+
* list `source`'s holds: for each length `source`'s takes, either `target`'s doesn't take one
|
|
1651
|
+
* shorter, or somewhere what's left puts an element where `target`'s reads nothing it can be
|
|
1652
|
+
* ({@link disjoint}) — before `position` the element that was there, from it on the one after.
|
|
1653
|
+
* Lengths past both lists' fixed positions repeat what the last of them shows, so the lengths
|
|
1654
|
+
* are taken up to there.
|
|
1655
|
+
*/
|
|
1656
|
+
const listRefuses = (source, target, position) => {
|
|
1657
|
+
const from = lengthsOf(source);
|
|
1658
|
+
const to = lengthsOf(target);
|
|
1659
|
+
const last = Math.min(from.max, Math.max(from.min, Math.max(source.elements.length, target.elements.length) + 2));
|
|
1660
|
+
for (let length = from.min; length <= last; length++) {
|
|
1661
|
+
if (length - 1 < to.min || length - 1 > to.max)
|
|
1662
|
+
continue;
|
|
1663
|
+
let refused = false;
|
|
1664
|
+
for (let i = 0; i < length - 1 && !refused; i++) {
|
|
1665
|
+
const held = elementAt(source, i < position ? i : i + 1);
|
|
1666
|
+
const read = elementAt(target, i);
|
|
1667
|
+
refused = held !== undefined && read !== undefined && disjoint(held, read);
|
|
1668
|
+
}
|
|
1669
|
+
if (!refused)
|
|
1670
|
+
return false;
|
|
1671
|
+
}
|
|
1672
|
+
return true;
|
|
1673
|
+
};
|
|
1674
|
+
/**
|
|
1675
|
+
* Removing list position `position` from the lists at the ends of `ways`. A list closes up when
|
|
1676
|
+
* an element is removed, so what's left is one shorter, read by every way to there. Readable when
|
|
1677
|
+
* some list may be too short to hold the position (DynamoDB leaves it as it was). Otherwise each
|
|
1678
|
+
* list a value is held by is paired with each that may read what's left: the pair refuses when
|
|
1679
|
+
* the reading list refuses it ({@link listRefuses}: its lengths, and its elements against the
|
|
1680
|
+
* ones that moved) or a struct on the reading way refuses what's beside the path
|
|
1681
|
+
* ({@link besideDisjoint}). Required when every pair refuses. An element that can fill a missing
|
|
1682
|
+
* one ({@link mayReadMissingKey}) makes the lengths unknown. Structs, records and scalars hold no
|
|
1683
|
+
* list, so a value of theirs that held one isn't one this reads, and they read no list that's
|
|
1684
|
+
* left.
|
|
1685
|
+
*/
|
|
1686
|
+
const positionRemoval = (ways, position, segments) => {
|
|
1687
|
+
if (ways.some(({ end }) => end._tag === "Unknown"))
|
|
1688
|
+
return "unknown";
|
|
1689
|
+
if (ways.some(({ end }) => end._tag === "Unread"))
|
|
1690
|
+
return "readable";
|
|
1691
|
+
const lists = [];
|
|
1692
|
+
for (const way of ways) {
|
|
1693
|
+
if (way.end._tag !== "At" || way.end.node._tag !== "List")
|
|
1694
|
+
continue;
|
|
1695
|
+
const list = way.end.node.ast;
|
|
1696
|
+
if (list.elements.some((element) => mayReadMissingKey(element)))
|
|
1697
|
+
return "unknown";
|
|
1698
|
+
lists.push({ ...way, list });
|
|
1699
|
+
}
|
|
1700
|
+
if (lists.length === 0)
|
|
1701
|
+
return "unheld";
|
|
1702
|
+
if (lists.some(({ list }) => lengthsOf(list).min <= position))
|
|
1703
|
+
return "readable";
|
|
1704
|
+
const refused = lists.every((source) => lists.every((target) => listRefuses(source.list, target.list, position) || besideDisjoint(source, target, segments)));
|
|
1705
|
+
return refused ? "required" : "readable";
|
|
1706
|
+
};
|
|
1707
|
+
/**
|
|
1708
|
+
* Removing list position `position` as 1.40.0 judged it, by the lists' lengths alone: required
|
|
1709
|
+
* when no list one shorter than a length some list takes is a length some list takes. A way
|
|
1710
|
+
* through a record 1.40.0 didn't model is `unknown`, as it was. What this reads of the row is the
|
|
1711
|
+
* list's length, which an update's other clauses on the list change too, so this is only the
|
|
1712
|
+
* answer {@link pathIsRequired} gives where they make the full judgement's answer unknown: as
|
|
1713
|
+
* 1.40.0 answered.
|
|
1714
|
+
*/
|
|
1715
|
+
const lengthsAlone = (ways, position) => {
|
|
1716
|
+
if (ways.some(({ end, since140 }) => end._tag === "Unknown" || since140 === true)) {
|
|
1717
|
+
return "unknown";
|
|
1718
|
+
}
|
|
1719
|
+
if (ways.some(({ end }) => end._tag === "Unread"))
|
|
1720
|
+
return "readable";
|
|
1721
|
+
const lengths = [];
|
|
1722
|
+
for (const { end } of ways) {
|
|
1723
|
+
if (end._tag !== "At" || end.node._tag !== "List")
|
|
1724
|
+
continue;
|
|
1725
|
+
if (end.node.ast.elements.some((element) => mayReadMissingKey(element)))
|
|
1726
|
+
return "unknown";
|
|
1727
|
+
lengths.push(lengthsOf(end.node.ast));
|
|
1728
|
+
}
|
|
1729
|
+
if (lengths.length === 0)
|
|
1730
|
+
return "unheld";
|
|
1731
|
+
if (lengths.some((list) => list.min <= position))
|
|
1732
|
+
return "readable";
|
|
1733
|
+
const shorter = (list) => lengths.some((other) => other.min <= list.max - 1 && list.min - 1 <= other.max);
|
|
1734
|
+
return lengths.some(shorter) ? "readable" : "required";
|
|
1735
|
+
};
|
|
1736
|
+
/**
|
|
1737
|
+
* Whether another clause of the same update, writing or removing `other` (a path inside the same
|
|
1738
|
+
* field, `[]` for all of it), changes what the judgement of removing list position `segments`
|
|
1739
|
+
* reads: the list itself (its length, its elements: `other` names another of its positions, or
|
|
1740
|
+
* something below one), what's beside the path where a struct holds it ({@link besideDisjoint}:
|
|
1741
|
+
* `other` names a sibling of a property on the way, or something below one), or the path itself
|
|
1742
|
+
* (`other` names it, a path above it or one below it, which DynamoDB refuses beside a removal).
|
|
1743
|
+
* Another element of a list further up isn't read.
|
|
1744
|
+
*/
|
|
1745
|
+
const touches = (other, segments, ways) => {
|
|
1746
|
+
const depth = segments.findIndex((segment, i) => i >= other.length || other[i] !== segment);
|
|
1747
|
+
if (depth === -1 || depth >= other.length)
|
|
1748
|
+
return true;
|
|
1749
|
+
if (depth === segments.length - 1)
|
|
1750
|
+
return true;
|
|
1751
|
+
if (typeof segments[depth] === "number")
|
|
1752
|
+
return false;
|
|
1753
|
+
return ways.some(({ structs }) => structs[depth] !== undefined);
|
|
1754
|
+
};
|
|
1755
|
+
/**
|
|
1756
|
+
* Whether removing `segments`, a path inside this field, leaves a value the field's schema
|
|
1757
|
+
* refuses, for every value it reads that holds the path, while none that holds what's above it
|
|
1758
|
+
* is without it. Refused only when the walk from the field passes through nodes it models exactly
|
|
1759
|
+
* as a read decodes them ({@link Modelled}); `false` for anything else, and the removal is sent
|
|
1760
|
+
* as 1.39.0 sent it.
|
|
1761
|
+
*
|
|
1762
|
+
* The walk takes every way through the schema to the path, through unions at every level and
|
|
1763
|
+
* every index signature a key matches, so a value one member holds is judged against every
|
|
1764
|
+
* member: a struct member without a property on the way doesn't read what a value holds under it,
|
|
1765
|
+
* so it may read what's left, and the removal is sent. A property is required when a row can't be
|
|
1766
|
+
* without it ({@link rowCanLack}): a required key nothing fills, whose absent attribute isn't one
|
|
1767
|
+
* of its values. A list position is required when no list on any way reads what's left
|
|
1768
|
+
* ({@link positionRemoval}): removing any position of a `[string, string]`; `f[0]` of a
|
|
1769
|
+
* `[string, number?]`, which leaves the number where the string was; and `p[0]` where a tag
|
|
1770
|
+
* beside the list names the member holding it, `{ kind: "a", p: [string, string] }` against
|
|
1771
|
+
* `{ kind: "b", p: [string] }`. `f[0]` of a `[string, string?]`, either position of
|
|
1772
|
+
* `Union([[string, string], [string]])` and an array's element aren't.
|
|
1773
|
+
*
|
|
1774
|
+
* The removal is judged against the update as a whole. A list position's judgement reads the
|
|
1775
|
+
* row's list and what's beside the path, which the update's other clauses (`update.beside`) may
|
|
1776
|
+
* write or remove in the same request: `f.kind = "b"` beside the removal of `f.p[0]` above makes
|
|
1777
|
+
* what's left the other member's, and `f.p[2] = "third"` (DynamoDB appends past the end) leaves a
|
|
1778
|
+
* list as long as before. Where another clause changes what it reads ({@link touches}), the
|
|
1779
|
+
* position is judged as 1.40.0 judged it ({@link lengthsAlone}), and below a sparse map's entry,
|
|
1780
|
+
* which 1.40.0 didn't judge, not at all. A property's judgement reads nothing of the row, so
|
|
1781
|
+
* another clause leaves it as it is.
|
|
1782
|
+
*
|
|
1783
|
+
* `false` means the schema can't say every such row breaks: a record's keys, a list's elements,
|
|
1784
|
+
* a path no value holds (DynamoDB refuses it, as on 1.39.0), and anything the walk doesn't model.
|
|
1785
|
+
*/
|
|
1786
|
+
export const pathIsRequired = (field, segments, update = {}) => {
|
|
1787
|
+
const last = segments[segments.length - 1];
|
|
1788
|
+
if (last === undefined)
|
|
1789
|
+
return false;
|
|
1790
|
+
const ways = [];
|
|
1791
|
+
waysOf(field.ast, segments.slice(0, -1), ways);
|
|
1792
|
+
// A property's verdict reads nothing of the row: no other clause changes it, short of one on
|
|
1793
|
+
// the path or above it, beside which DynamoDB refuses the removal.
|
|
1794
|
+
if (typeof last === "string")
|
|
1795
|
+
return propertyRemoval(ways, last) === "required";
|
|
1796
|
+
if (!(update.beside ?? []).some((other) => touches(other, segments, ways))) {
|
|
1797
|
+
return positionRemoval(ways, last, segments) === "required";
|
|
1798
|
+
}
|
|
1799
|
+
// Another clause changes what the judgement reads: answered as 1.40.0 answered (#238).
|
|
1800
|
+
return update.judgedBefore !== false && lengthsAlone(ways, last) === "required";
|
|
1801
|
+
};
|
|
1802
|
+
/**
|
|
1803
|
+
* The ways a value written at the end of a path is read, through the nodes {@link modelled}
|
|
1804
|
+
* models, collected into `readers`; `false` once the walk meets anything it can't say exactly.
|
|
1805
|
+
*
|
|
1806
|
+
* The position itself is read by whatever node is there, transformed, checked or declared: it is
|
|
1807
|
+
* the read's own. Above it, each segment is taken as DynamoDB takes it:
|
|
1808
|
+
*
|
|
1809
|
+
* - a struct's property, declared on the struct. One it doesn't declare isn't read at all, so any
|
|
1810
|
+
* value there reads, and neither can `__proto__` be judged. A struct that renames its keys as
|
|
1811
|
+
* it's stored (`Schema.encodeKeys`) reads a property by its stored form first, and can't be said
|
|
1812
|
+
* exactly;
|
|
1813
|
+
* - a record's value, under any key but `__proto__`, for a record of one signature keyed by
|
|
1814
|
+
* `Schema.String` (any other record can't be said exactly);
|
|
1815
|
+
* - a list's element. Below the end, an element that is there: the tuple's at that position, or
|
|
1816
|
+
* the rest's past them. At the end, DynamoDB writes past a list's last element at its end, so a
|
|
1817
|
+
* position is known only where it lands on one reader whatever the length: a position every
|
|
1818
|
+
* value holds, any position of an array, or one past the elements of a tuple holding nothing
|
|
1819
|
+
* optional and a rest;
|
|
1820
|
+
* - a union's members and a suspension's schema, each as itself.
|
|
1821
|
+
*
|
|
1822
|
+
* A field whose absent attribute the read takes as the value it stands for (`Schema.UndefinedOr`)
|
|
1823
|
+
* is the field: that default runs only where nothing is stored ({@link ReadsAbsentAs}).
|
|
1824
|
+
*
|
|
1825
|
+
* A way the path can't take (a property of a list or a scalar, a position of a map) adds nothing:
|
|
1826
|
+
* DynamoDB refuses the write on such a value.
|
|
1827
|
+
*/
|
|
1828
|
+
const readersBelow = (declared, segments, readers, seen = new Set()) => {
|
|
1829
|
+
const ast = asStored(declared);
|
|
1830
|
+
const [head, ...rest] = segments;
|
|
1831
|
+
if (head === undefined) {
|
|
1832
|
+
readers.push(ast);
|
|
1833
|
+
return true;
|
|
1834
|
+
}
|
|
1835
|
+
const node = seen.has(ast) ? undefined : modelled(ast);
|
|
1836
|
+
if (node === undefined)
|
|
1837
|
+
return false;
|
|
1838
|
+
if (node._tag === "Union" || node._tag === "Suspend") {
|
|
1839
|
+
const within = new Set(seen).add(ast);
|
|
1840
|
+
return (node._tag === "Union" ? node.members : [node.suspended]).every((member) => readersBelow(member, segments, readers, within));
|
|
1841
|
+
}
|
|
1842
|
+
if (node._tag === "Struct") {
|
|
1843
|
+
if (typeof head !== "string")
|
|
1844
|
+
return true;
|
|
1845
|
+
// A struct that renames its keys as it's stored reads each property by its stored form first,
|
|
1846
|
+
// which a reader of the property alone doesn't see (a `catchDecoding` reads what that refuses):
|
|
1847
|
+
// it can't be said exactly, as this walk never said it.
|
|
1848
|
+
if (isRenamedView.has(node.ast))
|
|
1849
|
+
return false;
|
|
1850
|
+
const property = node.ast.propertySignatures.find((signature) => signature.name === head);
|
|
1851
|
+
return property !== undefined && head !== "__proto__"
|
|
1852
|
+
? readersBelow(property.type, rest, readers)
|
|
1853
|
+
: false;
|
|
1854
|
+
}
|
|
1855
|
+
if (node._tag === "Record") {
|
|
1856
|
+
// Only the record this walk has always taken (one signature keyed by `Schema.String`): one of
|
|
1857
|
+
// several signatures, or keyed by a template literal, can't be said exactly.
|
|
1858
|
+
if (!recordOf140(node.signatures))
|
|
1859
|
+
return false;
|
|
1860
|
+
if (typeof head !== "string")
|
|
1861
|
+
return true;
|
|
1862
|
+
return head === "__proto__" ? false : readersBelow(node.signatures[0].type, rest, readers);
|
|
1863
|
+
}
|
|
1864
|
+
if (node._tag === "List") {
|
|
1865
|
+
if (typeof head !== "number")
|
|
1866
|
+
return true;
|
|
1867
|
+
const { elements, rest: tail } = node.ast;
|
|
1868
|
+
const required = elements.filter((element) => !SchemaAST.isOptional(element)).length;
|
|
1869
|
+
if (rest.length > 0 || head < required) {
|
|
1870
|
+
const element = elements[head] ?? (head >= elements.length ? tail[0] : undefined);
|
|
1871
|
+
return element === undefined ? true : readersBelow(element, rest, readers);
|
|
1872
|
+
}
|
|
1873
|
+
const element = required === elements.length && head >= elements.length ? tail[0] : undefined;
|
|
1874
|
+
return element === undefined ? false : readersBelow(element, rest, readers);
|
|
1875
|
+
}
|
|
1876
|
+
return true;
|
|
1877
|
+
};
|
|
1878
|
+
/**
|
|
1879
|
+
* The element reader of every list a value at the end of a path can be, through unions and
|
|
1880
|
+
* suspensions, collected into `readers`; `false` for a list that isn't one array (a tuple: where
|
|
1881
|
+
* added elements land depends on its length) or anything the walk doesn't model. A struct, a
|
|
1882
|
+
* record and a scalar add nothing: DynamoDB refuses to add elements to one.
|
|
1883
|
+
*/
|
|
1884
|
+
const elementReaders = (declared, readers, seen = new Set()) => {
|
|
1885
|
+
const ast = asStored(declared);
|
|
1886
|
+
const node = seen.has(ast) ? undefined : modelled(ast);
|
|
1887
|
+
if (node === undefined)
|
|
1888
|
+
return false;
|
|
1889
|
+
if (node._tag === "Union" || node._tag === "Suspend") {
|
|
1890
|
+
const within = new Set(seen).add(ast);
|
|
1891
|
+
return (node._tag === "Union" ? node.members : [node.suspended]).every((member) => elementReaders(member, readers, within));
|
|
1892
|
+
}
|
|
1893
|
+
// A record this walk never took (several signatures, a template literal key) can't be said.
|
|
1894
|
+
if (node._tag === "Record" && !recordOf140(node.signatures))
|
|
1895
|
+
return false;
|
|
1896
|
+
if (node._tag !== "List")
|
|
1897
|
+
return true;
|
|
1898
|
+
const [element, ...more] = node.ast.rest;
|
|
1899
|
+
if (node.ast.elements.length > 0 || element === undefined || more.length > 0)
|
|
1900
|
+
return false;
|
|
1901
|
+
readers.push(element);
|
|
1902
|
+
return true;
|
|
1903
|
+
};
|
|
1904
|
+
/**
|
|
1905
|
+
* What reads an operand an update writes at `segments` below a field whose READ schema is
|
|
1906
|
+
* `field`: for a value, the node at that position, one for each way the schema has to it; for
|
|
1907
|
+
* elements, each array's element there. `undefined` when the walk can't say exactly, or no value
|
|
1908
|
+
* holds the position (DynamoDB refuses such a write): the operand is then judged as 1.39.0
|
|
1909
|
+
* judged it.
|
|
1910
|
+
*
|
|
1911
|
+
* A row reads only where a reader reads what's written, so an operand none of them reads is one
|
|
1912
|
+
* no row holding it reads ({@link modelled} lists what the walk passes through; the rest is
|
|
1913
|
+
* `undefined`). An operand one of them reads may still be wrong for the member a row holds, as
|
|
1914
|
+
* it always was.
|
|
1915
|
+
*/
|
|
1916
|
+
export const readersAt = (field, segments, kind) => {
|
|
1917
|
+
const at = [];
|
|
1918
|
+
if (!readersBelow(field.ast, segments, at))
|
|
1919
|
+
return undefined;
|
|
1920
|
+
const readers = [];
|
|
1921
|
+
if (kind === "value")
|
|
1922
|
+
readers.push(...at);
|
|
1923
|
+
else if (!at.every((node) => elementReaders(node, readers)))
|
|
1924
|
+
return undefined;
|
|
1925
|
+
const distinct = [...new Set(readers)];
|
|
1926
|
+
return distinct.length === 0 ? undefined : distinct;
|
|
1927
|
+
};
|
|
212
1928
|
//# sourceMappingURL=SchemaAccessors.js.map
|