-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata.go
More file actions
177 lines (145 loc) · 3.67 KB
/
data.go
File metadata and controls
177 lines (145 loc) · 3.67 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
170
171
172
173
174
175
176
177
package tinybird
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
// Row is a generic map-based structure that can hold fields of any type.
// Each key corresponds to a field name, and each value can be any Go type.
type Row map[string]any
// Data represents a collection (slice) of rows returned from a query or dataset.
type Data []Row
// Count the items.
func (d Data) Len() int {
return len(d)
}
// First returns the first Row in the Data slice.
// It assumes the slice is not empty.
func (d Data) First() Row {
if d.Len() > 0 {
return d[0]
}
return Row{}
}
// FetchOne retrieves the value of a specific field from the first Row
// in the Data slice, using the provided field name (key).
// If the key doesn't exist, it returns nil.
func (d Data) FetchOne(in string) (out any) {
row := d.First()
out = row[in]
return out
}
// ToString converts the entire Data slice into a JSON-formatted string.
// If marshalling fails, it returns an empty string.
func (d Data) ToString() (out string) {
tmp, _ := json.Marshal(d)
return string(tmp)
}
// Get retrieves a value from the first row of the Data structure
// by traversing a dot-separated path.
//
// The path represents nested keys within Row maps (e.g. "a.b.c").
// If the Data slice is empty, the path does not exist, or an
// intermediate value is not a Row, Get returns nil.
func (d Data) Get(in string) any {
if len(d) == 0 {
return nil
}
parts := strings.Split(in, ".")
for _, row := range d {
if v, ok := get(row, parts); ok {
return v
}
}
return nil
}
func get(row Row, parts []string) (any, bool) {
var current any = row
for _, part := range parts {
switch v := current.(type) {
case Row:
val, ok := v[part]
if !ok {
return nil, false
}
current = val
case map[string]any:
val, ok := v[part]
if !ok {
return nil, false
}
current = val
default:
return nil, false
}
}
return current, true
}
// Set assigns a value in the first row of the Data structure
// at the location specified by a dot-separated path.
//
// Intermediate Row nodes are created as needed if they do not exist.
// An error is returned if Data is empty, the path is empty, or
// a non-Row value is encountered while traversing the path.
func (d *Data) Set(in string, value any) error {
if d == nil {
return errors.New("nil Data")
}
if len(*d) == 0 {
return errors.New("empty Data")
}
parts := strings.Split(in, ".")
for i := range *d {
if err := set((*d)[i], parts, value); err != nil {
return err
}
}
return nil
}
func set(row Row, parts []string, value any) error {
current := row
for index, part := range parts {
if index == len(parts)-1 {
current[part] = value
return nil
}
next, exists := current[part]
if !exists {
child := Row{}
current[part] = child
current = child
continue
}
switch v := next.(type) {
case Row:
current = v
case map[string]any:
current = Row(v)
default:
return errors.New("path collision at " + part)
}
}
return nil
}
// SetWithAutoCast works like Set but applies AutoCast to the value before
// storing it, converting numeric strings to their int64 or float64 equivalents.
func (d *Data) SetWithAutoCast(in string, value any) error {
return d.Set(in, AutoCast(value))
}
// AutoCast attempts to infer the real type of a value. If the value is
// a string it tries to parse it as int64 first, then float64. Non-string
// values and strings that cannot be parsed are returned unchanged.
func AutoCast(val any) any {
s, ok := val.(string)
if !ok {
return val
}
if n, err := strconv.ParseInt(s, 10, 64); err == nil {
return n
}
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
return val
}