bumping google.golang.org/genproto/googleapis/rpc 1744710...2d3300f:
> 2d3300f chore(all): update actions/checkout action to v4 (# 1052)
> b8732ec chore(all): auto-regenerate .pb.go files (# 1051)
> ccc8af3 chore(all): auto-regenerate .pb.go files (# 1050)
> 6bfd019 chore(all): auto-regenerate .pb.go files (# 1047)
> ca7cfce fix(analytics/admin/v1alpha): remove broken aliases (# 1045)
bumping google.golang.org/grpc 87bf02a...62726d4:
> 62726d4 update version to 1.58.1 (# 6629)
> fa6d9ab cherry-pick 6610 and 6620 (# 6627)
> 467fbf2 Change version to 1.58.1-dev (# 6580)
> c2b0797 Change version to 1.58.0 (# 6579)
> 0467e47 balancer/leastrequest: Cache atomic load and also add concurrent rpc test (# 6607)
> 5d1c0ae leastrequest: fix data race in leastrequest picker (# 6606)
> e26457d stream: swallow Header errors as we used to; RecvMsg can still return it (# 6591)
> 4c9777c clusterresolver: fix deadlock when dns resolver responds inline with update or error at build time (# 6563)
> 81b9df2 idle: move idleness manager to separate package and ~13s of tests into it (# 6566)
> 7d35b8e test: speed up TestServiceConfigTimeoutTD from 1.8s to 0.03s (# 6571)
> d51b3f4 interop/grpc_testing: update protos from grpc-proto repo (# 6567)
> fe1519e client: fix ClientStream.Header() behavior (# 6557)
> 8a2c220 cdsbalancer: test cleanup part 2/N (# 6554)
> 7f66074 vet.sh: fix interface{} check for macos (# 6561)
> b07bf5d cdsbalancer: test cleanup part 1/N (# 6546)
> 33f9fa2 test: speed up two tests (# 6558)
> aca07ce xds/internal/xdsclient: Add least request support in xDS (# 6517)
> e5d8eac test: improve and speed up channelz keepalive test (# 6556)
> ebf0b4e idle: speed up test by 5x even while running 2x more iterations (# 6555)
> 7d3996f grpctest: use an interface instead of reflection (# 6553)
> cc705fe interop: regenerate pb.gos (# 6551)
> 3e92504 status: optimize GRPCStatus() calls (# 6539)
> 402ba09 pick_first: de-experiment pick first (# 6549)
> 2821d7f resolver: remove outdated Target examples (# 6547)
> 53d1f23 benchmark: update proper benchmark binary to use larger buffers (# 6537)
> fbff2ab *: update `interface{}` to `any` and `go.mod` version to `go 1.19` (# 6544)
> e40da66 clientconn: release lock when returning from enterIdleMode() (# 6538)
> dbbc983 balancer/leastrequest: Add least request balancer (# 6510)
> a010079 *: remove references to old versions of go (# 6545)
> 03d32b9 orca: update example and interop to use StateListener (# 6529)
> c2bc22c testing: update Go versions tested to 1.19-1.21 (# 6543)
> 879faf6 test: update client state subscriber test to be not flaky and more stressful about rapid updates (# 6512)
> f3e94ec xds: improve error message when matched route on client is not of type RouteActionRoute (# 6248)
> bb41067 balancergroup: do not cache closed sub-balancers by default (# 6523)
> 68704f8 gracefulswitch, stub: remove last UpdateSubConnState references (# 6533)
> 4900699 balancer/rls, xds/wrrlocality: stop forwarding UpdateSubConnState calls (# 6532)
> ebc3c51 internal/balancergroup: remove usage of UpdateSubConnState (# 6528)
> 5da2731 balancer/weightedtarget: stop forwarding UpdateSubConnState calls (# 6525)
> 182b0ad interop/grpc_testing: regenerate protos (# 6534)
> e274152 rls: fix flaky test introduced by # 6514 (# 6535)
> 61a1f77 balancer/weightedroundrobin: migrate to StateListener (# 6530)
> 175c84c xds/ringhash: use StateListener instead of UpdateSubConnState (# 6522)
> 3fa17cc test: speed up test that was taking 10 seconds to timeout (# 6531)
> 694cb64 xds/clusterresolver: stop forwarding UpdateSubConnState calls (# 6526)
> 8f51ca8 tests: stop using UpdateSubConnState (# 6527)
> cea77bb xds/clustermanager: stop forwarding UpdateSubConnState calls (# 6519)
> ce68413 xds/priority: stop forwarding UpdateSubConnState calls (# 6521)
> dceb6ee xds/clusterimpl: stop forwarding UpdateSubConnState calls (# 6518)
> 8def12a xds/outlierdetection: Stop handling UpdateSubConnState forwarding (# 6520)
> 67a8e73 multiple/test: use stub balancer instead of defining wrapped balancers (# 6514)
> 92b481a test: allow set request/response size in interop soak test (# 6513)
> 07609e1 benchmark: restore old buffer size values for published benchmarks (# 6516)
> 2059c6e grpc: report connectivity state changes on the ClientConn for Subscribers (# 6437)
> 4832deb test: clean up deadlines set in tests (# 6506)
> 9c46304 xds/cdsbalancer: stop handling subconn state updates (# 6509)
> e9a4e94 base: update base balancer for new APIs (# 6503)
> 6c0c69e all: replace RemoveSubConn with Shutdown as much as possible (# 6505)
> 28ac6ef xdsclient: make watch timer a no-op if authority is closed (# 6502)
> d06ab0d pickfirst: receive state updates via callback instead of UpdateSubConnState (# 6495)
> 7aceafc balancer: add SubConn.Shutdown; deprecate Balancer.RemoveSubConn (# 6493)
> 4fe8d3d balancer: fix tests not properly updating subconn states (# 6501)
> 8ebe462 outlierdetection: fix unconditional calls of child UpdateSubConnState (# 6500)
> 5d3d9d7 grpc: perform a blocking close of the balancer in ccb (# 6497)
> ecc5645 clusterresolver: fix a flaky test (# 6499)
> b9356e3 client: fix race between connection error and subconn shutdown (# 6494)
> 2db7b17 test/xds: increase default test timeout (# 6498)
> 8f496b2 test/kokoro: Add bootstrap generator test into Go Kokoro script (# 6463)
> 0246373 testutils: remove TestSubConns for future extensibility (# 6492)
> c635404 balancer: add StateListener to NewSubConnOptions for SubConn state updates (# 6481)
> 94df716 resolver: State: add Endpoints and deprecate Addresses (# 6471)
> 20c51a9 pickfirst: add tests for resolver error scenarios (# 6484)
> b8d36ca pickfirst: add prefix logging (# 6482)
> 5ce5686 pickfirst: guard config parsing on GRPC_EXPERIMENTAL_PICKFIRST_LB_CONFIG (# 6470)
> 41d1232 resolver/weighted_round_robin: remove experimental suffix from name (# 6477)
> 2aa2615 clusterresolver: comply with A37 for handling errors from discovery mechanisms (# 6461)
> d7f45cd xds/server: create the xDS client when the xDS enabled gRPC server is created (# 6446)
> f1fc2ca clientconn: add channel ID to some idleness logs (# 6459)
> 9bb44fb transport: use a sync.Pool to share per-connection write buffer (# 6309)
> d524b40 multiple: update dependencies after 1.57 branch cut (# 6452)
> 7aab9c0 stats: Add RPC event for blocking for a picker update (# 6422)
> 02946a3 resolver: remove deprecated AddressType (# 6451)
> 919fe35 Change version to 1.58.0-dev (# 6450)
Signed-off-by: Knative Automation <automation@knative.team>
|
||
|---|---|---|
| .. | ||
| v5 | ||
| .gitignore | ||
| LICENSE | ||
| README.md | ||
| errors.go | ||
| merge.go | ||
| patch.go | ||
README.md
JSON-Patch
jsonpatch is a library which provides functionality for both applying
RFC6902 JSON patches against documents, as
well as for calculating & applying RFC7396 JSON merge patches.
Get It!
Latest and greatest:
go get -u github.com/evanphx/json-patch/v5
Stable Versions:
- Version 5:
go get -u gopkg.in/evanphx/json-patch.v5 - Version 4:
go get -u gopkg.in/evanphx/json-patch.v4
(previous versions below v3 are unavailable)
Use It!
- Create and apply a merge patch
- Create and apply a JSON Patch
- Comparing JSON documents
- Combine merge patches
Configuration
-
There is a global configuration variable
jsonpatch.SupportNegativeIndices. This defaults totrueand enables the non-standard practice of allowing negative indices to mean indices starting at the end of an array. This functionality can be disabled by settingjsonpatch.SupportNegativeIndices = false. -
There is a global configuration variable
jsonpatch.AccumulatedCopySizeLimit, which limits the total size increase in bytes caused by "copy" operations in a patch. It defaults to 0, which means there is no limit.
These global variables control the behavior of jsonpatch.Apply.
An alternative to jsonpatch.Apply is jsonpatch.ApplyWithOptions whose behavior
is controlled by an options parameter of type *jsonpatch.ApplyOptions.
Structure jsonpatch.ApplyOptions includes the configuration options above
and adds two new options: AllowMissingPathOnRemove and EnsurePathExistsOnAdd.
When AllowMissingPathOnRemove is set to true, jsonpatch.ApplyWithOptions will ignore
remove operations whose path points to a non-existent location in the JSON document.
AllowMissingPathOnRemove defaults to false which will lead to jsonpatch.ApplyWithOptions
returning an error when hitting a missing path on remove.
When EnsurePathExistsOnAdd is set to true, jsonpatch.ApplyWithOptions will make sure
that add operations produce all the path elements that are missing from the target object.
Use jsonpatch.NewApplyOptions to create an instance of jsonpatch.ApplyOptions
whose values are populated from the global configuration variables.
Create and apply a merge patch
Given both an original JSON document and a modified JSON document, you can create a Merge Patch document.
It can describe the changes needed to convert from the original to the modified JSON document.
Once you have a merge patch, you can apply it to other JSON documents using the
jsonpatch.MergePatch(document, patch) function.
package main
import (
"fmt"
jsonpatch "github.com/evanphx/json-patch"
)
func main() {
// Let's create a merge patch from these two documents...
original := []byte(`{"name": "John", "age": 24, "height": 3.21}`)
target := []byte(`{"name": "Jane", "age": 24}`)
patch, err := jsonpatch.CreateMergePatch(original, target)
if err != nil {
panic(err)
}
// Now lets apply the patch against a different JSON document...
alternative := []byte(`{"name": "Tina", "age": 28, "height": 3.75}`)
modifiedAlternative, err := jsonpatch.MergePatch(alternative, patch)
fmt.Printf("patch document: %s\n", patch)
fmt.Printf("updated alternative doc: %s\n", modifiedAlternative)
}
When ran, you get the following output:
$ go run main.go
patch document: {"height":null,"name":"Jane"}
updated alternative doc: {"age":28,"name":"Jane"}
Create and apply a JSON Patch
You can create patch objects using DecodePatch([]byte), which can then
be applied against JSON documents.
The following is an example of creating a patch from two operations, and applying it against a JSON document.
package main
import (
"fmt"
jsonpatch "github.com/evanphx/json-patch"
)
func main() {
original := []byte(`{"name": "John", "age": 24, "height": 3.21}`)
patchJSON := []byte(`[
{"op": "replace", "path": "/name", "value": "Jane"},
{"op": "remove", "path": "/height"}
]`)
patch, err := jsonpatch.DecodePatch(patchJSON)
if err != nil {
panic(err)
}
modified, err := patch.Apply(original)
if err != nil {
panic(err)
}
fmt.Printf("Original document: %s\n", original)
fmt.Printf("Modified document: %s\n", modified)
}
When ran, you get the following output:
$ go run main.go
Original document: {"name": "John", "age": 24, "height": 3.21}
Modified document: {"age":24,"name":"Jane"}
Comparing JSON documents
Due to potential whitespace and ordering differences, one cannot simply compare JSON strings or byte-arrays directly.
As such, you can instead use jsonpatch.Equal(document1, document2) to
determine if two JSON documents are structurally equal. This ignores
whitespace differences, and key-value ordering.
package main
import (
"fmt"
jsonpatch "github.com/evanphx/json-patch"
)
func main() {
original := []byte(`{"name": "John", "age": 24, "height": 3.21}`)
similar := []byte(`
{
"age": 24,
"height": 3.21,
"name": "John"
}
`)
different := []byte(`{"name": "Jane", "age": 20, "height": 3.37}`)
if jsonpatch.Equal(original, similar) {
fmt.Println(`"original" is structurally equal to "similar"`)
}
if !jsonpatch.Equal(original, different) {
fmt.Println(`"original" is _not_ structurally equal to "different"`)
}
}
When ran, you get the following output:
$ go run main.go
"original" is structurally equal to "similar"
"original" is _not_ structurally equal to "different"
Combine merge patches
Given two JSON merge patch documents, it is possible to combine them into a single merge patch which can describe both set of changes.
The resulting merge patch can be used such that applying it results in a document structurally similar as merging each merge patch to the document in succession.
package main
import (
"fmt"
jsonpatch "github.com/evanphx/json-patch"
)
func main() {
original := []byte(`{"name": "John", "age": 24, "height": 3.21}`)
nameAndHeight := []byte(`{"height":null,"name":"Jane"}`)
ageAndEyes := []byte(`{"age":4.23,"eyes":"blue"}`)
// Let's combine these merge patch documents...
combinedPatch, err := jsonpatch.MergeMergePatches(nameAndHeight, ageAndEyes)
if err != nil {
panic(err)
}
// Apply each patch individual against the original document
withoutCombinedPatch, err := jsonpatch.MergePatch(original, nameAndHeight)
if err != nil {
panic(err)
}
withoutCombinedPatch, err = jsonpatch.MergePatch(withoutCombinedPatch, ageAndEyes)
if err != nil {
panic(err)
}
// Apply the combined patch against the original document
withCombinedPatch, err := jsonpatch.MergePatch(original, combinedPatch)
if err != nil {
panic(err)
}
// Do both result in the same thing? They should!
if jsonpatch.Equal(withCombinedPatch, withoutCombinedPatch) {
fmt.Println("Both JSON documents are structurally the same!")
}
fmt.Printf("combined merge patch: %s", combinedPatch)
}
When ran, you get the following output:
$ go run main.go
Both JSON documents are structurally the same!
combined merge patch: {"age":4.23,"eyes":"blue","height":null,"name":"Jane"}
CLI for comparing JSON documents
You can install the commandline program json-patch.
This program can take multiple JSON patch documents as arguments,
and fed a JSON document from stdin. It will apply the patch(es) against
the document and output the modified doc.
patch.1.json
[
{"op": "replace", "path": "/name", "value": "Jane"},
{"op": "remove", "path": "/height"}
]
patch.2.json
[
{"op": "add", "path": "/address", "value": "123 Main St"},
{"op": "replace", "path": "/age", "value": "21"}
]
document.json
{
"name": "John",
"age": 24,
"height": 3.21
}
You can then run:
$ go install github.com/evanphx/json-patch/cmd/json-patch
$ cat document.json | json-patch -p patch.1.json -p patch.2.json
{"address":"123 Main St","age":"21","name":"Jane"}
Help It!
Contributions are welcomed! Leave an issue or create a PR.
Before creating a pull request, we'd ask that you make sure tests are passing and that you have added new tests when applicable.
Contributors can run tests using:
go test -cover ./...
Builds for pull requests are tested automatically using TravisCI.