-
Notifications
You must be signed in to change notification settings - Fork 19
/
object.go
253 lines (210 loc) · 5.78 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
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
package types
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)
const serializeDefaultAs = "__default__"
// XObject is an object with named properties.
//
// @(object("foo", 1, "bar", "x")) -> {bar: x, foo: 1}
// @(object("foo", 1, "bar", "x").bar) -> x
// @(object("foo", 1, "bar", "x")["bar"]) -> x
// @(count(object("foo", 1, "bar", "x"))) -> 2
// @(json(object("foo", 1, "bar", "x"))) -> {"bar":"x","foo":1}
//
// @type object
type XObject struct {
XValue
XCountable
def XValue
props map[string]XValue
source func() map[string]XValue
marshalDefault bool
}
// NewXObject returns a new object with the given properties
func NewXObject(properties map[string]XValue) *XObject {
return NewXLazyObject(func() map[string]XValue { return properties })
}
// NewXLazyObject returns a new lazy object with the source function and default
func NewXLazyObject(source func() map[string]XValue) *XObject {
return &XObject{
source: source,
}
}
// Describe returns a representation of this type for error messages
func (x *XObject) Describe() string { return "object" }
// Truthy determines truthiness for this type
func (x *XObject) Truthy() bool {
if x.hasDefault() {
return Truthy(x.Default())
}
return x.Count() > 0
}
// Render returns the canonical text representation
func (x *XObject) Render() string {
if x.hasDefault() {
return Render(x.Default())
}
pairs := make([]string, 0, x.Count())
for _, k := range x.Properties() {
rendered := Render(x.properties()[k])
pairs = append(pairs, fmt.Sprintf("%s: %s", k, rendered))
}
return "{" + strings.Join(pairs, ", ") + "}"
}
// Format returns the pretty text representation
func (x *XObject) Format(env envs.Environment) string {
if x.hasDefault() {
return Format(env, x.Default())
}
pairs := make([]string, 0, x.Count())
for _, k := range x.Properties() {
formatted := Format(env, x.properties()[k])
if strings.ContainsRune(formatted, '\n') {
formatted = utils.Indent(formatted, " ")
formatted = fmt.Sprintf("%s:\n%s", k, formatted)
} else {
formatted = fmt.Sprintf("%s: %s", k, formatted)
}
pairs = append(pairs, formatted)
}
return strings.Join(pairs, "\n")
}
// MarshalJSON converts this type to internal JSON
func (x *XObject) MarshalJSON() ([]byte, error) {
marshaled := make(map[string]json.RawMessage, x.Count())
for p, v := range x.properties() {
asJSON, err := ToXJSON(v)
if err == nil {
marshaled[p] = json.RawMessage(asJSON.Native())
}
}
if x.hasDefault() && x.marshalDefault {
asJSON, err := ToXJSON(x.def)
if err == nil {
marshaled[serializeDefaultAs] = json.RawMessage(asJSON.Native())
}
}
return jsonx.Marshal(marshaled)
}
// ReadXObject reads an instance of this type from JSON
func ReadXObject(data []byte) (*XObject, error) {
v := JSONToXValue(data)
switch typed := v.(type) {
case *XObject:
return typed, nil
case XError:
return nil, typed
default:
return nil, errors.New("JSON doesn't contain an object")
}
}
// String returns the native string representation of this type for debugging
func (x *XObject) String() string {
pairs := make([]string, 0, x.Count())
if x.hasDefault() {
pairs = append(pairs, fmt.Sprintf("%s: %s", serializeDefaultAs, String(x.Default())))
}
for _, k := range x.Properties() {
pairs = append(pairs, fmt.Sprintf("%s: %s", k, String(x.properties()[k])))
}
return "XObject{" + strings.Join(pairs, ", ") + "}"
}
// Count is called when the length of this object is requested in an expression
func (x *XObject) Count() int {
return len(x.properties())
}
// Get retrieves the named property
func (x *XObject) Get(key string) (XValue, bool) {
key = strings.ToLower(key)
for p, v := range x.properties() {
if strings.ToLower(p) == key {
return v, true
}
}
return nil, false
}
// Properties returns the sorted property names of this object
func (x *XObject) Properties() []string {
names := make([]string, 0, x.Count())
for name := range x.properties() {
names = append(names, name)
}
sort.Strings(names)
return names
}
// Equals determines equality for this type
func (x *XObject) Equals(other *XObject) bool {
if x.hasDefault() || other.hasDefault() {
if !Equals(x.Default(), other.Default()) {
return false
}
}
props1 := x.Properties()
props2 := other.Properties()
if len(props1) != len(props2) {
return false
}
for p, name := range props1 {
if name != props2[p] {
return false
}
if !Equals(x.properties()[name], other.properties()[name]) {
return false
}
}
return true
}
func (x *XObject) properties() map[string]XValue {
x.ensureInitialized()
return x.props
}
// Default returns the default value for this
func (x *XObject) Default() XValue {
x.ensureInitialized()
return x.def
}
func (x *XObject) SetMarshalDefault(marshal bool) {
x.marshalDefault = marshal
}
// Default returns the default value for this
func (x *XObject) hasDefault() bool {
return x.Default() != x
}
func (x *XObject) ensureInitialized() {
if x.props == nil {
props := x.source()
x.def = x
x.props = make(map[string]XValue, len(props))
for p, v := range props {
if p == serializeDefaultAs {
x.def = v
} else {
x.props[p] = v
}
}
}
}
// XObjectEmpty is the empty empty
var XObjectEmpty = NewXObject(map[string]XValue{})
var _ json.Marshaler = (*XObject)(nil)
// ToXObject converts the given value to an object
func ToXObject(env envs.Environment, x XValue) (*XObject, XError) {
if utils.IsNil(x) {
return XObjectEmpty, nil
}
if IsXError(x) {
return XObjectEmpty, x.(XError)
}
object, isObject := x.(*XObject)
if isObject && object != nil {
return object, nil
}
return XObjectEmpty, NewXErrorf("unable to convert %s to an object", Describe(x))
}