/
patcher_config.go
115 lines (105 loc) · 4.75 KB
/
patcher_config.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gopatch
// PatcherConfig is the configuration object used to initialize new
// instances of Patcher.
type PatcherConfig struct {
// EmbedPath, if set, prepends PatchResult.Map's keys with the specified
// string in dot notation. For example:
//
// // EmbedPath == "profile.metadata"
//
// updates, err := gopatch.Default()
// .Patch(myUser, map[string]interface{}{
// "updated_at": "2020-01-01T00:00:00Z",
// })
//
// // updates.Map == map[string]interface{}{
// // "profile.metadata.updated_at": "2020-01-01T00:00:00Z",
// // }
//
// This is useful in the case the updated fields and their values should
// also be applied to a database update operation.
EmbedPath string
// PatchSource, defaulting to "struct" when empty, determines from
// which source a field name comes when matching a patch map's field
// name to the struct. For "struct", the patch map's keys must match the
// struct's field names. For any other value, the patcher will match
// based on the struct fields' matching tags.
//
// An empty or "struct" value will use the field's Go-based name.
// Use of any other value will cause the patcher to search for that tag.
// Common values include "json", "bson", "msgpack", and "mapstructure"
PatchSource string
// PatchErrors causes the Patcher to immediately return an error if a
// field is encountered which isn't tagged with the one passed to
// PatchSource, and PatchSource is not empty or "struct". Defaults to
// false.
//
// WARNING: Using this option may result in half-patched structures!
// Only use this if you don't have further use of the half-patched
// structure or can reload it afterwards.
PatchErrors bool
// UpdatedMapSource, defaulting to "struct" when empty, determines from
// which source a field name comes when creating the PatchResult.Map
// map. For "struct", the field string will be the name of the struct
// field. For any other value, the patcher will look for the related
// tag and use its value.
//
// An empty or "struct" value will use the field's Go-based name.
// Use of any other value will cause the patcher to search for that tag.
// Common values include "json", "bson", "msgpack", and "mapstructure"
UpdatedMapSource string
// UpdatedMapErrors causes the Patcher to immediately return an error
// if a field is encountered which isn't tagged with the one passed to
// UpdatedMapSource, and UpdatedMapSource is not empty or "struct".
// Defaults to false.
//
// WARNING: Using this option may result in half-patched structures!
// Only use this if you don't have further use of the half-patched
// structure or can reload it afterwards.
UpdatedMapErrors bool
// UpdatedFieldSource, defaulting to "struct" when empty, determines
// from which source a field name comes when creating the
// PatchResult.Fields array. For "struct", the field string will be
// the name of the struct field. For any other value, the patcher
// will look for the related tag and use its value.
//
// An empty or "struct" value will use the field's Go-based name.
// Use of any other value will cause the patcher to search for that tag.
// Common values include "json", "bson", "msgpack", and "mapstructure"
UpdatedFieldSource string
// UpdatedFieldErrors causes the Patcher to immediately return an error
// if a field is encountered which isn't tagged with the one passed to
// UpdatedFieldSource, and UpdatedFieldSource is not empty or "struct".
// Defaults to false.
//
// WARNING: Using this option may result in half-patched structures!
// Only use this if you don't have further use of the half-patched
// structure or can reload it afterwards.
UpdatedFieldErrors bool
// PermittedFields, if set, will prevent patches from fields in the
// patch map that are not present in this array. For example:
//
// // PermittedFields == []string{"email_address"}
//
// updates, err := gopatch.Default()
// .Patch(myUser, map[string]interface{}{
// "email_address": "myemail@address.com",
// "password_hash": "injectedhash"
// })
//
// // updates.Fields == []string{"email_address"}
// // updates.Unpermitted == []string{"password_hash"}
//
// To permit all fields of an embedded struct, use `embedded.*`. All
// fields found to be unpermitted will be stored in dot notation in
// the PatchResult's UnpermittedFields array if UnpermittedErrors is
// false.
PermittedFields []string
// UnpermittedErrors causes the Patcher to immediately return an error
// if a field is found to be unpermitted.
//
// WARNING: Using this option may result in half-patched structures!
// Only use this if you don't have further use of the half-patched
// structure or can reload it afterwards.
UnpermittedErrors bool
}