Skip to content
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
10 changes: 3 additions & 7 deletions deckformat/entitypointers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -161,16 +161,12 @@ func GetEntities(deckfile *yaml.Node, entityType string) []*yaml.Node {
}

for _, entitySelector := range entitySelectors {
query, err := yamlpath.NewPath(entitySelector)
query, err := jsonpath.NewPath(entitySelector)
if err != nil {
panic("failed compiling " + entityType + " selector")
}

entities, err := query.Find(deckfile)
if err != nil {
panic("failed to collect " + entityType + " from the input data")
}

entities := query.Query(deckfile)
allEntities = append(allEntities, entities...)
}

Expand Down
6 changes: 0 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ Kong will be expanding the library of available tools leading up to a GA release

The below examples assume you have installed and are using the `deck` CLI tool.

## Notes
| :exclamation: Important compatibility notes :exclamation: |
|:---------------------------|
| The [jsonpath library](https://github.com/vmware-labs/yaml-jsonpath) in use has [a bug](https://github.com/vmware-labs/yaml-jsonpath/issues/54) related to the "recursive descent" operator (`..`). Filter expressions following a recursive descent will not succeed unless prefixed with a generic wilcard (`[*]`).<br/><br/>Non-recursive-descent works as expected:<br/>> `$.plugins[?(@.regex_priority>100)]`</br>where recursive-descent will fail:<br/>> `$..plugins[?(@.regex_priority>100)]`</br>The workaround is to add an extra wildcard (`[*]`) before the filter expression like this:<br/>> `$..plugins[*][?(@.regex_priority>100)]`</br><br/>To test and debug selectors, you can use the library’s built in web UI by following the [Web Application Instructions](https://github.com/vmware-labs/yaml-jsonpath/tree/main/web).|


## Commands

---
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ require (
github.com/kong/go-slugify v1.0.0
github.com/onsi/ginkgo/v2 v2.22.2
github.com/onsi/gomega v1.36.2
github.com/pb33f/libopenapi v0.16.13
github.com/pb33f/libopenapi v0.24.0
Copy link
Contributor

Choose a reason for hiding this comment

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

@mheap this is not necessary for speakeasy jsonpath, correct?
This breaks vacuum on deck, i'll downgrade it to 0.23.0 (which is required by vacuum), and bump vacuum version in deck together.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Update - will revert it to v0.16.13
Vacuum upgrade is very brittle - has unexpected test failures. Not going to club this with it.

github.com/speakeasy-api/jsonpath v0.6.2
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
github.com/vmware-labs/yaml-jsonpath v0.3.2
github.com/yuin/gopher-lua v1.1.1
golang.org/x/term v0.29.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -26,15 +26,14 @@ require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mozillazg/go-unidecode v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.9-0.20240815153524-6ea36470d1bd // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
123 changes: 8 additions & 115 deletions go.sum

Large diffs are not rendered by default.

26 changes: 11 additions & 15 deletions patch/deckpatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/kong/go-apiops/jsonbasics"
"github.com/kong/go-apiops/logbasics"
"github.com/kong/go-apiops/yamlbasics"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
"gopkg.in/yaml.v3"
)

Expand All @@ -16,7 +16,7 @@ var DefaultSelector = []string{"$"}
type DeckPatch struct {
// Format string // Name of the format specified
SelectorSources []string // Source query for the JSONpath object
Selectors []*yamlpath.Path // JSONpath object
Selectors []*jsonpath.JSONPath // JSONpath object
ObjValues map[string]interface{} // Values to set on target objects
ArrValues []interface{} // Values to set on target arrays
Remove []string // List of keys to remove from the target object
Expand All @@ -41,11 +41,11 @@ func (patch *DeckPatch) Parse(obj map[string]interface{}, breadCrumb string) (er
}

// compile JSONpath expressions
patch.Selectors = make([]*yamlpath.Path, len(patch.SelectorSources))
patch.Selectors = make([]*jsonpath.JSONPath, len(patch.SelectorSources))
for i, selector := range patch.SelectorSources {
patch.Selectors[i], err = yamlpath.NewPath(selector)
patch.Selectors[i], err = jsonpath.NewPath(selector)
if err != nil {
return fmt.Errorf("%s.selectors[%d] is not a valid JSONpath expression; %w", breadCrumb, i, err)
return fmt.Errorf("%s.selectors[%d] is not a valid JSONpath expression; %s", breadCrumb, i, err.Error())
}
}

Expand Down Expand Up @@ -159,9 +159,9 @@ func (patch *DeckPatch) ApplyToNodes(yamlData *yaml.Node) (err error) {

//nolint:gosimple
if patch.Selectors == nil || len(patch.Selectors) == 0 {
patch.Selectors = make([]*yamlpath.Path, len(patch.SelectorSources))
patch.Selectors = make([]*jsonpath.JSONPath, len(patch.SelectorSources))
for i, selector := range patch.SelectorSources {
patch.Selectors[i], err = yamlpath.NewPath(selector)
patch.Selectors[i], err = jsonpath.NewPath(selector)
if err != nil {
return fmt.Errorf("selector '%s' is not a valid JSONpath expression; %w", selector, err)
}
Expand All @@ -170,15 +170,11 @@ func (patch *DeckPatch) ApplyToNodes(yamlData *yaml.Node) (err error) {

// query the yamlData using the selector
nodes := make([]*yaml.Node, 0)
for _, selector := range patch.Selectors {
moreNodes, err := selector.Find(yamlData)
if err != nil {
return err
}
nodes = append(nodes, moreNodes...)
}

// 'nodes' is an array of nodes matching the selectors
for _, selector := range patch.Selectors {
results := selector.Query(yamlData)
nodes = append(nodes, results...)
} // 'nodes' is an array of nodes matching the selectors
for _, node := range nodes {
if len(patch.ArrValues) > 0 {
// since we're updating array fields, we'll skip anything that is
Expand Down
74 changes: 72 additions & 2 deletions patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var _ = Describe("Patch", func() {
err := patch.Parse(data, "file1.yml:patches[1]")

Expect(err).To(MatchError("file1.yml:patches[1].selectors[0] is not a valid JSONpath " +
"expression; invalid character ' ' at position 3, following \"not\""))
"expression; Error at line 1, column 0: expected '$'\nnot valid\n^..\n"))
})

It("fails on non object/array 'values'", func() {
Expand Down Expand Up @@ -261,7 +261,7 @@ var _ = Describe("Patch", func() {
data := []byte(`{}`)
err := testPatch.ApplyToNodes(jsonbasics.ConvertToYamlNode(MustDeserialize(data)))
Expect(err).To(MatchError("selector 'bad JSONpath' is not a valid JSONpath expression; " +
"invalid character ' ' at position 3, following \"bad\""))
"Error at line 1, column 0: expected '$'\nbad JSONpath\n^..\n"))
})
})

Expand Down Expand Up @@ -332,6 +332,76 @@ var _ = Describe("Patch", func() {
}`))
})

It("works with plain recursive descent", func() {
data := []byte(`{
"services": [
{
"name": "one",
"routes": [
{
"name": "demo-route_get",
"methods": ["GET"]
}
]
}
]
}`)
selector := "$..routes[*]"
valueFlags := []string{
"added:\"field\"",
}

Expect(applyUpdates(data, selector, valueFlags)).To(MatchJSON(`{
"services": [
{
"name": "one",
"routes": [
{
"added": "field",
"name": "demo-route_get",
"methods": ["GET"]
}
]
}
]
}`))
})

It("works with recursive descent using filters", func() {
data := []byte(`{
"services": [
{
"name": "one",
"routes": [
{
"name": "demo-route_get",
"methods": ["GET"]
}
]
}
]
}`)
selector := "$..routes[?(@.name==\"demo-route_get\")]"
valueFlags := []string{
"added:\"field\"",
}

Expect(applyUpdates(data, selector, valueFlags)).To(MatchJSON(`{
"services": [
{
"name": "one",
"routes": [
{
"added": "field",
"name": "demo-route_get",
"methods": ["GET"]
}
]
}
]
}`))
})

It("skips non-objects", func() {
data := []byte(`{
"plugins": [
Expand Down
22 changes: 10 additions & 12 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/kong/go-apiops/jsonbasics"
"github.com/kong/go-apiops/logbasics"
"github.com/kong/go-apiops/yamlbasics"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -58,7 +58,7 @@ type Plugger struct {
// list of JSONpointers to entities that can hold plugins, so the selector
// returns entities that can hold plugins, not the plugin arrays themselves.
// The default value is the main plugins array (at the file top-level).
selectors []*yamlpath.Path
selectors []*jsonpath.JSONPath
// list of Nodes (selected by the selectors) representing entities that can
// hold plugins, not the plugin arrays themselves
pluginOwners []*yaml.Node
Expand Down Expand Up @@ -106,12 +106,12 @@ func (ts *Plugger) SetSelectors(selectors []string) error {
selectors = defaultSelectors
}

compiledSelectors := make([]*yamlpath.Path, len(selectors))
compiledSelectors := make([]*jsonpath.JSONPath, len(selectors))
for i, selector := range selectors {
logbasics.Debug("compiling JSONpath", "path", selector)
compiledpath, err := yamlpath.NewPath(selector)
compiledpath, err := jsonpath.NewPath(selector)
if err != nil {
return fmt.Errorf("selector '%s' is not a valid JSONpath expression; %w", selector, err)
return fmt.Errorf("selector '%s' is not a valid JSONpath expression; %s", selector, err.Error())
}
compiledSelectors[i] = compiledpath
}
Expand Down Expand Up @@ -146,15 +146,13 @@ func (ts *Plugger) search() error {
// build list of targets by executing the selectors one by one
targets := make([]*yaml.Node, 0)
refs := make(map[*yaml.Node]bool, 0) // keeps references to prevent duplicates

for idx, selector := range ts.selectors {
nodes, err := selector.Find(ts.data)
if err != nil {
return err
}
results := selector.Query(ts.data)

// 'nodes' is an array of nodes matching the selector
// 'results' contains YAML nodes matching the selector
objCount := 0
for _, node := range nodes {
for _, node := range results {
// since we're updating object fields, we'll skip anything that is
// not a JSONobject
if node.Kind == yaml.MappingNode && !refs[node] {
Expand All @@ -163,7 +161,7 @@ func (ts *Plugger) search() error {
objCount++
}
}
logbasics.Debug("selector results", "selector", idx, "results", len(nodes), "objects", objCount)
logbasics.Debug("selector results", "selector", idx, "results", len(results), "objects", objCount)
}
ts.pluginOwners = targets

Expand Down
Loading
Loading