-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: Move patch.go into its own package
Also, rename some bits so that it more closely aligns with github.com/evanphx/json-patch, so it's simpler for us to use in the control plane.
- Loading branch information
Showing
4 changed files
with
64 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Construction of JSON patches. See https://jsonpatch.com/ | ||
|
||
package util | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// OpKind is the kind of operation being performed in a single step | ||
type OpKind string | ||
|
||
const ( | ||
OpAdd OpKind = "add" | ||
OpRemove OpKind = "remove" | ||
OpReplace OpKind = "replace" | ||
OpMove OpKind = "move" | ||
OpCopy OpKind = "copy" | ||
OpTest OpKind = "test" | ||
) | ||
|
||
type JSONPatch = []Operation | ||
|
||
// Operation is a single step in the overall JSON patch | ||
type Operation struct { | ||
// Op is the kind of operation being performed in this step. See [OpKind] for more. | ||
Op OpKind `json:"op"` | ||
// Path is a [JSON pointer] to the target location of the operation. | ||
// | ||
// In general, nesting is separated by '/'s, with special characters escaped by '~'. | ||
// [PathEscape] is provided to handle escaping, because it can get a little gnarly. | ||
// | ||
// As an example, if you want to add a field "foo" to the first element of an array, | ||
// you'd use the path `/0/foo`. The jsonpatch website has more details (and clearer examples), | ||
// refer there for more information: https://jsonpatch.com/#json-pointer | ||
// | ||
// [JSON pointer]: https://datatracker.ietf.org/doc/html/rfc6901/ | ||
Path string `json:"path"` | ||
// From gives the source location for "copy" or "move" operations. | ||
From string `json:"from,omitempty"` | ||
// Value is the new value to use, for "add", "replace", or "test" operations. | ||
Value any `json:"value,omitempty"` | ||
} | ||
|
||
var pathEscaper = strings.NewReplacer("~", "~0", "/", "~1") | ||
|
||
// PathEscape escapes a string for use in a segement of the Path field of an Operation | ||
// | ||
// This is useful, for example, when using arbitrary strings as map keys (like Kubernetes labels or | ||
// annotations). | ||
func PathEscape(s string) string { | ||
return pathEscaper.Replace(s) | ||
} |