You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`identifier`| If you need to compare arrays by a matching identifier and not based on order, you can specify the `identifier` tag. If an identifiable element is found in both the from and to structures, they will be directly compared. i.e. `diff:"name, identifier"`|
76
-
|`immutable`| Will omit this struct field from diffing. When using `diff.StructValues()` these values will be added to the returned changelog. It's use case is for when we have nothing to compare a struct to and want to show all of its relevant values. |
77
-
|`nocreate`| The default patch action is to allocate instances in the target strut, map or slice should they not exist. Adding this flag will tell patch to skip elements that it would otherwise need to allocate. This is separate from immutable, which is also honored while patching. |
|`identifier`| If you need to compare arrays by a matching identifier and not based on order, you can specify the `identifier` tag. If an identifiable element is found in both the from and to structures, they will be directly compared. i.e. `diff:"name, identifier"`|
76
+
|`immutable`| Will omit this struct field from diffing. When using `diff.StructValues()` these values will be added to the returned changelog. It's use case is for when we have nothing to compare a struct to and want to show all of its relevant values.|
77
+
|`nocreate`| The default patch action is to allocate instances in the target strut, map or slice should they not exist. Adding this flag will tell patch to skip elements that it would otherwise need to allocate. This is separate from immutable, which is also honored while patching.|
78
78
|`omitunequal`| Patching is a 'best effort' operation, and will by default attempt to update the 'correct' member of the target even if the underlying value has already changed to something other than the value in the change log 'from'. This tag will selectively ignore values that are not a 100% match. |
79
79
80
80
## Usage
@@ -84,7 +84,7 @@ In order for struct fields to be compared, they must be tagged with a given name
84
84
Diffing a basic set of values can be accomplished using the diff functions. Any items that specify a "diff" tag using a name will be compared.
85
85
86
86
```go
87
-
import"github.com/r3labs/diff"
87
+
import"github.com/r3labs/diff/v2"
88
88
89
89
typeOrderstruct {
90
90
IDstring`diff:"id"`
@@ -125,7 +125,7 @@ When marshalling the changelog to json, the output will look like:
125
125
126
126
Options can be set on the differ at call time which effect how diff acts when building the change log.
127
127
```go
128
-
import"github.com/r3labs/diff"
128
+
import"github.com/r3labs/diff/v2"
129
129
130
130
typeOrderstruct {
131
131
IDstring`diff:"id"`
@@ -151,7 +151,7 @@ func main() {
151
151
You can also create a new instance of a differ that allows options to be set.
152
152
153
153
```go
154
-
import"github.com/r3labs/diff"
154
+
import"github.com/r3labs/diff/v2"
155
155
156
156
typeOrderstruct {
157
157
IDstring`diff:"id"`
@@ -169,10 +169,10 @@ func main() {
169
169
Items: []int{1, 2, 4},
170
170
}
171
171
172
-
d, err:= diff.NewDiffer(diff.SliceOrdering(true))
173
-
if err != nil {
174
-
panic(err)
175
-
}
172
+
d, err:= diff.NewDiffer(diff.SliceOrdering(true))
173
+
if err != nil {
174
+
panic(err)
175
+
}
176
176
177
177
changelog, err:= d.Diff(a, b)
178
178
...
@@ -205,7 +205,7 @@ To accommodate this patch keeps track of each change log option it attempts to a
205
205
happened for further scrutiny.
206
206
207
207
```go
208
-
import"github.com/r3labs/diff"
208
+
import"github.com/r3labs/diff/v2"
209
209
210
210
typeOrderstruct {
211
211
IDstring`diff:"id"`
@@ -234,11 +234,44 @@ func main() {
234
234
}
235
235
```
236
236
237
+
Instances of differ with options set can also be used when patching.
238
+
239
+
```go
240
+
package main
241
+
242
+
import"github.com/r3labs/diff/v2"
243
+
244
+
typeOrderstruct {
245
+
IDstring`json:"id"`
246
+
Items []int`json:"items"`
247
+
}
248
+
249
+
funcmain() {
250
+
a:= Order{
251
+
ID: "1234",
252
+
Items: []int{1, 2, 3, 4},
253
+
}
254
+
255
+
b:= Order{
256
+
ID: "1234",
257
+
Items: []int{1, 2, 4},
258
+
}
259
+
260
+
d, _:= diff.NewDiffer(diff.TagName("json"))
261
+
262
+
changelog, _:= d.Diff(a, b)
263
+
264
+
d.Patch(changelog, &a)
265
+
// reflect.DeepEqual(a, b) == true
266
+
}
267
+
268
+
```
269
+
237
270
As a convenience, there is a Merge function that allows one to take three interfaces and perform all the tasks at the same
0 commit comments