Repository navigation
Expand file tree
/
Copy pathdiff.ts
More file actions
163 lines (145 loc) · 5.86 KB
/
Copy pathdiff.ts
File metadata and controls
163 lines (145 loc) · 5.86 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
import { Effect, Schema } from 'effect'
import type { CompiledMap, DecodedPatchOp, DecodedValue, DecodedSome, DecodedNone, Path, PatchApplyError } from './types'
import type { JsonValue } from '../schema'
import { PatchEncodeError, PatchSchemaError } 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)
}
// ---------------------------------------------------------------------------
// Encoding values for transport — Effect-native
// ---------------------------------------------------------------------------
function encodeValue(
compiled: CompiledMap<DecodedValue>,
path: Path,
value: DecodedValue,
decodedRoot: DecodedValue,
): Effect.Effect<JsonValue, PatchApplyError> {
if (isScalar(value)) return Effect.succeed(value)
const subSchema = compiled.subSchemaAt(path, decodedRoot)
if (subSchema === null) {
return Effect.fail(new PatchSchemaError({ path }))
}
return Schema.encode(subSchema)(value).pipe(
Effect.mapError((cause) => new PatchEncodeError({ path, cause })),
)
}
function encodeInnerValue(
compiled: CompiledMap<DecodedValue>,
path: Path,
value: DecodedValue,
decodedRoot: DecodedValue,
): Effect.Effect<JsonValue, PatchApplyError> {
if (isScalar(value)) return Effect.succeed(value)
const field = compiled.fieldAt(path, decodedRoot)
if (field !== null && field.innerSubSchema !== null) {
return Schema.encode(field.innerSubSchema)(value).pipe(
Effect.mapError((cause) => new PatchEncodeError({ path, cause })),
)
}
return encodeValue(compiled, path, value, decodedRoot)
}
// ---------------------------------------------------------------------------
// Core diff — Effect.gen, threads decodedRoot for union resolution
// ---------------------------------------------------------------------------
function diffRec(
prev: DecodedValue,
next: DecodedValue,
path: Path,
ops: DecodedPatchOp[],
compiled: CompiledMap<DecodedValue>,
decodedRoot: DecodedValue,
): Effect.Effect<void, PatchApplyError> {
return Effect.gen(function* () {
// Both Option
if (isDecodedOption(prev) && isDecodedOption(next)) {
if (prev._tag === 'Some' && next._tag === 'Some') {
yield* diffRec(prev.value, next.value, path, ops, compiled, decodedRoot)
} else if (prev._tag === 'Some' && next._tag === 'None') {
ops.push({ op: 'remove', path })
} else if (prev._tag === 'None' && next._tag === 'Some') {
const encoded = yield* encodeInnerValue(compiled, path, next.value, decodedRoot)
ops.push({ op: 'replace', path, value: encoded })
}
return
}
// One is Option, other is not → replace
if (isDecodedOption(prev) !== isDecodedOption(next)) {
const encoded = yield* encodeValue(compiled, path, next, decodedRoot)
ops.push({ op: 'replace', path, value: encoded })
return
}
// Both scalars
if (isScalar(prev) && isScalar(next)) {
if (prev !== next) {
ops.push({ op: 'replace', path, value: next })
}
return
}
// Both arrays
if (isArray(prev) && isArray(next)) {
const minLen = Math.min(prev.length, next.length)
for (let i = 0; i < minLen; i++) {
if (prev[i] !== next[i]) {
yield* diffRec(prev[i], next[i], [...path, i], ops, compiled, decodedRoot)
}
}
for (let i = minLen; i < next.length; i++) {
const encoded = yield* encodeValue(compiled, [...path, i], next[i], decodedRoot)
ops.push({ op: 'add', path: [...path, i], value: encoded })
}
for (let i = prev.length - 1; i >= next.length; i--) {
ops.push({ op: 'remove', path: [...path, i] })
}
return
}
// Both objects
if (isRecord(prev) && isRecord(next)) {
const prevKeys = Object.keys(prev)
const nextKeys = Object.keys(next)
for (const key of nextKeys) {
const nextVal = next[key]
if (!(key in prev)) {
const encoded = yield* encodeValue(compiled, [...path, key], nextVal, decodedRoot)
ops.push({ op: 'replace', path: [...path, key], value: encoded })
} else if (prev[key] !== nextVal) {
yield* diffRec(prev[key], nextVal, [...path, key], ops, compiled, decodedRoot)
}
}
for (const key of prevKeys) {
if (!(key in next)) {
ops.push({ op: 'remove', path: [...path, key] })
}
}
return
}
// Different types → replace with encoded value
const encoded = yield* encodeValue(compiled, path, next, decodedRoot)
ops.push({ op: 'replace', path, value: encoded })
})
}
// ---------------------------------------------------------------------------
// Public API — generic over A (the decoded type), constrained to DecodedValue
// ---------------------------------------------------------------------------
export function diffDecoded<A extends DecodedValue>(
prev: A,
next: A,
compiled: CompiledMap<DecodedValue>,
): Effect.Effect<readonly DecodedPatchOp[], PatchApplyError> {
return Effect.gen(function* () {
const ops: DecodedPatchOp[] = []
yield* diffRec(prev, next, [], ops, compiled, prev)
return ops
})
}