| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package validate |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"k8s.io/kube-openapi/pkg/validation/errors" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
type Result struct { |
| 37 |
Errors []error |
| 38 |
Warnings []error |
| 39 |
MatchCount int |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (r *Result) Merge(others ...*Result) *Result { |
| 44 |
for _, other := range others { |
| 45 |
if other != nil { |
| 46 |
r.AddErrors(other.Errors...) |
| 47 |
r.AddWarnings(other.Warnings...) |
| 48 |
r.MatchCount += other.MatchCount |
| 49 |
} |
| 50 |
} |
| 51 |
return r |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func (r *Result) MergeAsErrors(others ...*Result) *Result { |
| 58 |
for _, other := range others { |
| 59 |
if other != nil { |
| 60 |
r.AddErrors(other.Errors...) |
| 61 |
r.AddErrors(other.Warnings...) |
| 62 |
r.MatchCount += other.MatchCount |
| 63 |
} |
| 64 |
} |
| 65 |
return r |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func (r *Result) MergeAsWarnings(others ...*Result) *Result { |
| 72 |
for _, other := range others { |
| 73 |
if other != nil { |
| 74 |
r.AddWarnings(other.Errors...) |
| 75 |
r.AddWarnings(other.Warnings...) |
| 76 |
r.MatchCount += other.MatchCount |
| 77 |
} |
| 78 |
} |
| 79 |
return r |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func (r *Result) AddErrors(errors ...error) { |
| 88 |
for _, e := range errors { |
| 89 |
found := false |
| 90 |
if e != nil { |
| 91 |
for _, isReported := range r.Errors { |
| 92 |
if e.Error() == isReported.Error() { |
| 93 |
found = true |
| 94 |
break |
| 95 |
} |
| 96 |
} |
| 97 |
if !found { |
| 98 |
r.Errors = append(r.Errors, e) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (r *Result) AddWarnings(warnings ...error) { |
| 106 |
for _, e := range warnings { |
| 107 |
found := false |
| 108 |
if e != nil { |
| 109 |
for _, isReported := range r.Warnings { |
| 110 |
if e.Error() == isReported.Error() { |
| 111 |
found = true |
| 112 |
break |
| 113 |
} |
| 114 |
} |
| 115 |
if !found { |
| 116 |
r.Warnings = append(r.Warnings, e) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func (r *Result) keepRelevantErrors() *Result { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
strippedErrors := []error{} |
| 136 |
for _, e := range r.Errors { |
| 137 |
if strings.HasPrefix(e.Error(), "IMPORTANT!") { |
| 138 |
strippedErrors = append(strippedErrors, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!"))) |
| 139 |
} |
| 140 |
} |
| 141 |
strippedWarnings := []error{} |
| 142 |
for _, e := range r.Warnings { |
| 143 |
if strings.HasPrefix(e.Error(), "IMPORTANT!") { |
| 144 |
strippedWarnings = append(strippedWarnings, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!"))) |
| 145 |
} |
| 146 |
} |
| 147 |
strippedResult := new(Result) |
| 148 |
strippedResult.Errors = strippedErrors |
| 149 |
strippedResult.Warnings = strippedWarnings |
| 150 |
return strippedResult |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
func (r *Result) IsValid() bool { |
| 157 |
if r == nil { |
| 158 |
return true |
| 159 |
} |
| 160 |
return len(r.Errors) == 0 |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
func (r *Result) HasErrors() bool { |
| 167 |
if r == nil { |
| 168 |
return false |
| 169 |
} |
| 170 |
return !r.IsValid() |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
func (r *Result) HasWarnings() bool { |
| 177 |
if r == nil { |
| 178 |
return false |
| 179 |
} |
| 180 |
return len(r.Warnings) > 0 |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
func (r *Result) HasErrorsOrWarnings() bool { |
| 188 |
if r == nil { |
| 189 |
return false |
| 190 |
} |
| 191 |
return len(r.Errors) > 0 || len(r.Warnings) > 0 |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
func (r *Result) Inc() { |
| 196 |
r.MatchCount++ |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
func (r *Result) AsError() error { |
| 203 |
if r.IsValid() { |
| 204 |
return nil |
| 205 |
} |
| 206 |
return errors.CompositeValidationError(r.Errors...) |
| 207 |
} |