forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
70 lines (61 loc) · 1.96 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package jsondiff
// An Option changes the default behavior of a Differ.
type Option func(*Differ)
// Factorize enables factorization of operations.
func Factorize() Option {
return func(o *Differ) { o.opts.factorize = true }
}
// Rationalize enables rationalization of operations.
func Rationalize() Option {
return func(o *Differ) { o.opts.rationalize = true }
}
// Equivalent disables the generation of operations for
// arrays of equal length and unordered/equal elements.
func Equivalent() Option {
return func(o *Differ) { o.opts.equivalent = true }
}
// Invertible enables the generation of an invertible
// patch, by preceding each remove and replace operation
// by a test operation that verifies the value at the
// path that is being removed/replaced.
// Note that copy operations are not invertible, and as
// such, using this option disable the usage of copy
// operation in favor of add operations.
func Invertible() Option {
return func(o *Differ) { o.opts.invertible = true }
}
// Ignores defines the list of values that are ignored
// by the diff generation, represented as a list of JSON
// Pointer strings (RFC 6901).
func Ignores(ptrs ...string) Option {
return func(o *Differ) {
o.opts.ignores = make(map[string]struct{}, len(ptrs))
for _, ptr := range ptrs {
o.opts.ignores[ptr] = struct{}{}
}
}
}
// OmitEmpty ignores empty values
func OmitEmpty() Option {
return func(o *Differ) {
o.opts.omitEmpty = true
}
}
// MarshalFunc allows to define the function/package
// used to marshal objects to JSON.
// The prototype of fn must match the one of the
// encoding/json.Marshal function.
func MarshalFunc(fn marshalFunc) Option {
return func(o *Differ) {
o.opts.marshal = fn
}
}
// UnmarshalFunc allows to define the function/package
// used to unmarshal objects from JSON.
// The prototype of fn must match the one of the
// encoding/json.Unmarshal function.
func UnmarshalFunc(fn unmarshalFunc) Option {
return func(o *Differ) {
o.opts.unmarshal = fn
}
}