diff --git a/generate_influx_point.go b/generate_influx_point.go index b94aeb7..62a3361 100644 --- a/generate_influx_point.go +++ b/generate_influx_point.go @@ -104,6 +104,9 @@ func processFields(tags []string, org map[string]interface{}, val reflect.Value, // process decimal if val.Field(i).Type().PkgPath() == decimalPkgPath && val.Field(i).Type().Name() == decimalStructName { org[f] = v.(decimal.Decimal).InexactFloat64() + } else if val.Field(i).Kind() == reflect.Pointer { + underlyingVal := reflect.Indirect(val.Field(i)) + org[f] = underlyingVal.Interface() } else { org[f] = v } diff --git a/generate_influx_point_test.go b/generate_influx_point_test.go index 1039bd1..fd978f7 100644 --- a/generate_influx_point_test.go +++ b/generate_influx_point_test.go @@ -378,3 +378,113 @@ func Test_GenerateInfluxPoint_Using_Decimal(t *testing.T) { t.Error(e) } } + +func Test_GenerateInfluxPoint_Using_Pointer(t *testing.T) { + type Data struct { + Base string `influxqu:"measurement"` + T1 string `influxqu:"tag,t1"` + T2 string `influxqu:"tag,t2"` + F1 int `influxqu:"field,f1"` + F2 bool `influxqu:"field,f2"` + F3 *float64 `influxqu:"field,f3,omitempty"` + Timestamp time.Time `influxqu:"timestamp"` + } + + const ( + tag1 = "t1" + tag2 = "t2" + field1 = "f1" + field2 = "f2" + field3 = "f3" + ) + + var f3 float64 = 64 + + g := NewinfluxQu() + data := Data{ + Base: "base", + T1: "t1", + T2: "t2", + F1: 1, + F2: true, + F3: &f3, + Timestamp: time.Now(), + } + + p, e := g.GenerateInfluxPoint(&data) + if e != nil { + t.Error(e) + } + + if p.Name() != data.Base { + t.Error("point name is not base") + } + + if p.Time() != data.Timestamp { + t.Error("point timestamp is not data.Timestamp") + } + + if e := checkTags(p, map[string]string{tag1: data.T1, tag2: data.T2}); e != nil { + t.Error(e) + } + + if e := checkFields(p, map[string]interface{}{ + field1: int64(data.F1), field2: data.F2, field3: *data.F3, + }); e != nil { + t.Error(e) + } +} + +func Test_GenerateInfluxPoint_Using_Nil_Pointer(t *testing.T) { + type Data struct { + Base string `influxqu:"measurement"` + T1 string `influxqu:"tag,t1"` + T2 string `influxqu:"tag,t2"` + F1 int `influxqu:"field,f1"` + F2 bool `influxqu:"field,f2"` + F3 *float64 `influxqu:"field,f3,omitempty"` + Timestamp time.Time `influxqu:"timestamp"` + } + + const ( + tag1 = "t1" + tag2 = "t2" + field1 = "f1" + field2 = "f2" + field3 = "f3" + ) + + g := NewinfluxQu() + data := Data{ + Base: "base", + T1: "t1", + T2: "t2", + F1: 1, + F2: true, + F3: nil, + Timestamp: time.Now(), + } + + p, e := g.GenerateInfluxPoint(&data) + if e != nil { + t.Error(e) + } + + if p.Name() != data.Base { + t.Error("point name is not base") + } + + if p.Time() != data.Timestamp { + t.Error("point timestamp is not data.Timestamp") + } + + if e := checkTags(p, map[string]string{tag1: data.T1, tag2: data.T2}); e != nil { + t.Error(e) + } + + if e := checkFields(p, map[string]interface{}{ + field1: int64(data.F1), field2: data.F2, field3: nil, + }); e != nil { + t.Error(e) + } +}