Documentation ¶
Overview ¶
Package unmarshal provides utilities for unmarshalling YAML data that can represent either a single item or a slice of items. It includes a generic type `SingleOrSlice` to handle this pattern, allowing flexible unmarshalling from YAML input.
The package also provides functions to decode YAML nodes while optionally enforcing that only known fields are present, improving error handling in case of unexpected fields in the YAML input.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeWithOptionalKnownFields ¶
func DecodeWithOptionalKnownFields(value *yaml.Node, out any, useKnownFields bool, input string) error
DecodeWithOptionalKnownFields is a helper function that decodes a YAML node into the provided output (out) interface. It optionally enforces that all fields in the YAML node are known to the target type if useKnownFields is set to true. The input parameter is used for error message formatting.
func UnmarshalSingleOrSlice ¶
UnmarshalSingleOrSlice is a helper function that unmarshals a YAML node into a slice of type T. It handles cases where the node could represent a single item or a list. If the node is a scalar or a mapping, it wraps it in a sequence node. The useKnownFields parameter controls whether unknown fields trigger an error.
Types ¶
type SingleOrSlice ¶
type SingleOrSlice[T any] []T
SingleOrSlice represents a custom type that can unmarshal from YAML as either a single element or a slice of elements. It is a generic type that works for any type T.
func (*SingleOrSlice[T]) UnmarshalYAML ¶
func (ss *SingleOrSlice[T]) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML implements the yaml.Unmarshaler interface for SingleOrSlice. It allows the YAML value to be unmarshaled either as a single element or a slice. The unmarshaled result is assigned to the receiver.