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: support reference with foo["bar"] syntax #394

Merged
merged 1 commit into from
Feb 26, 2024
Merged
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
1 change: 1 addition & 0 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func main() {

w := worker.New(temporalClient, pipelineWorker.TaskQueue, worker.Options{
MaxConcurrentActivityExecutionSize: 2,
WorkflowPanicPolicy: worker.FailWorkflow,
})

w.RegisterWorkflow(cw.TriggerPipelineWorkflow)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.21.3

require (
cloud.google.com/go/longrunning v0.5.2
github.com/PaesslerAG/jsonpath v0.1.1
github.com/frankban/quicktest v1.14.6
github.com/gabriel-vasile/mimetype v1.4.3
github.com/go-redis/redis/v9 v9.0.0-beta.2
Expand All @@ -24,7 +25,6 @@ require (
github.com/instill-ai/x v0.4.0-alpha
github.com/knadh/koanf v1.5.0
github.com/mennanov/fieldmask-utils v1.0.0
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/openfga/go-sdk v0.2.3
go.einride.tech/aip v0.60.0
go.opentelemetry.io/contrib/propagators/b3 v1.17.0
Expand Down Expand Up @@ -58,6 +58,7 @@ require (
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/storage v1.34.1 // indirect
github.com/JohannesKaufmann/html-to-markdown v1.4.2 // indirect
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/allegro/bigcache v1.2.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/antchfx/htmlquery v1.2.3 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ github.com/Microsoft/hcsshim/test v0.0.0-20210227013316-43a75bb4edd3/go.mod h1:m
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8=
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
github.com/PuerkitoBio/goquery v1.4.1/go.mod h1:T9ezsOHcCrDCgA8aF1Cqr3sSYbO/xgdy8/R/XiIMAhA=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
Expand Down Expand Up @@ -1504,8 +1509,6 @@ github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:v
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
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 v0.0.0-20151202141238-7f8ab55aaf3b/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down
37 changes: 34 additions & 3 deletions pkg/service/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,14 @@
// TODO
str = str[2:]
str = str[:len(str)-1]
str = strings.ReplaceAll(str, " ", "")

// We can not strip space inside the ""
splits := strings.Split(str, "\"")
stripSplits := make([]string, len(splits))
for idx := range splits {
stripSplits[idx] = strings.ReplaceAll(splits[idx], " ", "")
}
str = strings.Join(stripSplits, "\"")

Check warning on line 212 in pkg/service/openapi.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/openapi.go#L205-L212

Added lines #L205 - L212 were not covered by tests

var b interface{}
unmarshalErr := json.Unmarshal([]byte(str), &b)
Expand Down Expand Up @@ -281,7 +288,8 @@

} else if comp.DefinitionName == "operator-definitions/start" {

walk = structpb.NewStructValue(openAPIInput)
// Clone the struct to avoid modify on the same address
walk = proto.Clone(structpb.NewStructValue(openAPIInput)).(*structpb.Value)

Check warning on line 292 in pkg/service/openapi.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/openapi.go#L291-L292

Added lines #L291 - L292 were not covered by tests

} else if utils.IsOperatorDefinition(comp.DefinitionName) {

Expand Down Expand Up @@ -336,7 +344,30 @@
} else {
break
}
walk = walk.GetStructValue().Fields["properties"].GetStructValue().Fields[target].GetStructValue().Fields["items"]

// Handle the case when using foo["bar"]
if strings.Contains(curr, "[\"") && strings.Contains(curr, "\"]") {
if _, ok := walk.GetStructValue().Fields["properties"]; ok {
if _, ok := walk.GetStructValue().Fields["properties"].GetStructValue().Fields[target]; !ok {
break

Check warning on line 352 in pkg/service/openapi.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/openapi.go#L349-L352

Added lines #L349 - L352 were not covered by tests
}
} else {
break

Check warning on line 355 in pkg/service/openapi.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/openapi.go#L354-L355

Added lines #L354 - L355 were not covered by tests
}
walk = walk.GetStructValue().Fields["properties"].GetStructValue().Fields[target]
target = curr[len(target)+2 : len(curr)-2]
if _, ok := walk.GetStructValue().Fields["properties"]; ok {
if _, ok := walk.GetStructValue().Fields["properties"].GetStructValue().Fields[target]; !ok {
break

Check warning on line 361 in pkg/service/openapi.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/openapi.go#L357-L361

Added lines #L357 - L361 were not covered by tests
}
} else {
break

Check warning on line 364 in pkg/service/openapi.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/openapi.go#L363-L364

Added lines #L363 - L364 were not covered by tests
}
walk = walk.GetStructValue().Fields["properties"].GetStructValue().Fields[target]
} else {
walk = walk.GetStructValue().Fields["properties"].GetStructValue().Fields[target].GetStructValue().Fields["items"]
}

Check warning on line 369 in pkg/service/openapi.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/openapi.go#L366-L369

Added lines #L366 - L369 were not covered by tests

} else {
target := curr

Expand Down
27 changes: 25 additions & 2 deletions pkg/utils/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"go/parser"
"go/token"

"github.com/PaesslerAG/jsonpath"
"github.com/instill-ai/pipeline-backend/pkg/datamodel"
"github.com/oliveagle/jsonpath"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -171,7 +171,7 @@ func (d *dag) TopologicalSort() ([][]*datamodel.Component, error) {

func traverseBinding(bindings any, path string) (any, error) {

res, err := jsonpath.JsonPathLookup(bindings, "$."+path)
res, err := jsonpath.Get("$."+path, bindings)
if err != nil {
// check primitive value
var ret any
Expand Down Expand Up @@ -499,6 +499,29 @@ func EvalCondition(expr ast.Expr, value map[string]any) (any, error) {

return value[e.Name], nil

case *ast.IndexExpr:
v, err := EvalCondition(e.X, value)
if err != nil {
return nil, err
}
switch idxVal := e.Index.(type) {
case *ast.BasicLit:
// handle arr[index]
if idxVal.Kind == token.INT {
index, err := strconv.Atoi(idxVal.Value)
if err != nil {
return nil, err
}
return v.([]any)[index], nil
}
// handle obj[key]
if idxVal.Kind == token.STRING {
// key: remove ""
key := idxVal.Value[1 : len(idxVal.Value)-1]
return v.(map[string]any)[key], nil
}
}

}
return false, fmt.Errorf("condition error")
}
Expand Down
Loading