Skip to content

Commit 99751a8

Browse files
feat - extend methods on response.Data
1 parent 07ac163 commit 99751a8

4 files changed

Lines changed: 104 additions & 7 deletions

File tree

data.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tinybird
2+
3+
import "encoding/json"
4+
5+
// Row is a generic map-based structure that can hold fields of any type.
6+
// Each key corresponds to a field name, and each value can be any Go type.
7+
type Row map[string]any
8+
9+
// Data represents a collection (slice) of rows returned from a query or dataset.
10+
type Data []Row
11+
12+
// Count returns the number of rows in the Data slice.
13+
func (d Data) Count() (out int) {
14+
return len(d)
15+
}
16+
17+
// First returns the first Row in the Data slice.
18+
// It assumes the slice is not empty.
19+
func (d Data) First() (out Row) {
20+
if d.Count() == 0 {
21+
out = Row{}
22+
} else {
23+
out = d[0]
24+
}
25+
26+
return out
27+
}
28+
29+
// FetchOne retrieves the value of a specific field from the first Row
30+
// in the Data slice, using the provided field name (key).
31+
// If the key doesn't exist, it returns nil.
32+
func (d Data) FetchOne(in string) (out any) {
33+
row := d.First()
34+
out = row[in]
35+
36+
return out
37+
}
38+
39+
// ToString converts the entire Data slice into a JSON-formatted string.
40+
// If marshalling fails, it returns an empty string.
41+
func (d Data) ToString() (out string) {
42+
if d == nil {
43+
return "[]"
44+
}
45+
46+
tmp, err := json.Marshal(d)
47+
48+
if err != nil {
49+
return "[]"
50+
}
51+
52+
return string(tmp)
53+
}

data_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tinybird_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/the-hotels-network/go-tinybird"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestData_Empty(t *testing.T) {
12+
d1 := tinybird.Data{}
13+
14+
assert.Equal(t, tinybird.Row{}, d1.First())
15+
assert.Equal(t, 0, d1.Count())
16+
assert.Equal(t, nil, d1.FetchOne("foo"))
17+
assert.Equal(t, "[]", d1.ToString())
18+
19+
var d2 tinybird.Data
20+
21+
assert.Equal(t, tinybird.Row{}, d2.First())
22+
assert.Equal(t, 0, d2.Count())
23+
assert.Equal(t, nil, d2.FetchOne("foo"))
24+
assert.Equal(t, "[]", d2.ToString())
25+
}
26+
27+
func TestData_First(t *testing.T) {
28+
d := tinybird.Data{
29+
{"id": 1, "name": "first"},
30+
{"id": 2, "name": "second"},
31+
}
32+
33+
assert.Equal(t, 2, d.Count())
34+
assert.Equal(t, tinybird.Row{"id": 1, "name": "first"}, d.First())
35+
}
36+
37+
func TestData_FetchOne(t *testing.T) {
38+
d := tinybird.Data{
39+
{"id": 10, "name": "first", "bool": true},
40+
}
41+
42+
assert.Equal(t, 1, d.Count())
43+
assert.Equal(t, "first", d.FetchOne("name"))
44+
assert.Equal(t, true, d.FetchOne("bool"))
45+
assert.Equal(t, 10, d.FetchOne("id"))
46+
assert.Nil(t, d.FetchOne("missing"))
47+
}

request_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestRequest(t *testing.T) {
3636
assert.Nil(t, req.Error)
3737
assert.Equal(t, res.Status, http.StatusOK)
3838
assert.Equal(t, res.Rows, uint(1))
39-
assert.Equal(t, res.Data, []tinybird.Row{{"Col1": "1", "Col2": float64(2)}})
39+
assert.Equal(t, res.Data, tinybird.Data{{"Col1": "1", "Col2": float64(2)}})
4040
}
4141

4242
func TestRequestWithCustomURL(t *testing.T) {
@@ -66,7 +66,7 @@ func TestRequestWithCustomURL(t *testing.T) {
6666
assert.Nil(t, req.Error)
6767
assert.Equal(t, res.Status, http.StatusOK)
6868
assert.Equal(t, res.Rows, uint(1))
69-
assert.Equal(t, res.Data, []tinybird.Row{{"Col1": "1", "Col2": float64(2)}})
69+
assert.Equal(t, res.Data, tinybird.Data{{"Col1": "1", "Col2": float64(2)}})
7070
}
7171

7272
func TestRequestWithRequestParamInspect(t *testing.T) {
@@ -204,5 +204,5 @@ func TestRequestEvent(t *testing.T) {
204204
assert.Nil(t, req.Error)
205205
assert.Equal(t, res.Status, http.StatusOK)
206206
assert.Equal(t, res.Rows, uint(0))
207-
assert.Equal(t, res.Data, []tinybird.Row(nil))
207+
assert.Equal(t, res.Data, tinybird.Data(nil))
208208
}

response.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Response struct {
1414
Cached bool // Flag control to use to determine the origin this response.
1515
Raw io.ReadCloser // Raw is the original data from response.
1616
Body string // Body in string format, same a Data but in struct format and Raw in original format.
17-
Data []Row `json:"data,omitempty"` // Data is part a tinybird response.
17+
Data Data `json:"data,omitempty"` // Data is part a tinybird response.
1818
Documentation string `json:"documentation,omitempty"` // Documentation is part a tinybird response.
1919
Error string `json:"error,omitempty"` // Error is part a tinybird response.
2020
Meta []Meta `json:"meta,omitempty"` // Meta is part a tinybird response.
@@ -28,9 +28,6 @@ type Response struct {
2828
QuarantinedRows int `json:"quarantined_rows"`
2929
}
3030

31-
// Generic row structure to allow any field with any type.
32-
type Row map[string]any
33-
3431
// Specific field with data type.
3532
type Meta struct {
3633
Name string `json:"name,omitempty"`

0 commit comments

Comments
 (0)