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
|
package tanja
import (
"encoding/json"
. "strconv"
)
type Element struct {
// This is either nil, int64, string, float64, []Element or map[string]Element
E interface{}
}
type Tuple []Element
func (e Element) IsInt() (bool, int64) {
switch v := e.E.(type) {
case int64:
return true, v
case string:
n, r := ParseInt(v, 10, 64)
if r != nil {
return false, 0
} else {
return true, n
}
case float64:
return true, int64(v)
}
return false, 0
}
// Just returns 0 on error
func (e Element) Int() int64 {
_, i := e.IsInt()
return i
}
// Returns empty string on error
func (e Element) String() string {
switch v := e.E.(type) {
case int64:
return FormatInt(v, 10)
case string:
return v
case float64:
return FormatFloat(v, 'g', -1, 64)
}
return ""
}
// Returns 0 on error
func (e Element) Float() float64 {
switch v := e.E.(type) {
case int64:
return float64(v)
case string:
n, r := ParseFloat(v, 64)
if r != nil {
return 0
} else {
return n
}
case float64:
return v
}
return 0
}
// Whether this is a wildcard or not
func (e Element) WC() bool {
return e.E == nil
}
// Returns an empty slice if this is no array
func (e Element) Slice() []Element {
if v, ok := e.E.([]Element); ok {
return v
}
return []Element{}
}
// Returns a nil map if this is no map.
func (e Element) Map() map[string]Element {
if v, ok := e.E.(map[string]Element); ok {
return v
}
return nil
}
func (a Element) Match(b Element) bool {
// Wildcards always match
if a.WC() || b.WC() {
return true
}
// Fuzzy int matching
if ca, ia := a.IsInt(); ca {
if cb, ib := b.IsInt(); cb && ia == ib {
return true
}
}
// Exact matching (works fine for strings)
return a.E == b.E
}
func (e Element) MarshalJSON() (r []byte, err error) {
r, err = json.Marshal(e.E)
return
}
func (e *Element) UnmarshalJSON(b []byte) (err error) {
switch b[0] {
case 'n': // null
e.E = nil
case 't': // true
e.E = int64(1)
case 'f': // false
e.E = int64(0)
case '"': // string
var s string
err = json.Unmarshal(b, &s)
e.E = s
case '{': // map
var m map[string]Element
err = json.Unmarshal(b, &m)
e.E = m
case '[': // array
var a []Element
err = json.Unmarshal(b, &a)
e.E = a
default: // then this must be a number
// The json package stores all numbers into a float64. We'd like to be
// able to use the full range of an int64. Note that numbers formatted
// with an exponent or fractional part are still interpreted as a
// float, regardless of whether it can be represented as a int64 or
// not.
if n, r := ParseInt(string(b), 10, 64); r == nil {
e.E = n
} else {
var f float64
err = json.Unmarshal(b, &f)
e.E = f
}
}
return
}
// Mostly the same as Element{e}, but also converts any int or float type to an
// int64 or float64, respectively.
// Throws a runtime panic if an unsupported type is given.
// TODO: Also convert composite types into arrays/maps?
func El(e interface{}) Element {
switch v := e.(type) {
// Core elements
case nil, string, int64, float64, []Element, map[string]Element:
return Element{v}
// Conversions
case int:
return Element{int64(v)}
case int8:
return Element{int64(v)}
case int16:
return Element{int64(v)}
case int32:
return Element{int64(v)}
case uint:
return Element{int64(v)}
case uint8:
return Element{int64(v)}
case uint16:
return Element{int64(v)}
case uint32:
return Element{int64(v)}
case uint64:
return Element{int64(v)}
case float32:
return Element{float64(v)}
}
panic("Invalid type")
}
func (t Tuple) Match(p Tuple) bool {
if len(t) < len(p) {
return false
}
for i := range p {
if !t[i].Match(p[i]) {
return false
}
}
return true
}
// Same as Tuple{El(a), El(b), ..}
func Tup(t ...interface{}) Tuple {
r := make(Tuple, len(t))
for i, v := range t {
r[i] = El(v)
}
return r
}
|