Skip to content

Commit

Permalink
EnvoyPatchPolicy JsonPath docs & fixes (envoyproxy#4256)
Browse files Browse the repository at this point in the history
* fix: jsonPath escape chars & api docs (envoyproxy#4162)

Signed-off-by: Dennis Kniep <kniepdennis@gmail.com>

* fix: throw error if jsonPath returns no jsonPointers (envoyproxy#4162)

Signed-off-by: Dennis Kniep <kniepdennis@gmail.com>

* docs: JSONPath usage in EnvoyPatchPolicy (envoyproxy#4043)

Signed-off-by: Dennis Kniep <kniepdennis@gmail.com>

---------

Signed-off-by: Dennis Kniep <kniepdennis@gmail.com>
  • Loading branch information
denniskniep authored Sep 19, 2024
1 parent 9cf7828 commit 1b56eda
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 42 deletions.
12 changes: 8 additions & 4 deletions api/v1alpha1/envoypatchpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ type JSONPatchOperationType string
type JSONPatchOperation struct {
// Op is the type of operation to perform
Op JSONPatchOperationType `json:"op"`
// Path is the location of the target document/field where the operation will be performed
// Refer to https://datatracker.ietf.org/doc/html/rfc6901 for more details.
// Path is a JSONPointer expression. Refer to https://datatracker.ietf.org/doc/html/rfc6901 for more details.
// It specifies the location of the target document/field where the operation will be performed
// +optional
Path *string `json:"path,omitempty"`
// JSONPath specifies the locations of the target document/field where the operation will be performed
// Refer to https://datatracker.ietf.org/doc/rfc9535/ for more details.
// JSONPath is a JSONPath expression. Refer to https://datatracker.ietf.org/doc/rfc9535/ for more details.
// It produces one or more JSONPointer expressions based on the given JSON document.
// If no JSONPointer is found, it will result in an error.
// If the 'Path' property is also set, it will be appended to the resulting JSONPointer expressions from the JSONPath evaluation.
// This is useful when creating a property that does not yet exist in the JSON document.
// The final JSONPointer expressions specifies the locations in the target document/field where the operation will be applied.
// +optional
JSONPath *string `json:"jsonPath,omitempty"`
// From is the source location of the value to be copied or moved. Only valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ spec:
type: string
jsonPath:
description: |-
JSONPath specifies the locations of the target document/field where the operation will be performed
Refer to https://datatracker.ietf.org/doc/rfc9535/ for more details.
JSONPath is a JSONPath expression. Refer to https://datatracker.ietf.org/doc/rfc9535/ for more details.
It produces one or more JSONPointer expressions based on the given JSON document.
If no JSONPointer is found, it will result in an error.
If the 'Path' property is also set, it will be appended to the resulting JSONPointer expressions from the JSONPath evaluation.
This is useful when creating a property that does not yet exist in the JSON document.
The final JSONPointer expressions specifies the locations in the target document/field where the operation will be applied.
type: string
op:
description: Op is the type of operation to perform
Expand All @@ -88,8 +92,8 @@ spec:
type: string
path:
description: |-
Path is the location of the target document/field where the operation will be performed
Refer to https://datatracker.ietf.org/doc/html/rfc6901 for more details.
Path is a JSONPointer expression. Refer to https://datatracker.ietf.org/doc/html/rfc6901 for more details.
It specifies the location of the target document/field where the operation will be performed
type: string
value:
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@ spec:
type: string
jsonPath:
description: |-
JSONPath specifies the locations of the target document/field where the operation will be performed
Refer to https://datatracker.ietf.org/doc/rfc9535/ for more details.
JSONPath is a JSONPath expression. Refer to https://datatracker.ietf.org/doc/rfc9535/ for more details.
It produces one or more JSONPointer expressions based on the given JSON document.
If no JSONPointer is found, it will result in an error.
If the 'Path' property is also set, it will be appended to the resulting JSONPointer expressions from the JSONPath evaluation.
This is useful when creating a property that does not yet exist in the JSON document.
The final JSONPointer expressions specifies the locations in the target document/field where the operation will be applied.
type: string
op:
description: Op is the type of operation to perform
Expand All @@ -223,8 +227,8 @@ spec:
type: string
path:
description: |-
Path is the location of the target document/field where the operation will be performed
Refer to https://datatracker.ietf.org/doc/html/rfc6901 for more details.
Path is a JSONPointer expression. Refer to https://datatracker.ietf.org/doc/html/rfc6901 for more details.
It specifies the location of the target document/field where the operation will be performed
type: string
value:
description: |-
Expand Down
18 changes: 16 additions & 2 deletions internal/utils/jsonpatch/jsonpathtopointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ func nthToPointer(f jp.Nth) ([]byte, error) {
return buf, nil
}

func toPointer(f jp.Frag) ([]byte, error) {
return f.Append(nil, false, true), nil
func toPointer(f jp.Child) ([]byte, error) {
var buf []byte

// JSONPointer escaping https://datatracker.ietf.org/doc/html/rfc6901#section-3
for _, b := range []byte(string(f)) {
switch b {
case '~':
buf = append(buf, "~0"...)
case '/':
buf = append(buf, "~1"...)
default:
buf = append(buf, b)
}
}

return buf, nil
}
Loading

0 comments on commit 1b56eda

Please sign in to comment.