Documentation
¶
Index ¶
Examples ¶
Constants ¶
const ( OperationAdd = "add" OperationReplace = "replace" OperationRemove = "remove" OperationMove = "move" OperationCopy = "copy" OperationTest = "test" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Operation ¶
type Operation struct {
Type string `json:"op"`
From pointer `json:"from,omitempty"`
Path pointer `json:"path"`
OldValue interface{} `json:"-"`
Value interface{} `json:"value,omitempty"`
}
Operation represents a RFC6902 JSON Patch operation.
func (Operation) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
type Option ¶
type Option func(*differ)
An Option overrides the default diff behavior of the CompareOpts and CompareJSONOpts function.
func Factorize ¶
func Factorize() Option
Factorize enables factorization of operations.
Example ¶
package main
import (
"fmt"
"log"
"github.com/wI2L/jsondiff"
)
func main() {
source := `{"a":[1,2,3],"b":{"foo":"bar"}}`
target := `{"a":[1,2,3],"c":[1,2,3],"d":{"foo":"bar"}}`
patch, err := jsondiff.CompareJSONOpts(
[]byte(source),
[]byte(target),
jsondiff.Factorize(),
)
if err != nil {
log.Fatal(err)
}
for _, op := range patch {
fmt.Printf("%s\n", op)
}
}
Output: {"op":"copy","from":"/a","path":"/c"} {"op":"move","from":"/b","path":"/d"}
func Invertible ¶
func Invertible() Option
Invertible enables the generation of an invertible patch, by preceding each remove and replace operation by a test operation that verifies the value at the path that is being removed/replaced. Note that copy operations are not invertible, and as such, using this option disable the usage of copy operation in favor of add operations.
Example ¶
package main
import (
"fmt"
"log"
"github.com/wI2L/jsondiff"
)
func main() {
source := `{"a":"1","b":"2"}`
target := `{"a":"3","c":"4"}`
patch, err := jsondiff.CompareJSONOpts(
[]byte(source),
[]byte(target),
jsondiff.Invertible(),
)
if err != nil {
log.Fatal(err)
}
for _, op := range patch {
fmt.Printf("%s\n", op)
}
}
Output: {"op":"test","path":"/a","value":"1"} {"op":"replace","path":"/a","value":"3"} {"op":"test","path":"/b","value":"2"} {"op":"remove","path":"/b"} {"op":"add","path":"/c","value":"4"}
type Patch ¶
type Patch []Operation
Patch represents a series of JSON Patch operations.
func Compare ¶
Compare compares the JSON representations of the given values and returns the differences relative to the former as a list of JSON Patch operations.
Example ¶
package main
import (
"fmt"
"log"
"github.com/wI2L/jsondiff"
corev1 "k8s.io/api/core/v1"
)
func main() {
pod := corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "webserver",
Image: "nginx:latest",
VolumeMounts: []corev1.VolumeMount{{
Name: "shared-data",
MountPath: "/usr/share/nginx/html",
}},
}},
Volumes: []corev1.Volume{{
Name: "shared-data",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: corev1.StorageMediumMemory,
},
},
}},
},
}
newPod := pod.DeepCopy()
newPod.Spec.Containers[0].Image = "nginx:1.19.5-alpine"
newPod.Spec.Volumes[0].EmptyDir.Medium = corev1.StorageMediumDefault
patch, err := jsondiff.Compare(pod, newPod)
if err != nil {
log.Fatal(err)
}
for _, op := range patch {
fmt.Printf("%s\n", op)
}
}
Output: {"op":"replace","path":"/spec/containers/0/image","value":"nginx:1.19.5-alpine"} {"op":"remove","path":"/spec/volumes/0/emptyDir/medium"}
func CompareJSON ¶
CompareJSON compares the given JSON documents and returns the differences relative to the former as a list of JSON Patch operations.
Example ¶
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"github.com/wI2L/jsondiff"
)
func main() {
type Phone struct {
Type string `json:"type"`
Number string `json:"number"`
}
type Person struct {
Firstname string `json:"firstName"`
Lastname string `json:"lastName"`
Gender string `json:"gender"`
Age int `json:"age"`
Phones []Phone `json:"phoneNumbers"`
}
source, err := ioutil.ReadFile("testdata/examples/john.json")
if err != nil {
log.Fatal(err)
}
var john Person
if err := json.Unmarshal(source, &john); err != nil {
log.Fatal(err)
}
john.Age = 30
john.Phones = append(john.Phones, Phone{
Type: "mobile",
Number: "209-212-0015",
})
target, err := json.Marshal(john)
if err != nil {
log.Fatal(err)
}
patch, err := jsondiff.CompareJSON(source, target)
if err != nil {
log.Fatal(err)
}
for _, op := range patch {
fmt.Printf("%s\n", op)
}
}
Output: {"op":"replace","path":"/age","value":30} {"op":"add","path":"/phoneNumbers/-","value":{"number":"209-212-0015","type":"mobile"}}
func CompareJSONOpts ¶
CompareJSONOpts is similar to CompareJSON, but also accepts a list of options to configure the diff behavior.
func CompareOpts ¶
CompareOpts is similar to Compare, but also accepts a list of options to configure the diff behavior.