yamlpatchcommon

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultIndentSpaces configures the yaml encoder used to marshal patched objects.
	DefaultIndentSpaces = 2
)
View Source
var ErrIllegalDocumentAccess = fmt.Errorf("*goyamlDocumentContainer does not allow non-empty key access")

Functions

func ApplyUsingYAMLLibrary

func ApplyUsingYAMLLibrary[NodeT any](yamllib YAMLLibrary[NodeT], originalBytes []byte, patch yamlpatch.Patch) ([]byte, error)

ApplyUsingYAMLLibrary applies the provided patch to a yaml document provided in originalBytes using the provided YAML library and returns the updated content. A best effort is made to minimize changes outside the patched paths but some whitespace changes are unavoidable.

func NewYAMLPatchApplier

func NewYAMLPatchApplier[NodeT any](lib YAMLLibrary[NodeT]) yamlpatch.Patcher

func ParseSeqIndex

func ParseSeqIndex(indexStr string) (int, error)

Types

type YAMLContainer

type YAMLContainer[NodeT any] interface {
	// Get returns the child node at the key index. If the key does not exist, it returns nil, nil.
	Get(key string) (NodeT, error)
	// Set overwrites the key with val. It returns an error if the key does not already exist (or index out of bounds, for sequences).
	Set(key string, val NodeT) error
	// Add adds a new node to the container. It returns an error if the key already exists.
	Add(key string, val NodeT) error
	// Remove removes a node from the container. It returns an error if the key does not exist.
	Remove(key string) error
}

YAMLContainer is an interface to abstract away indexing into sequence (list) and mapping (object) nodes. Keys are strings for compatibility with JSONPatch and JSON map keys. Sequence containers parse integer indices from the string representation.

type YAMLLibrary

type YAMLLibrary[NodeT any] interface {
	// Unmarshal unmarshals the provided YAML bytes into the provided output value.
	Unmarshal(in []byte, out interface{}) (err error)

	// BytesToNode unmarshals the provided YAML bytes into a node. The unmarshalled node will typically be a node that
	// represents a YAML document (a YAML document node), but the specifics depend on the implementation.
	BytesToNode(in []byte) (node NodeT, err error)

	// BytesToContentNode unmarshals the provided YAML bytes into a node. This function is similar to BytesToNode, but
	// if the node returned by the initial unmarshal is a document node, this function will return the content node of
	// the document node (if effectively "unwraps" the document node).
	BytesToContentNode(in []byte) (node NodeT, err error)

	NodeToBytes(node NodeT) (out []byte, err error)

	NodeToValue(node NodeT) (out any, err error)

	ValueToNode(value any, comment string) (NodeT, error)

	SetDocumentNodeContent(documentNode NodeT, valueNode NodeT) error

	NewContainer(node NodeT) (YAMLContainer[NodeT], error)

	CopyNode(node NodeT) (NodeT, error)

	// NodeIsNil returns true if the provided NodeT is nil (or its conceptual equivalent). For most implementations,
	// this will simply be implemented as "return node == nil". However, this function is necessary because the type
	// constraint of "NodeT" is "any", so in generic code "node == nil" / "node == *new(NodeT)" does not compile because
	// "NodeT" is not guaranteed to be comparable or a pointer type.
	// See https://groups.google.com/g/golang-nuts/c/bloyX1Zxjaw, https://github.com/golang/go/issues/61372
	NodeIsNil(node NodeT) bool
}

Jump to

Keyboard shortcuts

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