Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improving performance of jsonpath parsing #350

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ require (
github.com/iceber/iouring-go v0.0.0-20220609112130-b1dc8dd9fbfd
github.com/jedib0t/go-pretty/v6 v6.3.1
github.com/json-iterator/go v1.1.12
github.com/labstack/gommon v0.4.0
github.com/linkall-labs/embed-etcd v0.1.1
github.com/linkall-labs/vanus/client v0.5.1
github.com/linkall-labs/vanus/observability v0.5.1
github.com/linkall-labs/vanus/pkg v0.5.1
github.com/linkall-labs/vanus/proto v0.5.1
github.com/linkall-labs/vanus/raft v0.5.1
github.com/ncw/directio v1.0.5
github.com/ohler55/ojg v1.14.5
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change back to github.com/oliveagle/jsonpath?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason changed to github.com/ohler55/ojg v1.14.5 isn't github.com/oliveagle/jsonpath which doesn't support array, rather than github.com/oliveagle/jsonpath has many errors, you can see the comparison in https://cburgmer.github.io/json-path-comparison/

github.com/pkg/errors v0.9.1
github.com/prashantv/gostub v1.1.0
github.com/prometheus/client_golang v1.13.0
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/linkall-labs/embed-etcd v0.1.1 h1:WxV9wbnRtNf7DMW8SJauVYqhFLXzRfY5wpplFypXK9k=
github.com/linkall-labs/embed-etcd v0.1.1/go.mod h1:dmleSy0Myllw6W5awwjyDMipgICVDHTHuTcRT4cqaIc=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down Expand Up @@ -320,8 +322,8 @@ github.com/ncw/directio v1.0.5/go.mod h1:rX/pKEYkOXBGOggmcyJeJGloCkleSvphPx2eV3t
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/ohler55/ojg v1.14.5 h1:xCX2oyh/ZaoesbLH6fwVHStSJpk4o4eJs8ttXutzdg0=
github.com/ohler55/ojg v1.14.5/go.mod h1:7Ghirupn8NC8hSSDpI0gcjorPxj+vSVIONDWfliHR1k=
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI=
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Expand Down Expand Up @@ -412,6 +414,7 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 h1:uruHq4
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
Expand Down Expand Up @@ -633,6 +636,7 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
33 changes: 23 additions & 10 deletions internal/primitive/transform/function/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,32 @@ func Cast(val interface{}, target Type) (interface{}, error) {
case string:
return []string{value}, nil
case []interface{}:
stringArr := make([]string, len(value))
for i := range value {
v, err := Cast(value[i], String)
if err != nil {
return nil, err
}
str, _ := v.(string)
stringArr[i] = str
}
return stringArr, nil
arr, err := arrayCastString(value)
return arr, err
}
}

// AnyType doesn't need casting
return val, nil
}

func arrayCastString(value []interface{}) ([]string, error) {
stringArr := make([]string, 0, len(value))
for i := range value {
switch vv := value[i].(type) {
case []interface{}:
innerArray, err := arrayCastString(vv)
if err != nil {
return nil, err
}
stringArr = append(stringArr, innerArray...)
default:
v, err := Cast(value[i], String)
if err != nil {
return nil, err
}
stringArr = append(stringArr, v.(string))
}
}
return stringArr, nil
}
36 changes: 36 additions & 0 deletions internal/primitive/transform/function/cast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022 Linkall Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package function

import (
"strings"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestArrayCastString(t *testing.T) {
Convey("test array cast string", t, func() {
v := []interface{}{
[]interface{}{"a", "b"},
[]interface{}{"c", "d"},
[]interface{}{"e", "f"},
}
vv, err := arrayCastString(v)
So(err, ShouldBeNil)
So(len(vv), ShouldEqual, 6)
So(strings.Join(vv, ","), ShouldEqual, "a,b,c,d,e,f")
})
}
25 changes: 6 additions & 19 deletions internal/trigger/util/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
package util

import (
"encoding/json"
"fmt"
"strings"
"time"

ce "github.com/cloudevents/sdk-go/v2"
"github.com/ohler55/ojg/jp"
"github.com/ohler55/ojg/oj"
"github.com/oliveagle/jsonpath"
)

// LookupAttribute lookup event attribute value by attribute name.
Expand Down Expand Up @@ -56,25 +54,14 @@ func LookupAttribute(event ce.Event, attr string) (interface{}, bool) {

// LookupData lookup event data value by JSON path.
func LookupData(data interface{}, path string) (interface{}, error) {
d, err := json.Marshal(data)
if err != nil {
return nil, err
}
p, err := jp.ParseString(path)
if err != nil {
return nil, err
}
obj, err := oj.Parse(d)
v, err := jsonpath.JsonPathLookup(data, path)
if err != nil {
if strings.Contains(err.Error(), "not found in object") {
return nil, ErrKeyNotFound
}
return nil, err
}
res := p.Get(obj)
if len(res) == 0 {
return nil, ErrKeyNotFound
} else if len(res) == 1 {
return res[0], nil
}
return res, nil
return v, nil
}

var (
Expand Down