Skip to content

Commit 0773ab3

Browse files
Fix private map (#68)
* move tests to their own package * fix private maps * access private struct interface values
1 parent 514e132 commit 0773ab3

File tree

7 files changed

+390
-353
lines changed

7 files changed

+390
-353
lines changed

diff_comparative.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func (d *Differ) diffComparative(path []string, c *ComparativeList, parent inter
1414
if d.StructMapKeys {
1515
id = idComplex(k)
1616
}
17-
fpath := copyAppend(path, id)
1817

18+
fpath := copyAppend(path, id)
1919
nv := reflect.ValueOf(nil)
2020

2121
if c.m[k].A == nil {

diff_examples_test.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package diff
1+
package diff_test
22

33
import (
44
"fmt"
55
"math/big"
66
"reflect"
7+
8+
"github.com/r3labs/diff/v2"
79
)
810

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

71-
changelog, err := Diff(a, b)
73+
changelog, err := diff.Diff(a, b)
7274
if err != nil {
7375
panic(err)
7476
}
7577

7678
//This fails in total because c is not assignable (passed by Value)
77-
patchLog := Patch(changelog, c)
79+
patchLog := diff.Patch(changelog, c)
7880

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

81-
errors := patchLog[0].Errors.(*DiffError)
83+
errors := patchLog[0].Errors.(*diff.DiffError)
8284

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

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

92-
patchLog = Patch(changelog, &c)
94+
patchLog = diff.Patch(changelog, &c)
9395

94-
patchLog, _ = Merge(a, nil, &c)
96+
patchLog, _ = diff.Merge(a, nil, &c)
9597

96-
patchLog, _ = Merge(a, d, &c)
98+
patchLog, _ = diff.Merge(a, d, &c)
9799

98100
//try patching a string
99-
patchLog = Patch(changelog, message)
101+
patchLog = diff.Patch(changelog, message)
100102

101103
//test an invalid change Value
102-
var bad *ChangeValue
104+
var bad *diff.ChangeValue
103105
if bad.IsValid() {
104106
fmt.Print("this should never happen")
105107
}
@@ -158,7 +160,7 @@ func ExampleMerge() {
158160
c.Labels["colors"] = 42
159161

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

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

170172
//ExamplePrimitiveSlice demonstrates working with arrays and primitive values
171173
func ExamplePrimitiveSlice() {
172-
173174
sla := []string{
174175
"this",
175176
"is",
@@ -189,11 +190,11 @@ func ExamplePrimitiveSlice() {
189190
"ok",
190191
}
191192

192-
patch, err := Diff(sla, slb, StructMapKeySupport())
193+
patch, err := diff.Diff(sla, slb, diff.StructMapKeySupport())
193194
if err != nil {
194195
fmt.Print("failed to diff sla and slb")
195196
}
196-
cl := Patch(patch, &slc)
197+
cl := diff.Patch(patch, &slc)
197198

198199
//now the other way, round
199200
sla = []string{
@@ -210,11 +211,11 @@ func ExamplePrimitiveSlice() {
210211
"simple",
211212
}
212213

213-
patch, err = Diff(sla, slb)
214+
patch, err = diff.Diff(sla, slb)
214215
if err != nil {
215216
fmt.Print("failed to diff sla and slb")
216217
}
217-
cl = Patch(patch, &slc)
218+
cl = diff.Patch(patch, &slc)
218219

219220
//and finally a clean view
220221
sla = []string{
@@ -226,11 +227,11 @@ func ExamplePrimitiveSlice() {
226227
}
227228
slb = []string{}
228229

229-
patch, err = Diff(sla, slb)
230+
patch, err = diff.Diff(sla, slb)
230231
if err != nil {
231232
fmt.Print("failed to diff sla and slb")
232233
}
233-
cl = Patch(patch, &slc)
234+
cl = diff.Patch(patch, &slc)
234235

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

@@ -302,12 +303,12 @@ func ExampleComplexSlicePatch() {
302303
}
303304
c := Attributes{}
304305

305-
changelog, err := Diff(a, b, DiscardComplexOrigin(), StructMapKeySupport())
306+
changelog, err := diff.Diff(a, b, diff.DiscardComplexOrigin(), diff.StructMapKeySupport())
306307
if err != nil {
307308
panic(err)
308309
}
309310

310-
patchLog := Patch(changelog, &c)
311+
patchLog := diff.Patch(changelog, &c)
311312

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

@@ -367,12 +368,12 @@ func ExampleComplexMapPatch() {
367368
Number: 23.4453,
368369
}
369370

370-
changelog, err := Diff(a, b)
371+
changelog, err := diff.Diff(a, b)
371372
if err != nil {
372373
panic(err)
373374
}
374375

375-
patchLog := Patch(changelog, &c)
376+
patchLog := diff.Patch(changelog, &c)
376377

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

@@ -466,15 +467,15 @@ func ExamplePatch() {
466467
}
467468
d.Nutrients = append(d.Nutrients, "minerals")
468469

469-
changelog, err := Diff(a, b)
470+
changelog, err := diff.Diff(a, b)
470471
if err != nil {
471472
panic(err)
472473
}
473474

474-
patchLog := Patch(changelog, &c)
475+
patchLog := diff.Patch(changelog, &c)
475476

476-
changelog, _ = Diff(a, d)
477-
patchLog = Patch(changelog, &c)
477+
changelog, _ = diff.Diff(a, d)
478+
patchLog = diff.Patch(changelog, &c)
478479

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

@@ -532,7 +533,7 @@ func ExampleDiff() {
532533
},
533534
}
534535

535-
changelog, err := Diff(a, b)
536+
changelog, err := diff.Diff(a, b)
536537
if err != nil {
537538
panic(err)
538539
}
@@ -576,7 +577,7 @@ func ExampleFilter() {
576577
},
577578
}
578579

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

591592
fmt.Printf("%#v", changelog)
592-
// 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)}}
593+
// 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)}}
593594
}
594595

595596
func ExamplePrivatePtr() {
@@ -600,11 +601,11 @@ func ExamplePrivatePtr() {
600601
a := number{}
601602
b := number{value: big.NewInt(111)}
602603

603-
changelog, err := Diff(a, b)
604+
changelog, err := diff.Diff(a, b)
604605
if err != nil {
605606
panic(err)
606607
}
607608

608609
fmt.Printf("%#v", changelog)
609-
// Output: diff.Changelog{diff.Change{Type:"update", Path:[]string{"value"}, From:interface {}(nil), To:111, parent:diff.number{value:(*big.Int)(nil), exp:0}}}
610+
// 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}}}
610611
}

diff_interface.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import "reflect"
88

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

1515
if b.Kind() == reflect.Invalid {
16-
d.cl.Add(DELETE, path, a.Interface(), nil)
16+
d.cl.Add(DELETE, path, exportInterface(a), nil)
1717
return nil
1818
}
1919

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

2828
if a.IsNil() {
29-
d.cl.Add(UPDATE, path, nil, b.Interface(), parent)
29+
d.cl.Add(UPDATE, path, nil, exportInterface(b), parent)
3030
return nil
3131
}
3232

3333
if b.IsNil() {
34-
d.cl.Add(UPDATE, path, a.Interface(), nil, parent)
34+
d.cl.Add(UPDATE, path, exportInterface(a), nil, parent)
3535
return nil
3636
}
3737

diff_map.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ func (d *Differ) diffMap(path []string, a, b reflect.Value) error {
2424

2525
for _, k := range a.MapKeys() {
2626
ae := a.MapIndex(k)
27-
c.addA(k.Interface(), &ae)
27+
c.addA(exportInterface(k), &ae)
2828
}
2929

3030
for _, k := range b.MapKeys() {
3131
be := b.MapIndex(k)
32-
c.addB(k.Interface(), &be)
32+
c.addB(exportInterface(k), &be)
3333
}
3434

35-
return d.diffComparative(path, c, a.Interface())
35+
return d.diffComparative(path, c, exportInterface(a))
3636
}
3737

3838
func (d *Differ) mapValues(t string, path []string, a reflect.Value) error {

diff_struct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ func (d *Differ) diffStruct(path []string, a, b reflect.Value) error {
1616

1717
if a.Kind() == reflect.Invalid {
1818
if d.DisableStructValues {
19-
d.cl.Add(CREATE, path, nil, b.Interface())
19+
d.cl.Add(CREATE, path, nil, exportInterface(b))
2020
return nil
2121
}
2222
return d.structValues(CREATE, path, b)
2323
}
2424

2525
if b.Kind() == reflect.Invalid {
2626
if d.DisableStructValues {
27-
d.cl.Add(DELETE, path, a.Interface(), nil)
27+
d.cl.Add(DELETE, path, exportInterface(a), nil)
2828
return nil
2929
}
3030
return d.structValues(DELETE, path, a)

0 commit comments

Comments
 (0)