Skip to content

Commit

Permalink
Merge pull request #111 from bonitoo-io/fix/empty-values-#110
Browse files Browse the repository at this point in the history
fix: Handling empty values
  • Loading branch information
vlastahajek authored May 4, 2020
2 parents 110cc88 + 8ca083b commit 46f5436
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## 1.2.0
## Breaking Change
- [#107](https://github.com/influxdata/influxdb-client-go/pull/100) Renamed `InfluxDBClient` interface to `Client`, so the full name `influxdb2.Client` suits better to Go naming conventions


## Bug fixes
1. [#110](https://github.com/influxdata/influxdb-client-go/issues/110) Allowing empty (nil) values

## 1.1.0 [2020-04-24]
### Features
1. [#100](https://github.com/influxdata/influxdb-client-go/pull/100) HTTP request timeout made configurable
Expand Down
3 changes: 3 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ func stringTernary(a, b string) string {

// toValues converts s into type by t
func toValue(s, t, name string) (interface{}, error) {
if s == "" {
return nil, nil
}
switch t {
case stringDatatype:
return s, nil
Expand Down
33 changes: 33 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,39 @@ func TestDifferentNumberOfColumns(t *testing.T) {
assert.Equal(t, "parsing error, row has different number of columns than table: 11 vs 10", queryResult.Err().Error())
}

func TestEmptyValue(t *testing.T) {
csvTable := `#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string,string
#group,false,false,true,true,false,false,true,true,true,true
#default,_result,,,,,,,,,
,result,table,_start,_stop,_time,_value,_field,_measurement,a,b
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T10:34:08.135814545Z,,f,test,1,adsfasdf
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:08:44.850214724Z,6.6,f,test,,adsfasdf
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:11:32.225467895Z,1122.45,f,test,3,
`

reader := strings.NewReader(csvTable)
csvReader := csv.NewReader(reader)
csvReader.FieldsPerRecord = -1
queryResult := &QueryTableResult{Closer: ioutil.NopCloser(reader), csvReader: csvReader}

require.True(t, queryResult.Next(), queryResult.Err())
require.Nil(t, queryResult.Err())

require.NotNil(t, queryResult.Record())
assert.Nil(t, queryResult.Record().Value())

require.True(t, queryResult.Next(), queryResult.Err())
require.NotNil(t, queryResult.Record())
assert.Nil(t, queryResult.Record().ValueByKey("a"))

require.True(t, queryResult.Next(), queryResult.Err())
require.NotNil(t, queryResult.Record())
assert.Nil(t, queryResult.Record().ValueByKey("b"))

require.False(t, queryResult.Next())
require.Nil(t, queryResult.Err())
}

func TestFluxError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
Expand Down
4 changes: 2 additions & 2 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (r *FluxRecord) Time() time.Time {
return r.ValueByKey("_time").(time.Time)
}

// Value returns the actual field value
// Value returns the default _value column value or nil if not present
func (r *FluxRecord) Value() interface{} {
return r.ValueByKey("_value")
}
Expand All @@ -175,7 +175,7 @@ func (r *FluxRecord) Values() map[string]interface{} {
return r.values
}

// ValueByKey returns value for given column key for the record
// ValueByKey returns value for given column key for the record or nil of result has no value the column key
func (r *FluxRecord) ValueByKey(key string) interface{} {
return r.values[key]
}
Expand Down

0 comments on commit 46f5436

Please sign in to comment.