Repository navigation
Expand file tree
/
Copy pathtypes.ts
More file actions
128 lines (106 loc) · 4.47 KB
/
Copy pathtypes.ts
File metadata and controls
128 lines (106 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Data, ParseResult, Schema } from 'effect'
import type { JsonValue } from '../schema'
// ---------------------------------------------------------------------------
// Decoded value — recursive type for values in the decoded tree
// ---------------------------------------------------------------------------
/**
* A value in the decoded tree. By JsonSafeSchema construction, every field
* is either a `JsonValue` scalar/container or an `Option` wrapping one.
*
* `JsonValue` alone is insufficient because decoded objects contain Option
* fields (e.g. `{ id: string, label: Option<string> }`) — `Option<JsonValue>`
* does not satisfy `JsonValue`'s `{ readonly [key: string]: JsonValue }`
* index signature. This recursive type allows Options at any depth.
*
* Interfaces are used for indirect recursion (TypeScript restriction).
* `DecodedSome` and `DecodedNone` are structurally identical to Effect's
* `Option.Some` and `Option.None` — `Option.isOption` works at runtime.
*/
interface DecodedArray extends ReadonlyArray<DecodedValue> {}
interface DecodedRecord {
readonly [key: string]: DecodedValue
}
export interface DecodedSome {
readonly _tag: 'Some'
readonly value: DecodedValue
}
export interface DecodedNone {
readonly _tag: 'None'
}
export type DecodedValue =
| string
| number
| boolean
| null
| DecodedArray
| DecodedRecord
| DecodedSome
| DecodedNone
// ---------------------------------------------------------------------------
// Path
// ---------------------------------------------------------------------------
export type Path = readonly (string | number)[]
// ---------------------------------------------------------------------------
// Patch operations (decoded-level, path arrays not JSON Pointer strings)
// ---------------------------------------------------------------------------
export type DecodedPatchOp =
| { readonly op: 'replace'; readonly path: Path; readonly value: JsonValue }
| { readonly op: 'remove'; readonly path: Path }
| { readonly op: 'add'; readonly path: Path; readonly value: JsonValue }
| { readonly op: 'move'; readonly from: Path; readonly to: Path }
// ---------------------------------------------------------------------------
// Sub-schema type
// ---------------------------------------------------------------------------
export type JsonSubSchema = Schema.Schema<DecodedValue, JsonValue, never>
export type InnerSubSchema = JsonSubSchema
// ---------------------------------------------------------------------------
// Compiled field metadata
// ---------------------------------------------------------------------------
export interface CompiledField {
readonly isOption: boolean
readonly hasDefault: boolean
readonly defaultValue: JsonValue
readonly subSchema: JsonSubSchema
readonly innerSubSchema: InnerSubSchema | null
}
// ---------------------------------------------------------------------------
// Compiled map — generic over the decoded type A
// ---------------------------------------------------------------------------
export interface CompiledMap<A = DecodedValue> {
fieldAt(path: Path, decodedRoot?: A): CompiledField | null
subSchemaAt(path: Path, decodedRoot?: A): JsonSubSchema | null
}
// ---------------------------------------------------------------------------
// Errors — distinct tagged errors per failure category
// ---------------------------------------------------------------------------
/** Schema encoding failed at a path during diff. */
export class PatchEncodeError extends Data.TaggedError('PatchEncodeError')<{
readonly path: Path
readonly cause: ParseResult.ParseError
}> {}
/** Schema decoding failed at a path during apply. */
export class PatchDecodeError extends Data.TaggedError('PatchDecodeError')<{
readonly path: Path
readonly cause: ParseResult.ParseError
}> {}
/** Structural navigation failure — the path is valid but the value can't be traversed. */
export class PatchNavigationError extends Data.TaggedError('PatchNavigationError')<{
readonly path: Path
readonly reason:
| 'option_none'
| 'null'
| 'non_container'
| 'cannot_remove_root'
| 'move_source_missing'
| 'move_source_none'
}> {}
/** No sub-schema resolved at a path — the compiled map has no field info. */
export class PatchSchemaError extends Data.TaggedError('PatchSchemaError')<{
readonly path: Path
}> {}
/** Union of all patch errors. */
export type PatchApplyError =
| PatchEncodeError
| PatchDecodeError
| PatchNavigationError
| PatchSchemaError