...

Package typed

import "sigs.k8s.io/structured-merge-diff/v4/typed"
Overview
Index

Overview ▾

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

Index ▾

func ReconcileFieldSetWithSchema(fieldset *fieldpath.Set, tv *TypedValue) (*fieldpath.Set, error)
type Comparison
    func (c *Comparison) ExcludeFields(fields *fieldpath.Set) *Comparison
    func (c *Comparison) IsSame() bool
    func (c *Comparison) String() string
type ParseableType
    func (p ParseableType) FromStructured(in interface{}, opts ...ValidationOptions) (*TypedValue, error)
    func (p ParseableType) FromUnstructured(in interface{}, opts ...ValidationOptions) (*TypedValue, error)
    func (p ParseableType) FromYAML(object YAMLObject, opts ...ValidationOptions) (*TypedValue, error)
    func (p ParseableType) IsValid() bool
type Parser
    func NewParser(schema YAMLObject) (*Parser, error)
    func (p *Parser) Type(name string) ParseableType
    func (p *Parser) TypeNames() (names []string)
type TypedValue
    func AsTyped(v value.Value, s *schema.Schema, typeRef schema.TypeRef, opts ...ValidationOptions) (*TypedValue, error)
    func AsTypedUnvalidated(v value.Value, s *schema.Schema, typeRef schema.TypeRef) *TypedValue
    func (tv TypedValue) AsValue() value.Value
    func (tv TypedValue) Compare(rhs *TypedValue) (c *Comparison, err error)
    func (tv TypedValue) Empty() *TypedValue
    func (tv TypedValue) ExtractItems(items *fieldpath.Set) *TypedValue
    func (tv TypedValue) Merge(pso *TypedValue) (*TypedValue, error)
    func (tv TypedValue) RemoveItems(items *fieldpath.Set) *TypedValue
    func (tv TypedValue) Schema() *schema.Schema
    func (tv TypedValue) ToFieldSet() (*fieldpath.Set, error)
    func (tv TypedValue) TypeRef() schema.TypeRef
    func (tv TypedValue) Validate(opts ...ValidationOptions) error
type ValidationError
    func (ve ValidationError) Error() string
type ValidationErrors
    func (errs ValidationErrors) Error() string
    func (errs ValidationErrors) WithLazyPrefix(fn func() string) ValidationErrors
    func (errs ValidationErrors) WithPath(p string) ValidationErrors
    func (errs ValidationErrors) WithPrefix(prefix string) ValidationErrors
type ValidationOptions
type YAMLObject

Package files

compare.go doc.go helpers.go merge.go parser.go reconcile_schema.go remove.go tofieldset.go typed.go validate.go

func ReconcileFieldSetWithSchema

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

type Comparison

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.

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
}

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) 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 ParseableType

ParseableType allows for easy production of typed objects.

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

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

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_")

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

Parser implements YAMLParser and allows introspecting the schema.

type Parser struct {
    Schema schema.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

TypedValue is a value of some specific type.

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

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

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

func (tv TypedValue) ExtractItems(items *fieldpath.Set) *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:

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

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

ValidationError reports an error about a particular field

type ValidationError struct {
    Path         string
    ErrorMessage string
}

func (ValidationError) Error

func (ve ValidationError) Error() string

Error returns a human readable error message.

type ValidationErrors

ValidationErrors accumulates multiple validation error messages.

type ValidationErrors []ValidationError

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

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

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

type YAMLObject

YAMLObject is an object encoded in YAML.

type YAMLObject string