Skip to content

Commit

Permalink
support pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
chukiattohMatrixport committed Feb 27, 2023
1 parent 655d427 commit afdcfd5
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions generate_influx_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
110 changes: 110 additions & 0 deletions generate_influx_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit afdcfd5

Please sign in to comment.