Skip to content

Commit

Permalink
Merge pull request #2 from liujunlingx/main
Browse files Browse the repository at this point in the history
support decimal.Decimal struct
  • Loading branch information
XIELongDragon authored Feb 16, 2023
2 parents 64885a6 + fd5be77 commit 655d427
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
15 changes: 13 additions & 2 deletions generate_influx_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (

influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api/write"
"github.com/shopspring/decimal"
)

const (
decimalPkgPath = "github.com/shopspring/decimal"
decimalStructName = "Decimal"
)

func mergeTags(org, src map[string]string) error {
Expand Down Expand Up @@ -95,7 +101,12 @@ func processFields(tags []string, org map[string]interface{}, val reflect.Value,

v := val.Field(i).Interface()
if !isOmitempty || !isValueEmpty(v) {
org[f] = v
// process decimal
if val.Field(i).Type().PkgPath() == decimalPkgPath && val.Field(i).Type().Name() == decimalStructName {
org[f] = v.(decimal.Decimal).InexactFloat64()
} else {
org[f] = v
}
}

return nil
Expand Down Expand Up @@ -207,7 +218,7 @@ func (q *influxQu) getData(v interface{}, t reflect.Type) (
}

var tmp time.Time
tmp, err = getFiledAsTime(val, i)
tmp, err = getFieldAsTime(val, i)

if err != nil {
return "", nil, nil, nil, nil, err
Expand Down
57 changes: 57 additions & 0 deletions generate_influx_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/influxdata/influxdb-client-go/v2/api/write"
"github.com/shopspring/decimal"
)

func checkTags(p *write.Point, targets map[string]string) error {
Expand Down Expand Up @@ -321,3 +322,59 @@ func Test_GenerateInfluxPoint_Stringer(t *testing.T) {
t.Error(e)
}
}

func Test_GenerateInfluxPoint_Using_Decimal(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 decimal.Decimal `influxqu:"field,f3"`
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: decimal.NewFromFloat(1.35),
Timestamp: time.Now(),
}

p, e := g.GenerateInfluxPoint(&data)
if e != nil {
t.Error(e)
}

if p == nil {
t.Error("point is nil")
}

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.InexactFloat64()}); e != nil {
t.Error(e)
}
}
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ module github.com/XIELongDragon/go-influx-qu

go 1.18

require github.com/influxdata/influxdb-client-go/v2 v2.9.2
require (
github.com/influxdata/influxdb-client-go/v2 v2.9.2
github.com/shopspring/decimal v1.3.1
)

require (
github.com/deepmap/oapi-codegen v1.11.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaW
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
Expand Down Expand Up @@ -83,6 +84,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down Expand Up @@ -112,8 +115,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220513224357-95641704303c/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E=
golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -145,10 +148,15 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U=
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230209215440-0dfe4f8abfcc h1:ijGwO+0vL2hJt5gaygqP2j6PfflOBrRot0IczKbmtio=
google.golang.org/genproto v0.0.0-20230209215440-0dfe4f8abfcc/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
2 changes: 1 addition & 1 deletion utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func getFieldAsString(val reflect.Value, i int) (string, error) {
return "", &UnSupportedType{}
}

func getFiledAsTime(val reflect.Value, i int) (time.Time, error) {
func getFieldAsTime(val reflect.Value, i int) (time.Time, error) {
f := val.Field(i)

if f.Kind() == reflect.Ptr {
Expand Down

0 comments on commit 655d427

Please sign in to comment.