viell-effect-schema-http 0.5.0 → 0.6.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.
@@ -1,4 +1,4 @@
1
- import { Effect, Schema, type SchemaAST } from "effect";
1
+ import { Effect, Schema, SchemaAST } from "effect";
2
2
  import * as Excess from "./excess.js";
3
3
 
4
4
  /** What a schema's constructor takes. This is the one place that names the `~`-prefixed key. */
@@ -199,6 +199,44 @@ export const reader = <S extends Schema.Top, A, R>(
199
199
  };
200
200
  };
201
201
 
202
+ /**
203
+ * Whole-union alternatives for a bare anyOf group. Preserve every member and the whole input:
204
+ * trying one member alone would move its decoder ahead of another member's constructor.
205
+ * Parent behavior is deliberately excluded rather than copied or silently discarded.
206
+ */
207
+ export const reorderedReaders = <S extends Schema.Top>(contract: S) => {
208
+ const ast = contract.ast;
209
+ if (
210
+ ast._tag !== "Union" ||
211
+ (ast.options?.mode ?? "anyOf") !== "anyOf" ||
212
+ ast.encoding !== undefined ||
213
+ ast.context !== undefined ||
214
+ ast.checks !== undefined ||
215
+ ast.encodingChecks !== undefined ||
216
+ (ast.annotations !== undefined &&
217
+ Reflect.ownKeys(ast.annotations).length > 0) ||
218
+ !ast.types.every(
219
+ (member) =>
220
+ member._tag === "Objects" &&
221
+ member.encoding === undefined &&
222
+ member.indexSignatures.length === 0 &&
223
+ !member.propertySignatures.some(({ name }) => name === Symbol.iterator),
224
+ )
225
+ )
226
+ return [];
227
+ // The unchanged order already supplied `original`; repeating it adds no selection alternative.
228
+ return ast.types.slice(1).map((member, offset) => {
229
+ const index = offset + 1;
230
+ const reordered = Schema.make<S>(
231
+ new SchemaAST.Union(
232
+ [member, ...ast.types.filter((_, position) => position !== index)],
233
+ ast.options,
234
+ ),
235
+ );
236
+ return reader(reordered, Schema.encodeEffect(reordered));
237
+ });
238
+ };
239
+
202
240
  /**
203
241
  * Refuse the fields a schema would lose, unless another schema in the group took them. Only a field
204
242
  * of the group itself can be taken, so one further down a value stays lost.
@@ -20,6 +20,8 @@ export interface Taken {
20
20
  readonly fields: ReadonlySet<PropertyKey>;
21
21
  /** The compared names schemas with no union allow. Each settles a raw value under its names. */
22
22
  readonly settled: ReadonlySet<string>;
23
+ /** Actual claims per applied binding; absent on legacy custom drafts, which cannot recover. */
24
+ readonly certificates?: ReadonlyMap<object, ReadonlySet<PropertyKey>>;
23
25
  }
24
26
 
25
27
  /** What a schema's encoded side states, in the schema's own spelling. */
@@ -253,11 +255,44 @@ export const others = (
253
255
  export const taking = (
254
256
  taken: Taken | undefined,
255
257
  own: ReadonlySet<PropertyKey>,
258
+ token: object,
256
259
  ): Taken => ({
257
260
  fields: new Set([...(taken?.fields ?? []), ...own]),
258
261
  settled: taken?.settled ?? new Set(),
262
+ ...(taken?.certificates === undefined
263
+ ? {}
264
+ : { certificates: new Map([...taken.certificates, [token, own]]) }),
259
265
  });
260
266
 
267
+ /** Replace one binding's claim only if no previously certified name disappears. */
268
+ export const retaking = (
269
+ taken: Taken,
270
+ own: ReadonlySet<PropertyKey>,
271
+ token: object,
272
+ ): Taken | undefined => {
273
+ if (taken.certificates === undefined || !taken.certificates.has(token))
274
+ return undefined;
275
+ const certificates = new Map(taken.certificates);
276
+ certificates.set(token, own);
277
+ const fields = new Set(
278
+ Array.from(certificates.values()).flatMap((set) => [...set]),
279
+ );
280
+ return Array.from(taken.fields).every((name) => fields.has(name))
281
+ ? { ...taken, fields, certificates }
282
+ : undefined;
283
+ };
284
+
285
+ /** Names an actual other binding took, never merely names it permits. */
286
+ export const certifiedElsewhere = (
287
+ taken: Taken,
288
+ token: object,
289
+ ): ReadonlySet<PropertyKey> =>
290
+ new Set(
291
+ Array.from(taken.certificates ?? []).flatMap(([binding, fields]) =>
292
+ binding === token ? [] : [...fields],
293
+ ),
294
+ );
295
+
261
296
  /**
262
297
  * What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
263
298
  * can write less than it was given, and that must not pass as a group that was built.
@@ -275,7 +310,7 @@ export const lost = (
275
310
  };
276
311
 
277
312
  /** Only stable absent/data properties prove the caller did not supply this field. */
278
- const omitted = (input: object, name: string): boolean => {
313
+ const omitted = (input: object, name: PropertyKey): boolean => {
279
314
  const property = Object.getOwnPropertyDescriptor(input, name);
280
315
  return property === undefined
281
316
  ? !(name in input)
@@ -315,15 +350,46 @@ export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
315
350
  typeof name === "string" ? [name] : [],
316
351
  )
317
352
  : [];
353
+ const end = Excess.encodedEnd(contract.ast);
354
+ const closedEndpoints =
355
+ contract.ast._tag === "Objects" &&
356
+ contract.ast.indexSignatures.length === 0 &&
357
+ end._tag === "Objects" &&
358
+ end.indexSignatures.length === 0
359
+ ? [contract.ast, end]
360
+ : [];
361
+ const encodedNames =
362
+ end._tag === "Objects"
363
+ ? end.propertySignatures.flatMap(({ name }) =>
364
+ typeof name === "string" ? [name] : [],
365
+ )
366
+ : [];
367
+ /** No caller field can explain generated output when both closed endpoints are absent. */
368
+ const empty = (input: object): boolean => {
369
+ const prototype: unknown = Object.getPrototypeOf(input);
370
+ return (
371
+ closedEndpoints.length > 0 &&
372
+ (prototype === Object.prototype || prototype === null) &&
373
+ Reflect.ownKeys(input).length === 0 &&
374
+ closedEndpoints.every((endpoint) =>
375
+ endpoint.propertySignatures.every(
376
+ ({ name }) => omitted(input, name) && !(name in input),
377
+ ),
378
+ )
379
+ );
380
+ };
318
381
  /** Snapshot caller omission before preparation can run a default or encoder. */
319
382
  const candidates = (
320
383
  input: unknown,
321
384
  edits: ReadonlySet<string> | undefined,
322
385
  ): ReadonlyArray<string> =>
323
386
  edits !== undefined && typeof input === "object" && input !== null
324
- ? directNames.filter(
325
- (name) => edits.has(fold(name)) && omitted(input, name),
326
- )
387
+ ? (contract.ast.encoding === undefined
388
+ ? directNames
389
+ : empty(input)
390
+ ? encodedNames
391
+ : []
392
+ ).filter((name) => edits.has(fold(name)) && omitted(input, name))
327
393
  : [];
328
394
  /** Preparation supplied these omitted, edited fields in the record it actually writes. */
329
395
  const filled = (