| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package v3 |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"net" |
| 11 |
"net/mail" |
| 12 |
"net/url" |
| 13 |
"regexp" |
| 14 |
"sort" |
| 15 |
"strings" |
| 16 |
"time" |
| 17 |
"unicode/utf8" |
| 18 |
|
| 19 |
"google.golang.org/protobuf/types/known/anypb" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
var ( |
| 24 |
_ = bytes.MinRead |
| 25 |
_ = errors.New("") |
| 26 |
_ = fmt.Print |
| 27 |
_ = utf8.UTFMax |
| 28 |
_ = (*regexp.Regexp)(nil) |
| 29 |
_ = (*strings.Reader)(nil) |
| 30 |
_ = net.IPv4len |
| 31 |
_ = time.Duration(0) |
| 32 |
_ = (*url.URL)(nil) |
| 33 |
_ = (*mail.Address)(nil) |
| 34 |
_ = anypb.Any{} |
| 35 |
_ = sort.Sort |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func (m *StringMatcher) Validate() error { |
| 42 |
return m.validate(false) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
func (m *StringMatcher) ValidateAll() error { |
| 50 |
return m.validate(true) |
| 51 |
} |
| 52 |
|
| 53 |
func (m *StringMatcher) validate(all bool) error { |
| 54 |
if m == nil { |
| 55 |
return nil |
| 56 |
} |
| 57 |
|
| 58 |
var errors []error |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
oneofMatchPatternPresent := false |
| 63 |
switch v := m.MatchPattern.(type) { |
| 64 |
case *StringMatcher_Exact: |
| 65 |
if v == nil { |
| 66 |
err := StringMatcherValidationError{ |
| 67 |
field: "MatchPattern", |
| 68 |
reason: "oneof value cannot be a typed-nil", |
| 69 |
} |
| 70 |
if !all { |
| 71 |
return err |
| 72 |
} |
| 73 |
errors = append(errors, err) |
| 74 |
} |
| 75 |
oneofMatchPatternPresent = true |
| 76 |
|
| 77 |
case *StringMatcher_Prefix: |
| 78 |
if v == nil { |
| 79 |
err := StringMatcherValidationError{ |
| 80 |
field: "MatchPattern", |
| 81 |
reason: "oneof value cannot be a typed-nil", |
| 82 |
} |
| 83 |
if !all { |
| 84 |
return err |
| 85 |
} |
| 86 |
errors = append(errors, err) |
| 87 |
} |
| 88 |
oneofMatchPatternPresent = true |
| 89 |
|
| 90 |
if utf8.RuneCountInString(m.GetPrefix()) < 1 { |
| 91 |
err := StringMatcherValidationError{ |
| 92 |
field: "Prefix", |
| 93 |
reason: "value length must be at least 1 runes", |
| 94 |
} |
| 95 |
if !all { |
| 96 |
return err |
| 97 |
} |
| 98 |
errors = append(errors, err) |
| 99 |
} |
| 100 |
|
| 101 |
case *StringMatcher_Suffix: |
| 102 |
if v == nil { |
| 103 |
err := StringMatcherValidationError{ |
| 104 |
field: "MatchPattern", |
| 105 |
reason: "oneof value cannot be a typed-nil", |
| 106 |
} |
| 107 |
if !all { |
| 108 |
return err |
| 109 |
} |
| 110 |
errors = append(errors, err) |
| 111 |
} |
| 112 |
oneofMatchPatternPresent = true |
| 113 |
|
| 114 |
if utf8.RuneCountInString(m.GetSuffix()) < 1 { |
| 115 |
err := StringMatcherValidationError{ |
| 116 |
field: "Suffix", |
| 117 |
reason: "value length must be at least 1 runes", |
| 118 |
} |
| 119 |
if !all { |
| 120 |
return err |
| 121 |
} |
| 122 |
errors = append(errors, err) |
| 123 |
} |
| 124 |
|
| 125 |
case *StringMatcher_SafeRegex: |
| 126 |
if v == nil { |
| 127 |
err := StringMatcherValidationError{ |
| 128 |
field: "MatchPattern", |
| 129 |
reason: "oneof value cannot be a typed-nil", |
| 130 |
} |
| 131 |
if !all { |
| 132 |
return err |
| 133 |
} |
| 134 |
errors = append(errors, err) |
| 135 |
} |
| 136 |
oneofMatchPatternPresent = true |
| 137 |
|
| 138 |
if m.GetSafeRegex() == nil { |
| 139 |
err := StringMatcherValidationError{ |
| 140 |
field: "SafeRegex", |
| 141 |
reason: "value is required", |
| 142 |
} |
| 143 |
if !all { |
| 144 |
return err |
| 145 |
} |
| 146 |
errors = append(errors, err) |
| 147 |
} |
| 148 |
|
| 149 |
if all { |
| 150 |
switch v := interface{}(m.GetSafeRegex()).(type) { |
| 151 |
case interface{ ValidateAll() error }: |
| 152 |
if err := v.ValidateAll(); err != nil { |
| 153 |
errors = append(errors, StringMatcherValidationError{ |
| 154 |
field: "SafeRegex", |
| 155 |
reason: "embedded message failed validation", |
| 156 |
cause: err, |
| 157 |
}) |
| 158 |
} |
| 159 |
case interface{ Validate() error }: |
| 160 |
if err := v.Validate(); err != nil { |
| 161 |
errors = append(errors, StringMatcherValidationError{ |
| 162 |
field: "SafeRegex", |
| 163 |
reason: "embedded message failed validation", |
| 164 |
cause: err, |
| 165 |
}) |
| 166 |
} |
| 167 |
} |
| 168 |
} else if v, ok := interface{}(m.GetSafeRegex()).(interface{ Validate() error }); ok { |
| 169 |
if err := v.Validate(); err != nil { |
| 170 |
return StringMatcherValidationError{ |
| 171 |
field: "SafeRegex", |
| 172 |
reason: "embedded message failed validation", |
| 173 |
cause: err, |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
case *StringMatcher_Contains: |
| 179 |
if v == nil { |
| 180 |
err := StringMatcherValidationError{ |
| 181 |
field: "MatchPattern", |
| 182 |
reason: "oneof value cannot be a typed-nil", |
| 183 |
} |
| 184 |
if !all { |
| 185 |
return err |
| 186 |
} |
| 187 |
errors = append(errors, err) |
| 188 |
} |
| 189 |
oneofMatchPatternPresent = true |
| 190 |
|
| 191 |
if utf8.RuneCountInString(m.GetContains()) < 1 { |
| 192 |
err := StringMatcherValidationError{ |
| 193 |
field: "Contains", |
| 194 |
reason: "value length must be at least 1 runes", |
| 195 |
} |
| 196 |
if !all { |
| 197 |
return err |
| 198 |
} |
| 199 |
errors = append(errors, err) |
| 200 |
} |
| 201 |
|
| 202 |
case *StringMatcher_Custom: |
| 203 |
if v == nil { |
| 204 |
err := StringMatcherValidationError{ |
| 205 |
field: "MatchPattern", |
| 206 |
reason: "oneof value cannot be a typed-nil", |
| 207 |
} |
| 208 |
if !all { |
| 209 |
return err |
| 210 |
} |
| 211 |
errors = append(errors, err) |
| 212 |
} |
| 213 |
oneofMatchPatternPresent = true |
| 214 |
|
| 215 |
if all { |
| 216 |
switch v := interface{}(m.GetCustom()).(type) { |
| 217 |
case interface{ ValidateAll() error }: |
| 218 |
if err := v.ValidateAll(); err != nil { |
| 219 |
errors = append(errors, StringMatcherValidationError{ |
| 220 |
field: "Custom", |
| 221 |
reason: "embedded message failed validation", |
| 222 |
cause: err, |
| 223 |
}) |
| 224 |
} |
| 225 |
case interface{ Validate() error }: |
| 226 |
if err := v.Validate(); err != nil { |
| 227 |
errors = append(errors, StringMatcherValidationError{ |
| 228 |
field: "Custom", |
| 229 |
reason: "embedded message failed validation", |
| 230 |
cause: err, |
| 231 |
}) |
| 232 |
} |
| 233 |
} |
| 234 |
} else if v, ok := interface{}(m.GetCustom()).(interface{ Validate() error }); ok { |
| 235 |
if err := v.Validate(); err != nil { |
| 236 |
return StringMatcherValidationError{ |
| 237 |
field: "Custom", |
| 238 |
reason: "embedded message failed validation", |
| 239 |
cause: err, |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
default: |
| 245 |
_ = v |
| 246 |
} |
| 247 |
if !oneofMatchPatternPresent { |
| 248 |
err := StringMatcherValidationError{ |
| 249 |
field: "MatchPattern", |
| 250 |
reason: "value is required", |
| 251 |
} |
| 252 |
if !all { |
| 253 |
return err |
| 254 |
} |
| 255 |
errors = append(errors, err) |
| 256 |
} |
| 257 |
|
| 258 |
if len(errors) > 0 { |
| 259 |
return StringMatcherMultiError(errors) |
| 260 |
} |
| 261 |
|
| 262 |
return nil |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
type StringMatcherMultiError []error |
| 269 |
|
| 270 |
|
| 271 |
func (m StringMatcherMultiError) Error() string { |
| 272 |
msgs := make([]string, 0, len(m)) |
| 273 |
for _, err := range m { |
| 274 |
msgs = append(msgs, err.Error()) |
| 275 |
} |
| 276 |
return strings.Join(msgs, "; ") |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
func (m StringMatcherMultiError) AllErrors() []error { return m } |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
type StringMatcherValidationError struct { |
| 285 |
field string |
| 286 |
reason string |
| 287 |
cause error |
| 288 |
key bool |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
func (e StringMatcherValidationError) Field() string { return e.field } |
| 293 |
|
| 294 |
|
| 295 |
func (e StringMatcherValidationError) Reason() string { return e.reason } |
| 296 |
|
| 297 |
|
| 298 |
func (e StringMatcherValidationError) Cause() error { return e.cause } |
| 299 |
|
| 300 |
|
| 301 |
func (e StringMatcherValidationError) Key() bool { return e.key } |
| 302 |
|
| 303 |
|
| 304 |
func (e StringMatcherValidationError) ErrorName() string { return "StringMatcherValidationError" } |
| 305 |
|
| 306 |
|
| 307 |
func (e StringMatcherValidationError) Error() string { |
| 308 |
cause := "" |
| 309 |
if e.cause != nil { |
| 310 |
cause = fmt.Sprintf(" | caused by: %v", e.cause) |
| 311 |
} |
| 312 |
|
| 313 |
key := "" |
| 314 |
if e.key { |
| 315 |
key = "key for " |
| 316 |
} |
| 317 |
|
| 318 |
return fmt.Sprintf( |
| 319 |
"invalid %sStringMatcher.%s: %s%s", |
| 320 |
key, |
| 321 |
e.field, |
| 322 |
e.reason, |
| 323 |
cause) |
| 324 |
} |
| 325 |
|
| 326 |
var _ error = StringMatcherValidationError{} |
| 327 |
|
| 328 |
var _ interface { |
| 329 |
Field() string |
| 330 |
Reason() string |
| 331 |
Key() bool |
| 332 |
Cause() error |
| 333 |
ErrorName() string |
| 334 |
} = StringMatcherValidationError{} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
func (m *ListStringMatcher) Validate() error { |
| 340 |
return m.validate(false) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
func (m *ListStringMatcher) ValidateAll() error { |
| 348 |
return m.validate(true) |
| 349 |
} |
| 350 |
|
| 351 |
func (m *ListStringMatcher) validate(all bool) error { |
| 352 |
if m == nil { |
| 353 |
return nil |
| 354 |
} |
| 355 |
|
| 356 |
var errors []error |
| 357 |
|
| 358 |
if len(m.GetPatterns()) < 1 { |
| 359 |
err := ListStringMatcherValidationError{ |
| 360 |
field: "Patterns", |
| 361 |
reason: "value must contain at least 1 item(s)", |
| 362 |
} |
| 363 |
if !all { |
| 364 |
return err |
| 365 |
} |
| 366 |
errors = append(errors, err) |
| 367 |
} |
| 368 |
|
| 369 |
for idx, item := range m.GetPatterns() { |
| 370 |
_, _ = idx, item |
| 371 |
|
| 372 |
if all { |
| 373 |
switch v := interface{}(item).(type) { |
| 374 |
case interface{ ValidateAll() error }: |
| 375 |
if err := v.ValidateAll(); err != nil { |
| 376 |
errors = append(errors, ListStringMatcherValidationError{ |
| 377 |
field: fmt.Sprintf("Patterns[%v]", idx), |
| 378 |
reason: "embedded message failed validation", |
| 379 |
cause: err, |
| 380 |
}) |
| 381 |
} |
| 382 |
case interface{ Validate() error }: |
| 383 |
if err := v.Validate(); err != nil { |
| 384 |
errors = append(errors, ListStringMatcherValidationError{ |
| 385 |
field: fmt.Sprintf("Patterns[%v]", idx), |
| 386 |
reason: "embedded message failed validation", |
| 387 |
cause: err, |
| 388 |
}) |
| 389 |
} |
| 390 |
} |
| 391 |
} else if v, ok := interface{}(item).(interface{ Validate() error }); ok { |
| 392 |
if err := v.Validate(); err != nil { |
| 393 |
return ListStringMatcherValidationError{ |
| 394 |
field: fmt.Sprintf("Patterns[%v]", idx), |
| 395 |
reason: "embedded message failed validation", |
| 396 |
cause: err, |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
if len(errors) > 0 { |
| 404 |
return ListStringMatcherMultiError(errors) |
| 405 |
} |
| 406 |
|
| 407 |
return nil |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
type ListStringMatcherMultiError []error |
| 414 |
|
| 415 |
|
| 416 |
func (m ListStringMatcherMultiError) Error() string { |
| 417 |
msgs := make([]string, 0, len(m)) |
| 418 |
for _, err := range m { |
| 419 |
msgs = append(msgs, err.Error()) |
| 420 |
} |
| 421 |
return strings.Join(msgs, "; ") |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
func (m ListStringMatcherMultiError) AllErrors() []error { return m } |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
type ListStringMatcherValidationError struct { |
| 430 |
field string |
| 431 |
reason string |
| 432 |
cause error |
| 433 |
key bool |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
func (e ListStringMatcherValidationError) Field() string { return e.field } |
| 438 |
|
| 439 |
|
| 440 |
func (e ListStringMatcherValidationError) Reason() string { return e.reason } |
| 441 |
|
| 442 |
|
| 443 |
func (e ListStringMatcherValidationError) Cause() error { return e.cause } |
| 444 |
|
| 445 |
|
| 446 |
func (e ListStringMatcherValidationError) Key() bool { return e.key } |
| 447 |
|
| 448 |
|
| 449 |
func (e ListStringMatcherValidationError) ErrorName() string { |
| 450 |
return "ListStringMatcherValidationError" |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
func (e ListStringMatcherValidationError) Error() string { |
| 455 |
cause := "" |
| 456 |
if e.cause != nil { |
| 457 |
cause = fmt.Sprintf(" | caused by: %v", e.cause) |
| 458 |
} |
| 459 |
|
| 460 |
key := "" |
| 461 |
if e.key { |
| 462 |
key = "key for " |
| 463 |
} |
| 464 |
|
| 465 |
return fmt.Sprintf( |
| 466 |
"invalid %sListStringMatcher.%s: %s%s", |
| 467 |
key, |
| 468 |
e.field, |
| 469 |
e.reason, |
| 470 |
cause) |
| 471 |
} |
| 472 |
|
| 473 |
var _ error = ListStringMatcherValidationError{} |
| 474 |
|
| 475 |
var _ interface { |
| 476 |
Field() string |
| 477 |
Reason() string |
| 478 |
Key() bool |
| 479 |
Cause() error |
| 480 |
ErrorName() string |
| 481 |
} = ListStringMatcherValidationError{} |