Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/dozadiff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"github.com/sanity-io/mendoza"
)

func readJson(jsonPath string) (interface{}, error) {
func readJson(jsonPath string) (any, error) {
jsonFile, err := os.Open(jsonPath)
if err != nil {
return nil, err
}
decoder := json.NewDecoder(jsonFile)
var doc interface{}
var doc any
err = decoder.Decode(&doc)
if err != nil {
return nil, err
Expand Down
9 changes: 6 additions & 3 deletions cmd/dozapatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/sanity-io/mendoza"
)

func readJson(jsonPath string, data interface{}) ( error) {
func readJson(jsonPath string, data any) error {
jsonFile, err := os.Open(jsonPath)
if err != nil {
return err
Expand All @@ -18,7 +18,7 @@ func readJson(jsonPath string, data interface{}) ( error) {
}

func run(originalPath, patchPath string) error {
var original interface{}
var original any
if err := readJson(originalPath, &original); err != nil {
return err
}
Expand All @@ -28,7 +28,10 @@ func run(originalPath, patchPath string) error {
return err
}

result := mendoza.ApplyPatch(original, patch)
result, err := mendoza.ApplyPatch(original, patch)
if err != nil {
return err
}

encoder := json.NewEncoder(os.Stdout)
if err := encoder.Encode(result); err != nil {
Expand Down
40 changes: 22 additions & 18 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type CustomObject struct {
attrs map[string]interface{}
attrs map[string]any
}

func TestConvertObject(t *testing.T) {
opts := mendoza.DefaultOptions.WithConvertFunc(func(value interface{}) interface{} {
opts := mendoza.DefaultOptions.WithConvertFunc(func(value any) any {
if value, ok := value.(CustomObject); ok {
return value.attrs
}
Expand All @@ -22,13 +22,13 @@ func TestConvertObject(t *testing.T) {
})

customLeft := CustomObject{
attrs: map[string]interface{}{
attrs: map[string]any{
"a": "abcdefgh",
},
}

customRight := CustomObject{
attrs: map[string]interface{}{
attrs: map[string]any{
"a": "abcdefgh",
"b": 123.0,
},
Expand All @@ -42,43 +42,45 @@ func TestConvertObject(t *testing.T) {
patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)

newRight := opts.ApplyPatch(left, patch)
newRight, err := opts.ApplyPatch(left, patch)
require.NoError(t, err)
require.EqualValues(t, result, newRight)
})

t.Run("Nested", func(t *testing.T) {
left := map[string]interface{}{"a": customLeft}
right := map[string]interface{}{"a": customRight}
result := map[string]interface{}{"a": customRight.attrs}
left := map[string]any{"a": customLeft}
right := map[string]any{"a": customRight}
result := map[string]any{"a": customRight.attrs}

patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)

newRight := opts.ApplyPatch(left, patch)
newRight, err := opts.ApplyPatch(left, patch)
require.NoError(t, err)
require.EqualValues(t, result, newRight)
})
}

type CustomArray struct {
values []interface{}
values []any
}

func TestConvertArray(t *testing.T) {
opts := mendoza.DefaultOptions.WithConvertFunc(func(value interface{}) interface{} {
opts := mendoza.DefaultOptions.WithConvertFunc(func(value any) any {
if value, ok := value.(CustomArray); ok {
return value.values
}
return value
})

customLeft := CustomArray{
[]interface{}{map[string]interface{}{
[]any{map[string]any{
"a": "abcdefgh",
}},
}

customRight := CustomArray{
[]interface{}{map[string]interface{}{
[]any{map[string]any{
"a": "abcdefgh",
"b": 123.0,
}},
Expand All @@ -92,19 +94,21 @@ func TestConvertArray(t *testing.T) {
patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)

newRight := opts.ApplyPatch(left, patch)
newRight, err := opts.ApplyPatch(left, patch)
require.NoError(t, err)
require.EqualValues(t, result, newRight)
})

t.Run("Nested", func(t *testing.T) {
left := map[string]interface{}{"a": customLeft}
right := map[string]interface{}{"a": customRight}
result := map[string]interface{}{"a": customRight.values}
left := map[string]any{"a": customLeft}
right := map[string]any{"a": customRight}
result := map[string]any{"a": customRight.values}

patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)

newRight := opts.ApplyPatch(left, patch)
newRight, err := opts.ApplyPatch(left, patch)
require.NoError(t, err)
require.EqualValues(t, result, newRight)
})
}
8 changes: 4 additions & 4 deletions differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ type differ struct {
// Creates a patch which can be applied to the left document to produce the right document.
//
// This function uses the default options.
func CreatePatch(left, right interface{}) (Patch, error) {
func CreatePatch(left, right any) (Patch, error) {
return DefaultOptions.CreatePatch(left, right)
}

// Creates two patches: The first can be applied to the left document to produce the right document,
// the second can be applied to the right document to produce the left document.
//
// This function uses the default options.
func CreateDoublePatch(left, right interface{}) (Patch, Patch, error) {
func CreateDoublePatch(left, right any) (Patch, Patch, error) {
return DefaultOptions.CreateDoublePatch(left, right)
}

// Creates a patch which can be applied to the left document to produce the right document.
func (options *Options) CreatePatch(left, right interface{}) (Patch, error) {
func (options *Options) CreatePatch(left, right any) (Patch, error) {
if left == nil {
if right == nil {
return Patch{}, nil
Expand Down Expand Up @@ -56,7 +56,7 @@ func (options *Options) CreatePatch(left, right interface{}) (Patch, error) {

// Creates two patches: The first can be applied to the left document to produce the right document,
// the second can be applied to the right document to produce the left document.
func (options *Options) CreateDoublePatch(left, right interface{}) (Patch, Patch, error) {
func (options *Options) CreateDoublePatch(left, right any) (Patch, Patch, error) {
if left == nil && right == nil {
return Patch{}, Patch{}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ type Writer interface {
WriteUint8(v uint8) error
WriteUint(v int) error
WriteString(v string) error
WriteValue(v interface{}) error
WriteValue(v any) error
}

// Reader is an interface for reading values. This can be used for supporting a custom serialization format.
type Reader interface {
ReadUint8() (uint8, error)
ReadUint() (int, error)
ReadString() (string, error)
ReadValue() (interface{}, error)
ReadValue() (any, error)
}

type ValueReader interface {
ReadValue() (interface{}, error)
ReadValue() (any, error)
}

// Note: This code is intentionally very verbose/repetitive in order to be forward compatible.
Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
module github.com/sanity-io/mendoza

go 1.13
go 1.25

require (
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/vmihailenco/msgpack/v4 v4.3.5
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
google.golang.org/appengine v1.6.5 // indirect
)
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b h1:XxMZvQZtTXpWMNWK82vdjCLCe7uGMFXdTsJH0v3Hkvw=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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=
Expand All @@ -8,36 +6,26 @@ github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 h1:GD+A8+e+wFkqje55/2fOVnZPkoDIu1VooBWfNrnY8Uo=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0BqZYGxvYaXvFGUbCmPGy8DM7qWJJiIQ=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/vmihailenco/msgpack/v4 v4.3.5 h1:UBGCmLC4h5pe4sMyL3E8fqrpCTtbLesLH5mHb/0xC2M=
github.com/vmihailenco/msgpack/v4 v4.3.5/go.mod h1:DuaveEe48abshDmz5UBKyZ+yDugvaeFk5ayfrewUOaw=
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
Expand Down
10 changes: 8 additions & 2 deletions internal/fuzz/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ func Fuzz(data []byte) int {
panic(err)
}

constructedRight := mendoza.ApplyPatch(left, patch1)
constructedRight, err := mendoza.ApplyPatch(left, patch1)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(right, constructedRight) {
panic("up patch is incorrect")
}

constructedLeft := mendoza.ApplyPatch(right, patch2)
constructedLeft, err := mendoza.ApplyPatch(right, patch2)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(left, constructedLeft) {
panic("down patch is incorrect")
}
Expand Down
14 changes: 7 additions & 7 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (w *jsonWriter) WriteString(v string) error {
return w.WriteValue(v)
}

func (w *jsonWriter) WriteValue(v interface{}) error {
func (w *jsonWriter) WriteValue(v any) error {
w.next()
b, err := json.Marshal(v)
if err != nil {
Expand Down Expand Up @@ -83,12 +83,12 @@ func (r *jsonReader) ReadString() (string, error) {
return ReadStringFromValueReader(r)
}

func (r *jsonReader) ReadValue() (interface{}, error) {
func (r *jsonReader) ReadValue() (any, error) {
err := r.tryEof()
if err != nil {
return nil, err
}
var val interface{}
var val any
err = r.dec.Decode(&val)
if err != nil {
return nil, err
Expand All @@ -110,8 +110,8 @@ func (r *jsonReader) expectArray() error {
}

type jsonValueReader struct {
data []interface{}
idx int
data []any
idx int
}

func (r *jsonValueReader) ReadUint8() (uint8, error) {
Expand All @@ -126,7 +126,7 @@ func (r *jsonValueReader) ReadString() (string, error) {
return ReadStringFromValueReader(r)
}

func (r *jsonValueReader) ReadValue() (interface{}, error) {
func (r *jsonValueReader) ReadValue() (any, error) {
if r.idx >= len(r.data) {
return nil, io.EOF
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func (patch *Patch) UnmarshalJSON(data []byte) error {
}

// DecodeJSON decodes a patch from an []interface{} as parsed by encoding/json.
func (patch *Patch) DecodeJSON(data []interface{}) error {
func (patch *Patch) DecodeJSON(data []any) error {
r := jsonValueReader{data: data}
return patch.ReadFrom(&r)
}
Loading