-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathitem.go
346 lines (289 loc) · 7.47 KB
/
item.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package automerge
import (
"fmt"
"runtime"
"time"
"unsafe"
)
// #include <automerge.h>
import "C"
// Kind represents the underlying type of a Value
type Kind uint
var (
// KindVoid indicates the value was not present
KindVoid Kind = C.AM_VAL_TYPE_VOID
// KindBool indicates a bool
KindBool Kind = C.AM_VAL_TYPE_BOOL
// KindBytes indicates a []byte
KindBytes Kind = C.AM_VAL_TYPE_BYTES
// KindCounter indicates an *automerge.Counter
KindCounter Kind = C.AM_VAL_TYPE_COUNTER
// KindFloat64 indicates a float64
KindFloat64 Kind = C.AM_VAL_TYPE_F64
// KindInt indicates an int
KindInt64 Kind = C.AM_VAL_TYPE_INT
// KindUint indicates a uint
KindUint64 Kind = C.AM_VAL_TYPE_UINT
// KindNull indicates an explicit null was present
KindNull Kind = C.AM_VAL_TYPE_NULL
// KindStr indicates a string
KindStr Kind = C.AM_VAL_TYPE_STR
// KindTime indicates a time.Time
KindTime Kind = C.AM_VAL_TYPE_TIMESTAMP
// KindUnknown indicates an unknown type from a future version of automerge
KindUnknown Kind = C.AM_VAL_TYPE_UNKNOWN
// KindMap indicates an *automerge.Map
KindMap Kind = kindObjType | C.AM_OBJ_TYPE_MAP
// KindList indicates an *automerge.List
KindList Kind = kindObjType | C.AM_OBJ_TYPE_LIST
// KindText indicates an *automerge.Text
KindText Kind = kindObjType | C.AM_OBJ_TYPE_TEXT
kindActorID Kind = C.AM_VAL_TYPE_ACTOR_ID
kindDoc Kind = C.AM_VAL_TYPE_DOC
kindChange Kind = C.AM_VAL_TYPE_CHANGE
kindChangeHash Kind = C.AM_VAL_TYPE_CHANGE_HASH
kindObjType Kind = C.AM_VAL_TYPE_OBJ_TYPE
kindSyncState Kind = C.AM_VAL_TYPE_SYNC_STATE
kindSyncMessage Kind = C.AM_VAL_TYPE_SYNC_MESSAGE
kindMark Kind = C.AM_VAL_TYPE_MARK
)
var kindDescriptions = map[Kind]string{
KindVoid: "KindVoid",
KindBool: "KindBool",
KindBytes: "KindBytes",
KindCounter: "KindCounter",
KindFloat64: "KindFloat64",
KindInt64: "KindInt64",
KindUint64: "KindUint64",
KindNull: "KindNull",
KindStr: "KindStr",
KindTime: "KindTime",
KindUnknown: "KindUnknown",
KindMap: "KindMap",
KindList: "KindList",
KindText: "KindText",
kindActorID: "kindActorID",
kindDoc: "kindDoc",
kindChange: "kindChange",
kindChangeHash: "kindChangeHash",
kindObjType: "kindObjType",
kindSyncState: "kindSyncState",
kindSyncMessage: "kindSyncMessage",
kindMark: "KindMark",
}
// String returns a human-readable representation of the Kind
func (k Kind) String() string {
if s, ok := kindDescriptions[k]; ok {
return s
}
return fmt.Sprintf("Kind(%v)", uint(k))
}
// item wraps an AMitem
type item struct {
result *result
cItem *C.AMitem
kind Kind
}
func (i *item) Kind() Kind {
if i.kind == 0 {
defer runtime.KeepAlive(i)
i.kind = Kind(C.AMitemValType(i.cItem))
}
return i.kind
}
func (i *item) failCast(k Kind) {
panic(fmt.Errorf("automerge: expected item with %v, got %v", k, i.Kind()))
}
func (i *item) actorID() *actorID {
defer runtime.KeepAlive(i)
ai := &actorID{item: i}
if !C.AMitemToActorId(i.cItem, &ai.cActorID) {
i.failCast(kindActorID)
}
return ai
}
func (i *item) bool() (ret bool) {
defer runtime.KeepAlive(i)
if !C.AMitemToBool(i.cItem, (*C.bool)(&ret)) {
i.failCast(KindBool)
}
return ret
}
func (i *item) str() string {
defer runtime.KeepAlive(i)
var bs C.AMbyteSpan
if !C.AMitemToStr(i.cItem, &bs) {
i.failCast(KindBytes)
}
return fromByteSpanStr(bs)
}
func (i *item) bytes() []byte {
defer runtime.KeepAlive(i)
var bs C.AMbyteSpan
if !C.AMitemToBytes(i.cItem, &bs) {
i.failCast(KindBytes)
}
return fromByteSpan(bs)
}
func (i *item) change() *Change {
defer runtime.KeepAlive(i)
var c *C.AMchange
if !C.AMitemToChange(i.cItem, &c) {
i.failCast(kindChange)
}
return &Change{item: i, cChange: c}
}
func (i *item) changeHash() ChangeHash {
defer runtime.KeepAlive(i)
var bs C.AMbyteSpan
if !C.AMitemToChangeHash(i.cItem, &bs) {
i.failCast(kindChangeHash)
}
return *(*ChangeHash)(fromByteSpan(bs))
}
func (i *item) doc() *Doc {
defer runtime.KeepAlive(i)
d := &Doc{item: i}
if !C.AMitemToDoc(i.cItem, &d.cDoc) {
i.failCast(kindDoc)
}
return d
}
func (i *item) float64() (ret float64) {
defer runtime.KeepAlive(i)
if !C.AMitemToF64(i.cItem, (*C.double)(&ret)) {
i.failCast(KindFloat64)
}
return ret
}
func (i *item) int64() (ret int64) {
defer runtime.KeepAlive(i)
if !C.AMitemToInt(i.cItem, (*C.int64_t)(&ret)) {
i.failCast(KindInt64)
}
return ret
}
func (i *item) uint64() (ret uint64) {
defer runtime.KeepAlive(i)
if !C.AMitemToUint(i.cItem, (*C.uint64_t)(&ret)) {
i.failCast(KindUint64)
}
return ret
}
func (i *item) time() time.Time {
defer runtime.KeepAlive(i)
var ms int64
if !C.AMitemToTimestamp(i.cItem, (*C.int64_t)(&ms)) {
i.failCast(KindTime)
}
return time.UnixMilli(ms)
}
func (i *item) counter() *Counter {
defer runtime.KeepAlive(i)
var val int64
if !C.AMitemToCounter(i.cItem, (*C.int64_t)(&val)) {
i.failCast(KindCounter)
}
return &Counter{val: val}
}
func (i *item) mapKey() string {
defer runtime.KeepAlive(i)
var bs C.AMbyteSpan
if !C.AMitemKey(i.cItem, &bs) {
panic(fmt.Errorf("expected C.AM_IDX_TYPE_KEY, got %v", C.AMitemIdxType(i.cItem)))
}
return fromByteSpanStr(bs)
}
func (i *item) objID() *objID {
defer runtime.KeepAlive(i)
oi := C.AMitemObjId(i.cItem)
if oi == nil {
return nil
}
return &objID{item: i, cObjID: oi}
}
func (i *item) syncState() *SyncState {
defer runtime.KeepAlive(i)
ss := &SyncState{item: i}
if !C.AMitemToSyncState(i.cItem, &ss.cSyncState) {
i.failCast(kindSyncState)
}
return ss
}
func (i *item) syncMessage() *SyncMessage {
defer runtime.KeepAlive(i)
ss := &SyncMessage{item: i}
if !C.AMitemToSyncMessage(i.cItem, &ss.cSyncMessage) {
if i.Kind() == KindVoid {
return nil
}
i.failCast(kindSyncMessage)
}
return ss
}
type objID struct {
item *item
cObjID *C.AMobjId
}
func (o *objID) objKind(d *Doc) Kind {
// not using d.Lock() here because the doc
// is likely already locked by List.Get()/Map.Get()
defer runtime.KeepAlive(d)
defer runtime.KeepAlive(o)
return Kind(C.AMobjObjType(d.cDoc, o.cObjID)) | kindObjType
}
func itemFromActorID(id string) (*item, error) {
bytes, free := toByteSpanStr(id)
defer free()
return wrap(C.AMactorIdFromStr(bytes)).item()
}
func itemsFromChangeHashes(ch []ChangeHash) ([]*item, error) {
items := []*item{}
for _, c := range ch {
bytes, free := toByteSpan(c[:])
defer free()
item, err := wrap(C.AMitemFromChangeHash(bytes)).item()
if err != nil {
return nil, err
}
items = append(items, item)
}
return items, nil
}
func createItems(is []*item) (*C.AMitems, func()) {
if len(is) == 0 {
return nil, func() {}
}
result := is[0].result
for _, i := range is[1:] {
result = wrap(C.AMresultCat(result.cResult, i.result.cResult))
}
ret := C.AMresultItems(result.cResult)
return &ret, func() {
runtime.KeepAlive(result)
}
}
func mapItems[T any](is []*item, f func(i *item) T) []T {
ret := []T{}
for _, i := range is {
ret = append(ret, f(i))
}
return ret
}
func toByteSpan(b []byte) (C.AMbyteSpan, func()) {
if len(b) == 0 {
return C.AMbyteSpan{src: (*C.uchar)(C.NULL), count: 0}, func() {}
}
return C.AMbyteSpan{src: (*C.uchar)(&b[0]), count: C.size_t(len(b))}, func() {
runtime.KeepAlive(b)
}
}
func fromByteSpan(bs C.AMbyteSpan) []byte {
return C.GoBytes(unsafe.Pointer(bs.src), C.int(bs.count))
}
func toByteSpanStr(s string) (C.AMbyteSpan, func()) {
return toByteSpan([]byte(s))
}
func fromByteSpanStr(bs C.AMbyteSpan) string {
return string(fromByteSpan(bs))
}