mirror of
https://github.com/agent-substrate/substrate.git
synced 2026-11-12 10:35:06 +08:00
This mostly eliminates the need to hand-write validation code. This PR is a long series of commits which add DV for most of Actor and all of Atespace. Here is a map to the commits: * The first few take a dep on a new Kubernetes tag, import the code into third_party, and apply a single patch. Because we have different Go modules for tools, I had to do it twice. When that patch lands, we can revert these commits, but that won't be until the 1.38 cycle in a few months. * The next commits slowly add DV support, so a human can review each of them in a reasonable amount of time. The emphasis is on great test cases. * I made a bad choice early on as to where to generate the code into, so I moved it. Rebasing on that was exceedingly hard, so I left it as a move. * This required changing update/go-generate -> update/codegen -- we need to get the ordering of tools right, which `go generate` does not guarantee. * Then I added a "middle" layer called "ServiceImpl" between the RPC and storage layers. This allows things like workflow to call the same business logic as the RPC layer, including validation. Lots of test fixes. * Then I finished the Create() and Update() paths for Actor. Those represent the "right" (or closest to) way to implement resources, and tests for validation. I strongly encourage reviewers to read it commit-by-commit. Rebasing this is VERY tedious, so the sooner it lands or dies completely, the better. Then we can start converting the rest. `./hack/run-tool.sh validation-gen --docs` will produce some docs on the tool and the available tags. @laoj2 @juli4n @EItanya @HavenXia @lalitc375 @yongruilin @jpbetz FYI
61 lines
2.2 KiB
Go
61 lines
2.2 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2015-2025 go-swagger maintainers
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package jsonpointer
|
|
|
|
import "fmt"
|
|
|
|
type pointerError string
|
|
|
|
func (e pointerError) Error() string {
|
|
return string(e)
|
|
}
|
|
|
|
const (
|
|
// ErrPointer is a sentinel error raised by all errors from this package.
|
|
ErrPointer pointerError = "JSON pointer error"
|
|
|
|
// ErrInvalidStart states that a JSON pointer must start with a separator ("/").
|
|
ErrInvalidStart pointerError = `JSON pointer must be empty or start with a "` + pointerSeparator + `"`
|
|
|
|
// ErrUnsupportedValueType indicates that a value of the wrong type is being set.
|
|
ErrUnsupportedValueType pointerError = "only structs, pointers, maps and slices are supported for setting values"
|
|
|
|
// ErrDashToken indicates use of the RFC 6901 "-" reference token in a context where it cannot be
|
|
// resolved.
|
|
//
|
|
// Per RFC 6901 §4 the "-" token refers to the (nonexistent) element after the last array element.
|
|
// It may only be used as the terminal token of a [Pointer.Set] against a slice, where it means
|
|
// "append".
|
|
//
|
|
// Any other use (get, offset, intermediate traversal, non-slice target) is an error condition that
|
|
// wraps this sentinel.
|
|
ErrDashToken pointerError = `the "-" array token cannot be resolved here` //nolint:gosec // G101 false positive: this is a JSON Pointer reference token, not a credential.
|
|
)
|
|
|
|
const dashToken = "-"
|
|
|
|
func errNoKey(key string) error {
|
|
return fmt.Errorf("object has no key %q: %w", key, ErrPointer)
|
|
}
|
|
|
|
func errOutOfBounds(length, idx int) error {
|
|
return fmt.Errorf("index out of bounds array[0,%d] index '%d': %w", length-1, idx, ErrPointer)
|
|
}
|
|
|
|
func errInvalidReference(token string) error {
|
|
return fmt.Errorf("invalid token reference %q: %w", token, ErrPointer)
|
|
}
|
|
|
|
func errDashOnGet() error {
|
|
return fmt.Errorf("cannot resolve %q token on get: %w: %w", dashToken, ErrDashToken, ErrPointer)
|
|
}
|
|
|
|
func errDashIntermediate() error {
|
|
return fmt.Errorf("the %q token may only appear as the terminal token of a pointer: %w: %w", dashToken, ErrDashToken, ErrPointer)
|
|
}
|
|
|
|
func errDashOnOffset() error {
|
|
return fmt.Errorf("cannot compute offset for %q token (nonexistent element): %w: %w", dashToken, ErrDashToken, ErrPointer)
|
|
}
|