@effect-dynamodb/schema 1.38.0 → 1.40.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 +21 -2
- package/dist/Aggregate.js.map +1 -1
- package/dist/DynamoModel.d.ts +13 -0
- package/dist/DynamoModel.d.ts.map +1 -1
- package/dist/DynamoModel.js.map +1 -1
- package/dist/DynamoSchema.d.ts +33 -17
- package/dist/DynamoSchema.d.ts.map +1 -1
- package/dist/DynamoSchema.js +19 -13
- package/dist/DynamoSchema.js.map +1 -1
- package/dist/Entity.d.ts +121 -18
- package/dist/Entity.d.ts.map +1 -1
- package/dist/Entity.js +238 -2
- package/dist/Entity.js.map +1 -1
- package/dist/Errors.d.ts +0 -27
- package/dist/Errors.d.ts.map +1 -1
- package/dist/Errors.js +0 -24
- package/dist/Errors.js.map +1 -1
- package/dist/KeyComposer.d.ts +368 -14
- package/dist/KeyComposer.d.ts.map +1 -1
- package/dist/KeyComposer.js +835 -87
- package/dist/KeyComposer.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 +181 -14
- package/dist/internal/AggregateSchemas.d.ts.map +1 -1
- package/dist/internal/AggregateSchemas.js +505 -45
- 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 +274 -6
- package/dist/internal/CompositeCodec.d.ts.map +1 -1
- package/dist/internal/CompositeCodec.js +829 -25
- package/dist/internal/CompositeCodec.js.map +1 -1
- package/dist/internal/EffectInstances.d.ts +81 -0
- package/dist/internal/EffectInstances.d.ts.map +1 -0
- package/dist/internal/EffectInstances.js +656 -0
- package/dist/internal/EffectInstances.js.map +1 -0
- package/dist/internal/EntitySchemas.d.ts +103 -27
- package/dist/internal/EntitySchemas.d.ts.map +1 -1
- package/dist/internal/EntitySchemas.js +778 -131
- package/dist/internal/EntitySchemas.js.map +1 -1
- package/dist/internal/EntityTypes.d.ts +67 -11
- 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 +33 -0
- package/dist/internal/InstanceReads.d.ts.map +1 -0
- package/dist/internal/InstanceReads.js +165 -0
- package/dist/internal/InstanceReads.js.map +1 -0
- package/dist/internal/KeyText136.d.ts +58 -131
- package/dist/internal/KeyText136.d.ts.map +1 -1
- package/dist/internal/KeyText136.js +154 -264
- package/dist/internal/KeyText136.js.map +1 -1
- package/dist/internal/NumericKey.d.ts +29 -2
- package/dist/internal/NumericKey.d.ts.map +1 -1
- package/dist/internal/NumericKey.js +64 -1
- package/dist/internal/NumericKey.js.map +1 -1
- package/dist/internal/PackedCodec.d.ts +8 -1
- package/dist/internal/PackedCodec.d.ts.map +1 -1
- package/dist/internal/PackedCodec.js +49 -13
- package/dist/internal/PackedCodec.js.map +1 -1
- package/dist/internal/SchemaAccessors.d.ts +160 -1
- package/dist/internal/SchemaAccessors.d.ts.map +1 -1
- package/dist/internal/SchemaAccessors.js +994 -1
- package/dist/internal/SchemaAccessors.js.map +1 -1
- package/dist/internal/Shards.d.ts +47 -0
- package/dist/internal/Shards.d.ts.map +1 -0
- package/dist/internal/Shards.js +60 -0
- package/dist/internal/Shards.js.map +1 -0
- package/dist/internal/SharedKeyAttribute.d.ts +102 -0
- package/dist/internal/SharedKeyAttribute.d.ts.map +1 -0
- package/dist/internal/SharedKeyAttribute.js +288 -0
- package/dist/internal/SharedKeyAttribute.js.map +1 -0
- 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/package.json +2 -1
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Extracted from Entity.ts for decomposition. Not part of the public API.
|
|
5
5
|
*/
|
|
6
|
-
import { DateTime, Effect, Redacted, Schema, SchemaAST, SchemaGetter, SchemaIssue } from "effect";
|
|
6
|
+
import { DateTime, Effect, Option, Redacted, Schema, SchemaAST, SchemaGetter, SchemaIssue, } from "effect";
|
|
7
7
|
import { getEncoding, isConfiguredModel, isHidden, } from "../DynamoModel.js";
|
|
8
|
-
import {
|
|
8
|
+
import { reviveMarshalledDateTime } from "./EffectInstances.js";
|
|
9
|
+
import { readingInstances } from "./InstanceReads.js";
|
|
10
|
+
import { BETA_DATE_MARKER, DATE_MARKER } from "./PackedCodec.js";
|
|
11
|
+
import { absentStored, firstTypeParameterAst, getSchemaFields, holdsAbsentValue, holdsPlainBigint, holdsUndefined, isDecodingDefault, isSchemaLike, lackDecision, numericTypeWithStringEncoding, storedList, transformWireKind as transformWireKindImpl, } from "./SchemaAccessors.js";
|
|
9
12
|
import { parseZonedIso } from "./ZonedIso.js";
|
|
10
13
|
/**
|
|
11
14
|
* Resolve the effective DynamoEncoding for a model-declared field, applying
|
|
@@ -299,26 +302,42 @@ export const isSelfDateSchema = (schema) => {
|
|
|
299
302
|
* the inner AST with an optional context, so it needs no case of its own. The
|
|
300
303
|
* tolerant `substituteSchemaDeep` walks the same containers (see
|
|
301
304
|
* `walkedContainer`), except `Suspend`: a recursive schema keeps its own
|
|
302
|
-
* codec, so the aggregate encodes it through its own `encode`.
|
|
305
|
+
* codec, so the aggregate encodes it through its own `encode`. A declaration's
|
|
306
|
+
* type parameters are walked too: `Schema.Option(Schema.DateFromString)` stores
|
|
307
|
+
* a string only if it's encoded, and `holdsTransformation` lets it be taken.
|
|
303
308
|
*
|
|
304
309
|
* `hasEncodingTransformation` asks the same question of the TOP-LEVEL node
|
|
305
310
|
* only, and an `Arrays` / `Objects` node never carries an encoding itself — so a
|
|
306
311
|
* `Schema.Array(DateTimeUtcFromString)` attribute was treated as identity and
|
|
307
312
|
* its `DateTime`s were marshalled as maps.
|
|
313
|
+
*
|
|
314
|
+
* `throughClasses` looks through a `Schema.Class`'s own step, onto the struct of its fields, and
|
|
315
|
+
* walks those: a class whose fields hold none of the above reads back as it was given.
|
|
308
316
|
*/
|
|
309
|
-
export const containsWireTransform = (schema) => astContainsWireTransform(schema.ast, new Set());
|
|
310
|
-
|
|
317
|
+
export const containsWireTransform = (schema, options) => astContainsWireTransform(schema.ast, new Set(), options?.throughClasses === true);
|
|
318
|
+
/** A `Schema.Class`'s struct, where its only encoding step is the class's own; undefined otherwise. */
|
|
319
|
+
const classStepStruct = (ast) => {
|
|
320
|
+
if (!SchemaAST.isDeclaration(ast) || ast.encoding?.length !== 1)
|
|
321
|
+
return undefined;
|
|
322
|
+
const to = ast.encoding[0].to;
|
|
323
|
+
return SchemaAST.isObjects(to) && ast.typeParameters[0] === to ? to : undefined;
|
|
324
|
+
};
|
|
325
|
+
const astContainsWireTransform = (ast, seen, throughClasses) => {
|
|
311
326
|
if (seen.has(ast))
|
|
312
327
|
return false;
|
|
313
328
|
seen.add(ast);
|
|
314
|
-
if (ast.encoding !== undefined)
|
|
315
|
-
|
|
329
|
+
if (ast.encoding !== undefined) {
|
|
330
|
+
const struct = throughClasses ? classStepStruct(ast) : undefined;
|
|
331
|
+
return struct === undefined || astContainsWireTransform(struct, seen, throughClasses);
|
|
332
|
+
}
|
|
316
333
|
if (SchemaAST.isDeclaration(ast)) {
|
|
317
334
|
const resolved = SchemaAST.resolve(ast);
|
|
318
335
|
const rep = resolved?.representation;
|
|
319
|
-
return matchDateRepresentation(resolved) !== undefined ||
|
|
336
|
+
return (matchDateRepresentation(resolved) !== undefined ||
|
|
337
|
+
rep?.id === "effect/schema/Redacted" ||
|
|
338
|
+
ast.typeParameters.some((parameter) => astContainsWireTransform(parameter, seen, throughClasses)));
|
|
320
339
|
}
|
|
321
|
-
const walk = (child) => astContainsWireTransform(child, seen);
|
|
340
|
+
const walk = (child) => astContainsWireTransform(child, seen, throughClasses);
|
|
322
341
|
if (SchemaAST.isArrays(ast))
|
|
323
342
|
return ast.elements.some(walk) || ast.rest.some(walk);
|
|
324
343
|
if (SchemaAST.isObjects(ast)) {
|
|
@@ -358,55 +377,6 @@ const isPlainObject = (value) => {
|
|
|
358
377
|
* `DateTime` API that relies on the prototype.
|
|
359
378
|
*/
|
|
360
379
|
const isGenuineDateTime = (value) => DateTime.isDateTime(value) && !isPlainObject(value);
|
|
361
|
-
const reviveTimeZone = (zone) => {
|
|
362
|
-
if (!isPlainObject(zone))
|
|
363
|
-
return undefined;
|
|
364
|
-
try {
|
|
365
|
-
if (zone._tag === "Named" && typeof zone.id === "string") {
|
|
366
|
-
return DateTime.zoneMakeNamedUnsafe(zone.id);
|
|
367
|
-
}
|
|
368
|
-
if (zone._tag === "Offset" && typeof zone.offset === "number") {
|
|
369
|
-
return DateTime.zoneMakeOffset(zone.offset);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
catch {
|
|
373
|
-
return undefined;
|
|
374
|
-
}
|
|
375
|
-
return undefined;
|
|
376
|
-
};
|
|
377
|
-
/**
|
|
378
|
-
* Rebuild a `DateTime` from a MARSHALLED instance (#133).
|
|
379
|
-
*
|
|
380
|
-
* Before #133 an aggregate stored some `DateTime`s by marshalling the instance
|
|
381
|
-
* itself, which keeps its own enumerable properties:
|
|
382
|
-
*
|
|
383
|
-
* - `Utc`: `{ epochMilliseconds, <type-id>, _tag: "Utc" }`
|
|
384
|
-
* - `Zoned`: the same plus `zone`, itself `{ id, _tag: "Named" }` or
|
|
385
|
-
* `{ offset, _tag: "Offset" }` (a named zone's `Intl` formatter marshals to
|
|
386
|
-
* an empty map and is not needed).
|
|
387
|
-
*
|
|
388
|
-
* The type-id key is `~effect/time/DateTime` on effect 4.0.0-rc and
|
|
389
|
-
* `~effect/DateTime` on 4.0.0, so it is deliberately NOT inspected: the instant
|
|
390
|
-
* and the tag are what identify the value, and they are all a `DateTime` holds.
|
|
391
|
-
* A zoned value is rebuilt only when its zone is recoverable.
|
|
392
|
-
*
|
|
393
|
-
* Returns `undefined` for anything else — including a genuine `DateTime`, which
|
|
394
|
-
* needs no rebuilding.
|
|
395
|
-
*/
|
|
396
|
-
export const reviveMarshalledDateTime = (value) => {
|
|
397
|
-
if (!isPlainObject(value))
|
|
398
|
-
return undefined;
|
|
399
|
-
const epochMs = value.epochMilliseconds;
|
|
400
|
-
if (typeof epochMs !== "number" || !Number.isFinite(epochMs))
|
|
401
|
-
return undefined;
|
|
402
|
-
if (value._tag === "Utc")
|
|
403
|
-
return DateTime.makeUnsafe(epochMs);
|
|
404
|
-
if (value._tag === "Zoned") {
|
|
405
|
-
const timeZone = reviveTimeZone(value.zone);
|
|
406
|
-
return timeZone === undefined ? undefined : DateTime.makeZonedUnsafe(epochMs, { timeZone });
|
|
407
|
-
}
|
|
408
|
-
return undefined;
|
|
409
|
-
};
|
|
410
380
|
/**
|
|
411
381
|
* Convert any input value (string / number / DateTime / Date) to the wire
|
|
412
382
|
* primitive for a given encoding. Used by date substitutes' `encode` path.
|
|
@@ -628,9 +598,12 @@ const tryGetRedactedInner = (schema) => {
|
|
|
628
598
|
* branch logic in `decode`/`encode` then steers the value to the right
|
|
629
599
|
* representation.
|
|
630
600
|
*/
|
|
631
|
-
const buildRedactedSubstitute = (inner) => {
|
|
601
|
+
const buildRedactedSubstitute = (schema, inner) => {
|
|
602
|
+
const resolved = SchemaAST.resolve(schema.ast);
|
|
603
|
+
const declared = resolved?.representation?.payload?.label;
|
|
604
|
+
const label = typeof declared === "string" ? declared : undefined;
|
|
632
605
|
return Schema.Any.pipe(Schema.decodeTo(Schema.Redacted(inner), {
|
|
633
|
-
decode: SchemaGetter.transform((value) => Redacted.isRedacted(value) ? value : Redacted.make(value)),
|
|
606
|
+
decode: SchemaGetter.transform((value) => Redacted.isRedacted(value) ? value : Redacted.make(value, { label })),
|
|
634
607
|
encode: SchemaGetter.transform((r) => Redacted.isRedacted(r) ? Redacted.value(r) : r),
|
|
635
608
|
}));
|
|
636
609
|
};
|
|
@@ -646,15 +619,30 @@ const schemaFieldsOf = (schema) => schema.fields;
|
|
|
646
619
|
const isLeafEncodingTransform = (schema) => schemaFieldsOf(schema) === undefined &&
|
|
647
620
|
!isArraySchema(schema) &&
|
|
648
621
|
transformWireKindImpl(schema) !== undefined;
|
|
622
|
+
/**
|
|
623
|
+
* What an aggregate takes in either form: any transformation that isn't a class or a struct, whose
|
|
624
|
+
* fields are walked, whatever it's stored as, such as `Schema.Defect()`, a `Schema.toCodecJson` over
|
|
625
|
+
* a map, a map decoded from a record, `Schema.OptionFromNullOr` or a list held as JSON. A step
|
|
626
|
+
* encoded as optional has been unwrapped by now (`decodingDefaultField`), and it has to keep filling
|
|
627
|
+
* a missing key. A list whose elements are substituted is walked from its type side, whatever it's
|
|
628
|
+
* stored as, as an entity's is: what that stores decides what's in rows already written.
|
|
629
|
+
*/
|
|
630
|
+
const isTolerantLeaf = (schema) => {
|
|
631
|
+
if (schemaFieldsOf(schema) !== undefined || schema.ast.encoding === undefined)
|
|
632
|
+
return false;
|
|
633
|
+
if (!SchemaAST.isArrays(schema.ast) || schema.ast.elements.length > 0)
|
|
634
|
+
return true;
|
|
635
|
+
const element = arrayElementOf(schema);
|
|
636
|
+
return element === undefined || !needsDeepSubstitution(element, { tolerantTransforms: true });
|
|
637
|
+
};
|
|
649
638
|
/**
|
|
650
639
|
* Build a TOLERANT substitute for an arbitrary leaf transform — the general
|
|
651
640
|
* form of what `buildDateTransform` does for dates.
|
|
652
641
|
*
|
|
653
|
-
* Only reachable under `tolerantTransforms`, i.e. only from
|
|
654
|
-
* `
|
|
655
|
-
*
|
|
656
|
-
*
|
|
657
|
-
* string). A strict transform accepts only the latter, so any update on such a
|
|
642
|
+
* Only reachable under `tolerantTransforms`, i.e. only from an aggregate's
|
|
643
|
+
* schemas. An aggregate's `create` and `update` decode what a caller holds, the
|
|
644
|
+
* domain value (a `bigint` for a `Schema.BigIntFromString`), where a strict
|
|
645
|
+
* transform accepts only the wire value (the string), so any update on such a
|
|
658
646
|
* model failed before an item was ever built — regardless of which field the
|
|
659
647
|
* mutation touched.
|
|
660
648
|
*
|
|
@@ -662,16 +650,57 @@ const isLeafEncodingTransform = (schema) => schemaFieldsOf(schema) === undefined
|
|
|
662
650
|
* validates the value against the transform's own TYPE side (domain form). A
|
|
663
651
|
* value that is neither is still rejected, exactly as before — this widens the
|
|
664
652
|
* accepted input, it does not skip validation.
|
|
653
|
+
*
|
|
654
|
+
* `domainFirst` asks the domain side first, for a value a caller holds (see `domainValues`). What
|
|
655
|
+
* both sides take is the domain value, and comes out as what reading its stored form gives, as it
|
|
656
|
+
* would from an entity: a transformation lowercasing on decode gives the same text either way.
|
|
657
|
+
*
|
|
658
|
+
* `plainForm` also takes what a mutation's plain state holds for that domain (see `plainStateForms`),
|
|
659
|
+
* asked before the stored side: a set held as a list is a domain value, even where what's stored is a list.
|
|
660
|
+
*
|
|
661
|
+
* `domainStored`: for a type side that takes anything (`takesAnything`), any value it takes is a
|
|
662
|
+
* domain value only where the transform stores it, whether or not the transform also reads it as
|
|
663
|
+
* stored. `Unknown` takes another union member's value, a `many` element its ref fetch changed, or a
|
|
664
|
+
* field's stored form; 1.39.0 read those through the member's own struct or the transform, or refused
|
|
665
|
+
* them, and `create` returns what it did.
|
|
665
666
|
*/
|
|
666
|
-
const buildTolerantTransform = (schema) => {
|
|
667
|
+
const buildTolerantTransform = (schema, domainFirst = false, plainForm, domainStored = false) => {
|
|
667
668
|
const typeSide = Schema.make(SchemaAST.toType(schema.ast));
|
|
668
669
|
const codec = schema;
|
|
669
670
|
const decodeWire = Schema.decodeUnknownOption(codec);
|
|
670
|
-
const validateDomain = Schema.decodeUnknownOption(typeSide);
|
|
671
671
|
const encodeDomain = Schema.encodeUnknownOption(codec);
|
|
672
|
+
const validateType = Schema.decodeUnknownOption(typeSide);
|
|
673
|
+
// A transform's `encode` is the caller's own code, written for its domain values only.
|
|
674
|
+
const stores = (value) => {
|
|
675
|
+
try {
|
|
676
|
+
return encodeDomain(value)._tag === "Some";
|
|
677
|
+
}
|
|
678
|
+
catch {
|
|
679
|
+
return false;
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
const validateDomain = (value) => {
|
|
683
|
+
const typed = validateType(value);
|
|
684
|
+
return domainStored && typed._tag === "Some" && !stores(typed.value) ? Option.none() : typed;
|
|
685
|
+
};
|
|
672
686
|
const bigintDomain = SchemaAST.isBigInt(SchemaAST.toType(schema.ast));
|
|
687
|
+
const decodePlain = plainForm === undefined
|
|
688
|
+
? undefined
|
|
689
|
+
: Schema.decodeUnknownOption(Schema.toCodecIso(plainForm));
|
|
673
690
|
return Schema.Any.pipe(Schema.decodeTo(typeSide, {
|
|
674
691
|
decode: SchemaGetter.transformEffect((value) => {
|
|
692
|
+
if (domainFirst) {
|
|
693
|
+
const given = validateDomain(reviveMarshalledDateTime(value) ?? value);
|
|
694
|
+
if (given._tag === "Some") {
|
|
695
|
+
const read = decodeWire(value)._tag === "Some"
|
|
696
|
+
? Option.flatMap(encodeDomain(given.value), decodeWire)
|
|
697
|
+
: given;
|
|
698
|
+
return Effect.succeed(read._tag === "Some" ? read.value : given.value);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
const plain = decodePlain?.(value);
|
|
702
|
+
if (plain?._tag === "Some")
|
|
703
|
+
return Effect.succeed(plain.value);
|
|
675
704
|
const fromWire = decodeWire(value);
|
|
676
705
|
if (fromWire._tag === "Some")
|
|
677
706
|
return Effect.succeed(fromWire.value);
|
|
@@ -911,12 +940,14 @@ export const optionalField = (field) => {
|
|
|
911
940
|
// `Schema.optional(X)`: the value type is `UndefinedOr(X)`; `.members` exposes
|
|
912
941
|
// the real X alongside the `Undefined` member.
|
|
913
942
|
const members = value?.members;
|
|
914
|
-
const
|
|
943
|
+
const defined = members?.filter((m) => m.ast._tag !== "Undefined");
|
|
944
|
+
const inner = defined?.[0];
|
|
915
945
|
if (inner === undefined)
|
|
916
946
|
return undefined;
|
|
917
947
|
return {
|
|
918
948
|
inner,
|
|
919
949
|
rewrap: (s) => Schema.optional(s),
|
|
950
|
+
...(defined !== undefined && defined.length > 1 ? { union: value } : {}),
|
|
920
951
|
};
|
|
921
952
|
}
|
|
922
953
|
// `Schema.optionalKey(X)`: the value type is X directly (`.schema`).
|
|
@@ -932,15 +963,27 @@ const descend = (opts) => opts?.tolerantTransforms ||
|
|
|
932
963
|
opts?.resolveRef ||
|
|
933
964
|
opts?.strictWireKind ||
|
|
934
965
|
opts?.legacyReads ||
|
|
966
|
+
opts?.exactBigInts ||
|
|
967
|
+
opts?.decodeOnly ||
|
|
935
968
|
opts?.path ||
|
|
936
|
-
opts?.enforceChecks
|
|
969
|
+
opts?.enforceChecks ||
|
|
970
|
+
opts?.absentReads ||
|
|
971
|
+
opts?.plainStateForms ||
|
|
972
|
+
opts?.domainValues ||
|
|
973
|
+
opts?.undefinedPositionsOnly
|
|
937
974
|
? {
|
|
938
975
|
...(opts?.tolerantTransforms ? { tolerantTransforms: true } : {}),
|
|
939
976
|
...(opts?.resolveRef ? { resolveRef: opts.resolveRef } : {}),
|
|
940
977
|
...(opts?.strictWireKind ? { strictWireKind: true } : {}),
|
|
941
978
|
...(opts?.legacyReads ? { legacyReads: true } : {}),
|
|
979
|
+
...(opts?.exactBigInts ? { exactBigInts: true } : {}),
|
|
980
|
+
...(opts?.decodeOnly ? { decodeOnly: true } : {}),
|
|
942
981
|
...(opts?.path ? { path: opts.path } : {}),
|
|
943
982
|
...(opts?.enforceChecks ? { enforceChecks: true } : {}),
|
|
983
|
+
...(opts?.absentReads ? { absentReads: opts.absentReads } : {}),
|
|
984
|
+
...(opts?.plainStateForms ? { plainStateForms: true } : {}),
|
|
985
|
+
...(opts?.domainValues ? { domainValues: true } : {}),
|
|
986
|
+
...(opts?.undefinedPositionsOnly ? { undefinedPositionsOnly: true } : {}),
|
|
944
987
|
}
|
|
945
988
|
: undefined;
|
|
946
989
|
/** Options for a struct / class field: the inherited ones, with the field on the path. */
|
|
@@ -1053,6 +1096,10 @@ const containerShape = (schema) => {
|
|
|
1053
1096
|
}
|
|
1054
1097
|
return undefined;
|
|
1055
1098
|
};
|
|
1099
|
+
/** Whether a container holds nothing but dates, `Null` and `Undefined`: what an aggregate's date encoder takes. */
|
|
1100
|
+
const holdsOnlyDates = (container) => container.children.every((member) => SchemaAST.isNull(member.ast) ||
|
|
1101
|
+
SchemaAST.isUndefined(member.ast) ||
|
|
1102
|
+
inferDefaultEncoding(member) !== undefined);
|
|
1056
1103
|
/** Options for a tolerant container's children: a Union's members decode strictly by kind. */
|
|
1057
1104
|
const childOptions = (container, deeper, index, opts) => {
|
|
1058
1105
|
if (!container.isUnion)
|
|
@@ -1110,15 +1157,32 @@ const decodingDefaultField = (field) => {
|
|
|
1110
1157
|
const withUndefined = SchemaAST.isUnion(lastTo) && lastTo.types.some((t) => SchemaAST.isUndefined(t));
|
|
1111
1158
|
return {
|
|
1112
1159
|
inner,
|
|
1113
|
-
|
|
1160
|
+
transforms: !isDecodingDefault(ast, lastTo) || lastTo.encoding !== undefined,
|
|
1161
|
+
declared: field.to ?? inner,
|
|
1162
|
+
defaults: isDecodingDefault(SchemaAST.toEncoded(innerAst), lastTo),
|
|
1163
|
+
withUndefined,
|
|
1164
|
+
rewrap: (substitute, decode = transformation.decode) => {
|
|
1114
1165
|
const encodedSide = Schema.toEncoded(substitute);
|
|
1115
1166
|
const optionalEncoded = withUndefined
|
|
1116
1167
|
? Schema.optional(encodedSide)
|
|
1117
1168
|
: Schema.optionalKey(encodedSide);
|
|
1118
|
-
return optionalEncoded.pipe(Schema.decodeTo(substitute,
|
|
1169
|
+
return optionalEncoded.pipe(Schema.decodeTo(substitute, {
|
|
1170
|
+
decode,
|
|
1171
|
+
encode: transformation.encode,
|
|
1172
|
+
}));
|
|
1119
1173
|
},
|
|
1120
1174
|
};
|
|
1121
1175
|
};
|
|
1176
|
+
/**
|
|
1177
|
+
* Whether this substitution is an entity's or an aggregate's write, which substitutes only the first
|
|
1178
|
+
* member of a union under `Schema.optionalKey`. Later members are written as declared, so what's
|
|
1179
|
+
* stored for them stays what every release has stored: a `Schema.DateTimeUtc` declared after a
|
|
1180
|
+
* `Schema.String` is Effect's internal form, and one declared before it is a string. Changing that
|
|
1181
|
+
* would change a stored format. A read takes each member's forms in the order they're declared
|
|
1182
|
+
* (`declaredOrder`), as a later date isn't stored as the text a string before it takes, and a number
|
|
1183
|
+
* under a first `Schema.BigInt` was written as that bigint.
|
|
1184
|
+
*/
|
|
1185
|
+
const writesFirstMemberOnly = (opts) => !opts?.decodeOnly && !opts?.absentReads && !opts?.undefinedPositionsOnly;
|
|
1122
1186
|
const throughOptional = (opts) => {
|
|
1123
1187
|
const deeper = descend(opts);
|
|
1124
1188
|
return opts?.unionDateOverride || opts?.collidingKinds || opts?.dateEncodingOverride
|
|
@@ -1198,10 +1262,146 @@ const needsLegacyRead = (schema, opts) => opts?.legacyReads === true &&
|
|
|
1198
1262
|
schemaFieldsOf(schema) === undefined &&
|
|
1199
1263
|
!isArraySchema(schema) &&
|
|
1200
1264
|
(isDateTransform(schema) || numericTypeWithStringEncoding(schema));
|
|
1201
|
-
/** Whether a plain `Schema.BigInt`
|
|
1202
|
-
const
|
|
1203
|
-
|
|
1204
|
-
|
|
1265
|
+
/** Whether a plain `Schema.BigInt` gets the read lift here (aggregate decode, entity reads). */
|
|
1266
|
+
const liftsBigInts = (opts) => (opts?.tolerantTransforms === true || opts?.legacyReads === true) && opts.exactBigInts !== true;
|
|
1267
|
+
const needsBigIntLift = (schema, opts) => liftsBigInts(opts) && isSelfBigInt(schema);
|
|
1268
|
+
/** Whether what's stored for a schema the substitution can't walk holds a plain `Schema.BigInt`. */
|
|
1269
|
+
const holdsUnwalkedBigInt = (schema) => holdsPlainBigint(SchemaAST.toEncoded(schema.ast));
|
|
1270
|
+
/**
|
|
1271
|
+
* A list with a step on it that an entity writes through as declared, which is one with no date or
|
|
1272
|
+
* redacted value on its type side. A read walking that type side would skip the step, so it finds what
|
|
1273
|
+
* the step makes where the stored form is. This reads it as declared, with its bigints lifted, unless
|
|
1274
|
+
* nothing's in the way of the walk: a list stored as one, with no plain bigint on either side.
|
|
1275
|
+
*/
|
|
1276
|
+
const readsListAsDeclared = (schema, opts) => {
|
|
1277
|
+
if (opts?.legacyReads !== true)
|
|
1278
|
+
return false;
|
|
1279
|
+
const { ast } = schema;
|
|
1280
|
+
if (!SchemaAST.isArrays(ast) || ast.encoding === undefined)
|
|
1281
|
+
return false;
|
|
1282
|
+
if (needsDeepSubstitution(schema, undefined))
|
|
1283
|
+
return false;
|
|
1284
|
+
return (storedList(ast.encoding[ast.encoding.length - 1].to) === undefined ||
|
|
1285
|
+
holdsUnwalkedBigInt(schema) ||
|
|
1286
|
+
[...ast.elements, ...ast.rest].some((element) => holdsPlainBigint(SchemaAST.toEncoded(element))));
|
|
1287
|
+
};
|
|
1288
|
+
/** Whether what's stored for a schema the substitution can't walk can leave a value out. */
|
|
1289
|
+
const holdsUnwalkedAbsent = (schema) => holdsAbsentValue(SchemaAST.toEncoded(schema.ast));
|
|
1290
|
+
/** Whether a plain `Schema.BigInt` is anywhere the substitution walks. */
|
|
1291
|
+
const holdsBigInt = (schema) => {
|
|
1292
|
+
const opt = optionalField(schema);
|
|
1293
|
+
const defaulted = decodingDefaultField(schema);
|
|
1294
|
+
// What's beneath a step that isn't a default isn't what's stored, and it isn't walked.
|
|
1295
|
+
if (defaulted !== undefined && !defaulted.defaults)
|
|
1296
|
+
return holdsUnwalkedBigInt(schema);
|
|
1297
|
+
const inner = opt?.union ?? opt?.inner ?? defaulted?.declared;
|
|
1298
|
+
if (inner !== undefined)
|
|
1299
|
+
return holdsBigInt(inner);
|
|
1300
|
+
if (isSelfBigInt(schema))
|
|
1301
|
+
return true;
|
|
1302
|
+
const container = walkedContainer(schema);
|
|
1303
|
+
if (container !== undefined)
|
|
1304
|
+
return container.children.some((child) => holdsBigInt(child));
|
|
1305
|
+
if (isArraySchema(schema)) {
|
|
1306
|
+
const element = arrayElementOf(schema);
|
|
1307
|
+
return element !== undefined && holdsBigInt(element);
|
|
1308
|
+
}
|
|
1309
|
+
const fields = schemaFieldsOf(schema);
|
|
1310
|
+
return fields === undefined
|
|
1311
|
+
? holdsUnwalkedBigInt(schema)
|
|
1312
|
+
: Object.values(fields).some((field) => holdsBigInt(field));
|
|
1313
|
+
};
|
|
1314
|
+
/**
|
|
1315
|
+
* What the substitution works on beneath a decoding default (see `decodeOnly`). A read doesn't
|
|
1316
|
+
* enforce a check on what it rebuilds (see `enforceChecks`), and a write schema keeps the one added
|
|
1317
|
+
* after the default.
|
|
1318
|
+
*/
|
|
1319
|
+
const beneathDefault = (schema, defaulted, opts) => (opts?.legacyReads || opts?.decodeOnly) &&
|
|
1320
|
+
!(opts.enforceChecks && defaulted.declared.ast.checks !== schema.ast.checks)
|
|
1321
|
+
? defaulted.declared
|
|
1322
|
+
: defaulted.inner;
|
|
1323
|
+
/**
|
|
1324
|
+
* What an absent field reads as, for what asks for the domain value first (see `domainValues`), and
|
|
1325
|
+
* anything else as it was given. A decoding default is in the stored form and a step makes something
|
|
1326
|
+
* else of nothing at all, so absent is read through the field as declared, and what's beneath gets
|
|
1327
|
+
* the domain value that gives.
|
|
1328
|
+
*/
|
|
1329
|
+
const absentAsDeclared = (schema, withUndefined) => {
|
|
1330
|
+
const read = Schema.decodeUnknownEffect(Schema.Struct({ field: schema }));
|
|
1331
|
+
return SchemaGetter.transformOptionalEffect((input) => Option.isSome(input) && !(withUndefined && input.value === undefined)
|
|
1332
|
+
? Effect.succeed(input)
|
|
1333
|
+
: read({}).pipe(Effect.map(({ field }) => Option.some(field)), Effect.mapError((error) => error.issue)));
|
|
1334
|
+
};
|
|
1335
|
+
/**
|
|
1336
|
+
* The field, reading an absent attribute as the value it stands for (`absentStored`), which is what
|
|
1337
|
+
* its `declared` schema says, if the field is that schema read some other way.
|
|
1338
|
+
*/
|
|
1339
|
+
const readingAbsent = (field, declared = field) => {
|
|
1340
|
+
const reading = absentStored(declared);
|
|
1341
|
+
if (reading === undefined)
|
|
1342
|
+
return field;
|
|
1343
|
+
return field.pipe(Schema.withDecodingDefaultKey(Effect.succeed(reading.stored)));
|
|
1344
|
+
};
|
|
1345
|
+
/** Whether a struct field anywhere in the schema reads an absent attribute as a value. */
|
|
1346
|
+
const holdsAbsentRead = (schema) => {
|
|
1347
|
+
const opt = optionalField(schema);
|
|
1348
|
+
if (opt !== undefined)
|
|
1349
|
+
return holdsAbsentRead(opt.union ?? opt.inner);
|
|
1350
|
+
const defaulted = decodingDefaultField(schema);
|
|
1351
|
+
if (defaulted !== undefined)
|
|
1352
|
+
return holdsAbsentRead(defaulted.inner);
|
|
1353
|
+
const container = walkedContainer(schema);
|
|
1354
|
+
if (container !== undefined)
|
|
1355
|
+
return container.children.some((child) => holdsAbsentRead(child));
|
|
1356
|
+
if (isArraySchema(schema)) {
|
|
1357
|
+
const element = arrayElementOf(schema);
|
|
1358
|
+
return element !== undefined && holdsAbsentRead(element);
|
|
1359
|
+
}
|
|
1360
|
+
const fields = schemaFieldsOf(schema);
|
|
1361
|
+
return (fields !== undefined &&
|
|
1362
|
+
Object.values(fields).some((field) => absentStored(field) !== undefined || holdsAbsentRead(field)));
|
|
1363
|
+
};
|
|
1364
|
+
/** Whether a tuple has a fixed position that can hold `undefined`. */
|
|
1365
|
+
const hasUndefinedPosition = (schema) => SchemaAST.isArrays(schema.ast) && schema.ast.elements.some((element) => holdsUndefined(element));
|
|
1366
|
+
/** Whether a tuple with a position that can hold `undefined` is anywhere in the schema. */
|
|
1367
|
+
const holdsUndefinedPosition = (schema) => {
|
|
1368
|
+
const opt = optionalField(schema);
|
|
1369
|
+
const inner = opt?.union ?? opt?.inner ?? decodingDefaultField(schema)?.inner;
|
|
1370
|
+
if (inner !== undefined)
|
|
1371
|
+
return holdsUndefinedPosition(inner);
|
|
1372
|
+
if (hasUndefinedPosition(schema))
|
|
1373
|
+
return true;
|
|
1374
|
+
const container = walkedContainer(schema);
|
|
1375
|
+
if (container !== undefined) {
|
|
1376
|
+
return container.children.some((child) => holdsUndefinedPosition(child));
|
|
1377
|
+
}
|
|
1378
|
+
if (isArraySchema(schema)) {
|
|
1379
|
+
const element = arrayElementOf(schema);
|
|
1380
|
+
return element !== undefined && holdsUndefinedPosition(element);
|
|
1381
|
+
}
|
|
1382
|
+
const fields = schemaFieldsOf(schema);
|
|
1383
|
+
return (fields !== undefined && Object.values(fields).some((field) => holdsUndefinedPosition(field)));
|
|
1384
|
+
};
|
|
1385
|
+
/**
|
|
1386
|
+
* Marshalling drops `undefined` from a list, closing it up. In a tuple that moves
|
|
1387
|
+
* the values after it to other positions, or leaves a required position empty, so
|
|
1388
|
+
* the row reads back wrong or not at all. Only a last optional position can go, and
|
|
1389
|
+
* a position in a tuple's rest, which is a list.
|
|
1390
|
+
*/
|
|
1391
|
+
const refusingUndefinedPositions = (tuple) => {
|
|
1392
|
+
const optional = tuple.ast.elements.map((element) => SchemaAST.isOptional(element));
|
|
1393
|
+
return tuple.check(Schema.makeFilter((values) => {
|
|
1394
|
+
const position = values.findIndex((value, index) => value === undefined &&
|
|
1395
|
+
index < optional.length &&
|
|
1396
|
+
(optional[index] !== true ||
|
|
1397
|
+
values.slice(index + 1).some((later) => later !== undefined)));
|
|
1398
|
+
return (position === -1 ||
|
|
1399
|
+
`Position ${position} holds undefined, and a list can't keep an empty position: ` +
|
|
1400
|
+
"the values after it would move, or the row couldn't be read");
|
|
1401
|
+
}));
|
|
1402
|
+
};
|
|
1403
|
+
/** Whether a substitution of this schema refuses what its tuple positions can't keep. */
|
|
1404
|
+
const refusesUndefinedPositions = (schema, opts) => opts?.enforceChecks === true && opts.absentReads !== "only" && hasUndefinedPosition(schema);
|
|
1205
1405
|
/**
|
|
1206
1406
|
* Recursively determine whether a schema contains — at any depth reachable
|
|
1207
1407
|
* through nested `Schema.Struct` / `Schema.Class` / `Schema.Array` — a leaf that
|
|
@@ -1216,6 +1416,14 @@ const needsBigIntLift = (schema, opts) => (opts?.tolerantTransforms === true ||
|
|
|
1216
1416
|
const needsDeepSubstitution = (schema, opts) => {
|
|
1217
1417
|
if (schema == null || schema.ast == null)
|
|
1218
1418
|
return false;
|
|
1419
|
+
if (opts?.undefinedPositionsOnly)
|
|
1420
|
+
return holdsUndefinedPosition(schema);
|
|
1421
|
+
if (opts?.absentReads === "only")
|
|
1422
|
+
return holdsAbsentRead(schema);
|
|
1423
|
+
if (opts?.absentReads && holdsUnwalkedAbsent(schema))
|
|
1424
|
+
return true;
|
|
1425
|
+
if (opts?.enforceChecks && hasUndefinedPosition(schema))
|
|
1426
|
+
return true;
|
|
1219
1427
|
// `tolerantTransforms` AND `resolveRef` propagate through the recursion;
|
|
1220
1428
|
// `skipTopLevel` names top-level fields only, so it is dropped below.
|
|
1221
1429
|
//
|
|
@@ -1223,7 +1431,7 @@ const needsDeepSubstitution = (schema, opts) => {
|
|
|
1223
1431
|
// model is annotated, and `Schema.annotate` drops a `Schema.Class`'s `.fields`,
|
|
1224
1432
|
// so the walker cannot see through it and the target's own transformed fields
|
|
1225
1433
|
// keep their strict schemas. Only callers that pass `resolveRef` are affected
|
|
1226
|
-
// — the entity derivation passes
|
|
1434
|
+
// — the entity derivation passes none (#116).
|
|
1227
1435
|
const deeper = descend(opts);
|
|
1228
1436
|
// Optional wrapper FIRST — unwrap to the REAL inner before the leaf / array /
|
|
1229
1437
|
// struct checks. `optionalKey(X)` is a bare AST (Declaration/Arrays) with an
|
|
@@ -1232,11 +1440,19 @@ const needsDeepSubstitution = (schema, opts) => {
|
|
|
1232
1440
|
// required date and drop the optionality; and an `optionalKey(Array)` wrapper
|
|
1233
1441
|
// has the `Arrays` AST but no runtime `.value`.
|
|
1234
1442
|
const opt = optionalField(schema);
|
|
1235
|
-
if (opt !== undefined)
|
|
1236
|
-
|
|
1443
|
+
if (opt !== undefined) {
|
|
1444
|
+
const read = writesFirstMemberOnly(opts) ? undefined : opt.union;
|
|
1445
|
+
return needsDeepSubstitution(read ?? opt.inner, throughOptional(opts));
|
|
1446
|
+
}
|
|
1237
1447
|
const defaulted = decodingDefaultField(schema);
|
|
1238
|
-
if (defaulted !== undefined)
|
|
1239
|
-
return
|
|
1448
|
+
if (defaulted !== undefined) {
|
|
1449
|
+
return defaulted.defaults
|
|
1450
|
+
? (defaulted.transforms && liftsBigInts(opts) && holdsUnwalkedBigInt(schema)) ||
|
|
1451
|
+
needsDeepSubstitution(beneathDefault(schema, defaulted, opts), opts)
|
|
1452
|
+
: opts?.domainValues === true || (liftsBigInts(opts) && holdsUnwalkedBigInt(schema));
|
|
1453
|
+
}
|
|
1454
|
+
if (readsListAsDeclared(schema, opts))
|
|
1455
|
+
return readingUnwalked(schema, opts) !== schema;
|
|
1240
1456
|
if (isSelfDateSchema(schema))
|
|
1241
1457
|
return true;
|
|
1242
1458
|
if (opts?.tolerantTransforms && isDateTransform(schema))
|
|
@@ -1245,10 +1461,14 @@ const needsDeepSubstitution = (schema, opts) => {
|
|
|
1245
1461
|
return true;
|
|
1246
1462
|
// Any other leaf transform, tolerant mode only — same reason as the date
|
|
1247
1463
|
// case: after a mutation the field may hold either form (#116).
|
|
1248
|
-
if (opts?.tolerantTransforms &&
|
|
1464
|
+
if (opts?.tolerantTransforms && isTolerantLeaf(schema))
|
|
1249
1465
|
return true;
|
|
1250
1466
|
if (needsLegacyRead(schema, opts) || needsBigIntLift(schema, opts))
|
|
1251
1467
|
return true;
|
|
1468
|
+
if (opts?.plainStateForms && holdsPlainForm(schema.ast))
|
|
1469
|
+
return true;
|
|
1470
|
+
if (opts?.tolerantTransforms && holdsTransformation(schema.ast))
|
|
1471
|
+
return true;
|
|
1252
1472
|
const container = walkedContainer(schema);
|
|
1253
1473
|
if (container !== undefined) {
|
|
1254
1474
|
return container.children.some((child, index) => needsDeepSubstitution(child, childOptions(container, deeper, index, opts)));
|
|
@@ -1268,6 +1488,68 @@ const needsDeepSubstitution = (schema, opts) => {
|
|
|
1268
1488
|
return needsDeepSubstitution(refTarget ?? field, deeper);
|
|
1269
1489
|
});
|
|
1270
1490
|
}
|
|
1491
|
+
return liftsBigInts(opts) && holdsUnwalkedBigInt(schema);
|
|
1492
|
+
};
|
|
1493
|
+
/**
|
|
1494
|
+
* Whether a mutation can hold this as something else: a declaration of Effect's or a custom one,
|
|
1495
|
+
* as opposed to a `Schema.Class`, whose AST is a declaration encoded as its struct and whose
|
|
1496
|
+
* fields are walked, or whatever a `Schema.suspend` holds, which isn't. See `plainStateForms`.
|
|
1497
|
+
*/
|
|
1498
|
+
const holdsPlainForm = (ast) => (ast._tag === "Declaration" && ast.encoding === undefined) || SchemaAST.isSuspend(ast);
|
|
1499
|
+
/**
|
|
1500
|
+
* A declaration with a transformation inside, such as `Schema.Option(Schema.DateFromString)`, or a
|
|
1501
|
+
* `Schema.suspend` with one. The tolerant substitution doesn't reach under either, so an aggregate's
|
|
1502
|
+
* `create` would be handed the domain value and refuse it.
|
|
1503
|
+
*/
|
|
1504
|
+
const holdsTransformation = (ast) => SchemaAST.isSuspend(ast)
|
|
1505
|
+
? astContainsWireTransform(ast, new Set(), false)
|
|
1506
|
+
: holdsPlainForm(ast) &&
|
|
1507
|
+
ast.typeParameters.some((parameter) => SchemaAST.toType(parameter) !== parameter);
|
|
1508
|
+
/** A declaration or a suspended schema, or its domain value. See `holdsTransformation`. */
|
|
1509
|
+
const acceptingDomainForm = (declaration) => Schema.Union([declaration, Schema.toType(declaration)]);
|
|
1510
|
+
/**
|
|
1511
|
+
* A declaration, or either of what a mutation can hand back for it: the domain value it was
|
|
1512
|
+
* given, or the form its plain state holds it as. See `plainStateForms`.
|
|
1513
|
+
*/
|
|
1514
|
+
const acceptingPlainForm = (declaration) => buildTolerantTransform(declaration, true, declaration);
|
|
1515
|
+
/**
|
|
1516
|
+
* A type side whose own check takes what isn't the transform's: `Unknown`, `Any`, or a declaration
|
|
1517
|
+
* with nothing inside it (`Schema.declare`, `Schema.instanceOf`).
|
|
1518
|
+
*/
|
|
1519
|
+
const takesAnything = (type) => SchemaAST.isUnknown(type) ||
|
|
1520
|
+
SchemaAST.isAny(type) ||
|
|
1521
|
+
(SchemaAST.isDeclaration(type) && type.typeParameters.length === 0);
|
|
1522
|
+
/** A tolerant substitute for a field a read makes `read` of. See `buildTolerantTransform`. */
|
|
1523
|
+
const tolerantLeaf = (schema, opts, read) => {
|
|
1524
|
+
const type = SchemaAST.toType(schema.ast);
|
|
1525
|
+
return buildTolerantTransform(read, opts?.domainValues,
|
|
1526
|
+
// A plain state holds a declaration in another form. With nothing beneath the domain side that
|
|
1527
|
+
// is one, the plain form is the domain value: asked first, a value read wire first (an
|
|
1528
|
+
// aggregate's `decompose` input) would be taken as the domain value instead.
|
|
1529
|
+
opts?.plainStateForms && holdsDeclaration(type, new Set())
|
|
1530
|
+
? Schema.make(type)
|
|
1531
|
+
: undefined,
|
|
1532
|
+
// 1.39.0's own leaves (stored as a string or a number) take a domain value as they did.
|
|
1533
|
+
!isLeafEncodingTransform(schema) && takesAnything(type));
|
|
1534
|
+
};
|
|
1535
|
+
/** Whether a declaration (a set, an `Option`, a class, a date) is anywhere in `ast`. */
|
|
1536
|
+
const holdsDeclaration = (ast, seen) => {
|
|
1537
|
+
if (seen.has(ast))
|
|
1538
|
+
return false;
|
|
1539
|
+
seen.add(ast);
|
|
1540
|
+
if (SchemaAST.isDeclaration(ast))
|
|
1541
|
+
return true;
|
|
1542
|
+
const walk = (child) => holdsDeclaration(child, seen);
|
|
1543
|
+
if (SchemaAST.isArrays(ast))
|
|
1544
|
+
return ast.elements.some(walk) || ast.rest.some(walk);
|
|
1545
|
+
if (SchemaAST.isObjects(ast)) {
|
|
1546
|
+
return (ast.propertySignatures.some((ps) => walk(ps.type)) ||
|
|
1547
|
+
ast.indexSignatures.some((is) => walk(is.type)));
|
|
1548
|
+
}
|
|
1549
|
+
if (SchemaAST.isUnion(ast))
|
|
1550
|
+
return ast.types.some(walk);
|
|
1551
|
+
if (SchemaAST.isSuspend(ast))
|
|
1552
|
+
return walk(ast.thunk());
|
|
1271
1553
|
return false;
|
|
1272
1554
|
};
|
|
1273
1555
|
/**
|
|
@@ -1296,7 +1578,7 @@ export const substituteSchemaDeep = (schema, opts) => {
|
|
|
1296
1578
|
// model is annotated, and `Schema.annotate` drops a `Schema.Class`'s `.fields`,
|
|
1297
1579
|
// so the walker cannot see through it and the target's own transformed fields
|
|
1298
1580
|
// keep their strict schemas. Only callers that pass `resolveRef` are affected
|
|
1299
|
-
// — the entity derivation passes
|
|
1581
|
+
// — the entity derivation passes none (#116).
|
|
1300
1582
|
const deeper = descend(opts);
|
|
1301
1583
|
// Optional wrapper FIRST — unwrap to the REAL inner, substitute it, then
|
|
1302
1584
|
// re-apply the same optionality. Handled BEFORE the leaf / array / struct
|
|
@@ -1305,14 +1587,53 @@ export const substituteSchemaDeep = (schema, opts) => {
|
|
|
1305
1587
|
// the optional key), and an optional array's `.value` lives only on the inner.
|
|
1306
1588
|
const opt = optionalField(schema);
|
|
1307
1589
|
if (opt !== undefined) {
|
|
1308
|
-
|
|
1590
|
+
const first = substituteSchemaDeep(opt.inner, throughOptional(opts));
|
|
1591
|
+
if (opt.union === undefined)
|
|
1592
|
+
return opt.rewrap(first);
|
|
1593
|
+
if (!writesFirstMemberOnly(opts)) {
|
|
1594
|
+
const read = substituteSchemaDeep(opt.union, {
|
|
1595
|
+
...throughOptional(opts),
|
|
1596
|
+
declaredOrder: true,
|
|
1597
|
+
});
|
|
1598
|
+
// A member of a union reads only its own forms, so it doesn't claim a later member's rows.
|
|
1599
|
+
// The first was read as the whole field until now, and a row it took then still reads.
|
|
1600
|
+
return opt.rewrap(first === opt.inner
|
|
1601
|
+
? read
|
|
1602
|
+
: Schema.Union([
|
|
1603
|
+
read,
|
|
1604
|
+
first,
|
|
1605
|
+
]));
|
|
1606
|
+
}
|
|
1607
|
+
// Over the members themselves, so `optionalField` still tells them apart in what's built here.
|
|
1608
|
+
return Schema.optionalKey(opt.union.mapMembers((members) => [
|
|
1609
|
+
...members.map((member) => (member === opt.inner ? first : member)),
|
|
1610
|
+
Schema.Undefined,
|
|
1611
|
+
], { unsafePreserveChecks: true }));
|
|
1309
1612
|
}
|
|
1310
1613
|
// A decoding default (or any optional-encoded step) on top: substitute the
|
|
1311
1614
|
// field beneath it and keep the step, so a missing key still defaults (#133).
|
|
1615
|
+
// What the walk below can't enter: a declaration, a `Schema.suspend` or a transformation. What a
|
|
1616
|
+
// read makes of a plain bigint or an absent value in one, it makes of it where it sits.
|
|
1617
|
+
const unwalked = () => readingUnwalked(schema, opts);
|
|
1312
1618
|
const defaulted = decodingDefaultField(schema);
|
|
1313
1619
|
if (defaulted !== undefined) {
|
|
1314
|
-
|
|
1620
|
+
const domainValues = opts?.domainValues === true;
|
|
1621
|
+
// A step that isn't a default turns what's stored into something else. What's beneath it isn't
|
|
1622
|
+
// what's stored, so it stays as declared, and a caller, who holds that something else, goes round it.
|
|
1623
|
+
if (!defaulted.defaults) {
|
|
1624
|
+
return domainValues
|
|
1625
|
+
? defaulted.rewrap(tolerantLeaf(schema, opts, schema), absentAsDeclared(schema, defaulted.withUndefined))
|
|
1626
|
+
: unwalked();
|
|
1627
|
+
}
|
|
1628
|
+
if (defaulted.transforms && !domainValues && unwalked() !== schema)
|
|
1629
|
+
return unwalked();
|
|
1630
|
+
const beneath = substituteSchemaDeep(beneathDefault(schema, defaulted, opts), opts);
|
|
1631
|
+
return defaulted.rewrap(refusesUndefinedPositions(defaulted.inner, opts)
|
|
1632
|
+
? refusingUndefinedPositions(beneath)
|
|
1633
|
+
: beneath, domainValues ? absentAsDeclared(schema, defaulted.withUndefined) : undefined);
|
|
1315
1634
|
}
|
|
1635
|
+
if (readsListAsDeclared(schema, opts))
|
|
1636
|
+
return unwalked();
|
|
1316
1637
|
// Leaf: self-date schema → tolerant bidirectional date transform.
|
|
1317
1638
|
if (isSelfDateSchema(schema))
|
|
1318
1639
|
return selfDateSubstitute(schema, opts);
|
|
@@ -1324,7 +1645,7 @@ export const substituteSchemaDeep = (schema, opts) => {
|
|
|
1324
1645
|
// enough to claim a value that belongs to a later member (#133).
|
|
1325
1646
|
if (opts?.tolerantTransforms && isDateTransform(schema)) {
|
|
1326
1647
|
if (opts.strictWireKind)
|
|
1327
|
-
return buildTolerantTransform(
|
|
1648
|
+
return buildTolerantTransform(unwalked(), opts.domainValues);
|
|
1328
1649
|
const encoding = getEncoding(schema) ?? inferDefaultEncoding(schema);
|
|
1329
1650
|
if (encoding)
|
|
1330
1651
|
return buildDateTransform(encoding);
|
|
@@ -1332,21 +1653,28 @@ export const substituteSchemaDeep = (schema, opts) => {
|
|
|
1332
1653
|
// Leaf: RedactedFromValue → tolerant Redacted transform.
|
|
1333
1654
|
const redactedInner = tryGetRedactedInner(schema);
|
|
1334
1655
|
if (redactedInner !== undefined)
|
|
1335
|
-
return buildRedactedSubstitute(redactedInner);
|
|
1656
|
+
return buildRedactedSubstitute(schema, redactedInner);
|
|
1336
1657
|
// Leaf: any OTHER encoding transform (only under `tolerantTransforms`) →
|
|
1337
1658
|
// a substitute whose decode accepts the wire form and the domain form. Placed
|
|
1338
1659
|
// after the date and Redacted branches so those keep their dedicated
|
|
1339
1660
|
// substitutes, and before the array / struct recursion so containers are
|
|
1340
1661
|
// still walked rather than swallowed (#116).
|
|
1341
|
-
if (opts?.tolerantTransforms &&
|
|
1342
|
-
return
|
|
1662
|
+
if (opts?.tolerantTransforms && isTolerantLeaf(schema)) {
|
|
1663
|
+
return tolerantLeaf(schema, opts, unwalked());
|
|
1343
1664
|
}
|
|
1344
1665
|
// Leaf: read leniency for legacy domain-form values (entity read schemas) and
|
|
1345
1666
|
// the plain-bigint lift (#133).
|
|
1346
1667
|
if (needsLegacyRead(schema, opts))
|
|
1347
|
-
return buildLegacyReadTransform(
|
|
1668
|
+
return buildLegacyReadTransform(unwalked());
|
|
1348
1669
|
if (needsBigIntLift(schema, opts))
|
|
1349
1670
|
return buildBigIntLift(schema);
|
|
1671
|
+
if (opts?.plainStateForms && holdsPlainForm(schema.ast))
|
|
1672
|
+
return acceptingPlainForm(unwalked());
|
|
1673
|
+
if (holdsTransformation(schema.ast)) {
|
|
1674
|
+
return opts?.domainValues
|
|
1675
|
+
? buildTolerantTransform(unwalked(), true)
|
|
1676
|
+
: acceptingDomainForm(unwalked());
|
|
1677
|
+
}
|
|
1350
1678
|
// Union / Record / Tuple / *WithRest: substitute every child and rebuild the
|
|
1351
1679
|
// same container kind, metadata included (#133). Without `tolerantTransforms`
|
|
1352
1680
|
// (entity derivation) only self-date / Redacted leaves inside are
|
|
@@ -1356,14 +1684,71 @@ export const substituteSchemaDeep = (schema, opts) => {
|
|
|
1356
1684
|
if (container !== undefined) {
|
|
1357
1685
|
const children = container.children.map((child, index) => {
|
|
1358
1686
|
const childOpts = childOptions(container, deeper, index, opts);
|
|
1687
|
+
const member = substituteSchemaDeep(child, childOpts);
|
|
1688
|
+
// An aggregate stores a first date transform in its date encoding, not its own wire form, and
|
|
1689
|
+
// a date in a union of nothing else but null in the form its domain infers, whatever it declares.
|
|
1690
|
+
const stored = !opts?.tolerantTransforms
|
|
1691
|
+
? undefined
|
|
1692
|
+
: holdsOnlyDates(container)
|
|
1693
|
+
? inferDefaultEncoding(child)
|
|
1694
|
+
: index === 0 && opts.declaredOrder
|
|
1695
|
+
? (getEncoding(child) ?? inferDefaultEncoding(child))
|
|
1696
|
+
: undefined;
|
|
1359
1697
|
return {
|
|
1360
|
-
|
|
1361
|
-
|
|
1698
|
+
child,
|
|
1699
|
+
childOpts,
|
|
1700
|
+
substituted: stored === undefined
|
|
1701
|
+
? member
|
|
1702
|
+
: Schema.Union([
|
|
1703
|
+
member,
|
|
1704
|
+
buildDateTransform(stored, {
|
|
1705
|
+
strictWireKind: true,
|
|
1706
|
+
canonicalOnly: true,
|
|
1707
|
+
}),
|
|
1708
|
+
]),
|
|
1709
|
+
first: container.isUnion &&
|
|
1710
|
+
opts?.absentReads !== "only" &&
|
|
1711
|
+
!opts?.declaredOrder &&
|
|
1712
|
+
isCanonicalDateMember(child, childOpts),
|
|
1362
1713
|
};
|
|
1363
1714
|
});
|
|
1715
|
+
const ordered = [...children.filter((c) => c.first), ...children.filter((c) => !c.first)];
|
|
1716
|
+
const lifting = (c) => container.isUnion &&
|
|
1717
|
+
liftsBigInts(c.childOpts) &&
|
|
1718
|
+
holdsBigInt(c.child) &&
|
|
1719
|
+
!(opts?.declaredOrder && c === ordered[0]);
|
|
1720
|
+
const lenient = ordered.filter((c) => (opts?.absentReads && holdsAbsentRead(c.child)) ||
|
|
1721
|
+
(opts?.absentReads === true && holdsUnwalkedAbsent(c.child)) ||
|
|
1722
|
+
lifting(c) ||
|
|
1723
|
+
(opts?.plainStateForms && holdsPlainForm(c.child.ast)));
|
|
1724
|
+
if (!container.isUnion || lenient.length === 0) {
|
|
1725
|
+
const rebuilt = container.rebuild(ordered.map((c) => c.substituted));
|
|
1726
|
+
return refusesUndefinedPositions(schema, opts) ? refusingUndefinedPositions(rebuilt) : rebuilt;
|
|
1727
|
+
}
|
|
1728
|
+
// A member reading an absent attribute as a value, or lifting a number to a
|
|
1729
|
+
// bigint, accepts more than it declares, enough to claim a row of a later
|
|
1730
|
+
// member. So every member gets its try as declared first, and only a row none
|
|
1731
|
+
// of them takes reads absent or is lifted. A `oneOf` union refuses a value two
|
|
1732
|
+
// members accept: it stays as declared, with only the lift tried outside it,
|
|
1733
|
+
// since a row as a bigint is what it was written as.
|
|
1734
|
+
const declared = (c) => opts?.absentReads === "only"
|
|
1735
|
+
? c.child
|
|
1736
|
+
: substituteSchemaDeep(c.child, {
|
|
1737
|
+
...c.childOpts,
|
|
1738
|
+
absentReads: undefined,
|
|
1739
|
+
plainStateForms: undefined,
|
|
1740
|
+
exactBigInts: true,
|
|
1741
|
+
});
|
|
1742
|
+
if (schema.ast.options?.mode === "oneOf") {
|
|
1743
|
+
const exact = container.rebuild(ordered.map((c) => declared(c)));
|
|
1744
|
+
const lifted = ordered.filter((c) => lifting(c)).map((c) => c.substituted);
|
|
1745
|
+
return lifted.length === 0
|
|
1746
|
+
? exact
|
|
1747
|
+
: withMetadataOf(schema, Schema.Union([exact, ...lifted]));
|
|
1748
|
+
}
|
|
1364
1749
|
return container.rebuild([
|
|
1365
|
-
...
|
|
1366
|
-
...
|
|
1750
|
+
...ordered.map((c) => (lenient.includes(c) ? declared(c) : c.substituted)),
|
|
1751
|
+
...lenient.map((c) => c.substituted),
|
|
1367
1752
|
]);
|
|
1368
1753
|
}
|
|
1369
1754
|
// Array: substitute the element schema.
|
|
@@ -1394,7 +1779,8 @@ export const substituteSchemaDeep = (schema, opts) => {
|
|
|
1394
1779
|
subFields[name] = fieldOpt ? fieldOpt.rewrap(sub) : sub;
|
|
1395
1780
|
}
|
|
1396
1781
|
else {
|
|
1397
|
-
|
|
1782
|
+
const sub = substituteSchemaDeep(field, atField(deeper, opts, name));
|
|
1783
|
+
subFields[name] = opts?.absentReads ? readingAbsent(sub) : sub;
|
|
1398
1784
|
}
|
|
1399
1785
|
}
|
|
1400
1786
|
const plainStruct = Schema.Struct(subFields);
|
|
@@ -1415,8 +1801,214 @@ export const substituteSchemaDeep = (schema, opts) => {
|
|
|
1415
1801
|
encode: SchemaGetter.transform((instance) => ({ ...instance })),
|
|
1416
1802
|
}));
|
|
1417
1803
|
}
|
|
1418
|
-
return
|
|
1804
|
+
return unwalked();
|
|
1805
|
+
};
|
|
1806
|
+
/**
|
|
1807
|
+
* What a declaration is, when its stored form is a marshalled `DateTime`. A `.check()`, a brand or an
|
|
1808
|
+
* annotation keeps the declaration, so it's still the date it was; a transformation over one isn't,
|
|
1809
|
+
* and the date beneath it is reached through its encoding.
|
|
1810
|
+
*/
|
|
1811
|
+
const isMarshalledDate = (ast) => {
|
|
1812
|
+
if (!SchemaAST.isDeclaration(ast) || ast.encoding !== undefined)
|
|
1813
|
+
return false;
|
|
1814
|
+
const domain = matchDateRepresentation(ast.annotations)?.domain;
|
|
1815
|
+
return domain === "DateTime.Utc" || domain === "DateTime.Zoned";
|
|
1816
|
+
};
|
|
1817
|
+
/**
|
|
1818
|
+
* A stored date, rebuilt, before it meets its declaration. Only a value carrying the marker a
|
|
1819
|
+
* marshalled date carries is touched: the declaration's guard takes the current one, so what it
|
|
1820
|
+
* accepts doesn't change, and the one Effect's betas and early rcs wrote is the one form added. Any
|
|
1821
|
+
* other value goes on to the declaration as it was, which refuses it or takes it as it always has, so
|
|
1822
|
+
* a union picks the member it picked before.
|
|
1823
|
+
*/
|
|
1824
|
+
const rebuildingMarshalledDate = {
|
|
1825
|
+
decode: SchemaGetter.transform((value) => typeof value === "object" &&
|
|
1826
|
+
value !== null &&
|
|
1827
|
+
(DATE_MARKER in value || BETA_DATE_MARKER in value)
|
|
1828
|
+
? (reviveMarshalledDateTime(value) ?? value)
|
|
1829
|
+
: value),
|
|
1830
|
+
encode: SchemaGetter.passthrough(),
|
|
1831
|
+
};
|
|
1832
|
+
/** A plain `Schema.BigInt`, checked or branded or not, stored as `N`: a transformation is reached through. */
|
|
1833
|
+
const isMarshalledBigInt = (ast) => SchemaAST.isBigInt(ast) && ast.encoding === undefined;
|
|
1834
|
+
/**
|
|
1835
|
+
* A stored bigint, lifted, before it meets its declaration: a number that's a safe integer becomes
|
|
1836
|
+
* the bigint it was, and a bigint the unmarshaller returned stays one. Any other value goes on to the
|
|
1837
|
+
* declaration as it was. The walker refuses a union in which another member takes the number
|
|
1838
|
+
* (`EDD-9125`), so a number reaching this is a number no member takes.
|
|
1839
|
+
*/
|
|
1840
|
+
const liftingMarshalledBigInt = {
|
|
1841
|
+
decode: SchemaGetter.transform((value) => legacyBigInt(value) ?? value),
|
|
1842
|
+
encode: SchemaGetter.passthrough(),
|
|
1843
|
+
};
|
|
1844
|
+
const astWith = (ast, changes) => Object.assign(Object.create(Object.getPrototypeOf(ast)), ast, changes);
|
|
1845
|
+
/**
|
|
1846
|
+
* The same node with its children through `f`, or the node itself when none of them changed. Walks
|
|
1847
|
+
* the AST, not the schema's accessors, so it reaches every position: a `Schema.annotate` drops a
|
|
1848
|
+
* class's `.fields`, and a declaration's type parameters and a transformation's encoded side have no
|
|
1849
|
+
* accessor at all. A suspended schema is walked when it's forced, as a recursive one has no end.
|
|
1850
|
+
*/
|
|
1851
|
+
const withChildren = (ast, f, absentValues = false) => {
|
|
1852
|
+
switch (ast._tag) {
|
|
1853
|
+
case "Declaration": {
|
|
1854
|
+
const typeParameters = SchemaAST.mapOrSame(ast.typeParameters, (parameter) => absentValues ? readingAbsentParameter(parameter, f(parameter)) : f(parameter));
|
|
1855
|
+
return typeParameters === ast.typeParameters ? ast : astWith(ast, { typeParameters });
|
|
1856
|
+
}
|
|
1857
|
+
case "Arrays": {
|
|
1858
|
+
const elements = SchemaAST.mapOrSame(ast.elements, f);
|
|
1859
|
+
const rest = SchemaAST.mapOrSame(ast.rest, f);
|
|
1860
|
+
return elements === ast.elements && rest === ast.rest ? ast : astWith(ast, { elements, rest });
|
|
1861
|
+
}
|
|
1862
|
+
case "Objects": {
|
|
1863
|
+
const propertySignatures = SchemaAST.mapOrSame(ast.propertySignatures, (property) => {
|
|
1864
|
+
const read = f(property.type);
|
|
1865
|
+
const type = absentValues
|
|
1866
|
+
? readingAbsent(Schema.make(read), Schema.make(property.type)).ast
|
|
1867
|
+
: read;
|
|
1868
|
+
return type === property.type
|
|
1869
|
+
? property
|
|
1870
|
+
: new SchemaAST.PropertySignature(property.name, type);
|
|
1871
|
+
});
|
|
1872
|
+
const indexSignatures = SchemaAST.mapOrSame(ast.indexSignatures, (index) => {
|
|
1873
|
+
const type = f(index.type);
|
|
1874
|
+
return type === index.type ? index : new SchemaAST.IndexSignature(index.parameter, type);
|
|
1875
|
+
});
|
|
1876
|
+
return propertySignatures === ast.propertySignatures &&
|
|
1877
|
+
indexSignatures === ast.indexSignatures
|
|
1878
|
+
? ast
|
|
1879
|
+
: astWith(ast, { propertySignatures, indexSignatures });
|
|
1880
|
+
}
|
|
1881
|
+
case "Union": {
|
|
1882
|
+
const types = SchemaAST.mapOrSame(ast.types, f);
|
|
1883
|
+
return types === ast.types ? ast : astWith(ast, { types });
|
|
1884
|
+
}
|
|
1885
|
+
case "Suspend":
|
|
1886
|
+
return astWith(ast, { thunk: () => f(ast.thunk()) });
|
|
1887
|
+
default:
|
|
1888
|
+
return ast;
|
|
1889
|
+
}
|
|
1890
|
+
};
|
|
1891
|
+
/**
|
|
1892
|
+
* A declaration's value, read as what it stands for when it's absent. The marshaller leaves an empty
|
|
1893
|
+
* set out of what it stores, and an `Option`, a `Result`, an `Exit` or a `Cause` holding one then has
|
|
1894
|
+
* no value at all. `undefined` needs nothing: it's what an absent one reads as already.
|
|
1895
|
+
*/
|
|
1896
|
+
const readingAbsentParameter = (declared, read) => {
|
|
1897
|
+
const stored = absentStored(Schema.make(declared))?.stored;
|
|
1898
|
+
if (stored === undefined)
|
|
1899
|
+
return read;
|
|
1900
|
+
return Schema.Unknown.pipe(Schema.decodeTo(Schema.make(read), {
|
|
1901
|
+
decode: SchemaGetter.transform((value) => value === undefined ? stored : value),
|
|
1902
|
+
encode: SchemaGetter.passthrough(),
|
|
1903
|
+
})).ast;
|
|
1904
|
+
};
|
|
1905
|
+
/**
|
|
1906
|
+
* A union where a member takes more than it declares, by reading an absent value, is what
|
|
1907
|
+
* `substituteSchemaDeep` makes one it can walk: enough to claim a row of a later member, so the
|
|
1908
|
+
* members as declared get their try first, and only a row none of them takes reads absent. Each side
|
|
1909
|
+
* stays whole, so a `oneOf` one still refuses a value two members take.
|
|
1910
|
+
*/
|
|
1911
|
+
const tryingDeclaredFirst = (ast, read, declared) => ast.types.some((member) => holdsAbsentValue(SchemaAST.toEncoded(member)))
|
|
1912
|
+
? astWith(ast, {
|
|
1913
|
+
types: [astWith(ast, { types: ast.types.map((member) => declared(member)) }), read],
|
|
1914
|
+
options: { ...ast.options, mode: "anyOf" },
|
|
1915
|
+
})
|
|
1916
|
+
: read;
|
|
1917
|
+
/**
|
|
1918
|
+
* A schema with what `rebuildOf` names, wherever it sits, rebuilt before it meets its node, and with
|
|
1919
|
+
* `absentValues` a value the marshaller left out read as what it stands for.
|
|
1920
|
+
*/
|
|
1921
|
+
const readingStored = (rebuildOf, absentValues = false) => {
|
|
1922
|
+
const declared = absentValues ? readingStored(rebuildOf) : undefined;
|
|
1923
|
+
const memo = new WeakMap();
|
|
1924
|
+
const reading = (ast) => {
|
|
1925
|
+
const known = memo.get(ast);
|
|
1926
|
+
if (known !== undefined)
|
|
1927
|
+
return known;
|
|
1928
|
+
let read;
|
|
1929
|
+
const rebuild = rebuildOf(ast);
|
|
1930
|
+
if (rebuild !== undefined) {
|
|
1931
|
+
const stored = ast.context?.isOptional ? Schema.optionalKey(Schema.Unknown) : Schema.Unknown;
|
|
1932
|
+
read = stored.pipe(Schema.decodeTo(Schema.make(ast), rebuild)).ast;
|
|
1933
|
+
}
|
|
1934
|
+
else {
|
|
1935
|
+
// What's stored is the encoded side: a type side holds what its transformation made, and
|
|
1936
|
+
// nothing was left out of that.
|
|
1937
|
+
const stored = declared !== undefined && ast.encoding === undefined;
|
|
1938
|
+
read = stored ? withChildren(ast, reading, true) : withChildren(ast, declared ?? reading);
|
|
1939
|
+
if (stored && SchemaAST.isUnion(ast)) {
|
|
1940
|
+
read = tryingDeclaredFirst(ast, read, declared);
|
|
1941
|
+
}
|
|
1942
|
+
const encoding = ast.encoding &&
|
|
1943
|
+
SchemaAST.mapOrSame(ast.encoding, (link) => {
|
|
1944
|
+
const to = reading(link.to);
|
|
1945
|
+
return to === link.to ? link : new SchemaAST.Link(to, link.transformation);
|
|
1946
|
+
});
|
|
1947
|
+
if (encoding !== ast.encoding)
|
|
1948
|
+
read = astWith(read, { encoding });
|
|
1949
|
+
}
|
|
1950
|
+
memo.set(ast, read);
|
|
1951
|
+
return read;
|
|
1952
|
+
};
|
|
1953
|
+
return reading;
|
|
1954
|
+
};
|
|
1955
|
+
/**
|
|
1956
|
+
* Every marshalled date and bigint in a schema, wherever it sits, read back as a real one, and every
|
|
1957
|
+
* value the marshaller left out as what it stands for.
|
|
1958
|
+
*/
|
|
1959
|
+
const readingMarshalled = readingStored((ast) => isMarshalledDate(ast)
|
|
1960
|
+
? rebuildingMarshalledDate
|
|
1961
|
+
: isMarshalledBigInt(ast)
|
|
1962
|
+
? liftingMarshalledBigInt
|
|
1963
|
+
: undefined, true);
|
|
1964
|
+
const bigIntRebuild = (ast) => isMarshalledBigInt(ast) ? liftingMarshalledBigInt : undefined;
|
|
1965
|
+
const liftingBigInts = readingStored(bigIntRebuild);
|
|
1966
|
+
const liftingBigIntsAndAbsent = readingStored(bigIntRebuild, true);
|
|
1967
|
+
/**
|
|
1968
|
+
* A schema the substitution can't walk, read as the walk reads what it can reach: every plain bigint
|
|
1969
|
+
* in it as one, and on a read that wants them, every absent value as what it stands for.
|
|
1970
|
+
*/
|
|
1971
|
+
const readingUnwalked = (schema, opts) => {
|
|
1972
|
+
if (opts?.absentReads === true && holdsUnwalkedAbsent(schema)) {
|
|
1973
|
+
return Schema.make(liftingBigIntsAndAbsent(schema.ast));
|
|
1974
|
+
}
|
|
1975
|
+
return liftsBigInts(opts) && holdsUnwalkedBigInt(schema)
|
|
1976
|
+
? Schema.make(liftingBigInts(schema.ast))
|
|
1977
|
+
: schema;
|
|
1419
1978
|
};
|
|
1979
|
+
/**
|
|
1980
|
+
* The read schema of a value the library stores but doesn't derive: an event, its metadata, a
|
|
1981
|
+
* snapshot state, an outbox effect. Every read of one goes through it, so anything a stream's read
|
|
1982
|
+
* does differently from its declaration belongs here. Differently, as it stands, in four ways:
|
|
1983
|
+
*
|
|
1984
|
+
* - An absent attribute reads as the value it stands for (`absentStored`).
|
|
1985
|
+
* - A `Schema.DateTimeUtc` or `Schema.DateTimeZoned` comes back as a real date. Nothing substitutes
|
|
1986
|
+
* these on write, so one is stored as the map the marshaller makes of it, and Effect's guard only
|
|
1987
|
+
* looks for the marker and the tag, which that map has. Left to the guard, it reads back as a plain
|
|
1988
|
+
* object that passes `DateTime.isDateTime` and isn't one: `Equal` is false, and on a named zone
|
|
1989
|
+
* `DateTime.getPart` throws.
|
|
1990
|
+
* - A `Schema.BigInt` comes back as a bigint. The marshaller stores one as a number, and the
|
|
1991
|
+
* unmarshaller returns a safe integer as a number, so without this only the values past 2^53 read.
|
|
1992
|
+
* - Each of Effect's other look-alike declarations (a `BigDecimal`, a `Duration`, a zone, an address,
|
|
1993
|
+
* ...) comes back as a real instance, where the read takes one (`readingInstances`).
|
|
1994
|
+
*
|
|
1995
|
+
* Returned as it came when none applies to it.
|
|
1996
|
+
*
|
|
1997
|
+
* @internal
|
|
1998
|
+
*/
|
|
1999
|
+
export const readingStreamValues = (schema) => {
|
|
2000
|
+
const absent = substituteSchemaDeep(schema, { absentReads: "only", enforceChecks: true });
|
|
2001
|
+
const read = readingMarshalled(absent.ast);
|
|
2002
|
+
return readingInstances(read === absent.ast ? absent : Schema.make(read));
|
|
2003
|
+
};
|
|
2004
|
+
/**
|
|
2005
|
+
* A schema the library stores but doesn't derive, refusing a tuple position holding
|
|
2006
|
+
* `undefined` that a stored list can't keep, and otherwise exactly as declared.
|
|
2007
|
+
* Unchanged when no tuple of it can hold one.
|
|
2008
|
+
*
|
|
2009
|
+
* @internal
|
|
2010
|
+
*/
|
|
2011
|
+
export const writingTuplePositions = (schema) => substituteSchemaDeep(schema, { undefinedPositionsOnly: true, enforceChecks: true });
|
|
1420
2012
|
/**
|
|
1421
2013
|
* Substitute model fields with bidirectional transforms where needed:
|
|
1422
2014
|
*
|
|
@@ -1447,6 +2039,7 @@ export const substituteSchemas = (modelFields, fieldEncodings, options) => {
|
|
|
1447
2039
|
const deep = {
|
|
1448
2040
|
...(options?.legacyReads ? { legacyReads: true } : {}),
|
|
1449
2041
|
...(options?.enforceChecks ? { enforceChecks: true } : {}),
|
|
2042
|
+
...(options?.absentReads ? { absentReads: true } : {}),
|
|
1450
2043
|
};
|
|
1451
2044
|
for (const [name, schema] of Object.entries(modelFields)) {
|
|
1452
2045
|
// 1. Self-date substitution. Pattern A: user declared `Schema.DateTimeUtc`
|
|
@@ -1474,7 +2067,7 @@ export const substituteSchemas = (modelFields, fieldEncodings, options) => {
|
|
|
1474
2067
|
// transform so the round-trip works for storage.
|
|
1475
2068
|
const redactedInner = tryGetRedactedInner(schema);
|
|
1476
2069
|
if (redactedInner !== undefined) {
|
|
1477
|
-
out[name] = buildRedactedSubstitute(redactedInner);
|
|
2070
|
+
out[name] = buildRedactedSubstitute(schema, redactedInner);
|
|
1478
2071
|
continue;
|
|
1479
2072
|
}
|
|
1480
2073
|
// 3. Default: recurse for nested substitution. Pattern B transforms
|
|
@@ -1493,6 +2086,10 @@ export const substituteSchemas = (modelFields, fieldEncodings, options) => {
|
|
|
1493
2086
|
...(unionOverride === undefined ? {} : { unionDateOverride: unionOverride }),
|
|
1494
2087
|
});
|
|
1495
2088
|
}
|
|
2089
|
+
if (options?.absentReads) {
|
|
2090
|
+
for (const [name, field] of Object.entries(out))
|
|
2091
|
+
out[name] = readingAbsent(field);
|
|
2092
|
+
}
|
|
1496
2093
|
return out;
|
|
1497
2094
|
};
|
|
1498
2095
|
/**
|
|
@@ -1556,26 +2153,34 @@ export const validateNoTransformOverride = (modelFields, configuredAttributes) =
|
|
|
1556
2153
|
if (configOverride !== undefined) {
|
|
1557
2154
|
throw new Error(conflictMessage(name));
|
|
1558
2155
|
}
|
|
1559
|
-
// 2. DynamoModel.storedAs() modifier on the schema itself?
|
|
1560
|
-
|
|
1561
|
-
// by comparing the annotation's storage with the transform's actual
|
|
1562
|
-
// wire format (resolved via `transformWireKind`, which walks the typed
|
|
1563
|
-
// `Base.encoding` chain to its leaf). Granularity: we can distinguish
|
|
1564
|
-
// "string" vs "number" wire forms; we cannot distinguish epochMs vs
|
|
1565
|
-
// epochSeconds at this layer. The string-vs-number mismatch covers the
|
|
1566
|
-
// most common breaking case.
|
|
1567
|
-
const annotated = getEncoding(schema);
|
|
1568
|
-
if (annotated === undefined)
|
|
1569
|
-
continue;
|
|
1570
|
-
const transformWire = transformWireKind(schema);
|
|
1571
|
-
if (transformWire === undefined)
|
|
1572
|
-
continue;
|
|
1573
|
-
const annotatedKind = annotated.storage === "string" ? "string" : "number";
|
|
1574
|
-
if (transformWire !== annotatedKind) {
|
|
2156
|
+
// 2. DynamoModel.storedAs() modifier on the schema itself?
|
|
2157
|
+
if (storedAsOverTransform(schema)) {
|
|
1575
2158
|
throw new Error(conflictMessage(name));
|
|
1576
2159
|
}
|
|
1577
2160
|
}
|
|
1578
2161
|
};
|
|
2162
|
+
/**
|
|
2163
|
+
* Whether a date transform schema carries a `DynamoModel.storedAs(...)` annotation of the other wire
|
|
2164
|
+
* kind: one {@link validateNoTransformOverride} refuses. The modifier applies a `DynamoEncoding`
|
|
2165
|
+
* annotation to the AST; it conflicts when its storage disagrees with the transform's actual wire
|
|
2166
|
+
* format (resolved via `transformWireKind`, which walks the typed `Base.encoding` chain to its leaf).
|
|
2167
|
+
* Granularity: "string" vs "number" wire forms; epochMs vs epochSeconds can't be told apart at this
|
|
2168
|
+
* layer. The string-vs-number mismatch covers the most common breaking case. Such a field stores the
|
|
2169
|
+
* transform's form, so a value in the annotation's form can't be read back from it.
|
|
2170
|
+
*
|
|
2171
|
+
* @internal Also read by `Entity.make`: a `Schema.optional` date of this kind isn't a stamp.
|
|
2172
|
+
*/
|
|
2173
|
+
export const storedAsOverTransform = (schema) => {
|
|
2174
|
+
if (!isDateTransform(schema))
|
|
2175
|
+
return false;
|
|
2176
|
+
const annotated = getEncoding(schema);
|
|
2177
|
+
if (annotated === undefined)
|
|
2178
|
+
return false;
|
|
2179
|
+
const transformWire = transformWireKind(schema);
|
|
2180
|
+
if (transformWire === undefined)
|
|
2181
|
+
return false;
|
|
2182
|
+
return transformWire !== (annotated.storage === "string" ? "string" : "number");
|
|
2183
|
+
};
|
|
1579
2184
|
/**
|
|
1580
2185
|
* Read the wire form ("string" or "number") that a transform schema actually
|
|
1581
2186
|
* encodes to, by walking the encoding chain. Returns undefined when the chain
|
|
@@ -1597,10 +2202,14 @@ export const buildDerivedSchemas = (modelFields, indexes, systemFields, resolved
|
|
|
1597
2202
|
// the substitution rebuilds; the READ schemas below do not (#133).
|
|
1598
2203
|
const fields = substituteSchemas(modelFields, fieldEncodings, { enforceChecks: true });
|
|
1599
2204
|
// READ schemas (model / record / item / deleted / history decode) also take
|
|
1600
|
-
// the domain-form values earlier releases left on transform fields (#133)
|
|
2205
|
+
// the domain-form values earlier releases left on transform fields (#133),
|
|
2206
|
+
// and read an absent attribute as the value it stands for (`absentStored`).
|
|
1601
2207
|
// Their encode is the strict one, and keys are composed from `inputSchema`,
|
|
1602
2208
|
// so neither writes nor keys see a difference.
|
|
1603
|
-
const readFields = substituteSchemas(modelFields, fieldEncodings, {
|
|
2209
|
+
const readFields = substituteSchemas(modelFields, fieldEncodings, {
|
|
2210
|
+
legacyReads: true,
|
|
2211
|
+
absentReads: true,
|
|
2212
|
+
});
|
|
1604
2213
|
// Ref fields carry the `DynamoModel.ref` annotation (an opaque Declaration that
|
|
1605
2214
|
// hides the target's fields), so `substituteSchemaDeep` above can't see into
|
|
1606
2215
|
// them. Re-point each denormalized ref field at its target model, recursively
|
|
@@ -1615,7 +2224,16 @@ export const buildDerivedSchemas = (modelFields, indexes, systemFields, resolved
|
|
|
1615
2224
|
? targetModel.model
|
|
1616
2225
|
: targetModel;
|
|
1617
2226
|
fields[ref.fieldName] = substituteSchemaDeep(resolved, { enforceChecks: true });
|
|
1618
|
-
readFields[ref.fieldName] = substituteSchemaDeep(resolved, {
|
|
2227
|
+
readFields[ref.fieldName] = substituteSchemaDeep(resolved, {
|
|
2228
|
+
legacyReads: true,
|
|
2229
|
+
absentReads: true,
|
|
2230
|
+
});
|
|
2231
|
+
}
|
|
2232
|
+
// Each of Effect's look-alike declarations (a `BigDecimal`, a `Duration`, an address, a date in
|
|
2233
|
+
// an `Option`, ...) reads back as a real instance, not the map DynamoDB hands back for it. A field
|
|
2234
|
+
// holding none is the same schema it was. Encoding through one is as it was.
|
|
2235
|
+
for (const [name, field] of Object.entries(readFields)) {
|
|
2236
|
+
readFields[name] = readingInstances(field);
|
|
1619
2237
|
}
|
|
1620
2238
|
// --- Model Schema: pure model fields (decode of stored items in "model" mode) ---
|
|
1621
2239
|
const modelSchema = Schema.Struct(readFields);
|
|
@@ -1714,13 +2332,16 @@ export const buildDerivedSchemas = (modelFields, indexes, systemFields, resolved
|
|
|
1714
2332
|
}
|
|
1715
2333
|
const createSchema = Schema.Struct(createFieldsAcc);
|
|
1716
2334
|
// --- Update Schema: optional model fields minus key composites & immutable ---
|
|
1717
|
-
//
|
|
1718
|
-
//
|
|
1719
|
-
//
|
|
1720
|
-
//
|
|
1721
|
-
//
|
|
1722
|
-
//
|
|
1723
|
-
//
|
|
2335
|
+
// The key may always be omitted (the structural composer treats absent keys
|
|
2336
|
+
// as absent for GSI composition under v3's two-way classification). Whether
|
|
2337
|
+
// its value may be `undefined` follows the row: `undefined` clears the field,
|
|
2338
|
+
// so a field a row can lack takes `Schema.optional` and one it can't takes
|
|
2339
|
+
// `Schema.optionalKey`, as do ref ids. A library-generated value (a colliding
|
|
2340
|
+
// `updatedAt`) stays `Schema.optional`: `undefined` there means generate it.
|
|
2341
|
+
// The field's value type matches the model declaration exactly — there is
|
|
2342
|
+
// no `null` widening, because EDD-9025 prevents composites from declaring
|
|
2343
|
+
// `null` and the standard update payload follows the same shape. If the model
|
|
2344
|
+
// declares a non-composite attribute as `Schema.NullOr(X)`, `set({ attr: null })`
|
|
1724
2345
|
// still typechecks; the v1.6 `NullishOr` wrap that allowed null on
|
|
1725
2346
|
// arbitrary fields (regardless of model declaration) is removed.
|
|
1726
2347
|
const buildUpdateFieldsAcc = () => {
|
|
@@ -1734,14 +2355,17 @@ export const buildDerivedSchemas = (modelFields, indexes, systemFields, resolved
|
|
|
1734
2355
|
continue;
|
|
1735
2356
|
if (strippedFromUpdate.has(key))
|
|
1736
2357
|
continue;
|
|
1737
|
-
|
|
2358
|
+
const clearable = optionalOnCollide.has(key) || lackDecision(field) !== "refuse";
|
|
2359
|
+
acc[key] = clearable
|
|
2360
|
+
? Schema.optional(field)
|
|
2361
|
+
: Schema.optionalKey(field);
|
|
1738
2362
|
}
|
|
1739
2363
|
for (const ref of resolvedRefs) {
|
|
1740
2364
|
if (pkComposites.has(ref.fieldName))
|
|
1741
2365
|
continue;
|
|
1742
2366
|
if (immutableFields.has(ref.fieldName))
|
|
1743
2367
|
continue;
|
|
1744
|
-
acc[ref.idFieldName] = Schema.
|
|
2368
|
+
acc[ref.idFieldName] = Schema.optionalKey(ref.identifierSchema);
|
|
1745
2369
|
}
|
|
1746
2370
|
return acc;
|
|
1747
2371
|
};
|
|
@@ -1855,7 +2479,8 @@ export const resolveUniqueFields = (def) => Array.isArray(def) ? def : def.field
|
|
|
1855
2479
|
// ---------------------------------------------------------------------------
|
|
1856
2480
|
/** The schema one path segment addresses inside `schema`, or `undefined`. */
|
|
1857
2481
|
const childAtSegment = (schema, segment) => {
|
|
1858
|
-
const
|
|
2482
|
+
const opt = optionalField(schema);
|
|
2483
|
+
const inner = opt?.union ?? opt?.inner ?? schema;
|
|
1859
2484
|
const s = inner;
|
|
1860
2485
|
const fields = schemaFieldsOf(inner);
|
|
1861
2486
|
if (fields !== undefined)
|
|
@@ -1909,7 +2534,18 @@ const childAtSegment = (schema, segment) => {
|
|
|
1909
2534
|
};
|
|
1910
2535
|
/** The element schema of the list a path addresses (for append / prepend). */
|
|
1911
2536
|
const listElementOf = (schema) => {
|
|
1912
|
-
const
|
|
2537
|
+
const opt = optionalField(schema);
|
|
2538
|
+
if (opt?.union !== undefined) {
|
|
2539
|
+
const members = opt.union
|
|
2540
|
+
.members;
|
|
2541
|
+
for (const member of members) {
|
|
2542
|
+
const element = listElementOf(member);
|
|
2543
|
+
if (element !== undefined)
|
|
2544
|
+
return element;
|
|
2545
|
+
}
|
|
2546
|
+
return undefined;
|
|
2547
|
+
}
|
|
2548
|
+
const inner = opt?.inner ?? schema;
|
|
1913
2549
|
if (!SchemaAST.isArrays(inner.ast))
|
|
1914
2550
|
return undefined;
|
|
1915
2551
|
const value = inner.value;
|
|
@@ -2132,27 +2768,38 @@ refTargets = {}) => {
|
|
|
2132
2768
|
}
|
|
2133
2769
|
return out;
|
|
2134
2770
|
};
|
|
2135
|
-
/**
|
|
2771
|
+
/**
|
|
2772
|
+
* The model's own schema at a path, before any substitution, and whether the path goes through a
|
|
2773
|
+
* later member of an optional union, which is written as declared ({@link writesFirstMemberOnly}).
|
|
2774
|
+
*/
|
|
2136
2775
|
const originalAt = (segments) => {
|
|
2137
2776
|
const [head, ...rest] = segments;
|
|
2138
2777
|
if (typeof head !== "string")
|
|
2139
2778
|
return undefined;
|
|
2140
2779
|
let current = refTargets[head] ?? modelFields[head];
|
|
2780
|
+
let asDeclared = false;
|
|
2141
2781
|
for (const segment of rest) {
|
|
2142
2782
|
if (current === undefined)
|
|
2143
2783
|
return undefined;
|
|
2784
|
+
const opt = optionalField(current);
|
|
2785
|
+
if (opt?.union !== undefined && childAtSegment(opt.inner, segment) === undefined) {
|
|
2786
|
+
asDeclared = true;
|
|
2787
|
+
}
|
|
2144
2788
|
current = childAtSegment(current, segment);
|
|
2145
2789
|
}
|
|
2146
|
-
return current;
|
|
2790
|
+
return current === undefined ? undefined : { schema: current, asDeclared };
|
|
2147
2791
|
};
|
|
2148
2792
|
const schemaAt = (segments) => {
|
|
2149
|
-
const
|
|
2150
|
-
if (
|
|
2793
|
+
const at = originalAt(segments);
|
|
2794
|
+
if (at === undefined)
|
|
2151
2795
|
return undefined;
|
|
2796
|
+
const original = at.schema;
|
|
2152
2797
|
const head = segments[0];
|
|
2153
2798
|
const stored = segments.length === 1
|
|
2154
2799
|
? recordFields[head]
|
|
2155
|
-
:
|
|
2800
|
+
: at.asDeclared
|
|
2801
|
+
? original
|
|
2802
|
+
: substituteSchemaDeep(original, { enforceChecks: true });
|
|
2156
2803
|
return stored === undefined ? undefined : { original, stored };
|
|
2157
2804
|
};
|
|
2158
2805
|
const validators = new Map();
|