Skip to content

Commit 4187359

Browse files
authored
Merge pull request #302 from bonitoo-io/fix/record_table
fix: FluxRecord.Table() returns value of the table column
2 parents a00324e + 7137cc0 commit 4187359

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
## unreleased
22
### Features
3-
- [#297](https://github.com/influxdata/influxdb-client-go/pull/297),[#298](https://github.com/influxdata/influxdb-client-go/pull/298) Optimized `WriteRecord` of [WriteAPIBlocking](https://pkg.go.dev/github.com/influxdata/influxdb-client-go/v2/api#WriteAPIBlocking).
3+
- [#297](https://github.com/influxdata/influxdb-client-go/pull/297),[#298](https://github.com/influxdata/influxdb-client-go/pull/298) Optimized `WriteRecord` of [WriteAPIBlocking](https://pkg.go.dev/github.com/influxdata/influxdb-client-go/v2/api#WriteAPIBlocking). Custom batch can be written by single argument.
44

55
### Bug fixes
66
- [#294](https://github.com/influxdata/influxdb-client-go/pull/294) `WritePoint` and `WriteRecord` of [WriteAPIBlocking](https://pkg.go.dev/github.com/influxdata/influxdb-client-go/v2/api#WriteAPIBlocking) returns always full error information.
7-
7+
- [300](https://github.com/influxdata/influxdb-client-go/pull/300) Closing the response body after write batch.
8+
- [302](https://github.com/influxdata/influxdb-client-go/pull/302) FluxRecord.Table() returns value of the table column.
89

910
## 2.6.0[2021-11-26]
1011
### Features

api/query/table.go

+30-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package query
77

88
import (
99
"fmt"
10+
"sort"
1011
"strings"
1112
"time"
1213
)
@@ -146,9 +147,10 @@ func NewFluxRecord(table int, values map[string]interface{}) *FluxRecord {
146147
return &FluxRecord{table: table, values: values}
147148
}
148149

149-
// Table returns index of the table record belongs to
150+
// Table returns value of the table column
151+
// It returns zero if the table column is not found
150152
func (r *FluxRecord) Table() int {
151-
return r.table
153+
return int(intValue(r.values, "table"))
152154
}
153155

154156
// Start returns the inclusive lower time bound of all records in the current table.
@@ -204,15 +206,23 @@ func (r *FluxRecord) ValueByKey(key string) interface{} {
204206

205207
// String returns FluxRecord string dump
206208
func (r *FluxRecord) String() string {
207-
var buffer strings.Builder
209+
if len(r.values) == 0 {
210+
return ""
211+
}
212+
208213
i := 0
209-
for k, v := range r.values {
210-
if i > 0 {
211-
buffer.WriteString(",")
212-
}
213-
buffer.WriteString(fmt.Sprintf("%s:%v", k, v))
214+
keys := make([]string, len(r.values))
215+
for k := range r.values {
216+
keys[i] = k
214217
i++
215218
}
219+
sort.Strings(keys)
220+
var buffer strings.Builder
221+
buffer.WriteString(fmt.Sprintf("%s:%v", keys[0], r.values[keys[0]]))
222+
for _, k := range keys[1:] {
223+
buffer.WriteString(",")
224+
buffer.WriteString(fmt.Sprintf("%s:%v", k, r.values[k]))
225+
}
216226
return buffer.String()
217227
}
218228

@@ -227,7 +237,7 @@ func timeValue(values map[string]interface{}, key string) time.Time {
227237
return time.Time{}
228238
}
229239

230-
// timeValue returns string value from values map according to the key
240+
// stringValue returns string value from values map according to the key
231241
// Empty string is returned if key is not found
232242
func stringValue(values map[string]interface{}, key string) string {
233243
if val, ok := values[key]; ok {
@@ -237,3 +247,14 @@ func stringValue(values map[string]interface{}, key string) string {
237247
}
238248
return ""
239249
}
250+
251+
// intValue returns int64 value from values map according to the key
252+
// Zero value is returned if key is not found
253+
func intValue(values map[string]interface{}, key string) int64 {
254+
if val, ok := values[key]; ok {
255+
if i, ok := val.(int64); ok {
256+
return i
257+
}
258+
}
259+
return 0
260+
}

api/query/table_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestRecord(t *testing.T) {
7070
record := &FluxRecord{table: 2,
7171
values: map[string]interface{}{
7272
"result": "_result",
73-
"_table": int64(0),
73+
"table": int64(2),
7474
"_start": mustParseTime("2020-02-17T22:19:49.747562847Z"),
7575
"_stop": mustParseTime("2020-02-18T22:19:49.747562847Z"),
7676
"_time": mustParseTime("2020-02-18T10:34:08.135814545Z"),

api/query_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
3535
#group,false,false,true,true,false,false,true,true,true,true
3636
#default,_result,,,,,,,,,
3737
,result,table,_start,_stop,_time,_value,_field,_measurement,a,b
38-
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T10:34:08.135814545Z,1.4,f,test,1,adsfasdf
39-
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:08:44.850214724Z,6.6,f,test,1,adsfasdf
38+
,,1,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T10:34:08.135814545Z,1.4,f,test,1,adsfasdf
39+
,,1,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:08:44.850214724Z,6.6,f,test,1,adsfasdf
4040
4141
`
4242
expectedTable := query.NewFluxTableMetadataFull(0,
@@ -56,7 +56,7 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
5656
expectedRecord1 := query.NewFluxRecord(0,
5757
map[string]interface{}{
5858
"result": "_result",
59-
"table": int64(0),
59+
"table": int64(1),
6060
"_start": mustParseTime("2020-02-17T22:19:49.747562847Z"),
6161
"_stop": mustParseTime("2020-02-18T22:19:49.747562847Z"),
6262
"_time": mustParseTime("2020-02-18T10:34:08.135814545Z"),
@@ -71,7 +71,7 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
7171
expectedRecord2 := query.NewFluxRecord(0,
7272
map[string]interface{}{
7373
"result": "_result",
74-
"table": int64(0),
74+
"table": int64(1),
7575
"_start": mustParseTime("2020-02-17T22:19:49.747562847Z"),
7676
"_stop": mustParseTime("2020-02-18T22:19:49.747562847Z"),
7777
"_time": mustParseTime("2020-02-18T22:08:44.850214724Z"),
@@ -100,6 +100,8 @@ func TestQueryCVSResultSingleTable(t *testing.T) {
100100
assert.False(t, queryResult.tableChanged)
101101
require.NotNil(t, queryResult.Record())
102102
require.Equal(t, queryResult.Record(), expectedRecord2)
103+
assert.Equal(t, "_result", queryResult.Record().Result())
104+
assert.Equal(t, 1, queryResult.Record().Table())
103105

104106
require.False(t, queryResult.Next())
105107
require.Nil(t, queryResult.Err())
@@ -383,6 +385,7 @@ func TestQueryCVSResultMultiTables(t *testing.T) {
383385
require.NotNil(t, queryResult.Record())
384386
require.Equal(t, queryResult.Record(), expectedRecord42)
385387
assert.Equal(t, "_result4", queryResult.Record().Result())
388+
assert.Equal(t, 3, queryResult.Record().Table())
386389

387390
require.False(t, queryResult.Next())
388391
require.Nil(t, queryResult.Err())

0 commit comments

Comments
 (0)