Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix private map #68

Merged
merged 3 commits into from Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion diff_comparative.go
Expand Up @@ -14,8 +14,8 @@ func (d *Differ) diffComparative(path []string, c *ComparativeList, parent inter
if d.StructMapKeys {
id = idComplex(k)
}
fpath := copyAppend(path, id)

fpath := copyAppend(path, id)
nv := reflect.ValueOf(nil)

if c.m[k].A == nil {
Expand Down
63 changes: 32 additions & 31 deletions diff_examples_test.go
@@ -1,9 +1,11 @@
package diff
package diff_test

import (
"fmt"
"math/big"
"reflect"

"github.com/r3labs/diff/v2"
)

//Try to do a bunch of stuff that will result in some or all failures
Expand Down Expand Up @@ -68,38 +70,38 @@ func ExamplePatchWithErrors() {
Name: "second",
}

changelog, err := Diff(a, b)
changelog, err := diff.Diff(a, b)
if err != nil {
panic(err)
}

//This fails in total because c is not assignable (passed by Value)
patchLog := Patch(changelog, c)
patchLog := diff.Patch(changelog, c)

//this also demonstrated the nested errors with 'next'

errors := patchLog[0].Errors.(*DiffError)
errors := patchLog[0].Errors.(*diff.DiffError)

//we can also continue to nest errors if we like
message := errors.WithCause(NewError("This is a custom message")).
message := errors.WithCause(diff.NewError("This is a custom message")).
WithCause(fmt.Errorf("this is an error from somewhere else but still compatible")).
Error()

//invoke a few failures, i.e. bad changelog
changelog[2].Path[1] = "bad index"
changelog[3].Path[0] = "bad struct field"

patchLog = Patch(changelog, &c)
patchLog = diff.Patch(changelog, &c)

patchLog, _ = Merge(a, nil, &c)
patchLog, _ = diff.Merge(a, nil, &c)

patchLog, _ = Merge(a, d, &c)
patchLog, _ = diff.Merge(a, d, &c)

//try patching a string
patchLog = Patch(changelog, message)
patchLog = diff.Patch(changelog, message)

//test an invalid change Value
var bad *ChangeValue
var bad *diff.ChangeValue
if bad.IsValid() {
fmt.Print("this should never happen")
}
Expand Down Expand Up @@ -158,7 +160,7 @@ func ExampleMerge() {
c.Labels["colors"] = 42

//the only error that can happen here comes from the diff step
patchLog, _ := Merge(a, b, &c)
patchLog, _ := diff.Merge(a, b, &c)

//Note that unlike our patch version we've not included 'create' in the
//tag for nutrients. This will omit "vitamin e" from ending up in c
Expand All @@ -169,7 +171,6 @@ func ExampleMerge() {

//ExamplePrimitiveSlice demonstrates working with arrays and primitive values
func ExamplePrimitiveSlice() {

sla := []string{
"this",
"is",
Expand All @@ -189,11 +190,11 @@ func ExamplePrimitiveSlice() {
"ok",
}

patch, err := Diff(sla, slb, StructMapKeySupport())
patch, err := diff.Diff(sla, slb, diff.StructMapKeySupport())
if err != nil {
fmt.Print("failed to diff sla and slb")
}
cl := Patch(patch, &slc)
cl := diff.Patch(patch, &slc)

//now the other way, round
sla = []string{
Expand All @@ -210,11 +211,11 @@ func ExamplePrimitiveSlice() {
"simple",
}

patch, err = Diff(sla, slb)
patch, err = diff.Diff(sla, slb)
if err != nil {
fmt.Print("failed to diff sla and slb")
}
cl = Patch(patch, &slc)
cl = diff.Patch(patch, &slc)

//and finally a clean view
sla = []string{
Expand All @@ -226,11 +227,11 @@ func ExamplePrimitiveSlice() {
}
slb = []string{}

patch, err = Diff(sla, slb)
patch, err = diff.Diff(sla, slb)
if err != nil {
fmt.Print("failed to diff sla and slb")
}
cl = Patch(patch, &slc)
cl = diff.Patch(patch, &slc)

fmt.Printf("%d changes made to string array; %v", len(cl), slc)

Expand Down Expand Up @@ -302,12 +303,12 @@ func ExampleComplexSlicePatch() {
}
c := Attributes{}

changelog, err := Diff(a, b, DiscardComplexOrigin(), StructMapKeySupport())
changelog, err := diff.Diff(a, b, diff.DiscardComplexOrigin(), diff.StructMapKeySupport())
if err != nil {
panic(err)
}

patchLog := Patch(changelog, &c)
patchLog := diff.Patch(changelog, &c)

fmt.Printf("Patched %d entries and encountered %d errors", len(patchLog), patchLog.ErrorCount())

Expand Down Expand Up @@ -367,12 +368,12 @@ func ExampleComplexMapPatch() {
Number: 23.4453,
}

changelog, err := Diff(a, b)
changelog, err := diff.Diff(a, b)
if err != nil {
panic(err)
}

patchLog := Patch(changelog, &c)
patchLog := diff.Patch(changelog, &c)

fmt.Printf("%#v", len(patchLog))

Expand Down Expand Up @@ -466,15 +467,15 @@ func ExamplePatch() {
}
d.Nutrients = append(d.Nutrients, "minerals")

changelog, err := Diff(a, b)
changelog, err := diff.Diff(a, b)
if err != nil {
panic(err)
}

patchLog := Patch(changelog, &c)
patchLog := diff.Patch(changelog, &c)

changelog, _ = Diff(a, d)
patchLog = Patch(changelog, &c)
changelog, _ = diff.Diff(a, d)
patchLog = diff.Patch(changelog, &c)

fmt.Printf("%#v", len(patchLog))

Expand Down Expand Up @@ -532,7 +533,7 @@ func ExampleDiff() {
},
}

changelog, err := Diff(a, b)
changelog, err := diff.Diff(a, b)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -576,7 +577,7 @@ func ExampleFilter() {
},
}

d, err := NewDiffer(Filter(func(path []string, parent reflect.Type, field reflect.StructField) bool {
d, err := diff.NewDiffer(diff.Filter(func(path []string, parent reflect.Type, field reflect.StructField) bool {
return field.Name != "Name"
}))
if err != nil {
Expand All @@ -589,7 +590,7 @@ func ExampleFilter() {
}

fmt.Printf("%#v", changelog)
// Output: diff.Changelog{diff.Change{Type:"update", Path:[]string{"id"}, From:1, To:2, parent:diff.Fruit{ID:1, Name:"Green Apple", Healthy:true, Nutrients:[]string{"vitamin c", "vitamin d"}, Tags:[]diff.Tag(nil)}}, diff.Change{Type:"create", Path:[]string{"nutrients", "2"}, From:interface {}(nil), To:"vitamin e", parent:interface {}(nil)}}
// Output: diff.Changelog{diff.Change{Type:"update", Path:[]string{"id"}, From:1, To:2, parent:diff_test.Fruit{ID:1, Name:"Green Apple", Healthy:true, Nutrients:[]string{"vitamin c", "vitamin d"}, Tags:[]diff_test.Tag(nil)}}, diff.Change{Type:"create", Path:[]string{"nutrients", "2"}, From:interface {}(nil), To:"vitamin e", parent:interface {}(nil)}}
}

func ExamplePrivatePtr() {
Expand All @@ -600,11 +601,11 @@ func ExamplePrivatePtr() {
a := number{}
b := number{value: big.NewInt(111)}

changelog, err := Diff(a, b)
changelog, err := diff.Diff(a, b)
if err != nil {
panic(err)
}

fmt.Printf("%#v", changelog)
// Output: diff.Changelog{diff.Change{Type:"update", Path:[]string{"value"}, From:interface {}(nil), To:111, parent:diff.number{value:(*big.Int)(nil), exp:0}}}
// Output: diff.Changelog{diff.Change{Type:"update", Path:[]string{"value"}, From:interface {}(nil), To:111, parent:diff_test.number{value:(*big.Int)(nil), exp:0}}}
}
8 changes: 4 additions & 4 deletions diff_interface.go
Expand Up @@ -8,12 +8,12 @@ import "reflect"

func (d *Differ) diffInterface(path []string, a, b reflect.Value, parent interface{}) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand All @@ -26,12 +26,12 @@ func (d *Differ) diffInterface(path []string, a, b reflect.Value, parent interfa
}

if a.IsNil() {
d.cl.Add(UPDATE, path, nil, b.Interface(), parent)
d.cl.Add(UPDATE, path, nil, exportInterface(b), parent)
return nil
}

if b.IsNil() {
d.cl.Add(UPDATE, path, a.Interface(), nil, parent)
d.cl.Add(UPDATE, path, exportInterface(a), nil, parent)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions diff_map.go
Expand Up @@ -24,15 +24,15 @@ func (d *Differ) diffMap(path []string, a, b reflect.Value) error {

for _, k := range a.MapKeys() {
ae := a.MapIndex(k)
c.addA(k.Interface(), &ae)
c.addA(exportInterface(k), &ae)
}

for _, k := range b.MapKeys() {
be := b.MapIndex(k)
c.addB(k.Interface(), &be)
c.addB(exportInterface(k), &be)
}

return d.diffComparative(path, c, a.Interface())
return d.diffComparative(path, c, exportInterface(a))
}

func (d *Differ) mapValues(t string, path []string, a reflect.Value) error {
Expand Down
4 changes: 2 additions & 2 deletions diff_struct.go
Expand Up @@ -16,15 +16,15 @@ func (d *Differ) diffStruct(path []string, a, b reflect.Value) error {

if a.Kind() == reflect.Invalid {
if d.DisableStructValues {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}
return d.structValues(CREATE, path, b)
}

if b.Kind() == reflect.Invalid {
if d.DisableStructValues {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}
return d.structValues(DELETE, path, a)
Expand Down