-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
78 lines (62 loc) · 1.35 KB
/
object.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
package client
import (
"fmt"
)
// Object has an unique ID, attributes, and relations
type Object interface {
ID() ID
Data() map[string]interface{}
GetString(name string) string
GetRelation(name string) (ID, error)
GetRelations(name string) ([]ID, error)
}
type object struct {
id ID
data map[string]interface{}
}
// NewObject creates a new Object
func NewObject(data map[string]interface{}) (Object, error) {
id, err := ParseIDFromMap(data)
if err != nil {
return nil, err
}
return &object{id, data}, nil
}
func (o *object) ID() ID {
return o.id
}
func (o *object) Data() map[string]interface{} {
return o.data
}
func (o *object) GetString(key string) string {
val := o.data[key]
if val == nil {
return ""
}
return val.(string)
}
func (o *object) GetRelation(name string) (ID, error) {
val := o.data[name]
if val == nil {
return nil, fmt.Errorf("relation %s not found", name)
}
idData := val.(map[string]interface{})
return ParseIDFromMap(idData)
}
func (o *object) GetRelations(name string) ([]ID, error) {
val := o.data[name]
if val == nil {
return make([]ID, 0), nil
}
idList := val.([]interface{})
relations := make([]ID, len(idList))
for i, v := range idList {
idData := v.(map[string]interface{})
id, err := ParseIDFromMap(idData)
if err != nil {
return nil, err
}
relations[i] = id
}
return relations, nil
}