1
- package diff
1
+ package diff_test
2
2
3
3
import (
4
4
"fmt"
5
5
"math/big"
6
6
"reflect"
7
+
8
+ "github.com/r3labs/diff/v2"
7
9
)
8
10
9
11
//Try to do a bunch of stuff that will result in some or all failures
@@ -68,38 +70,38 @@ func ExamplePatchWithErrors() {
68
70
Name : "second" ,
69
71
}
70
72
71
- changelog , err := Diff (a , b )
73
+ changelog , err := diff . Diff (a , b )
72
74
if err != nil {
73
75
panic (err )
74
76
}
75
77
76
78
//This fails in total because c is not assignable (passed by Value)
77
- patchLog := Patch (changelog , c )
79
+ patchLog := diff . Patch (changelog , c )
78
80
79
81
//this also demonstrated the nested errors with 'next'
80
82
81
- errors := patchLog [0 ].Errors .(* DiffError )
83
+ errors := patchLog [0 ].Errors .(* diff. DiffError )
82
84
83
85
//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" )).
85
87
WithCause (fmt .Errorf ("this is an error from somewhere else but still compatible" )).
86
88
Error ()
87
89
88
90
//invoke a few failures, i.e. bad changelog
89
91
changelog [2 ].Path [1 ] = "bad index"
90
92
changelog [3 ].Path [0 ] = "bad struct field"
91
93
92
- patchLog = Patch (changelog , & c )
94
+ patchLog = diff . Patch (changelog , & c )
93
95
94
- patchLog , _ = Merge (a , nil , & c )
96
+ patchLog , _ = diff . Merge (a , nil , & c )
95
97
96
- patchLog , _ = Merge (a , d , & c )
98
+ patchLog , _ = diff . Merge (a , d , & c )
97
99
98
100
//try patching a string
99
- patchLog = Patch (changelog , message )
101
+ patchLog = diff . Patch (changelog , message )
100
102
101
103
//test an invalid change Value
102
- var bad * ChangeValue
104
+ var bad * diff. ChangeValue
103
105
if bad .IsValid () {
104
106
fmt .Print ("this should never happen" )
105
107
}
@@ -158,7 +160,7 @@ func ExampleMerge() {
158
160
c .Labels ["colors" ] = 42
159
161
160
162
//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 )
162
164
163
165
//Note that unlike our patch version we've not included 'create' in the
164
166
//tag for nutrients. This will omit "vitamin e" from ending up in c
@@ -169,7 +171,6 @@ func ExampleMerge() {
169
171
170
172
//ExamplePrimitiveSlice demonstrates working with arrays and primitive values
171
173
func ExamplePrimitiveSlice () {
172
-
173
174
sla := []string {
174
175
"this" ,
175
176
"is" ,
@@ -189,11 +190,11 @@ func ExamplePrimitiveSlice() {
189
190
"ok" ,
190
191
}
191
192
192
- patch , err := Diff (sla , slb , StructMapKeySupport ())
193
+ patch , err := diff . Diff (sla , slb , diff . StructMapKeySupport ())
193
194
if err != nil {
194
195
fmt .Print ("failed to diff sla and slb" )
195
196
}
196
- cl := Patch (patch , & slc )
197
+ cl := diff . Patch (patch , & slc )
197
198
198
199
//now the other way, round
199
200
sla = []string {
@@ -210,11 +211,11 @@ func ExamplePrimitiveSlice() {
210
211
"simple" ,
211
212
}
212
213
213
- patch , err = Diff (sla , slb )
214
+ patch , err = diff . Diff (sla , slb )
214
215
if err != nil {
215
216
fmt .Print ("failed to diff sla and slb" )
216
217
}
217
- cl = Patch (patch , & slc )
218
+ cl = diff . Patch (patch , & slc )
218
219
219
220
//and finally a clean view
220
221
sla = []string {
@@ -226,11 +227,11 @@ func ExamplePrimitiveSlice() {
226
227
}
227
228
slb = []string {}
228
229
229
- patch , err = Diff (sla , slb )
230
+ patch , err = diff . Diff (sla , slb )
230
231
if err != nil {
231
232
fmt .Print ("failed to diff sla and slb" )
232
233
}
233
- cl = Patch (patch , & slc )
234
+ cl = diff . Patch (patch , & slc )
234
235
235
236
fmt .Printf ("%d changes made to string array; %v" , len (cl ), slc )
236
237
@@ -302,12 +303,12 @@ func ExampleComplexSlicePatch() {
302
303
}
303
304
c := Attributes {}
304
305
305
- changelog , err := Diff (a , b , DiscardComplexOrigin (), StructMapKeySupport ())
306
+ changelog , err := diff . Diff (a , b , diff . DiscardComplexOrigin (), diff . StructMapKeySupport ())
306
307
if err != nil {
307
308
panic (err )
308
309
}
309
310
310
- patchLog := Patch (changelog , & c )
311
+ patchLog := diff . Patch (changelog , & c )
311
312
312
313
fmt .Printf ("Patched %d entries and encountered %d errors" , len (patchLog ), patchLog .ErrorCount ())
313
314
@@ -367,12 +368,12 @@ func ExampleComplexMapPatch() {
367
368
Number : 23.4453 ,
368
369
}
369
370
370
- changelog , err := Diff (a , b )
371
+ changelog , err := diff . Diff (a , b )
371
372
if err != nil {
372
373
panic (err )
373
374
}
374
375
375
- patchLog := Patch (changelog , & c )
376
+ patchLog := diff . Patch (changelog , & c )
376
377
377
378
fmt .Printf ("%#v" , len (patchLog ))
378
379
@@ -466,15 +467,15 @@ func ExamplePatch() {
466
467
}
467
468
d .Nutrients = append (d .Nutrients , "minerals" )
468
469
469
- changelog , err := Diff (a , b )
470
+ changelog , err := diff . Diff (a , b )
470
471
if err != nil {
471
472
panic (err )
472
473
}
473
474
474
- patchLog := Patch (changelog , & c )
475
+ patchLog := diff . Patch (changelog , & c )
475
476
476
- changelog , _ = Diff (a , d )
477
- patchLog = Patch (changelog , & c )
477
+ changelog , _ = diff . Diff (a , d )
478
+ patchLog = diff . Patch (changelog , & c )
478
479
479
480
fmt .Printf ("%#v" , len (patchLog ))
480
481
@@ -532,7 +533,7 @@ func ExampleDiff() {
532
533
},
533
534
}
534
535
535
- changelog , err := Diff (a , b )
536
+ changelog , err := diff . Diff (a , b )
536
537
if err != nil {
537
538
panic (err )
538
539
}
@@ -576,7 +577,7 @@ func ExampleFilter() {
576
577
},
577
578
}
578
579
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 {
580
581
return field .Name != "Name"
581
582
}))
582
583
if err != nil {
@@ -589,7 +590,7 @@ func ExampleFilter() {
589
590
}
590
591
591
592
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)}}
593
594
}
594
595
595
596
func ExamplePrivatePtr () {
@@ -600,11 +601,11 @@ func ExamplePrivatePtr() {
600
601
a := number {}
601
602
b := number {value : big .NewInt (111 )}
602
603
603
- changelog , err := Diff (a , b )
604
+ changelog , err := diff . Diff (a , b )
604
605
if err != nil {
605
606
panic (err )
606
607
}
607
608
608
609
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}}}
610
611
}
0 commit comments