-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.go
More file actions
169 lines (121 loc) · 2.39 KB
/
objects.go
File metadata and controls
169 lines (121 loc) · 2.39 KB
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
/* Copyright 2021 Kilobit Labs Inc. */
package objected
import _ "fmt"
import "errors"
import "strings"
type Value interface{}
type Values []interface{}
type ValueMapFunc func(i int, val Value) Value
func (vals Values) Map(f ValueMapFunc) Values {
results := Values{}
for i, val := range vals {
result := f(i, val)
if result != nil {
results = append(results, result)
}
}
return results
}
// Gets values from a collection of Objects.
//
func (vals Values) GetValues(query string) Values {
results := vals.Map(ValueMapFunc(func(i int, val Value) Value {
var obj Object
switch v := val.(type) {
case map[string]interface{}:
obj = Object(v)
case Object:
obj = v
default:
return nil
}
res, ok := obj.Get(query)
if !ok {
return nil
}
return res
}))
return results
}
type Object map[string]interface{}
// Recursively looks up key segments in Objects using '.' as a
// separator.
//
func (obj Object) Get(query string) (Value, bool) {
if strings.HasSuffix(query, ".") {
return nil, false
}
key := query
rest := ""
i := strings.Index(query, ".")
if i != -1 {
key = query[:i]
rest = query[i+1:]
}
val, ok := obj[key]
if !ok {
return nil, false
}
if rest == "" {
return val, true
}
switch v := val.(type) {
case map[string]interface{}:
return Object(v).Get(rest)
case Object:
return v.Get(rest)
default:
return nil, false
}
}
// Gets a value and coerces it into a string.
//
// Returns "" if not found or not coercible.
//
func (obj Object) GetString(query string) string {
result := ""
val, ok := obj.Get(query)
if ok {
str, ok := val.(string)
if ok {
result = str
}
}
return result
}
var ErrorNotFound = errors.New("Not found.")
var ErrorNotANumber error = errors.New("Not a number.")
// Gets a value and coerces it into a number.
//
func (obj Object) GetNumber(query string) (float64, error) {
result := float64(0)
var err error = nil
val, ok := obj.Get(query)
if ok {
switch v := val.(type) {
case int:
result = float64(v)
case float64:
result = v
default:
err = ErrorNotANumber
}
} else {
err = ErrorNotFound
}
return result, err
}
func (obj Object) Keys() []string {
keys := []string{}
for key, _ := range obj {
keys = append(keys, key)
}
return keys
}
func (obj Object) Values() Values {
vals := Values{}
for _, val := range obj {
vals = append(vals, val)
}
return vals
}