* Add FieldError#GetErrors to allow downstream reads
* Rename based on PR feedback
* Rename correctly for real
* Also expose `Normalized`
* WrappedErrors = merge(normalized)
* Add support for admission webhook warnings.
This extends `apis.FieldError` to support designating certain FieldErrors as "warnings" (or explicitly as "errors", however, this is the default for back-compat).
You can turn an `apis.FieldError` into a warning using: `fe.At(apis.WarningLevel)` or force it into an error using: `fe.At(apis.ErrorLevel)`.
You can get the errors at a particular diagnostic level using: `fe.Filter(apis.WarningLevel)`.
This change also hooks this into the admission webhook infrastructure to support surfacing the "warning" level `apis.FieldError`s via the `Warnings` section of the `AdmissionResponse`.
Fixes: #2497
* Add a comment about the use of defer.
* Skip doing any work if Also receives no errors
1. if no errors are passed skip doing stuff (checks, allocations, etc)
2. if there's one error passed and it's empty (the same)
- this is a quite popular pattern where in API validation we do
`x.Also(validateY())` and if that succeeded then it'll be an empty
error. Which is in reality most of the time.
3. Improve tests by better printing
4. better test coverage shows commutativity of Also(a, b) and
Also(a).Also(b)
* fix flow
* Introduce error util ErrInvalidCombination for invalid combination
Sometimes valid value becomes invalid value by combination.
example 1. https://github.com/knative/serving/issues/5382
example 2. following combination in `spec.traffic`.
```
traffic:
- latestRevision: true
revisionName: hello-example-dk7nd
percent: 100
```
But there are no error util for them, so we need to create
custom error like c1583f3045
or `ErrInvalidValue`.
The custom error will make code complicated and `ErrInvalidValue` is
not debug friendly.
To solve it, this patch introduces an util func `ErrInvalidCombination`.
* Introduce ErrGeneric instead of ErrInvalidCombination
* have simple tests. working on impl.
* strict setting, reflection based.
* ran codegen.
* adding license.
* update based on feedback and merge better.
* getting closer to something simpler assuming shallow reflect.
* adding validation test.
* use the json tag.
* Golang things nil typed pointers are not nil.
* Use real value of reflect invalid.
* add a missing test.
* two methods, one for update, one for single check.
* checkdep is now in apis.
* fix pkg.
* Update apis/deprecated_test.go
Co-Authored-By: n3wscott <32305648+n3wscott@users.noreply.github.com>
* add code clarity.
* include inlined struct objects recursively.
* Update commnets and add a flatten error test for inlined.
* Add `AtIndex` method to deal to attach index at the current error.
This is useful when we are dealing with primite leaf list (e.g.
a list of strings).
The alternative are
a) creating dummy error and then executing ViaFieldIndex(), which is
plain ugly
b) implementing Validate() on the primitive type via type aliasing and
then collecting errors, which seems an overkill.
In addition it seems ViaFieldIndex was not covered by unit tests,
so I've added one to take care of that.
* implement suggestions from the review
* cleanup
* cosmetic
* cosmetic
* Some clean ups in the FieldError code and its tests.
- rename FieldError.getNormalizedError to be normalized:
- Go kind of discourages GetA() vs A()
- it's an error, so normalizing itself is also an error, so
normalizedError() is a tautology at this point
- flatten was doing a[x]=fmt.Sprintf("%s%s", a[x],y), which is a very
expensive substitution for a[x]+=y
- some minor test cleanups (fix strings, streamline `got, want:=`
pattern, etc.
* Also make normalize() and merge() operate on pointers.
As the code currently is structured we are doing lots of duplication
and copies of the objects, since we're doing []FieldError throughout
normalize() and merge() even though normalize already returns a copy of
the initial error tree, rather than a list of pointers to existing
errors.
Hence list returned by normalize() and hence merge() can actually be pointer
lists which would save us some CPU and memory.
* checkpoint.
* Tried a new way.
* Some more slimplifying based on feedback.
* use bang vs == false.
* not sure why I did not use go style on this forloop, fixed.
* Testing an idea for field errors.
* wrap other tests for webhook.
* move FieldError to Error, FieldErrors to FieldError
* Rename files to reflect their type.
* Reworked this to require no changes externally.
* test also.
* gotta cover those tests.
* nil check
* Adding Also.
* Clean up comments.
* Don't delete the tests.
* Null check test the clear.
* Fix nits.
* FieldError is now non-mutating.
* More errorf formatting fixes.
* I can make this even simpiler
* I can make this even MORE simpiler
* Add comment to make flow of also clear.
* fix comment, optimize make errors
* Adding .ViaIndex(1). to make spec.param[1] easier to do.
* Adding ViaKey
* fix order of bag example.
* Add code coverage to missed line.
* Adding ViaFieldIndex helper
* cleaning up.
* compress more.
* more cleanup
* test name