yaml

package
v1.160.1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package yaml mirrors the repository's JSON helper shape for YAML data.

The package converts YAML input to JSON before delegating to the JSON helpers, which keeps one implementation of the fast, non-reflective JSON decoding paths and their context-aware stream handling. On the write path, it encodes to JSON first, then converts that JSON to YAML.

YAML parsing support comes from sigs.k8s.io/yaml, which uses yaml/go-yaml underneath. In practice that means the package supports most of YAML 1.2 while preserving some YAML 1.1 behaviour for compatibility, including YAML aliases and anchors, as documented at https://github.com/yaml/go-yaml#compatibility.

Index

Constants

This section is empty.

Variables

View Source
var (
	// YAMLExtensions is the list of file extensions that are considered YAML files.
	YAMLExtensions = []string{".yaml", ".yml"}
)

Functions

func IsYAMLFile added in v1.159.0

func IsYAMLFile(extension string) bool

IsYAMLFile returns true if the given extension is a YAML file extension.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal encodes a value to a YAML byte slice. It follows the same helper shape as the repository's JSON `Marshal` helper.

Values are first encoded through the JSON helpers so any generated fast JSON serialisers can still be used, then the resulting JSON is converted to YAML.

func MarshalWithContext

func MarshalWithContext(ctx context.Context, v any) (content []byte, err error)

MarshalWithContext encodes a value to a YAML byte slice using a context-aware Encoder. It follows the same helper shape as Marshal, but routes the write through the package's context-aware streaming helpers.

func ToJSON

func ToJSON(yaml []byte) (json []byte, err error)

ToJSON converts YAML data to JSON.

YAML parsing support comes from sigs.k8s.io/yaml, which uses yaml/go-yaml underneath. In practice that means it supports most of YAML 1.2 while preserving some YAML 1.1 behaviour for compatibility, as documented at https://github.com/yaml/go-yaml#compatibility. That includes support for YAML aliases and anchors. See also https://github.com/yaml/go-yaml.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal decodes a YAML byte slice into a destination value. It follows the same helper shape as the repository's JSON `Unmarshal` helper.

YAML is converted to JSON first, so the JSON helpers can retain their fast- generated decoding paths and shared decoding behaviour.

func UnmarshallWithContext

func UnmarshallWithContext(ctx context.Context, data []byte, v any) error

UnmarshallWithContext decodes a YAML byte slice into a destination value using a context-aware Decoder. It follows the same helper shape as Unmarshal, but routes the read through the package's context-aware streaming helpers.

Types

type Decoder

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

Decoder reads YAML from a reader and decodes it into Go values. It follows the same helper shape as the repository's JSON `Decoder`.

Reads are context-aware, and the input is converted to JSON before the JSON helpers are used, so the repository keeps a single fast decoding path. This should not be used by more than one goroutine at a time.

func NewDecoder

func NewDecoder(ctx context.Context, r io.Reader) *Decoder

NewDecoder creates a Decoder that reads YAML values from r. It follows the same helper shape as the repository's JSON `NewDecoder` helper.

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

Decode reads YAML from the decoder and stores the result in v. It follows the same helper shape as the repository's JSON `Decoder.Decode` method.

The data is converted to JSON first, then passed to the JSON helpers, so the same fast and implementation-agnostic decoding logic is reused here.

type Encoder

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

Encoder writes YAML values to a writer. It follows the same helper shape as the repository's JSON `Encoder`.

Writes are context-aware. Values are first encoded through the JSON helpers so any generated fast JSON serialisers can still be used before converting the JSON output to YAML. This should not be used by more than one goroutine at a time.

func NewEncoder

func NewEncoder(ctx context.Context, w io.Writer) *Encoder

NewEncoder creates an Encoder that writes YAML values to w. It follows the same helper shape as the repository's JSON `NewEncoder` helper.

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode writes v as YAML to the encoder's writer. It follows the same helper shape as the repository's JSON `Encoder.Encode` method.

The value is first encoded through the JSON helpers, then the JSON output is converted to YAML before it is written.

Directories

Path Synopsis
yamltest

Jump to

Keyboard shortcuts

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