Repository navigation
Expand file tree
/
Copy pathapply.ts
More file actions
316 lines (269 loc) · 11.2 KB
/
Copy pathapply.ts
File metadata and controls
316 lines (269 loc) · 11.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { Effect, Schema, Option } from 'effect'
import type { CompiledField, CompiledMap, DecodedPatchOp, DecodedValue, DecodedSome, DecodedNone, Path, PatchApplyError } from './types'
import type { JsonValue } from '../schema'
import { PatchDecodeError, PatchNavigationError, PatchSchemaError, PatchEncodeError } from './types'
// ---------------------------------------------------------------------------
// Type guards — proper narrowing, no `as`, no `unknown`
// ---------------------------------------------------------------------------
function isScalar(v: DecodedValue): v is string | number | boolean | null {
return v === null || typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean'
}
function isDecodedOption(v: DecodedValue): v is DecodedSome | DecodedNone {
return typeof v === 'object' && v !== null && !Array.isArray(v) && '_tag' in v && (v._tag === 'Some' || v._tag === 'None')
}
function isRecord(v: DecodedValue): v is { readonly [key: string]: DecodedValue } {
return typeof v === 'object' && v !== null && !Array.isArray(v) && !isDecodedOption(v)
}
function isArray(v: DecodedValue): v is readonly DecodedValue[] {
return Array.isArray(v)
}
// ---------------------------------------------------------------------------
// Decode a leaf value — Effect-native, typed
// ---------------------------------------------------------------------------
function decodeLeaf(
opValue: JsonValue,
field: CompiledField | null,
): Effect.Effect<DecodedValue, PatchApplyError> {
if (isScalar(opValue)) {
if (field !== null && field.isOption) {
return Effect.succeed(Option.some(opValue))
}
return Effect.succeed(opValue)
}
if (field !== null && field.isOption && field.innerSubSchema !== null) {
return Schema.decode(field.innerSubSchema)(opValue).pipe(
Effect.mapBoth({
onFailure: (cause) => new PatchDecodeError({ path: [], cause }),
onSuccess: (decoded) => Option.some(decoded),
}),
)
}
if (field !== null) {
return Schema.decode(field.subSchema)(opValue).pipe(
Effect.mapError((cause) => new PatchDecodeError({ path: [], cause })),
)
}
return Effect.fail(new PatchSchemaError({ path: [] }))
}
// ---------------------------------------------------------------------------
// Unwrap Option for navigation
// ---------------------------------------------------------------------------
function unwrapOption(v: DecodedValue): DecodedValue | null {
if (isDecodedOption(v)) {
return v._tag === 'Some' ? v.value : null
}
return v
}
function toIndex(key: string | number): number {
return typeof key === 'number' ? key : parseInt(String(key), 10)
}
// ---------------------------------------------------------------------------
// Recursive replace/add — Effect.gen
// ---------------------------------------------------------------------------
function replaceOrAdd(
current: DecodedValue,
path: Path,
fullPath: Path,
value: JsonValue,
isAdd: boolean,
compiled: CompiledMap<DecodedValue>,
decodedRoot: DecodedValue,
): Effect.Effect<DecodedValue, PatchApplyError> {
return Effect.gen(function* () {
if (path.length === 0) {
const field = compiled.fieldAt(fullPath, decodedRoot)
return yield* decodeLeaf(value, field)
}
const key = path[0]
const rest = path.slice(1)
const unwrapped = unwrapOption(current)
if (unwrapped === null) {
// For replace ops, navigating into Option.none or null is an error
if (!isAdd) {
return yield* Effect.fail(new PatchNavigationError({ path: fullPath, reason: 'option_none' }))
}
// For add ops, auto-create container for new paths
if (typeof key === 'number') {
const newArr: DecodedValue[] = []
const newChild = yield* replaceOrAdd(Option.none(), rest, fullPath, value, isAdd, compiled, decodedRoot)
if (isAdd) {
newArr.splice(toIndex(key), 0, newChild)
} else {
newArr[toIndex(key)] = newChild
}
const isOptionWrapped = isDecodedOption(current)
return isOptionWrapped ? Option.some(newArr) : newArr
}
const newObj: Record<string, DecodedValue> = {}
const newChild = yield* replaceOrAdd(Option.none(), rest, fullPath, value, isAdd, compiled, decodedRoot)
newObj[String(key)] = newChild
const isOptionWrapped = isDecodedOption(current)
return isOptionWrapped ? Option.some(newObj) : newObj
}
if (!isArray(unwrapped) && !isRecord(unwrapped)) {
return yield* Effect.fail(new PatchNavigationError({ path: fullPath, reason: 'non_container' }))
}
const isOptionWrapped = isDecodedOption(current)
if (rest.length === 0) {
// AT LEAF — apply the operation
const field = compiled.fieldAt(fullPath, decodedRoot)
const decodedValue = yield* decodeLeaf(value, field)
if (isArray(unwrapped)) {
const idx = toIndex(key)
const newArr = isAdd
? [...unwrapped.slice(0, idx), decodedValue, ...unwrapped.slice(idx)]
: [...unwrapped.slice(0, idx), decodedValue, ...unwrapped.slice(idx + 1)]
return isOptionWrapped ? Option.some(newArr) : newArr
}
const newObj = { ...unwrapped, [String(key)]: decodedValue }
return isOptionWrapped ? Option.some(newObj) : newObj
}
// DESCEND — recurse into the child, then rebuild
const child: DecodedValue = isArray(unwrapped)
? (unwrapped[toIndex(key)] ?? Option.none())
: (unwrapped[String(key)] ?? Option.none())
const newChild = yield* replaceOrAdd(child, rest, fullPath, value, isAdd, compiled, decodedRoot)
const rebuilt: DecodedValue = isArray(unwrapped)
? [...unwrapped.slice(0, toIndex(key)), newChild, ...unwrapped.slice(toIndex(key) + 1)]
: { ...unwrapped, [String(key)]: newChild }
return isOptionWrapped ? Option.some(rebuilt) : rebuilt
})
}
// ---------------------------------------------------------------------------
// Recursive remove — Effect.gen
// ---------------------------------------------------------------------------
function removeAt(
current: DecodedValue,
path: Path,
fullPath: Path,
compiled: CompiledMap<DecodedValue>,
decodedRoot: DecodedValue,
): Effect.Effect<DecodedValue, PatchApplyError> {
return Effect.gen(function* () {
if (path.length === 0) {
return yield* Effect.fail(new PatchNavigationError({ path: fullPath, reason: 'cannot_remove_root' }))
}
const key = path[0]
const rest = path.slice(1)
const unwrapped = unwrapOption(current)
if (unwrapped === null) {
return yield* Effect.fail(new PatchNavigationError({ path: fullPath, reason: 'option_none' }))
}
if (!isArray(unwrapped) && !isRecord(unwrapped)) {
return yield* Effect.fail(new PatchNavigationError({ path: fullPath, reason: 'non_container' }))
}
const isOptionWrapped = isDecodedOption(current)
if (rest.length === 0) {
// AT LEAF — apply removal
if (isArray(unwrapped)) {
const idx = toIndex(key)
const newArr = [...unwrapped.slice(0, idx), ...unwrapped.slice(idx + 1)]
return isOptionWrapped ? Option.some(newArr) : newArr
}
// Object removal — resolve field metadata at the leaf
const field = compiled.fieldAt(fullPath, decodedRoot)
if (field !== null) {
if (field.isOption) {
const newObj = { ...unwrapped, [String(key)]: Option.none() }
return isOptionWrapped ? Option.some(newObj) : newObj
} else if (field.hasDefault) {
const newObj = { ...unwrapped, [String(key)]: field.defaultValue }
return isOptionWrapped ? Option.some(newObj) : newObj
}
}
// Plain field or record entry — delete key
const newObj = { ...unwrapped }
delete newObj[String(key)]
return isOptionWrapped ? Option.some(newObj) : newObj
}
// DESCEND — recurse, then rebuild
const child: DecodedValue = isArray(unwrapped)
? (unwrapped[toIndex(key)] ?? Option.none())
: (unwrapped[String(key)] ?? Option.none())
const newChild = yield* removeAt(child, rest, fullPath, compiled, decodedRoot)
const rebuilt: DecodedValue = isArray(unwrapped)
? [...unwrapped.slice(0, toIndex(key)), newChild, ...unwrapped.slice(toIndex(key) + 1)]
: { ...unwrapped, [String(key)]: newChild }
return isOptionWrapped ? Option.some(rebuilt) : rebuilt
})
}
// ---------------------------------------------------------------------------
// Move — Effect.gen
// ---------------------------------------------------------------------------
function encodeForMove(
compiled: CompiledMap<DecodedValue>,
targetPath: Path,
value: DecodedValue,
decodedRoot: DecodedValue,
): Effect.Effect<JsonValue, PatchApplyError> {
const subSchema = compiled.subSchemaAt(targetPath, decodedRoot)
if (subSchema !== null) {
return Schema.encode(subSchema)(value).pipe(
Effect.mapError((cause) => new PatchEncodeError({ path: targetPath, cause })),
)
}
return Effect.fail(new PatchSchemaError({ path: targetPath }))
}
function moveOp(
root: DecodedValue,
from: Path,
to: Path,
compiled: CompiledMap<DecodedValue>,
decodedRoot: DecodedValue,
): Effect.Effect<DecodedValue, PatchApplyError> {
return Effect.gen(function* () {
// Extract the value at 'from' by navigating
let value: DecodedValue = root
for (const key of from) {
const unwrapped = unwrapOption(value)
if (unwrapped === null || (!isArray(unwrapped) && !isRecord(unwrapped))) {
return yield* Effect.fail(new PatchNavigationError({ path: from, reason: 'move_source_missing' }))
}
value = isArray(unwrapped)
? (unwrapped[toIndex(key)] ?? Option.none())
: (unwrapped[String(key)] ?? Option.none())
}
const extracted = unwrapOption(value)
if (extracted === null) {
return yield* Effect.fail(new PatchNavigationError({ path: from, reason: 'move_source_none' }))
}
// Remove from source
const afterRemove = yield* removeAt(root, from, from, compiled, decodedRoot)
// Add at target
const encoded = isScalar(extracted) ? extracted : yield* encodeForMove(compiled, to, extracted, afterRemove)
return yield* replaceOrAdd(afterRemove, to, to, encoded, true, compiled, afterRemove)
})
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
function applyOp(
root: DecodedValue,
op: DecodedPatchOp,
compiled: CompiledMap<DecodedValue>,
decodedRoot: DecodedValue,
): Effect.Effect<DecodedValue, PatchApplyError> {
switch (op.op) {
case 'replace':
return replaceOrAdd(root, op.path, op.path, op.value, false, compiled, root)
case 'add':
return replaceOrAdd(root, op.path, op.path, op.value, true, compiled, root)
case 'remove':
return removeAt(root, op.path, op.path, compiled, root)
case 'move':
return moveOp(root, op.from, op.to, compiled, root)
}
}
export function applyDecodedPatch<A extends DecodedValue>(
prev: A,
ops: readonly DecodedPatchOp[],
compiled: CompiledMap<DecodedValue>,
): Effect.Effect<A, PatchApplyError> {
return Effect.gen(function* () {
let current: DecodedValue = prev
for (const op of ops) {
current = yield* applyOp(current, op, compiled, current)
}
return current as A
})
}