-
Notifications
You must be signed in to change notification settings - Fork 7
/
path.go
279 lines (239 loc) · 6.11 KB
/
path.go
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
package automerge
import (
"fmt"
"reflect"
)
// Path is a cursor that lets you reach into the document
type Path struct {
d *Doc
path []any
}
type deleteIndicator int
var toDelete = deleteIndicator(0)
// Path extends the cursor with more path segments.
// It panics if any path segment is not a string or an int.
// It does not check that the path segment is traversable until you call
// a method that accesses the document.
func (p *Path) Path(path ...any) *Path {
for i, v := range path {
rv := reflect.ValueOf(v)
if rv.CanInt() {
path[i] = int(rv.Int())
} else if rv.CanConvert(reflect.TypeOf("")) {
path[i] = rv.String()
} else {
panic(fmt.Errorf("automerge: invalid path segment, expected string or int, got: %T(%#v)", v, v))
}
}
return &Path{d: p.d, path: append(p.path, path...)}
}
// Get returns the value at a given path
func (p *Path) Get() (*Value, error) {
obj := p.d.Root()
var err error
for _, i := range p.path {
switch idx := i.(type) {
case string:
if obj.Kind() == KindVoid {
return obj, nil
}
if obj.Kind() != KindMap {
return nil, fmt.Errorf("%#v: tried to read property %#v of non-map %#v", p, idx, obj.val)
}
obj, err = obj.Map().Get(idx)
if err != nil {
return nil, err
}
case int:
if obj.Kind() == KindVoid {
return obj, nil
}
if obj.Kind() != KindList {
return nil, fmt.Errorf("%#v: tried to read index %#v of non-list %#v", p, idx, obj.val)
}
obj, err = obj.List().Get(idx)
if err != nil {
return nil, err
}
default:
panic("unreachable")
}
}
return obj, nil
}
// Set sets the value at the given path, and creates any missing parent
// Maps or Lists needed.
func (p *Path) Set(v any) error {
_, set, err := p.ensure()
if err != nil {
return err
}
if err := set(v); err != nil {
return err
}
return nil
}
// Delete removes the value at this path from the document.
func (p *Path) Delete() error {
return p.Set(toDelete)
}
func (p *Path) ensureMap(debugKey string) (*Map, error) {
if len(p.path) == 0 {
return p.d.RootMap(), nil
}
v, set, err := p.ensure()
if err != nil {
return nil, err
}
if v.Kind() == KindVoid {
t := NewMap()
if err := set(t); err != nil {
return nil, err
}
return t, nil
}
if v.Kind() == KindMap {
return v.Map(), nil
}
return nil, fmt.Errorf("%#v: tried to write property %#v of non-map %#v", p, debugKey, v.val)
}
func (p *Path) ensureList(debugKey int) (*List, error) {
v, set, err := p.ensure()
if err != nil {
return nil, err
}
if v.Kind() == KindVoid {
l := NewList()
if err := set(l); err != nil {
return nil, err
}
return l, nil
}
if v.Kind() == KindList {
return v.List(), nil
}
return nil, fmt.Errorf("%#v: tried to write index %v of non-list %#v", p, debugKey, v.val)
}
func (p *Path) ensureText() (*Text, error) {
v, set, err := p.ensure()
if err != nil {
return nil, err
}
if v.Kind() == KindVoid {
t := NewText("")
if err := set(t); err != nil {
return nil, err
}
return t, nil
}
if v.Kind() == KindText {
return v.Text(), nil
}
return nil, fmt.Errorf("%#v: tried to edit non-text %#v", p, v.val)
}
func (p *Path) ensureCounter() (*Counter, error) {
v, set, err := p.ensure()
if err != nil {
return nil, err
}
if v.Kind() == KindVoid {
t := NewCounter(0)
if err := set(t); err != nil {
return nil, err
}
return t, nil
}
if v.Kind() == KindCounter {
return v.Counter(), nil
}
return nil, fmt.Errorf("%#v: tried to increment non-counter %#v", p, v.val)
}
func (p *Path) ensure() (*Value, func(v any) error, error) {
if len(p.path) == 0 {
return p.d.Root(), func(v any) error {
return fmt.Errorf("%#v: tried to overwrite root of document", p)
}, nil
}
last := p.path[len(p.path)-1]
parent := &Path{d: p.d, path: p.path[0 : len(p.path)-1]}
switch key := last.(type) {
case string:
m, err := parent.ensureMap(key)
if err != nil {
return nil, nil, err
}
v, err := m.Get(key)
return v, func(v any) error {
if v == toDelete {
return m.Delete(key)
}
return m.Set(key, v)
}, err
case int:
l, err := parent.ensureList(key)
if err != nil {
return nil, nil, err
}
v, err := l.Get(key)
if err != nil {
return nil, nil, err
}
return v, func(v any) error {
if key > l.Len() || key == l.Len() && v == toDelete {
return fmt.Errorf("%#v: tried to write index %v beyond end of list length %v", p, key, l.Len())
}
if v == toDelete {
return l.Delete(key)
}
if key == l.Len() {
return l.Append(v)
}
return l.Set(key, v)
}, nil
default:
panic("unreachable")
}
}
// Map assumes there is a [Map] at the given path.
// Calling methods on the map will error if the path cannot be traversed
// or if the value at this path is not a map.
// If there is a void at this location, writing to this map
// will implicitly create it (and the path as necessary).
func (p *Path) Map() *Map {
return &Map{doc: p.d, path: p}
}
// List assumes there is a [List] at the given path.
// Calling methods on the list will error if the path cannot be traversed
// or if the value at this path is not a list.
// If there is a void at this location, reading from the list will return void,
// and writing to the list will implicitly create it (and the path as necesasry).
func (p *Path) List() *List {
return &List{doc: p.d, path: p}
}
// Counter assumes there is a [Counter] at the given path.
// Calling methods on the counter will error if the path cannot be traversed
// or if the value at this path is not a counter.
func (p *Path) Counter() *Counter {
return &Counter{path: p}
}
// Text assumes there is a [Text] at the given path.
// Calling methods on the counter will error if the path cannot be traversed
// or if the value at this path is not a counter.
func (p *Path) Text() *Text {
return &Text{doc: p.d, path: p}
}
// String returns a representation suitable for debugging.
func (p *Path) String() string {
return p.GoString()
}
// GoString returns a representation suitable for debugging
func (p *Path) GoString() string {
str := "&automerge.Path{"
for i, p := range p.path {
if i > 0 {
str += ", "
}
str += fmt.Sprintf("%#v", p)
}
return str + "}"
}