Skip to content

Commit

Permalink
Merge pull request #9 from breml/add-other-patchlibs
Browse files Browse the repository at this point in the history
Add other patchlibs
  • Loading branch information
breml authored Jun 1, 2024
2 parents 2a4d8a5 + b8fcf94 commit d74dded
Show file tree
Hide file tree
Showing 46 changed files with 2,769 additions and 50 deletions.
2 changes: 2 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ tasks:
generate:
dir: './cmd/generate_testdata'
cmds:
- rm -rf ../../testdata/generated
- mkdir -p ../../testdata/generated
- go generate .
106 changes: 81 additions & 25 deletions cmd/generate_testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ import (
"log"
"os"
"path/filepath"
"strings"

mianxiang "github.com/520MianXiangDuiXiang520/json-diff"
victorlowther "github.com/VictorLowther/jsonpatch2"
cameront "github.com/cameront/go-jsonpatch"
herkyl "github.com/herkyl/patchwerk"
mattbaird "github.com/mattbaird/jsonpatch"
snorwin "github.com/snorwin/jsonpatch"
wI2L "github.com/wI2L/jsondiff"

"github.com/qri-io/jsonpointer"
"github.com/wI2L/jsondiff"
"golang.org/x/tools/txtar"
)

Expand All @@ -31,6 +39,7 @@ type metadata struct {
} `json:"terraform,omitempty"`
Metadata map[string]map[string]any `json:"metadata,omitempty"`
JSONInJSON []string `json:"jsonInJSON,omitempty"`
PatchLib *string `json:"patchLib,omitempty"`
}

func main() {
Expand All @@ -50,24 +59,22 @@ func main() {
err = json.Unmarshal(txtarchive.Comment, &metadata)
die(err)

var before, after interface{}
err = json.Unmarshal(txtarchive.Files[0].Data, &before)
die(err)
beforeJSON := txtarchive.Files[0].Data
afterJSON := txtarchive.Files[1].Data

err = json.Unmarshal(txtarchive.Files[1].Data, &after)
var before, after interface{}
err = json.Unmarshal(beforeJSON, &before)
die(err)

patch, err := jsondiff.Compare(before, after)
err = json.Unmarshal(afterJSON, &after)
die(err)

buf := bytes.Buffer{}
encoder := json.NewEncoder(&buf)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
err = encoder.Encode(patch)
die(err)
patchLib := "wI2L"
if metadata.PatchLib != nil {
patchLib = *metadata.PatchLib
}

txtarchive.Files[1].Data = buf.Bytes()
txtarchive.Files[1].Data = compare(patchLib, beforeJSON, afterJSON)
txtarchive.Files[1].Name = "patch.json"

for i, pointer := range metadata.JSONInJSON {
Expand All @@ -80,18 +87,7 @@ func main() {
afterStr, err := ptr.Eval(after)
die(err)

var before, after interface{}
err = json.Unmarshal([]byte(beforeStr.(string)), &before)
die(err)

err = json.Unmarshal([]byte(afterStr.(string)), &after)
die(err)

patch, err := jsondiff.Compare(before, after)
die(err)

patchData, err := json.MarshalIndent(patch, "", " ")
die(err)
patchData := compare(patchLib, []byte(beforeStr.(string)), []byte(afterStr.(string)))

patchFile := txtar.File{
Name: fmt.Sprintf("jsonInJSON.%d.json", i),
Expand All @@ -102,6 +98,7 @@ func main() {
}

metadata.JSONInJSON = nil
metadata.PatchLib = nil
txtarchive.Comment, err = json.MarshalIndent(metadata, "", " ")
die(err)

Expand All @@ -118,6 +115,65 @@ func main() {
}
}

func compare(patchLib string, beforeJSON, afterJSON []byte) []byte {
var before, after interface{}
err := json.Unmarshal(beforeJSON, &before)
die(err)

err = json.Unmarshal(afterJSON, &after)
die(err)

var marshal bool
var patch any
switch strings.ToLower(patchLib) {
case "cameront":
patch, err = cameront.MakePatch(before, after)
marshal = true
case "herkyl":
patch, err = herkyl.Diff(beforeJSON, afterJSON)
marshal = true
case "mattbaird":
patch, err = mattbaird.CreatePatch(beforeJSON, afterJSON)
marshal = true
case "mianxiang":
patch, err = mianxiang.AsDiffs(beforeJSON, afterJSON)
case "snorwin":
var patchList snorwin.JSONPatchList
patchList, err = snorwin.CreateJSONPatch(after, before)
patch = patchList.Raw()
case "victorlowther":
patch, err = victorlowther.Generate(beforeJSON, afterJSON, false)
marshal = true
case "victorlowther-paranoid":
patch, err = victorlowther.Generate(beforeJSON, afterJSON, true)
marshal = true
case "wi2l":
patch, err = wI2L.Compare(before, after)
marshal = true
default:
fmt.Fprintf(os.Stderr, `Unknown patch lib %q, default to "wI2L"`, patchLib)
patch, err = wI2L.Compare(before, after)
marshal = true
}
die(err)

var patchData []byte
if marshal {
buf := bytes.Buffer{}
encoder := json.NewEncoder(&buf)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
err = encoder.Encode(patch)
die(err)
patchData = buf.Bytes()
} else {
patchData = patch.([]byte)
patchData = append(patchData, '\n')
}

return patchData
}

func die(err error) {
if err != nil {
log.Panic(err)
Expand Down
16 changes: 15 additions & 1 deletion cmd/go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
module github.com/breml/jsondiffprinter/cmd

go 1.21.7
go 1.22.3

require (
github.com/breml/jsondiffprinter v0.0.5
github.com/qri-io/jsonpointer v0.1.1
github.com/snorwin/jsonpatch v1.5.0
github.com/wI2L/jsondiff v0.5.2
golang.org/x/tools v0.21.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v0.5.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
)

require (
github.com/520MianXiangDuiXiang520/json-diff v0.2.2
github.com/VictorLowther/jsonpatch2 v1.0.1
github.com/cameront/go-jsonpatch v0.0.0-20180223123257-a8710867776e
github.com/herkyl/patchwerk v0.0.0-20190629103337-f0ea77068152
github.com/mattbaird/jsonpatch v0.0.0-20240118010651-0ba75a80ca38
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
Expand Down
51 changes: 51 additions & 0 deletions cmd/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
github.com/520MianXiangDuiXiang520/json-diff v0.2.2 h1:F9LvnoP8OXuOcEJDK7YKbFwGaQfZZrsstgSaEYpq3xA=
github.com/520MianXiangDuiXiang520/json-diff v0.2.2/go.mod h1:CvZu4GzZOS8w/et7AeljQUa/O2mplywH0gfc04ZOhKs=
github.com/VictorLowther/jsonpatch2 v1.0.1 h1:+r9GAjpCFyIDvv/xnqqlWURFF1FcQpYQXF9ezLZYkEI=
github.com/VictorLowther/jsonpatch2 v1.0.1/go.mod h1:MagdKGtUJ6bwyDgLk502ME/LDMKy9Rqh9iOf41iG5ds=
github.com/cameront/go-jsonpatch v0.0.0-20180223123257-a8710867776e h1:6c3+GQuYUWljNcReOg4gxMUss9Gjll+5Y9vqDM+ILy8=
github.com/cameront/go-jsonpatch v0.0.0-20180223123257-a8710867776e/go.mod h1:kdPJxKAfR3ZdD+MWYorN1oTdV9+qwJy9jO/0meJmcxU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/go-faker/faker/v4 v4.4.1 h1:LY1jDgjVkBZWIhATCt+gkl0x9i/7wC61gZx73GTFb+Q=
github.com/go-faker/faker/v4 v4.4.1/go.mod h1:HRLrjis+tYsbFtIHufEPTAIzcZiRu0rS9EYl2Ccwme4=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/herkyl/patchwerk v0.0.0-20190629103337-f0ea77068152 h1:GITUy7r2Eijl7u/Xe5AOs3HBV3Gt/3+l0j2bTh2UO5Y=
github.com/herkyl/patchwerk v0.0.0-20190629103337-f0ea77068152/go.mod h1:bRtCxY0f88xmUw+Y0Xfl/uGaCWMUy04wUGSKbnQdsJs=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/mattbaird/jsonpatch v0.0.0-20240118010651-0ba75a80ca38 h1:hQWBtNqRYrI7CWIaUSXXtNKR90KzcUA5uiuxFVWw7sU=
github.com/mattbaird/jsonpatch v0.0.0-20240118010651-0ba75a80ca38/go.mod h1:M1qoD/MqPgTZIk0EWKB38wE28ACRfVcn+cU08jyArI0=
github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8=
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk=
github.com/onsi/gomega v1.32.0/go.mod h1:a4x4gW6Pz2yK1MAmvluYme5lvYTn61afQ2ETw/8n4Lg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA=
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
github.com/snorwin/jsonpatch v1.5.0 h1:0m56YSt9cHiJOn8U+OcqdPGcDQZmhPM/zsG7Dv5QQP0=
github.com/snorwin/jsonpatch v1.5.0/go.mod h1:e0IDKlyFBLTFPqM0wa79dnMwjMs3XFvmKcrgCRpDqok=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand All @@ -10,7 +49,19 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/wI2L/jsondiff v0.5.2 h1:f68drsfk/Xgvt3BpLVDlGkQzOC4o+qUCl9jtGr0sbfE=
github.com/wI2L/jsondiff v0.5.2/go.mod h1:96+qu+Fhb323v//55RjkiTWYaGkiNWUqRV/w670bTAE=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
60 changes: 57 additions & 3 deletions cmd/jd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/wI2L/jsondiff"
mianxiang "github.com/520MianXiangDuiXiang520/json-diff"
victorlowther "github.com/VictorLowther/jsonpatch2"
cameront "github.com/cameront/go-jsonpatch"
herkyl "github.com/herkyl/patchwerk"
mattbaird "github.com/mattbaird/jsonpatch"
snorwin "github.com/snorwin/jsonpatch"
wI2L "github.com/wI2L/jsondiff"

"github.com/breml/jsondiffprinter"
)

var format = flag.String("format", "ascii", "output format to use (ascii, terraform)")
var (
// Call it showPatch?
debug = flag.Bool("debug", false, "enable debug output")
format = flag.String("format", "ascii", "output format to use (ascii, terraform)")
patchLib = flag.String("patchlib", "wI2L", "patch library to use (cameront, herkyl, mattbaird, MianXiang, snorwin, VictorLowther, VictorLowther-paranoid, wI2L)")
)

func main() {
flag.Parse()
Expand Down Expand Up @@ -49,11 +61,34 @@ func run(args []string) error {
return err
}

patch, err := jsondiff.Compare(before, after)
var patch any
switch strings.ToLower(*patchLib) {
case "cameront":
patch, err = cameront.MakePatch(before, after)
case "herkyl":
patch, err = herkyl.Diff(beforeJSON, afterJSON)
case "mattbaird":
patch, err = mattbaird.CreatePatch(beforeJSON, afterJSON)
case "mianxiang":
// TODO: consider options offered by 520MianXiangDuiXiang520/json-diff
patch, err = mianxiang.AsDiffs(beforeJSON, afterJSON)
case "snorwin":
var patchList snorwin.JSONPatchList
patchList, err = snorwin.CreateJSONPatch(after, before)
patch = patchList.Raw()
case "victorlowther":
patch, err = victorlowther.Generate(beforeJSON, afterJSON, false)
case "victorlowther-paranoid":
patch, err = victorlowther.Generate(beforeJSON, afterJSON, true)
default: // "wI2L"
patch, err = wI2L.Compare(before, after)
}
if err != nil {
return err
}

printPatch(patch)

switch *format {
case "ascii":
err = jsondiffprinter.NewJSONFormatter(os.Stdout).Format(before, patch)
Expand All @@ -65,3 +100,22 @@ func run(args []string) error {

return err
}

func printPatch(patch any) {
if !*debug {
return
}

switch p := patch.(type) {
case []byte:
fmt.Println(string(p))
default:
patchJSON, err := json.MarshalIndent(patch, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return
}

fmt.Println(string(patchJSON))
}
}
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions cmd/jd/testdata/simple_array/after.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"append": [1, 2, 3, 4],
"insert": [1, 2, 3],
"prepend": [0, 1, 2, 3],
"remove": [1, 3],
"update": [1, 22, 3]
}
7 changes: 7 additions & 0 deletions cmd/jd/testdata/simple_array/before.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"append": [1, 2, 3],
"insert": [1, 3],
"prepend": [1, 2, 3],
"remove": [1, 2, 3],
"update": [1, 2, 3]
}
8 changes: 8 additions & 0 deletions cmd/jd/testdata/simple_object/after.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"a": 2,
"b": {
"b1": "a",
"b2": "b1"
},
"c": true
}
7 changes: 7 additions & 0 deletions cmd/jd/testdata/simple_object/before.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"a": 1,
"b": {
"b1": "a",
"b2": "b"
}
}
5 changes: 5 additions & 0 deletions cmd/jd/testdata/type_change/after.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"object_to_array": [
1
]
}
5 changes: 5 additions & 0 deletions cmd/jd/testdata/type_change/before.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"object_to_array": {
"a": 1
}
}
Loading

0 comments on commit d74dded

Please sign in to comment.