| 3960 | * @internal |
| 3961 | */ |
| 3962 | export const snapshotMapSchema = ( |
| 3963 | query: { readonly [TypeId]: unknown }, |
| 3964 | label: string, |
| 3965 | log = false |
| 3966 | ): Schema.Top => { |
| 3967 | const def = defOf(query) |
| 3968 | const is = (u: unknown): u is SnapshotMapImpl => |
| 3969 | u instanceof SnapshotMapImpl && |
| 3970 | u[SnapshotMapTypeId].def.name === def.name && |
| 3971 | u[SnapshotMapTypeId].label === label |
| 3972 | const makeRun = (decoding: boolean) => (typeParameters: ReadonlyArray<SchemaAST.AST>) => { |
| 3973 | const one = snapshotRun(def, label, decoding)(typeParameters) |
| 3974 | return ( |
| 3975 | input: unknown, |
| 3976 | ast: SchemaAST.Declaration, |
| 3977 | options: SchemaAST.ParseOptions |
| 3978 | ): Effect.Effect<SnapshotMapImpl, SchemaIssue.Issue> => { |
| 3979 | if (!is(input)) { |
| 3980 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 3981 | } |
| 3982 | return Effect.map( |
| 3983 | Effect.forEach( |
| 3984 | input[SnapshotMapTypeId].entries.values(), |
| 3985 | (snapshot) => one(snapshot, ast, options) |
| 3986 | ), |
| 3987 | (snapshots) => makeSnapshotMap(def, label, snapshots) |
| 3988 | ) |
| 3989 | } |
| 3990 | } |
| 3991 | const toCodecJson = (typeParameters: ReadonlyArray<Schema.Top>) => { |
| 3992 | const wire = snapshotWire(def, label, typeParameters, log) |
| 3993 | return Schema.link<SnapshotMapImpl>()( |
| 3994 | Schema.Array(wire.tuple), |
| 3995 | SchemaTransformation.transformEffect<SnapshotMapImpl, ReadonlyArray<any>>( |
| 3996 | { |
| 3997 | decode: (tuples, options) => |
| 3998 | Effect.flatMap( |
| 3999 | Effect.forEach(tuples, (tuple) => wire.decode(tuple, options)), |
| 4000 | (snapshots) => { |
| 4001 | const keys = new Set<string>() |
| 4002 | for (const snapshot of snapshots) { |
| 4003 | const key = snapshot[SnapshotTypeId].argsKey |
| 4004 | if (key === undefined) { |
| 4005 | return Effect.fail( |
| 4006 | snapshotIssue( |
| 4007 | def, |
| 4008 | "a keyed field holds an empty snapshot" |
| 4009 | ) |
| 4010 | ) |
| 4011 | } |
| 4012 | if (keys.has(key)) { |
| 4013 | return Effect.fail( |
| 4014 | snapshotIssue( |
| 4015 | def, |
| 4016 | "a keyed field holds one args key twice" |
| 4017 | ) |
| 4018 | ) |
| 4019 | } |