-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
202 lines (157 loc) · 4.02 KB
/
result.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package golidate
type Metadata map[string]any
type OnRename func(result Result)
type Result struct {
ok bool `json:"-"`
conditions []Condition `json:"-"`
onRename OnRename `json:"-"`
children Results `json:"-"`
prefixedChildren Results `json:"-"`
Attribute string `json:"attribute"`
Value any `json:"value"`
Code string `json:"code"`
Message string `json:"message"`
Metadata Metadata `json:"metadata"`
}
func (result Result) Results(name string) Results {
results := make(Results, 1)
results[0] = result
for _, child := range result.children {
if name != "" {
results = append(results, child.Name(name).Results(name)...)
} else {
results = append(results, child.Results(name)...)
}
}
for _, child := range result.prefixedChildren.Prefixed(result.Attribute) {
results = append(results, child.Results(name)...)
}
return results
}
func (result Result) WithChild(child Result) Result {
result.children = append(result.children, child)
return result
}
func (result Result) WithPrefixedChild(child Result) Result {
result.prefixedChildren = append(result.prefixedChildren, child)
return result
}
func (result Result) Name(attribute string) Result {
result.Attribute = attribute
if result.onRename != nil {
result.onRename(result)
}
return result
}
func OnRenameMany(key string) OnRename {
return func(result Result) {
if operations, ok := result.Metadata[key].(Results); ok {
for i := range operations {
operations[i] = operations[i].Name(result.Attribute)
}
}
}
}
func OnRenameSingle(key string) OnRename {
return func(result Result) {
if operation, ok := result.Metadata[key].(Result); ok {
result.Metadata[key] = operation.Name(result.Attribute)
}
}
}
func (result Result) OnRename(callback OnRename) Result {
result.onRename = callback
return result
}
func (result Result) With(key string, value any) Result {
return result.WithMetadataMerged(
Metadata{
key: value,
},
)
}
func (result Result) WithMetadata(metadata Metadata) Result {
result.Metadata = metadata
return result
}
func (result Result) WithMetadataMerged(metadata Metadata) Result {
for key, value := range metadata {
result.Metadata[key] = value
}
return result
}
func (result Result) Pass() Result {
result.ok = true
return result
}
func (result Result) Fail() Result {
result.ok = false
return result
}
func (result Result) Passes() bool {
for _, condition := range result.conditions {
if !condition(result.Value) {
return true
}
}
return result.ok
}
func (result Result) PassesChilds() bool {
for _, child := range result.children {
if !child.Passes() {
return false
}
}
for _, child := range result.prefixedChildren {
if !child.Passes() {
return false
}
}
return true
}
func (result Result) PassesAll() bool {
return result.Passes() && result.PassesChilds()
}
func (result Result) Conditions(conditions ...Condition) Result {
result.conditions = conditions
return result
}
func (result Result) When(condition bool) Result {
result.conditions = []Condition{
func(any) bool {
return condition
},
}
return result
}
func (result Result) Translate(dictionaries ...Dictionary) Result {
dictionary := make(Dictionary)
for _, dict := range dictionaries {
for key, value := range dict {
dictionary[key] = value
}
}
if entry, ok := dictionary[result.Code]; ok {
return entry(dictionary, result)
}
return result
}
func Uncertain(value any, code string) Result {
return Result{
ok: false,
conditions: make([]Condition, 0),
children: make(Results, 0),
prefixedChildren: make(Results, 0),
Attribute: "attribute",
Value: value,
Code: code,
Message: code,
Metadata: make(Metadata),
}
}
func Fail(value any, code string) Result {
return Uncertain(value, code).Fail()
}
func Pass(value any, code string) Result {
return Uncertain(value, code).Pass()
}