@effect-dynamodb/schema 1.40.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.
Files changed (104) hide show
  1. package/dist/Aggregate.d.ts.map +1 -1
  2. package/dist/Aggregate.js +4 -1
  3. package/dist/Aggregate.js.map +1 -1
  4. package/dist/DynamoModel.d.ts +35 -7
  5. package/dist/DynamoModel.d.ts.map +1 -1
  6. package/dist/DynamoModel.js +34 -16
  7. package/dist/DynamoModel.js.map +1 -1
  8. package/dist/DynamoSchema.d.ts +20 -0
  9. package/dist/DynamoSchema.d.ts.map +1 -1
  10. package/dist/DynamoSchema.js.map +1 -1
  11. package/dist/Entity.d.ts +5 -3
  12. package/dist/Entity.d.ts.map +1 -1
  13. package/dist/Entity.js +82 -32
  14. package/dist/Entity.js.map +1 -1
  15. package/dist/Errors.d.ts +61 -4
  16. package/dist/Errors.d.ts.map +1 -1
  17. package/dist/Errors.js +83 -6
  18. package/dist/Errors.js.map +1 -1
  19. package/dist/KeyComposer.d.ts +46 -9
  20. package/dist/KeyComposer.d.ts.map +1 -1
  21. package/dist/KeyComposer.js +148 -58
  22. package/dist/KeyComposer.js.map +1 -1
  23. package/dist/VectorIndex.d.ts.map +1 -1
  24. package/dist/VectorIndex.js +5 -4
  25. package/dist/VectorIndex.js.map +1 -1
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +1 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/internal/AggregateSchemas.d.ts +131 -6
  31. package/dist/internal/AggregateSchemas.d.ts.map +1 -1
  32. package/dist/internal/AggregateSchemas.js +250 -31
  33. package/dist/internal/AggregateSchemas.js.map +1 -1
  34. package/dist/internal/CompositeCodec.d.ts +26 -4
  35. package/dist/internal/CompositeCodec.d.ts.map +1 -1
  36. package/dist/internal/CompositeCodec.js +127 -25
  37. package/dist/internal/CompositeCodec.js.map +1 -1
  38. package/dist/internal/EffectInstances.d.ts +8 -2
  39. package/dist/internal/EffectInstances.d.ts.map +1 -1
  40. package/dist/internal/EffectInstances.js +33 -27
  41. package/dist/internal/EffectInstances.js.map +1 -1
  42. package/dist/internal/EntityPacked.d.ts.map +1 -1
  43. package/dist/internal/EntityPacked.js +5 -4
  44. package/dist/internal/EntityPacked.js.map +1 -1
  45. package/dist/internal/EntitySchemas.d.ts +136 -3
  46. package/dist/internal/EntitySchemas.d.ts.map +1 -1
  47. package/dist/internal/EntitySchemas.js +836 -80
  48. package/dist/internal/EntitySchemas.js.map +1 -1
  49. package/dist/internal/EntityTypes.d.ts +53 -20
  50. package/dist/internal/EntityTypes.d.ts.map +1 -1
  51. package/dist/internal/InstanceReads.d.ts +7 -0
  52. package/dist/internal/InstanceReads.d.ts.map +1 -1
  53. package/dist/internal/InstanceReads.js +47 -29
  54. package/dist/internal/InstanceReads.js.map +1 -1
  55. package/dist/internal/KeyReadings.d.ts +30 -0
  56. package/dist/internal/KeyReadings.d.ts.map +1 -0
  57. package/dist/internal/KeyReadings.js +99 -0
  58. package/dist/internal/KeyReadings.js.map +1 -0
  59. package/dist/internal/KeyText136.d.ts.map +1 -1
  60. package/dist/internal/KeyText136.js +6 -4
  61. package/dist/internal/KeyText136.js.map +1 -1
  62. package/dist/internal/LostValues.d.ts +50 -0
  63. package/dist/internal/LostValues.d.ts.map +1 -0
  64. package/dist/internal/LostValues.js +465 -0
  65. package/dist/internal/LostValues.js.map +1 -0
  66. package/dist/internal/ObjectKeyText.d.ts +29 -0
  67. package/dist/internal/ObjectKeyText.d.ts.map +1 -0
  68. package/dist/internal/ObjectKeyText.js +50 -0
  69. package/dist/internal/ObjectKeyText.js.map +1 -0
  70. package/dist/internal/OwnNames.d.ts +205 -0
  71. package/dist/internal/OwnNames.d.ts.map +1 -0
  72. package/dist/internal/OwnNames.js +415 -0
  73. package/dist/internal/OwnNames.js.map +1 -0
  74. package/dist/internal/PackedCodec.d.ts +7 -8
  75. package/dist/internal/PackedCodec.d.ts.map +1 -1
  76. package/dist/internal/PackedCodec.js +81 -55
  77. package/dist/internal/PackedCodec.js.map +1 -1
  78. package/dist/internal/RestoredKeyText.d.ts +10 -0
  79. package/dist/internal/RestoredKeyText.d.ts.map +1 -0
  80. package/dist/internal/RestoredKeyText.js +50 -0
  81. package/dist/internal/RestoredKeyText.js.map +1 -0
  82. package/dist/internal/SchemaAccessors.d.ts +132 -12
  83. package/dist/internal/SchemaAccessors.d.ts.map +1 -1
  84. package/dist/internal/SchemaAccessors.js +779 -56
  85. package/dist/internal/SchemaAccessors.js.map +1 -1
  86. package/dist/internal/SchemaIssue.d.ts +21 -0
  87. package/dist/internal/SchemaIssue.d.ts.map +1 -0
  88. package/dist/internal/SchemaIssue.js +34 -0
  89. package/dist/internal/SchemaIssue.js.map +1 -0
  90. package/dist/internal/SharedKeyAttribute.d.ts.map +1 -1
  91. package/dist/internal/SharedKeyAttribute.js +2 -1
  92. package/dist/internal/SharedKeyAttribute.js.map +1 -1
  93. package/dist/internal/StoredForm.d.ts +139 -0
  94. package/dist/internal/StoredForm.d.ts.map +1 -0
  95. package/dist/internal/StoredForm.js +503 -0
  96. package/dist/internal/StoredForm.js.map +1 -0
  97. package/dist/internal/ZOrderCurve.d.ts.map +1 -1
  98. package/dist/internal/ZOrderCurve.js +2 -1
  99. package/dist/internal/ZOrderCurve.js.map +1 -1
  100. package/dist/internal/ZoneKeyText.d.ts +55 -0
  101. package/dist/internal/ZoneKeyText.d.ts.map +1 -0
  102. package/dist/internal/ZoneKeyText.js +264 -0
  103. package/dist/internal/ZoneKeyText.js.map +1 -0
  104. package/package.json +4 -4
@@ -816,6 +816,42 @@ export const holdsCustomDeclaration = (ast, seen = new Set()) => {
816
816
  return false;
817
817
  }
818
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
+ };
819
855
  /**
820
856
  * The value on a stored side that DynamoDB can't store, if there is one. With
821
857
  * both in one union, `undefined` is what an absent attribute means.
@@ -1012,6 +1048,308 @@ export const holdsAbsentValue = (ast, seen = new Set()) => {
1012
1048
  * this belongs to a write and is never asked while an entity is defined.
1013
1049
  */
1014
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
+ };
1015
1353
  const SCALARS = new Set([
1016
1354
  "Null",
1017
1355
  "Undefined",
@@ -1029,10 +1367,31 @@ const SCALARS = new Set([
1029
1367
  const bare = (ast) => ast.checks === undefined &&
1030
1368
  ast.encoding === undefined &&
1031
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);
1032
1387
  /** What the guard models a node as ({@link Modelled}), or `undefined` for one it doesn't. */
1033
1388
  const modelled = (ast) => {
1034
1389
  if (SchemaAST.isDeclaration(ast))
1035
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
+ }
1036
1395
  if (!bare(ast))
1037
1396
  return undefined;
1038
1397
  switch (ast._tag) {
@@ -1040,13 +1399,9 @@ const modelled = (ast) => {
1040
1399
  if (ast.indexSignatures.length === 0) {
1041
1400
  return ast.propertySignatures.length > 0 ? { _tag: "Struct", ast } : undefined;
1042
1401
  }
1043
- const [index, ...more] = ast.indexSignatures;
1044
- return index !== undefined &&
1045
- more.length === 0 &&
1046
- ast.propertySignatures.length === 0 &&
1047
- index.parameter._tag === "String" &&
1048
- bare(index.parameter)
1049
- ? { _tag: "Record", value: index.type }
1402
+ return ast.propertySignatures.length === 0 &&
1403
+ ast.indexSignatures.every((signature) => signatureKey(signature.parameter))
1404
+ ? { _tag: "Record", signatures: ast.indexSignatures }
1050
1405
  : undefined;
1051
1406
  }
1052
1407
  case "Arrays":
@@ -1076,51 +1431,196 @@ const classStruct = (ast) => {
1076
1431
  const struct = modelled(link.to);
1077
1432
  return struct?._tag === "Struct" ? struct : undefined;
1078
1433
  };
1079
- const endsOf = (ast, segments, ends, seen = new Set()) => {
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) => {
1080
1449
  const node = seen.has(ast) ? undefined : modelled(ast);
1450
+ const mark = since140 === undefined ? {} : { since140 };
1081
1451
  if (node === undefined) {
1082
- ends.push({ _tag: "Unknown" });
1452
+ ways.push({ end: { _tag: "Unknown" }, structs, ...mark });
1083
1453
  return;
1084
1454
  }
1085
1455
  if (node._tag === "Union" || node._tag === "Suspend") {
1086
1456
  const within = new Set(seen).add(ast);
1087
1457
  for (const member of node._tag === "Union" ? node.members : [node.suspended]) {
1088
- endsOf(member, segments, ends, within);
1458
+ waysOf(member, segments, ways, structs, within, since140);
1089
1459
  }
1090
1460
  return;
1091
1461
  }
1462
+ const here = [...structs, node._tag === "Struct" ? node.ast : undefined];
1092
1463
  const [head, ...rest] = segments;
1093
1464
  if (head === undefined) {
1094
- ends.push({ _tag: "At", node });
1465
+ ways.push({ end: { _tag: "At", node }, structs: here, ...mark });
1095
1466
  return;
1096
1467
  }
1097
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;
1098
1471
  const property = node.ast.propertySignatures.find((signature) => signature.name === head);
1099
- if (property === undefined)
1100
- ends.push({ _tag: "Unread" });
1472
+ if (property === undefined) {
1473
+ ways.push({
1474
+ end: { _tag: "Unread" },
1475
+ structs: here,
1476
+ ...(struct === undefined ? {} : { since140: struct }),
1477
+ });
1478
+ }
1101
1479
  else
1102
- endsOf(property.type, rest, ends);
1480
+ waysOf(property.type, rest, ways, here, new Set(), struct);
1103
1481
  }
1104
1482
  else if (node._tag === "Record" && typeof head === "string") {
1105
- endsOf(node.value, rest, ends);
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);
1106
1497
  }
1107
1498
  else if (node._tag === "List" && typeof head === "number") {
1108
1499
  const element = node.ast.elements[head] ?? node.ast.rest[0];
1109
1500
  if (element !== undefined)
1110
- endsOf(element, rest, ends);
1501
+ waysOf(element, rest, ways, here, new Set(), since140);
1111
1502
  }
1112
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
+ });
1113
1611
  /**
1114
- * Removing a property `name` from the maps at `ends`. A struct requires it when a row can't be
1115
- * without it ({@link rowCanLack}, asked of the property as of a field). A record never does, and
1116
- * a struct without it holds no such value, so removing it leaves the value as it was. Lists and
1117
- * scalars hold no property, so a value of theirs that held one isn't one this reads.
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.
1118
1618
  */
1119
- const propertyRemoval = (ends, name) => {
1120
- if (ends.some((end) => end._tag === "Unknown"))
1619
+ const propertyRemoval = (ways, name) => {
1620
+ if (ways.some(({ end }) => end._tag === "Unknown"))
1121
1621
  return "unknown";
1122
1622
  let held = false;
1123
- for (const end of ends) {
1623
+ for (const { end } of ways) {
1124
1624
  if (end._tag === "Unread")
1125
1625
  return "readable";
1126
1626
  if (end._tag !== "At")
@@ -1138,31 +1638,93 @@ const propertyRemoval = (ends, name) => {
1138
1638
  }
1139
1639
  return held ? "required" : "unheld";
1140
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];
1141
1648
  /**
1142
- * Removing list position `position` from the lists at `ends`. A list closes up when an element
1143
- * is removed, so what's left is one shorter, and is judged by length alone: a list accepts from
1144
- * its required positions to all of them, or with a rest any length from its required positions
1145
- * on. Readable when some list may be too short to hold the position (DynamoDB leaves it as it
1146
- * was), or one shorter than a length some list accepts is a length some list accepts. An element
1147
- * that can fill a missing one ({@link mayReadMissingKey}) makes the lengths unknown. Structs,
1148
- * records and scalars hold no list, so a value of theirs that held one isn't one this reads.
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.
1149
1655
  */
1150
- const positionRemoval = (ends, position) => {
1151
- if (ends.some((end) => end._tag === "Unknown"))
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"))
1152
1688
  return "unknown";
1153
- if (ends.some((end) => end._tag === "Unread"))
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"))
1154
1720
  return "readable";
1155
1721
  const lengths = [];
1156
- for (const end of ends) {
1722
+ for (const { end } of ways) {
1157
1723
  if (end._tag !== "At" || end.node._tag !== "List")
1158
1724
  continue;
1159
- const { elements, rest } = end.node.ast;
1160
- if (elements.some((element) => mayReadMissingKey(element)))
1725
+ if (end.node.ast.elements.some((element) => mayReadMissingKey(element)))
1161
1726
  return "unknown";
1162
- lengths.push({
1163
- min: elements.filter((element) => !SchemaAST.isOptional(element)).length,
1164
- max: rest.length > 0 ? Number.POSITIVE_INFINITY : elements.length,
1165
- });
1727
+ lengths.push(lengthsOf(end.node.ast));
1166
1728
  }
1167
1729
  if (lengths.length === 0)
1168
1730
  return "unheld";
@@ -1171,6 +1733,25 @@ const positionRemoval = (ends, position) => {
1171
1733
  const shorter = (list) => lengths.some((other) => other.min <= list.max - 1 && list.min - 1 <= other.max);
1172
1734
  return lengths.some(shorter) ? "readable" : "required";
1173
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
+ };
1174
1755
  /**
1175
1756
  * Whether removing `segments`, a path inside this field, leaves a value the field's schema
1176
1757
  * refuses, for every value it reads that holds the path, while none that holds what's above it
@@ -1178,28 +1759,170 @@ const positionRemoval = (ends, position) => {
1178
1759
  * as a read decodes them ({@link Modelled}); `false` for anything else, and the removal is sent
1179
1760
  * as 1.39.0 sent it.
1180
1761
  *
1181
- * The walk takes every way through the schema to the path, through unions at every level, so a
1182
- * value one member holds is judged against every member: a struct member without a property on
1183
- * the way doesn't read what a value holds under it, so it may read what's left, and the removal
1184
- * is sent. A property is required when a row can't be without it ({@link rowCanLack}): a required
1185
- * key nothing fills, whose absent attribute isn't one of its values. A list position is judged by
1186
- * length alone ({@link positionRemoval}): removing any position of a `[string, string]` is
1187
- * required; `f[0]` of a `[string, string?]`, either position of
1188
- * `Union([[string, string], [string]])` and an array's element aren't. Length alone can't see
1189
- * that what's left puts a value where its position can't hold it (`f[0]` of a
1190
- * `[string, number?]`), or is a list only a member a tag rules out accepts, so those aren't
1191
- * either, as 1.39.0 sent them.
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.
1192
1782
  *
1193
1783
  * `false` means the schema can't say every such row breaks: a record's keys, a list's elements,
1194
1784
  * a path no value holds (DynamoDB refuses it, as on 1.39.0), and anything the walk doesn't model.
1195
1785
  */
1196
- export const pathIsRequired = (field, segments) => {
1786
+ export const pathIsRequired = (field, segments, update = {}) => {
1197
1787
  const last = segments[segments.length - 1];
1198
1788
  if (last === undefined)
1199
1789
  return false;
1200
- const ends = [];
1201
- endsOf(field.ast, segments.slice(0, -1), ends);
1202
- const removal = typeof last === "number" ? positionRemoval(ends, last) : propertyRemoval(ends, last);
1203
- return removal === "required";
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;
1204
1927
  };
1205
1928
  //# sourceMappingURL=SchemaAccessors.js.map