Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handling empty values #111

Merged
merged 4 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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