yml

package
v0.98.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TagLock             = "!lock"
	TagLocal            = "!local"
	TagOrgLocal         = "!org-local"
	TagOrgLocalPlusLock = "!org-local+lock"
)

Variables

View Source
var DiskWriter = writerFunc(func(file *File) error {
	return os.WriteFile(file.Path, file.Yaml, 0o644)
})
View Source
var KnownTags = append(StandardTags, CustomTags...)
View Source
var StandardTags = []string{
	"",
	"!!int",
	"!!bool",
	"!!float",
	"!!map",
	"!!str",
	"!!seq",
	"!!null",
	"!!merge",
}

Functions

func Clone added in v0.20.3

func Clone(node *yaml.Node) *yaml.Node

func Compare

func Compare(yaml1, yaml2 []byte) bool

Compare returns true if the two yaml documents are equivalent in terms of contents and comments, false otherwise, disregarding differences in formatting / whitespace.

func CopyMetadata added in v0.98.0

func CopyMetadata(dst, src *yaml.Node)

CopyMetadata reconciles dst to the authored form of src, restoring presentation that is lost when a tree is rebuilt from JSON (e.g. after applying a JSON Patch). It copies, onto matching nodes:

  • custom YAML tags (those in CustomTags, e.g. !lock),
  • head/line/foot comments,
  • and, for mappings, src's key order (keys present only in dst — e.g. added by a patch — are kept and appended after the src-ordered ones).

It walks both trees in parallel, stopping on any branch where the nodes are nil or differ in kind (i.e. structurally changed relative to src). It never moves data between branches: every dst node is preserved.

Sequence *elements* are not reconciled: a JSON Patch may add/remove/move items, so matching by index is unreliable and would risk copying metadata onto the wrong element. The sequence node's own tag/comments are still restored; per-element metadata inside sequences is a known limitation.

func EqualWithExclusion added in v0.21.0

func EqualWithExclusion(a, b *yaml.Node, excludedPath ...string) bool

func FindNode

func FindNode(node *yaml.Node, path string) (resultNode *yaml.Node, err error)

FindNode traverses a given yaml.Node to locate the value Node of the provided path. The path is interpreted as being relative to the given yaml.Node.

func FindNodeValueOrDefault

func FindNodeValueOrDefault(node *yaml.Node, path string, defaultValue string) string

func GetMappingValueNodesWithTags added in v0.94.0

func GetMappingValueNodesWithTags(node *yaml.Node) (nodes []*yaml.Node)

func HasLockedTodos added in v0.51.0

func HasLockedTodos(node *yaml.Node) bool

func IsLocal added in v0.75.0

func IsLocal(node *yaml.Node) bool

func IsLocked

func IsLocked(node *yaml.Node) bool

func IsOrgLocal added in v0.87.0

func IsOrgLocal(node *yaml.Node) bool

func Merge

func Merge(dst, src *yaml.Node, sameOrg bool) *yaml.Node

func SetOrAddNodeValue

func SetOrAddNodeValue(node *yaml.Node, path string, value string) error

func TreeToYaml

func TreeToYaml(tree *yaml.Node, indent int) (string, error)

func UnmarshalStrict added in v0.54.4

func UnmarshalStrict(data []byte, ptr any) error

Types

type File

type File struct {
	// Path is the path to the yaml file.
	Path string

	// Yaml is the raw yaml of the yaml file.
	Yaml []byte

	// Tree is the root node of the tree representation of the yaml file.
	Tree *yaml.Node

	// ApiVersion is the API version of the CRD, if any.
	ApiVersion string

	// Kind is the kind of the CRD, if any.
	Kind string

	// MetadataName is the name of the CRD resource, if any.
	MetadataName string

	// Indent is the indent size of the yaml file to be used when writing it back to disk.
	Indent int
}

File represents a yaml file loaded into memory in different forms, that can be round-tripped back to disk.

func LoadFile

func LoadFile(filePath string) (*File, error)

func NewFile

func NewFile(filePath string, content []byte) (*File, error)

func NewFileFromObject added in v0.46.2

func NewFileFromObject(filePath string, indent int, obj any) (*File, error)

func NewFileFromTree

func NewFileFromTree(filePath string, indent int, node *yaml.Node) (*File, error)

func (*File) CopyWithNewTree

func (y *File) CopyWithNewTree(newTree *yaml.Node) (*File, error)

func (*File) ToYaml

func (y *File) ToYaml() (string, error)

func (*File) UpdateYamlFromTree

func (y *File) UpdateYamlFromTree() error

type KeyValuePair added in v0.20.2

type KeyValuePair struct {
	Key   *yaml.Node
	Value *yaml.Node
}

func FindNodeKeyPair added in v0.94.0

func FindNodeKeyPair(node *yaml.Node, path string) (keypair *KeyValuePair, err error)

FindNodeKeyPair traverses a given yaml.Node to locate the value Node of the provided path. The path is interpreted as being relative to the given yaml.Node.

type Writer added in v0.39.0

type Writer interface {
	WriteFile(file *File) error
}

type WriterMock added in v0.39.0

type WriterMock struct {
	// WriteFileFunc mocks the WriteFile method.
	WriteFileFunc func(file *File) error
	// contains filtered or unexported fields
}

WriterMock is a mock implementation of Writer.

func TestSomethingThatUsesWriter(t *testing.T) {

	// make and configure a mocked Writer
	mockedWriter := &WriterMock{
		WriteFileFunc: func(file *File) error {
			panic("mock out the WriteFile method")
		},
	}

	// use mockedWriter in code that requires Writer
	// and then make assertions.

}

func (*WriterMock) WriteFile added in v0.39.0

func (mock *WriterMock) WriteFile(file *File) error

WriteFile calls WriteFileFunc.

func (*WriterMock) WriteFileCalls added in v0.39.0

func (mock *WriterMock) WriteFileCalls() []struct {
	File *File
}

WriteFileCalls gets all the calls that were made to WriteFile. Check the length with:

len(mockedWriter.WriteFileCalls())

Jump to

Keyboard shortcuts

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