unmarshal

package
v0.0.0-...-af98851 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 4 Imported by: 0

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

func UnmarshalSingleOrSlice[T any](node *yaml.Node, useKnownFields bool) ([]T, error)

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.

Jump to

Keyboard shortcuts

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