model

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeAndValidate

func DecodeAndValidate(r io.Reader, filename string, outPtr Validator) error

DecodeAndValidate unmarshals the YAML text in the given Reader into the given pointer-to-struct, and calls Validate() on it. Returns any unmarshaling error or validation error.

func IsKnownAPIVersion

func IsKnownAPIVersion(parentPos *ConfigPos, apiVersion String, fieldName string) error

IsKnownAPIVersion returns error if the given string is not one of the accepted abc schema versions.

parentPos is the position of the yaml object that contains the api_version field. We need this because if the api_version field is missing from the YAML, then we won't have any position information for it.

func IsValidRegexGroupName

func IsValidRegexGroupName(s String, fieldName string) error

IsValidRegexGroupName returns whether the given string will be accepted by the regexp library as an RE2 group name.

func NonEmptySlice

func NonEmptySlice[T any](pos *ConfigPos, s []T, fieldName string) error

NonEmptySlice returns error if the given slice is empty.

func NotZero

func NotZero[T comparable](pos *ConfigPos, t T, fieldName string) error

NotZero returns error if the given value is equal to the zero value for type T.

func NotZeroModel

func NotZeroModel[T comparable](pos *ConfigPos, x valWithPos[T], fieldName string) error

NotZeroModel returns error if the given model's value is equal to the zero value for type T.

func OneOf

func OneOf[T comparable](parentPos *ConfigPos, x valWithPos[T], allowed []T, fieldName string) error

OneOf returns error if x.Val is not one of the given allowed choices.

func UnmarshalPlain

func UnmarshalPlain(n *yaml.Node, outPtr any, outPos *ConfigPos, extraYAMLFields ...string) error

UnmarshalPlain unmarshals the yaml node n into the struct pointer outPtr, as if it did not have an UnmarshalYAML method. This lets you still use the default unmarshaling logic to populate the fields of your struct, while adding custom logic before and after.

outPtr must be a pointer to a struct which will be modified by this function.

pos will be modified by this function to contain the position of this yaml node within the input file.

The `yaml:"..."` tags of the outPtr struct are used to determine the set of valid fields. Unexpected fields in the yaml are treated as an error. To allow extra yaml fields that don't correspond to a field of outPtr, provide their names in extraYAMLFields. This allows some fields to be handled specially.

func ValidateEach

func ValidateEach[T Validator](s []T) error

ValidateEach calls Validate() on each element of the input and returns all errors encountered.

func ValidateUnlessNil

func ValidateUnlessNil(v Validator) error

ValidateUnlessNil is intended to be used in a model Validate() method. Semantically it means "if this model field is present (non-nil), then validate it. If not present, then skip validation." This is useful for polymorphic models like Step that have many possible child types, only one of which will be set.

Types

type Bool

type Bool = valWithPos[bool]

Bool represents a boolean field in a model, together with its location in the input file.

type ConfigPos

type ConfigPos struct {
	Line   int
	Column int
}

ConfigPos stores the position of an config value so error messages post-validation can point to problems. The zero value means "position unknown or there is no position."

This is theoretically agnostic to input format; we could decide to support alternative serialization formats besides YAML in the future.

func YAMLPos

func YAMLPos(n *yaml.Node) *ConfigPos

YAMLPos constructs a position struct based on a YAML parse cursor.

func (*ConfigPos) Errorf

func (c *ConfigPos) Errorf(fmtStr string, args ...any) error

Errorf returns a error prepended with spec.yaml position information, if available.

Examples:

Wrapping an error: c.Errorf("foo(): %w", err)

Creating an error: c.Errorf("something went wrong doing action %s", action)

func (ConfigPos) IsZero

func (c ConfigPos) IsZero() bool

type Int

type Int = valWithPos[int]

Int represents an integer field in a model, together with its location in the input file.

type String

type String = valWithPos[string]

String represents a string field in a model, together with its location in the input file.

type Validator

type Validator interface {
	Validate() error
}

Validator is any model struct that has a validate method. It's useful for "higher order" validation functions like "validate each entry in a list".

Directories

Path Synopsis
Package model contains the structs for unmarshaled YAML files.
Package model contains the structs for unmarshaled YAML files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL