typed

package
v4.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: Apache-2.0 Imports: 8 Imported by: 38

Documentation

Overview

Package typed contains logic for operating on values with given schemas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReconcileFieldSetWithSchema added in v4.0.2

func ReconcileFieldSetWithSchema(fieldset *fieldpath.Set, tv *TypedValue) (*fieldpath.Set, error)

ReconcileFieldSetWithSchema reconciles the a field set with any changes to the object's schema since the field set was written. Returns the reconciled field set, or nil of no changes were made to the field set.

Supports: - changing types from atomic to granular - changing types from granular to atomic

Types

type Comparison

type Comparison struct {
	// Removed contains any fields removed by rhs (the right-hand-side
	// object in the comparison).
	Removed *fieldpath.Set
	// Modified contains fields present in both objects but different.
	Modified *fieldpath.Set
	// Added contains any fields added by rhs.
	Added *fieldpath.Set
}

Comparison is the return value of a TypedValue.Compare() operation.

No field will appear in more than one of the three fieldsets. If all of the fieldsets are empty, then the objects must have been equal.

func (*Comparison) ExcludeFields

func (c *Comparison) ExcludeFields(fields *fieldpath.Set) *Comparison

ExcludeFields fields from the compare recursively removes the fields from the entire comparison

func (*Comparison) FilterFields added in v4.4.2

func (c *Comparison) FilterFields(filter fieldpath.Filter) *Comparison

func (*Comparison) IsSame

func (c *Comparison) IsSame() bool

IsSame returns true if the comparison returned no changes (the two compared objects are similar).

func (*Comparison) String

func (c *Comparison) String() string

String returns a human readable version of the comparison.

type ExtractItemsOption added in v4.5.0

type ExtractItemsOption func(*extractItemsOptions)

func WithAppendKeyFields added in v4.5.0

func WithAppendKeyFields() ExtractItemsOption

WithAppendKeyFields configures ExtractItems to include key fields. It is exported for use in configuring ExtractItems.

type ParseableType

type ParseableType struct {
	TypeRef schema.TypeRef
	Schema  *schema.Schema
}

ParseableType allows for easy production of typed objects.

var DeducedParseableType ParseableType = createOrDie(YAMLObject(`types:
- name: __untyped_atomic_
  scalar: untyped
  list:
    elementType:
      namedType: __untyped_atomic_
    elementRelationship: atomic
  map:
    elementType:
      namedType: __untyped_atomic_
    elementRelationship: atomic
- name: __untyped_deduced_
  scalar: untyped
  list:
    elementType:
      namedType: __untyped_atomic_
    elementRelationship: atomic
  map:
    elementType:
      namedType: __untyped_deduced_
    elementRelationship: separable
`)).Type("__untyped_deduced_")

DeducedParseableType is a ParseableType that deduces the type from the content of the object.

func (ParseableType) FromStructured

func (p ParseableType) FromStructured(in interface{}, opts ...ValidationOptions) (*TypedValue, error)

FromStructured converts a go "interface{}" type, typically an structured object in Kubernetes, to a TypedValue. It will return an error if the resulting object fails schema validation. The provided "interface{}" value must be a pointer so that the value can be modified via reflection. The provided "interface{}" may contain structs and types that are converted to Values by the jsonMarshaler interface.

func (ParseableType) FromUnstructured

func (p ParseableType) FromUnstructured(in interface{}, opts ...ValidationOptions) (*TypedValue, error)

FromUnstructured converts a go "interface{}" type, typically an unstructured object in Kubernetes world, to a TypedValue. It returns an error if the resulting object fails schema validation. The provided interface{} must be one of: map[string]interface{}, map[interface{}]interface{}, []interface{}, int types, float types, string or boolean. Nested interface{} must also be one of these types.

func (ParseableType) FromYAML

func (p ParseableType) FromYAML(object YAMLObject, opts ...ValidationOptions) (*TypedValue, error)

FromYAML parses a yaml string into an object with the current schema and the type "typename" or an error if validation fails.

func (ParseableType) IsValid

func (p ParseableType) IsValid() bool

IsValid return true if p's schema and typename are valid.

type Parser

type Parser struct {
	Schema schema.Schema
}

Parser implements YAMLParser and allows introspecting the schema.

func NewParser

func NewParser(schema YAMLObject) (*Parser, error)

NewParser will build a YAMLParser from a schema. The schema is validated.

func (*Parser) Type

func (p *Parser) Type(name string) ParseableType

Type returns a helper which can produce objects of the given type. Any errors are deferred until a further function is called.

func (*Parser) TypeNames

func (p *Parser) TypeNames() (names []string)

TypeNames returns a list of types this parser understands.

type TypedValue

type TypedValue struct {
	// contains filtered or unexported fields
}

TypedValue is a value of some specific type.

func AsTyped

func AsTyped(v value.Value, s *schema.Schema, typeRef schema.TypeRef, opts ...ValidationOptions) (*TypedValue, error)

AsTyped accepts a value and a type and returns a TypedValue. 'v' must have type 'typeName' in the schema. An error is returned if the v doesn't conform to the schema.

func AsTypedUnvalidated deprecated

func AsTypedUnvalidated(v value.Value, s *schema.Schema, typeRef schema.TypeRef) *TypedValue

AsTypeUnvalidated is just like AsTyped, but doesn't validate that the type conforms to the schema, for cases where that has already been checked or where you're going to call a method that validates as a side-effect (like ToFieldSet).

Deprecated: This function was initially created because validation was expensive. Now that this has been solved, objects should always be created as validated, using `AsTyped`.

func (TypedValue) AsValue

func (tv TypedValue) AsValue() value.Value

AsValue removes the type from the TypedValue and only keeps the value.

func (TypedValue) Compare

func (tv TypedValue) Compare(rhs *TypedValue) (c *Comparison, err error)

Compare compares the two objects. See the comments on the `Comparison` struct for details on the return value.

tv and rhs must both be of the same type (their Schema and TypeRef must match), or an error will be returned. Validation errors will be returned if the objects don't conform to the schema.

func (TypedValue) Empty

func (tv TypedValue) Empty() *TypedValue

func (TypedValue) ExtractItems added in v4.1.0

func (tv TypedValue) ExtractItems(items *fieldpath.Set, opts ...ExtractItemsOption) *TypedValue

ExtractItems returns a value with only the provided list or map items extracted from the value.

func (TypedValue) Merge

func (tv TypedValue) Merge(pso *TypedValue) (*TypedValue, error)

Merge returns the result of merging tv and pso ("partially specified object") together. Of note:

  • No fields can be removed by this operation.
  • If both tv and pso specify a given leaf field, the result will keep pso's value.
  • Container typed elements will have their items ordered: 1. like tv, if pso doesn't change anything in the container 2. like pso, if pso does change something in the container.

tv and pso must both be of the same type (their Schema and TypeRef must match), or an error will be returned. Validation errors will be returned if the objects don't conform to the schema.

func (TypedValue) RemoveItems

func (tv TypedValue) RemoveItems(items *fieldpath.Set) *TypedValue

RemoveItems removes each provided list or map item from the value.

func (TypedValue) Schema added in v4.0.2

func (tv TypedValue) Schema() *schema.Schema

Schema gets the schema from the TypedValue.

func (TypedValue) ToFieldSet

func (tv TypedValue) ToFieldSet() (*fieldpath.Set, error)

ToFieldSet creates a set containing every leaf field and item mentioned, or validation errors, if any were encountered.

func (TypedValue) TypeRef

func (tv TypedValue) TypeRef() schema.TypeRef

TypeRef is the type of the value.

func (TypedValue) Validate

func (tv TypedValue) Validate(opts ...ValidationOptions) error

Validate returns an error with a list of every spec violation.

type ValidationError

type ValidationError struct {
	Path         string
	ErrorMessage string
}

ValidationError reports an error about a particular field

func (ValidationError) Error

func (ve ValidationError) Error() string

Error returns a human readable error message.

type ValidationErrors

type ValidationErrors []ValidationError

ValidationErrors accumulates multiple validation error messages.

func (ValidationErrors) Error

func (errs ValidationErrors) Error() string

Error returns a human readable error message reporting each error in the list.

func (ValidationErrors) WithLazyPrefix

func (errs ValidationErrors) WithLazyPrefix(fn func() string) ValidationErrors

WithLazyPrefix prefixes all errors path with the given pathelement. This is useful when unwinding the stack on errors. Prefix is computed lazily only if there is an error.

func (ValidationErrors) WithPath

func (errs ValidationErrors) WithPath(p string) ValidationErrors

Set the given path to all the validation errors.

func (ValidationErrors) WithPrefix

func (errs ValidationErrors) WithPrefix(prefix string) ValidationErrors

WithPrefix prefixes all errors path with the given pathelement. This is useful when unwinding the stack on errors.

type ValidationOptions added in v4.4.0

type ValidationOptions int

ValidationOptions is the list of all the options available when running the validation.

const (
	// AllowDuplicates means that sets and associative lists can have duplicate similar items.
	AllowDuplicates ValidationOptions = iota
)

type YAMLObject

type YAMLObject string

YAMLObject is an object encoded in YAML.

Jump to

Keyboard shortcuts

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