-
Notifications
You must be signed in to change notification settings - Fork 41
/
option.go
91 lines (80 loc) · 2.54 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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 }
}
// LCS uses a Longest Common Subsequence to compare
// arrays.
func LCS() Option {
return func(o *Differ) { o.opts.lcs = 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 }
}
// 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
}
}
// SkipCompact instructs to skip the compaction of the input
// JSON documents when the Rationalize option is enabled.
func SkipCompact() Option {
return func(o *Differ) {
o.isCompact = true
}
}
// InPlaceCompaction instructs to compact the input JSON
// documents in place; it does not allocate to create a
// copy, but modify the original byte slice instead.
// This option has no effect if used alongside SkipCompact.
func InPlaceCompaction() Option {
return func(o *Differ) {
o.compactInPlace = 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) {
if len(ptrs) == 0 {
return
}
o.opts.ignores = make(map[string]struct{}, len(ptrs))
for _, ptr := range ptrs {
o.opts.ignores[ptr] = struct{}{}
}
o.opts.hasIgnore = true
}
}