From 5fcc9fe193d0915cc32e639034bad5e1f82483f1 Mon Sep 17 00:00:00 2001 From: Alexander Emelin Date: Fri, 10 Sep 2021 16:24:31 +0300 Subject: [PATCH] temporary switch to alternative jsonpath implementation (#39090) to work with go1.16 and avoid replace in go.mod --- go.mod | 5 +- go.sum | 4 +- .../live/pipeline/converter_json_exact.go | 83 +++++++++++++++---- 3 files changed, 70 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 1a45aa71662..12992fd9c0f 100644 --- a/go.mod +++ b/go.mod @@ -11,9 +11,6 @@ replace github.com/denisenkom/go-mssqldb => github.com/grafana/go-mssqldb v0.0.0 // It's also present on grafana/loki's go.mod so we'll need till it gets updated. replace k8s.io/client-go => k8s.io/client-go v0.22.1 -// Temporary replace till https://github.com/ohler55/ojg/issues/72 solved to fix builds on 32-bit CPU. -replace github.com/ohler55/ojg => github.com/FZambia/ojg v1.12.6-0.20210910083152-5e71da98845c - require ( cloud.google.com/go/storage v1.14.0 cuelang.org/go v0.3.2 @@ -73,7 +70,6 @@ require ( github.com/mattn/go-sqlite3 v1.14.7 github.com/matttproud/golang_protobuf_extensions v1.0.1 github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f - github.com/ohler55/ojg v1.12.3 github.com/opentracing/opentracing-go v1.2.0 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/browser v0.0.0-20210904010418-6d279e18f982 // indirect @@ -87,6 +83,7 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/russellhaering/goxmldsig v1.1.0 github.com/smartystreets/goconvey v1.6.4 + github.com/spyzhov/ajson v0.4.2 github.com/stretchr/testify v1.7.0 github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf github.com/timberio/go-datemath v0.1.1-0.20200323150745-74ddef604fff diff --git a/go.sum b/go.sum index c223b07823a..c2c4212c7f9 100644 --- a/go.sum +++ b/go.sum @@ -160,8 +160,6 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3 github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= github.com/FZambia/eagle v0.0.1 h1:FN1yTkPihMb5nE8SrlRjoCf7T9H9bTKJFQOm6ach2YU= github.com/FZambia/eagle v0.0.1/go.mod h1:xq6u/JeNZ5/8mrAQ76MMhzNTodASh9FavQlCgg4j48w= -github.com/FZambia/ojg v1.12.6-0.20210910083152-5e71da98845c h1:V4weFplCUxKfzuWcwDGYbsGA3nKhP+FZwADA93GnhMs= -github.com/FZambia/ojg v1.12.6-0.20210910083152-5e71da98845c/go.mod h1:LBbIVRAgoFbYBXQhRhuEpaJIqq+goSO63/FQ+nyJU88= github.com/FZambia/sentinel v1.1.0 h1:qrCBfxc8SvJihYNjBWgwUI93ZCvFe/PJIPTHKmlp8a8= github.com/FZambia/sentinel v1.1.0/go.mod h1:ytL1Am/RLlAoAXG6Kj5LNuw/TRRQrv2rt2FT26vP5gI= github.com/HdrHistogram/hdrhistogram-go v0.9.0/go.mod h1:nxrse8/Tzg2tg3DZcZjm6qEclQKK70g0KxO61gFFZD4= @@ -1931,6 +1929,8 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns= +github.com/spyzhov/ajson v0.4.2 h1:JMByd/jZApPKDvNsmO90X2WWGbmT2ahDFp73QhZbg3s= +github.com/spyzhov/ajson v0.4.2/go.mod h1:63V+CGM6f1Bu/p4nLIN8885ojBdt88TbLoSFzyqMuVA= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= github.com/streadway/amqp v0.0.0-20180528204448-e5adc2ada8b8/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= diff --git a/pkg/services/live/pipeline/converter_json_exact.go b/pkg/services/live/pipeline/converter_json_exact.go index ea13b6037ad..d48ccc0e5d0 100644 --- a/pkg/services/live/pipeline/converter_json_exact.go +++ b/pkg/services/live/pipeline/converter_json_exact.go @@ -9,8 +9,7 @@ import ( "time" "github.com/grafana/grafana-plugin-sdk-go/data" - "github.com/ohler55/ojg/jp" - "github.com/ohler55/ojg/oj" + "github.com/spyzhov/ajson" ) type ExactJsonConverterConfig struct { @@ -29,10 +28,10 @@ func NewExactJsonConverter(c ExactJsonConverterConfig) *ExactJsonConverter { } func (c *ExactJsonConverter) Convert(_ context.Context, vars Vars, body []byte) ([]*ChannelFrame, error) { - obj, err := oj.Parse(body) - if err != nil { - return nil, err - } + //obj, err := oj.Parse(body) + //if err != nil { + // return nil, err + //} var fields []*data.Field @@ -46,15 +45,17 @@ func (c *ExactJsonConverter) Convert(_ context.Context, vars Vars, body []byte) if strings.HasPrefix(f.Value, "$") { // JSON path. - x, err := jp.ParseString(f.Value[1:]) + nodes, err := ajson.JSONPath(body, f.Value) if err != nil { return nil, err } - value := x.Get(obj) - if len(value) == 0 { + if len(nodes) == 0 { field.Set(0, nil) - } else if len(value) == 1 { - val := value[0] + } else if len(nodes) == 1 { + val, err := nodes[0].Value() + if err != nil { + return nil, err + } switch f.Type { case data.FieldTypeNullableFloat64: if val == nil { @@ -81,6 +82,41 @@ func (c *ExactJsonConverter) Convert(_ context.Context, vars Vars, body []byte) } else { return nil, errors.New("too many values") } + //x, err := jp.ParseString(f.Value[1:]) + //if err != nil { + // return nil, err + //} + //value := x.Get(obj) + //if len(value) == 0 { + // field.Set(0, nil) + //} else if len(value) == 1 { + // val := value[0] + // switch f.Type { + // case data.FieldTypeNullableFloat64: + // if val == nil { + // field.Set(0, nil) + // } else { + // switch v := val.(type) { + // case float64: + // field.SetConcrete(0, v) + // case int64: + // field.SetConcrete(0, float64(v)) + // default: + // return nil, errors.New("malformed float64 type for: " + f.Name) + // } + // } + // case data.FieldTypeNullableString: + // v, ok := val.(string) + // if !ok { + // return nil, errors.New("malformed string type") + // } + // field.SetConcrete(0, v) + // default: + // return nil, fmt.Errorf("unsupported field type: %s (%s)", f.Type, f.Name) + // } + //} else { + // return nil, errors.New("too many values") + //} } else if strings.HasPrefix(f.Value, "{") { // Goja script. script := strings.Trim(f.Value, "{}") @@ -120,18 +156,33 @@ func (c *ExactJsonConverter) Convert(_ context.Context, vars Vars, body []byte) labels := map[string]string{} for _, label := range f.Labels { if strings.HasPrefix(label.Value, "$") { - x, err := jp.ParseString(label.Value[1:]) + nodes, err := ajson.JSONPath(body, label.Value) if err != nil { return nil, err } - value := x.Get(obj) - if len(value) == 0 { + if len(nodes) == 0 { labels[label.Name] = "" - } else if len(value) == 1 { - labels[label.Name] = fmt.Sprintf("%v", value[0]) + } else if len(nodes) == 1 { + value, err := nodes[0].Value() + if err != nil { + return nil, err + } + labels[label.Name] = fmt.Sprintf("%v", value) } else { return nil, errors.New("too many values for a label") } + //x, err := jp.ParseString(label.Value[1:]) + //if err != nil { + // return nil, err + //} + //value := x.Get(obj) + //if len(value) == 0 { + // labels[label.Name] = "" + //} else if len(value) == 1 { + // labels[label.Name] = fmt.Sprintf("%v", value[0]) + //} else { + // return nil, errors.New("too many values for a label") + //} } else if strings.HasPrefix(label.Value, "{") { script := strings.Trim(label.Value, "{}") var err error