chore(vendor): Add jsonparser dependency for filter predicates
* Added jsonparser helper package to decode influxdb.IDpull/16019/head
parent
ba5dc411df
commit
36b0dc7da3
1
go.mod
1
go.mod
|
@ -13,6 +13,7 @@ require (
|
|||
github.com/benbjohnson/tmpl v1.0.0
|
||||
github.com/boltdb/bolt v1.3.1 // indirect
|
||||
github.com/bouk/httprouter v0.0.0-20160817010721-ee8b3818a7f5
|
||||
github.com/buger/jsonparser v0.0.0-20191004114745-ee4c978eae7e
|
||||
github.com/cespare/xxhash v1.1.0
|
||||
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
|
||||
github.com/coreos/bbolt v1.3.1-coreos.6
|
||||
|
|
2
go.sum
2
go.sum
|
@ -64,6 +64,8 @@ github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
|
|||
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
|
||||
github.com/bouk/httprouter v0.0.0-20160817010721-ee8b3818a7f5 h1:kS0dw4K730x7cxT+bVyTyYJZHuSoH7ofSr/Ijit56Qw=
|
||||
github.com/bouk/httprouter v0.0.0-20160817010721-ee8b3818a7f5/go.mod h1:CDReaxg1cmLrtcasZy43l4EYPAknXLiQSrb7tLw5zXM=
|
||||
github.com/buger/jsonparser v0.0.0-20191004114745-ee4c978eae7e h1:oJCXMss/3rg5F6Poy9wG3JQusc58Mzk5B9Z6wSnssNE=
|
||||
github.com/buger/jsonparser v0.0.0-20191004114745-ee4c978eae7e/go.mod h1:errmMKH8tTB49UR2A8C8DPYkyudelsYJwJFaZHQ6ik8=
|
||||
github.com/c-bata/go-prompt v0.2.2 h1:uyKRz6Z6DUyj49QVijyM339UJV9yhbr70gESwbNU3e0=
|
||||
github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34=
|
||||
github.com/caarlos0/ctrlc v1.0.0 h1:2DtF8GSIcajgffDFJzyG15vO+1PuBWOMUdFut7NnXhw=
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package jsonparser
|
||||
|
||||
import (
|
||||
"github.com/buger/jsonparser"
|
||||
"github.com/influxdata/influxdb"
|
||||
)
|
||||
|
||||
// GetID returns an influxdb.ID for the specified keys path or an error if
|
||||
// the value cannot be decoded or does not exist.
|
||||
func GetID(data []byte, keys ...string) (val influxdb.ID, err error) {
|
||||
v, _, _, err := jsonparser.Get(data, keys...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var id influxdb.ID
|
||||
err = id.Decode(v)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// GetOptionalID returns an influxdb.ID for the specified keys path or an error if
|
||||
// the value cannot be decoded. The value of exists will be false if the keys path
|
||||
// does not exist.
|
||||
func GetOptionalID(data []byte, keys ...string) (val influxdb.ID, exists bool, err error) {
|
||||
v, typ, _, err := jsonparser.Get(data, keys...)
|
||||
if typ == jsonparser.NotExist {
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
var id influxdb.ID
|
||||
err = id.Decode(v)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
return id, true, nil
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package jsonparser_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/influxdata/influxdb"
|
||||
"github.com/influxdata/influxdb/pkg/jsonparser"
|
||||
)
|
||||
|
||||
func TestGetID(t *testing.T) {
|
||||
t.Run("decode valid id", func(t *testing.T) {
|
||||
json := `{ "id": "000000000000000a" }`
|
||||
got, err := jsonparser.GetID([]byte(json), "id")
|
||||
if err != nil {
|
||||
t.Error("unexpected error:", err)
|
||||
}
|
||||
|
||||
if exp := influxdb.ID(10); got != exp {
|
||||
t.Error("unexpected value: -got/+exp", cmp.Diff(got, exp))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error invalid id", func(t *testing.T) {
|
||||
json := `{ "id": "00000000000a" }`
|
||||
_, err := jsonparser.GetID([]byte(json), "id")
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetOptionalID(t *testing.T) {
|
||||
t.Run("missing id", func(t *testing.T) {
|
||||
json := `{ "name": "foo" }`
|
||||
_, got, err := jsonparser.GetOptionalID([]byte(json), "id")
|
||||
if err != nil {
|
||||
t.Error("unexpected error:", err)
|
||||
}
|
||||
|
||||
if exp := false; got != exp {
|
||||
t.Error("unexpected value: -got/+exp", cmp.Diff(got, exp))
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue