This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
forked from tpjg/goriakpbc
/
model.go
584 lines (538 loc) · 15.6 KB
/
model.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package riak
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/cupcake/go-riak/json"
)
/*
Make structs work like a Document Model, similar to how the Ruby based "ripple"
gem works. This is done by parsing the JSON data and mapping it to the struct's
fields. To enable easy integration with Ruby/ripple projects the struct "tag"
feature of Go is used to possibly get around the naming convention differences
between Go and Ruby (Uppercase starting letter required for export and
typically CamelCase versus underscores). Also it stores the model/struct name
as _type in Riak.
For example the following Ruby/Ripple class:
class Device
include Ripple::Document
property :ip, String
property :description, String
property :download_enabled, Boolean
end
can be mapped to the following Go class:
type Device struct {
Download_enabled bool `riak:"download_enabled"`
Ip string `riak:"ip"`
Description string `riak:"description"`
riak.Model
}
Note that it is required to have a riak.Model field.
Also if the field name in Ripple is equal the extra tag is not needed, (e.g.
if the Ripple class above would have a "property :Ip, String").
*/
type Model struct {
robject *RObject
parent Resolver // Pointer to the parent struct (Device in example above)
}
type Resolver interface {
Resolve(int) error
}
// Error definitions
var (
ResolveNotImplemented = errors.New("Resolve not implemented")
DestinationError = errors.New("Destination is not a pointer (to a struct)")
DestinationIsNotModel = errors.New("Destination has no riak.Model field")
DestinationIsNotSlice = errors.New("Must supply a slice to GetSiblings")
DestinationLengthError = errors.New("Length of slice does not match number of siblings")
DestinationNotInitialized = errors.New("Destination struct is not initialized (correctly) using riak.New or riak.Load")
ModelNotNew = errors.New("Destination struct already has an instantiated riak.Model (this struct is probably not new)")
NoSiblingData = errors.New("No non-empty sibling data")
)
/*
Return is an error is really a warning, e.g. a common json error, or
ModelDoesNotMatch.
*/
func IsWarning(err error) bool {
if err != nil {
if strings.HasPrefix(err.Error(), "<nil> - json: cannot unmarshal") && strings.Contains(err.Error(), "into Go value of type") {
return true
}
} else {
// In case there is no error reply true anyway since this is probably
// what is expected - a check whether it is safe to continue.
return true
}
return false
}
func (*Model) Resolve(count int) (err error) {
return ResolveNotImplemented
}
// Link to one other model
type One struct {
model interface{}
link Link
client *Client
}
// Link to many other models
type Many []One
func (c *Client) setOneLink(source Link, dest reflect.Value) {
if dest.Kind() == reflect.Struct && dest.Type() == reflect.TypeOf(One{}) {
one := One{link: source, client: c}
mv := reflect.ValueOf(one)
dest.Set(mv)
return
}
}
func (c *Client) addOneLink(source Link, dest reflect.Value) {
if dest.Kind() == reflect.Slice && dest.Type() == reflect.TypeOf(Many{}) {
one := One{link: source, client: c}
mv := reflect.ValueOf(one)
// Add this One link to the slice
dest.Set(reflect.Append(dest, mv))
}
}
// Check if the passed destination is a pointer to a struct with riak.Model field
func check_dest(dest interface{}) (dv reflect.Value, dt reflect.Type, rm reflect.Value, err error) {
dv = reflect.ValueOf(dest)
if dv.Kind() != reflect.Ptr || dv.IsNil() {
err = DestinationError
return
}
dv = dv.Elem()
dt = reflect.TypeOf(dest)
dt = dt.Elem()
if dt.Kind() != reflect.Struct {
err = DestinationError
return
}
for i := 0; i < dt.NumField(); i++ {
dobj := dt.Field(i)
if dobj.Type.Kind() == reflect.Struct && dobj.Type == reflect.TypeOf(Model{}) {
rm = dv.Field(i) // Return the Model field value
return
}
}
err = DestinationIsNotModel
return
}
/*
mapData, maps the decoded JSON data onto the right struct fields, including
decoding of links.
*/
func (c *Client) mapData(dv reflect.Value, dt reflect.Type, data []byte, links []Link, dest interface{}) (err error) {
// Unmarshal the destination model
jserr := json.Unmarshal(data, dest)
if jserr != nil {
err = fmt.Errorf("%v - %v", err, jserr) // Add error
}
// For all the links in the struct, find the correct mapping
for i := 0; i < dt.NumField(); i++ {
ft := dt.Field(i)
fv := dv.Field(i)
if ft.Type == reflect.TypeOf(One{}) {
name := ft.Tag.Get("riak")
if name == "" {
name = ft.Name
}
// Search in Links
for _, v := range links {
if v.Tag == name {
c.setOneLink(v, fv)
}
}
} else if ft.Type == reflect.TypeOf(Many{}) {
name := ft.Tag.Get("riak")
if name == "" {
name = ft.Name
}
// Search in Links
for _, v := range links {
if v.Tag == name {
c.addOneLink(v, fv)
}
}
}
}
return
}
func (m *Model) GetSiblings(dest interface{}) (err error) {
client, err := m.getClient()
if err != nil {
return err
}
// Check if a slice is supplied
v := reflect.ValueOf(dest)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
if v.Kind() != reflect.Slice {
return DestinationIsNotSlice
}
// Check the length of the supplied slice against the number of non-empty siblings
count := 0
for _, s := range m.robject.Siblings {
if len(s.Data) != 0 {
count += 1
}
}
if count == 0 {
return NoSiblingData
}
if v.Len() != count {
return DestinationLengthError
}
// Check the type of the slice elements
if reflect.TypeOf(v.Index(0).Interface()) != reflect.TypeOf(m.parent).Elem() {
return fmt.Errorf("Slice elements incorrect, must be %v", reflect.TypeOf(m.parent).Elem())
}
count = 0
// Double check the parent and get the Value and Type
dv, dt, _, err := check_dest(m.parent)
if err != nil {
return err
}
// Walk over the slice and map the data for each sibling
for _, sibling := range m.robject.Siblings {
if len(sibling.Data) != 0 {
dest := reflect.New(reflect.TypeOf(m.parent).Elem())
err = client.mapData(dv, dt, sibling.Data, sibling.Links, dest.Interface())
err = client.mapData(dv, dt, sibling.Data, sibling.Links, m.parent)
v.Index(count).Set(dest.Elem())
count += 1
}
}
return
}
/*
The Load function retrieves the data from Riak and stores it in the struct
that is passed as destination. It stores some necessary information in the
riak.Model field so it can be used later in other (Save) operations.
Unfortunately you also need to pass the bucketname as it is probably
different from the struct name.
Using the "Device" struct as an example:
dev := &Device{}
err := client.Load("devices", "12345", dev)
*/
func (c *Client) Load(bucketname string, key string, dest Resolver, options ...map[string]uint32) (err error) {
// Check destination
dv, dt, rm, err := check_dest(dest)
if err != nil {
return err
}
// Fetch the object from Riak.
bucket, err := c.Bucket(bucketname)
if bucket == nil || err != nil {
err = fmt.Errorf("Can't get bucket for %v - %v", dt.Name(), err)
return
}
obj, err := bucket.Get(key, options...)
if err != nil {
return err
}
if obj == nil {
return NotFound
}
if obj.Conflict() {
// Count number of non-empty siblings for which a conflict must be resolved
count := 0
for _, s := range obj.Siblings {
if len(s.Data) != 0 {
count += 1
}
}
// Set the RObject in the destination struct so it can be used for resolving the conflict
model := &Model{robject: obj, parent: dest}
mv := reflect.ValueOf(model)
mv = mv.Elem()
rm.Set(mv)
// Resolve the conflict and return the errorcode
return dest.Resolve(count)
}
// Map the data onto the struct.
err = c.mapData(dv, dt, obj.Data, obj.Links, dest)
// Set the values in the riak.Model field
model := &Model{robject: obj, parent: dest}
mv := reflect.ValueOf(model)
mv = mv.Elem()
rm.Set(mv)
return
}
/*
Create a new Document Model, passing in the bucketname and key. The key can be
empty in which case Riak will pick a key. The destination must be a pointer to
a struct that has the riak.Model field.
*/
func (c *Client) New(bucketname string, key string, dest Resolver, options ...map[string]uint32) (err error) {
// Check destination
_, dt, rm, err := check_dest(dest)
if err != nil {
return err
}
// Fetch the object from Riak.
bucket, err := c.Bucket(bucketname)
if bucket == nil || err != nil {
err = fmt.Errorf("Can't get bucket for %v - %v", dt.Name(), err)
return
}
// Check if the RObject field within riak.Model is still nill, otherwise
// this destination (dest) is probably an already fully instantiated
// struct.
model := &Model{}
mv := reflect.ValueOf(model)
mv = mv.Elem()
mv.Set(rm)
if model.robject != nil {
return ModelNotNew
}
// For the riak.Model field within the struct, set the Client and Bucket
// and fields and set the RObject field to nil.
model.robject = &RObject{Bucket: bucket, Key: key, ContentType: "application/json", Options: options}
model.parent = dest
rm.Set(mv)
return
}
// Creates a link to a given model
func (c *Client) linkToModel(dest interface{}) (link Link, err error) {
// Check destination
_, _, rm, err := check_dest(dest)
if err != nil {
return
}
// Get the Model field
model := &Model{}
mv := reflect.ValueOf(model)
mv = mv.Elem()
mv.Set(rm)
// Now check if there is an RObject, otherwise probably not correctly instantiated with .New (or Load).
if model.robject == nil {
err = DestinationNotInitialized
return
}
link = Link{Bucket: model.robject.Bucket.name, Key: model.robject.Key}
return
}
// Save a Document Model to Riak under a new key, if empty a Key will be choosen by Riak
func (c *Client) SaveAs(newKey string, dest Resolver) (err error) {
// Check destination
dv, dt, rm, err := check_dest(dest)
if err != nil {
return err
}
// Get the Model field
model := &Model{}
mv := reflect.ValueOf(model)
mv = mv.Elem()
mv.Set(rm)
// Now check if there is an RObject, otherwise probably not correctly instantiated with .New (or Load).
if model.robject == nil {
return DestinationNotInitialized
}
// JSON encode the entire struct
data, err := json.Marshal(dest)
if err != nil {
return err
}
// Now add the Links
for i := 0; i < dt.NumField(); i++ {
ft := dt.Field(i)
fv := dv.Field(i)
fieldname := ft.Tag.Get("riak")
if fieldname == "" {
fieldname = ft.Name
}
if ft.Type == reflect.TypeOf(One{}) {
// Save a link, set the One struct first
lmodel := &One{}
lmv := reflect.ValueOf(lmodel)
lmv = lmv.Elem()
lmv.Set(fv)
// If the link is not set, create it now.
if lmodel.link.Bucket == "" || lmodel.link.Key == "" {
lmodel.link, _ = c.linkToModel(lmodel.model)
}
// Add the link (if not already in the object's links)
model.robject.AddLink(Link{lmodel.link.Bucket, lmodel.link.Key, fieldname})
}
if ft.Type == reflect.TypeOf(Many{}) {
// Save the links, create a Many struct first
lmodels := &Many{}
lmv := reflect.ValueOf(lmodels)
lmv = lmv.Elem()
lmv.Set(fv)
// Now walk over those links...
for _, lmodel := range *lmodels {
// If the link is not set, create it now.
if lmodel.link.Bucket == "" || lmodel.link.Key == "" {
lmodel.link, _ = c.linkToModel(lmodel.model)
}
// Add the link (if not already in the object's links)
model.robject.AddLink(Link{lmodel.link.Bucket, lmodel.link.Key, fieldname})
}
}
}
//fmt.Printf("Saving data for %v as %v\n", dt.Name(), string(data))
model.robject.Data = data
model.robject.ContentType = "application/json"
if newKey != "±___unchanged___±" {
model.robject.Key = newKey
}
// Store the RObject in Riak
err = model.robject.Store()
return
}
// Save a Document Model to Riak
func (c *Client) Save(dest Resolver) (err error) {
return c.SaveAs("±___unchanged___±", dest)
}
// Get the client from a given model
func (m *Model) getClient() (c *Client, err error) {
if m.robject == nil {
return nil, DestinationNotInitialized
}
if m.robject.Bucket == nil {
return nil, DestinationNotInitialized
}
if m.robject.Bucket.client == nil {
return nil, DestinationNotInitialized
}
return m.robject.Bucket.client, nil
}
// Save a Document Model to Riak under a new key, if empty a Key will be choosen by Riak
func (m *Model) SaveAs(newKey string) (err error) {
client, err := m.getClient()
if err != nil {
return err
}
return client.SaveAs(newKey, m.parent)
}
// Save a Document Model to Riak
func (m *Model) Save() (err error) {
return m.SaveAs("±___unchanged___±")
}
// Delete a Document Model
func (m *Model) Delete() (err error) {
return m.robject.Destroy()
}
// Reload a Document Model
func (m *Model) Reload() (err error) {
vclock := string(m.robject.Vclock)
err = m.robject.Reload()
if err != nil {
return err
}
// Check if there was any change
if string(m.robject.Vclock) != vclock {
// Set the content again
if m.robject.Conflict() {
// Count number of non-empty siblings for which a conflict must be resolved
count := 0
for _, s := range m.robject.Siblings {
if len(s.Data) != 0 {
count += 1
}
}
// Resolve the conflict and return the errorcode
return m.parent.Resolve(count)
}
// Map the data onto the struct.
dv, dt, _, err := check_dest(m.parent)
c, err := m.getClient()
if err != nil {
return err
}
err = c.mapData(dv, dt, m.robject.Data, m.robject.Links, m.parent)
if err != nil {
return err
}
}
return
}
// Get a models Key, e.g. needed when Riak has picked it
func (c *Client) Key(dest interface{}) (key string, err error) {
// Check destination
_, _, rm, err := check_dest(dest)
if err != nil {
return
}
// Get the Model field
model := &Model{}
mv := reflect.ValueOf(model)
mv = mv.Elem()
mv.Set(rm)
// Now check if there is an RObject, otherwise probably not correctly instantiated with .New (or Load).
if model.robject == nil {
err = errors.New("Destination struct is not instantiated using riak.New or riak.Load")
return
}
return model.robject.Key, nil
}
// Get a models Key, e.g. needed when Riak has picked it
func (m Model) Key() (key string) {
if m.robject == nil {
return ""
}
return m.robject.Key
}
// Set the Key value, note that this does not save the model, it only changes the data structure
func (c *Client) SetKey(newKey string, dest interface{}) (err error) {
// Check destination
_, _, rm, err := check_dest(dest)
if err != nil {
return
}
// Get the Model field
model := &Model{}
mv := reflect.ValueOf(model)
mv = mv.Elem()
mv.Set(rm)
// Now check if there is an RObject, otherwise probably not correctly instantiated with .New (or Load).
if model.robject == nil {
return DestinationNotInitialized
}
model.robject.Key = newKey
return
}
// Set the Key value, note that this does not save the model, it only changes the data structure
func (m Model) SetKey(newKey string) (err error) {
if m.robject == nil {
return DestinationNotInitialized
}
m.robject.Key = newKey
return
}
func (o One) Link() (link Link) {
return o.link
}
// Set the link to a given Model (dest)
func (o *One) Set(dest Resolver) (err error) {
_, _, _, err = check_dest(dest)
if err == nil {
o.model = dest
o.link, err = o.client.linkToModel(dest)
}
return
}
func (o *One) Get(dest Resolver) (err error) {
if o.client == nil {
return DestinationNotInitialized
}
return o.client.Load(o.link.Bucket, o.link.Key, dest)
}
// Add a Link to the given Model (dest)
func (m *Many) Add(dest Resolver) (err error) {
_, _, _, err = check_dest(dest)
if err == nil {
o := One{model: dest}
o.link, err = o.client.linkToModel(dest)
*m = append(*m, o)
}
return err
}
//TODO: create "Remove" for Many links
func init() {
json.SkipTypes[reflect.TypeOf(Model{})] = true
json.SkipTypes[reflect.TypeOf(One{})] = true
json.SkipTypes[reflect.TypeOf(Many{})] = true
}