Documentation ¶
Index ¶
Examples ¶
Constants ¶
const ( OperationAdd = "add" OperationReplace = "replace" OperationRemove = "remove" OperationMove = "move" OperationCopy = "copy" OperationTest = "test" )
JSON Patch operation types. These are defined in RFC 6902 section 4.
Variables ¶
This section is empty.
Functions ¶
func UpdateSeparator ¶
func UpdateSeparator(newSeparator string)
Types ¶
type Operation ¶
type Operation struct { Type string `json:"op"` From pointer `json:"from,omitempty"` Field pointer `json:"field"` 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/revzim/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","field":"c"} {"op":"move","from":"b","field":"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/revzim/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","field":"a","value":"1"} {"op":"replace","field":"a","value":"3"} {"op":"test","field":"b","value":"2"} {"op":"remove","field":"b"} {"op":"add","field":"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/revzim/jsondiff" ) type ( Pod struct { Spec PodSpec `json:"spec,omitempty"` } PodSpec struct { Containers []Container `json:"containers,omitempty"` Volumes []Volume `json:"volumes,omitempty"` } Container struct { Name string `json:"name"` Image string `json:"image,omitempty"` VolumeMounts []VolumeMount `json:"volumeMounts,omitempty"` } Volume struct { Name string `json:"name"` VolumeSource `json:",inline"` } VolumeSource struct { EmptyDir *EmptyDirVolumeSource `json:"emptyDir,omitempty"` } VolumeMount struct { Name string `json:"name"` MountPath string `json:"mountPath"` } EmptyDirVolumeSource struct { Medium StorageMedium `json:"medium,omitempty"` } StorageMedium string ) const ( StorageMediumDefault StorageMedium = "" StorageMediumMemory StorageMedium = "Memory" ) func main() { createPod := func() Pod { return Pod{ Spec: PodSpec{ Containers: []Container{{ Name: "webserver", Image: "nginx:latest", VolumeMounts: []VolumeMount{{ Name: "shared-data", MountPath: "usr/share/nginx/html", }}, }}, Volumes: []Volume{{ Name: "shared-data", VolumeSource: VolumeSource{ EmptyDir: &EmptyDirVolumeSource{ Medium: StorageMediumMemory, }, }, }}, }, } } oldPod := createPod() newPod := createPod() newPod.Spec.Containers[0].Image = "nginx:1.19.5-alpine" newPod.Spec.Volumes[0].EmptyDir.Medium = StorageMediumDefault patch, err := jsondiff.Compare(oldPod, newPod) if err != nil { log.Fatal(err) } for _, op := range patch { fmt.Printf("%s\n", op) } }
Output: {"op":"replace","field":"spec/containers/0/image","value":"nginx:1.19.5-alpine"} {"op":"remove","field":"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/revzim/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","field":"age","value":30} {"op":"add","field":"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.
func (Patch) MutableForEach ¶
MutableForEach -- Loop - manipulate by *