-
Notifications
You must be signed in to change notification settings - Fork 42
/
record.go
137 lines (121 loc) · 2.97 KB
/
record.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
package sfdc
import (
"encoding/json"
"errors"
)
const (
// RecordAttributes is the attribute map from the record JSON
RecordAttributes = "attributes"
recordAttrType = "type"
recordAttrURL = "url"
)
// Record is a representation of a Salesforce
// record.
type Record struct {
sobject string
url string
fields map[string]interface{}
lookUps map[string]*Record
}
// RecordFromJSONMap creates a recrod from a JSON map.
func RecordFromJSONMap(jsonMap map[string]interface{}) (*Record, error) {
if jsonMap == nil {
return nil, errors.New("record: map can not be nil")
}
r := &Record{}
r.fromJSONMap(jsonMap)
return r, nil
}
// UnmarshalJSON provides a custom unmarshaling of a
// JSON byte array.
func (r *Record) UnmarshalJSON(data []byte) error {
if r == nil {
return errors.New("record: can't unmarshal to a nil struct")
}
if data == nil {
return errors.New("record: can't unmarshal to a nil byte array")
}
var jsonMap map[string]interface{}
err := json.Unmarshal(data, &jsonMap)
if err != nil {
return err
}
r.fromJSONMap(jsonMap)
return nil
}
func (r *Record) fromJSONMap(jsonMap map[string]interface{}) {
r.fields = make(map[string]interface{})
r.lookUps = make(map[string]*Record)
for k, v := range jsonMap {
if k == RecordAttributes {
if attr, ok := v.(map[string]interface{}); ok {
if obj, ok := attr[recordAttrType]; ok {
if sobj, ok := obj.(string); ok {
r.sobject = sobj
}
}
if obj, ok := attr[recordAttrURL]; ok {
if url, ok := obj.(string); ok {
r.url = url
}
}
}
} else {
if v != nil {
if obj, is := v.(map[string]interface{}); is == false {
r.fields[k] = v
} else {
if r.isLookUp(obj) {
if rec, err := RecordFromJSONMap(obj); err == nil {
r.lookUps[k] = rec
}
}
}
}
}
}
}
func (r *Record) isLookUp(jsonMap map[string]interface{}) bool {
_, has := jsonMap[RecordAttributes]
return has
}
// LookUps returns all of the record's look ups
func (r *Record) LookUps() []*Record {
records := make([]*Record, len(r.lookUps))
var idx int
for _, r := range r.lookUps {
records[idx] = r
idx++
}
return records
}
// LookUp returns the look up record
func (r *Record) LookUp(lookUp string) (*Record, bool) {
if len(r.lookUps) == 0 {
return nil, false
}
rec, has := r.lookUps[lookUp]
return rec, has
}
// SObject returns attribute's Salesforce object name.
func (r *Record) SObject() string {
return r.sobject
}
// URL returns the record attribute's URL.
func (r *Record) URL() string {
return r.url
}
// FieldValue returns the field's value. If there is no field
// for the field name, then false will be returned.
func (r *Record) FieldValue(field string) (interface{}, bool) {
value, has := r.fields[field]
return value, has
}
// Fields returns the map of field name to value relationships.
func (r *Record) Fields() map[string]interface{} {
fields := make(map[string]interface{})
for k, v := range r.fields {
fields[k] = v
}
return fields
}