Documentation
¶
Index ¶
- Variables
- func DotPathToSlice(path string) []string
- func GetRoot(doc *YamlDoc) *yaml.RNode
- func IsCommentKey(key string) bool
- func JSONPointerToPath(path string) string
- func JSONPointerToSlice(path string) ([]string, error)
- func NormalizeYAML(y string) string
- func SetRoot(doc *YamlDoc, node *yaml.RNode)
- func UnescapeDotsInPathSegment(segment string) string
- func YAMLPointerToSlice(path string) ([]string, error)
- func YamlIsEmpty(y string) bool
- type Container
- type ElementRemover
- type YamlDoc
- func New() *YamlDoc
- func NewFromData(data interface{}) (*YamlDoc, error)
- func ParseJSON(y []byte) (*YamlDoc, error)
- func ParseYAML(y []byte) (*YamlDoc, error)
- func ParseYAMLBuffer(buffer io.Reader) (*YamlDoc, error)
- func ParseYAMLFile(path string) (*YamlDoc, error)
- func Wrap(node *yaml.RNode) *YamlDoc
- func (c *YamlDoc) Array(hierarchy ...string) (*YamlDoc, error)
- func (c *YamlDoc) ArrayAppend(value interface{}, hierarchy ...string) error
- func (c *YamlDoc) ArrayAppendP(value interface{}, path string) error
- func (c *YamlDoc) ArrayConcat(value interface{}, hierarchy ...string) error
- func (c *YamlDoc) ArrayConcatP(value interface{}, path string) error
- func (c *YamlDoc) ArrayCount(hierarchy ...string) (int, error)
- func (c *YamlDoc) ArrayCountP(path string) (int, error)
- func (c *YamlDoc) ArrayElement(index int, hierarchy ...string) (*YamlDoc, error)
- func (c *YamlDoc) ArrayElementP(index int, path string) (*YamlDoc, error)
- func (c *YamlDoc) ArrayI(index int) (*YamlDoc, error)
- func (c *YamlDoc) ArrayInsert(value interface{}, index int, hierarchy ...string) error
- func (c *YamlDoc) ArrayInsertP(value interface{}, index int, path string) error
- func (c *YamlDoc) ArrayOfSize(size int, hierarchy ...string) (*YamlDoc, error)
- func (c *YamlDoc) ArrayOfSizeI(size, index int) (*YamlDoc, error)
- func (c *YamlDoc) ArrayOfSizeP(size int, path string) (*YamlDoc, error)
- func (c *YamlDoc) ArrayP(path string) (*YamlDoc, error)
- func (c *YamlDoc) ArrayRemove(index int, hierarchy ...string) error
- func (c *YamlDoc) ArrayRemoveP(index int, path string) error
- func (c *YamlDoc) Bytes() []byte
- func (c *YamlDoc) BytesIndent(indent int) []byte
- func (c *YamlDoc) BytesWithoutCommentKeys() []byte
- func (c *YamlDoc) Children() []*YamlDoc
- func (c *YamlDoc) ChildrenMap() map[string]*YamlDoc
- func (c *YamlDoc) Data() interface{}
- func (c *YamlDoc) DataOrdered() interface{}
- func (c *YamlDoc) Delete(hierarchy ...string) error
- func (c *YamlDoc) DeleteCommentKeysForPath(path string) error
- func (c *YamlDoc) DeleteP(path string) error
- func (c *YamlDoc) Exists(hierarchy ...string) bool
- func (c *YamlDoc) ExistsP(path string) bool
- func (c *YamlDoc) ExtractCommentsToKeys() error
- func (c *YamlDoc) Flatten() (map[string]interface{}, error)
- func (c *YamlDoc) FlattenIncludeEmpty() (map[string]interface{}, error)
- func (c *YamlDoc) GetCommentKeys(path string) (head, line, foot string)
- func (c *YamlDoc) GetComments() string
- func (c *YamlDoc) GetDocComment() (string, bool)
- func (c *YamlDoc) Index(index int) *YamlDoc
- func (c *YamlDoc) InjectCommentsFromKeys() error
- func (c *YamlDoc) IsArray() bool
- func (c *YamlDoc) IsEmptyDoc() bool
- func (c *YamlDoc) JSONPointer(path string) (*YamlDoc, error)
- func (c *YamlDoc) MarshalJSON() ([]byte, error)
- func (c *YamlDoc) MarshalJSONWithoutCommentKeys() ([]byte, error)
- func (c *YamlDoc) MarshalYAML() ([]byte, error)
- func (c *YamlDoc) Merge(source *YamlDoc) error
- func (c *YamlDoc) MergeDoc(source *YamlDoc) error
- func (c *YamlDoc) MergeDocP(source *YamlDoc, path string) error
- func (c *YamlDoc) MergeFn(source *YamlDoc, collisionFn func(destination, source interface{}) interface{}) error
- func (c *YamlDoc) Object(hierarchy ...string) (*YamlDoc, error)
- func (c *YamlDoc) ObjectI(index int) (*YamlDoc, error)
- func (c *YamlDoc) ObjectP(path string) (*YamlDoc, error)
- func (c *YamlDoc) Path(path string) *YamlDoc
- func (c *YamlDoc) S(hierarchy ...string) *YamlDoc
- func (c *YamlDoc) Search(hierarchy ...string) *YamlDoc
- func (c *YamlDoc) Set(value interface{}, hierarchy ...string) (*YamlDoc, error)
- func (c *YamlDoc) SetComment(comment string)
- func (c *YamlDoc) SetCommentKey(path string, commentType string, text string) error
- func (c *YamlDoc) SetDocComment(comment string)
- func (c *YamlDoc) SetDocExpandP(doc *YamlDoc, path string) (*YamlDoc, error)
- func (c *YamlDoc) SetDocP(doc *YamlDoc, path string) (*YamlDoc, error)
- func (c *YamlDoc) SetExpand(value interface{}, hierarchy ...string) (*YamlDoc, error)
- func (c *YamlDoc) SetExpandP(value interface{}, path string) (*YamlDoc, error)
- func (c *YamlDoc) SetIndex(value interface{}, index int) (*YamlDoc, error)
- func (c *YamlDoc) SetP(value interface{}, path string) (*YamlDoc, error)
- func (c *YamlDoc) SetYAMLPointer(value interface{}, path string) (*YamlDoc, error)
- func (c *YamlDoc) String() string
- func (c *YamlDoc) StringIndent(indent int) string
- func (c *YamlDoc) StripCommentKeys() *YamlDoc
- func (c *YamlDoc) YNode() *yaml.Node
Constants ¶
This section is empty.
Variables ¶
var ( // ErrOutOfBounds indicates an index was out of bounds. ErrOutOfBounds = errors.New("out of bounds") // ErrNotObjOrArray is returned when a target is not an object or array type // but needs to be for the intended operation. ErrNotObjOrArray = errors.New("not an object or array") // ErrNotObj is returned when a target is not an object but needs to be for // the intended operation. ErrNotObj = errors.New("not an object") // ErrInvalidQuery is returned when a search query was not valid. ErrInvalidQuery = errors.New("invalid search query") // ErrNotArray is returned when a target is not an array but needs to be for // the intended operation. ErrNotArray = errors.New("not an array") // ErrPathCollision is returned when creating a path failed because an // element collided with an existing value. ErrPathCollision = errors.New("encountered value collision whilst building path") // ErrInvalidInputObj is returned when the input value was not a // map[string]interface{}. ErrInvalidInputObj = errors.New("invalid input object") // ErrInvalidInputText is returned when the input data could not be parsed. ErrInvalidInputText = errors.New("input text could not be parsed") // ErrNotFound is returned when a query leaf is not found. ErrNotFound = errors.New("field not found") // ErrInvalidPath is returned when the filepath was not valid. ErrInvalidPath = errors.New("invalid file path") // ErrInvalidBuffer is returned when the input buffer contained an invalid // YAML string. ErrInvalidBuffer = errors.New("input buffer contained invalid YAML") )
Error variables similar to gabs
var EmptyDocument = []byte("null")
Functions ¶
func DotPathToSlice ¶
DotPathToSlice returns a slice of path segments parsed out of a dot path.
func IsCommentKey ¶ added in v0.1.25
IsCommentKey returns true if the key starts with the $comment$ prefix and contains a valid comment type (head, line, or foot) followed by "$". Matches keys like "$comment$head$foo" and "$comment$line$" (empty target).
func JSONPointerToPath ¶
func JSONPointerToSlice ¶
JSONPointerToSlice parses a JSON pointer path (https://tools.ietf.org/html/rfc6901) and returns the path segments as a slice.
Because the characters '~' (%x7E) and '/' (%x2F) have special meanings in gabs paths, '~' needs to be encoded as '~0' and '/' needs to be encoded as '~1' when these characters appear in a reference key.
func NormalizeYAML ¶
func UnescapeDotsInPathSegment ¶
UnescapeDotsInPathSegment unescapes tilde-encoded characters in a path segment. Reverses the encoding done by yamlkit.EscapeDotsInPathSegment: - ~1 becomes . (dot) - ~0 becomes ~ (tilde)
func YAMLPointerToSlice ¶
YAMLPointerToSlice parses a YAML pointer path and returns the path segments as a slice.
func YamlIsEmpty ¶
Returns true if YAML doc is trivially empty, even no comments
Types ¶
type ElementRemover ¶
type ElementRemover struct {
Index int
}
type YamlDoc ¶
type YamlDoc struct {
// contains filtered or unexported fields
}
YamlDoc references a specific element within a YAML structure.
func NewFromData ¶ added in v0.1.25
NewFromData creates a YamlDoc from arbitrary Go data. Supports map[string]interface{}, *orderedmap.OrderedMap[string, interface{}], []interface{}, and scalar types.
func ParseYAMLBuffer ¶
ParseYAMLBuffer reads a buffer and unmarshals the contents into a *YamlDoc.
func ParseYAMLFile ¶
ParseYAMLFile reads a file and unmarshals the contents into a *YamlDoc.
func (*YamlDoc) Array ¶
Array creates a new YAML array at a path. Returns an error if the path contains a collision with a non object type.
func (*YamlDoc) ArrayAppend ¶
ArrayAppend attempts to append a value onto a YAML array at a path. If the target is not a YAML array then it will be converted into one, with its original contents set to the first element of the array.
func (*YamlDoc) ArrayAppendP ¶
ArrayAppendP attempts to append a value onto a YAML array at a path using dot notation. If the target is not a YAML array then it will be converted into one, with its original contents set to the first element of the array.
func (*YamlDoc) ArrayConcat ¶
ArrayConcat attempts to append a value onto a YAML array at a path. If the target is not a YAML array then it will be converted into one, with its original contents set to the first element of the array.
ArrayConcat differs from ArrayAppend in that it will expand a value of type []interface{} during the append operation, resulting in concatenation of each element, rather than appending as a single element.
func (*YamlDoc) ArrayConcatP ¶
ArrayConcatP attempts to append a value onto a YAML array at a path using dot notation. If the target is not a YAML array then it will be converted into one, with its original contents set to the first element of the array.
ArrayConcatP differs from ArrayAppendP in that it will expand a value of type []interface{} during the append operation, resulting in concatenation of each element, rather than appending as a single element.
func (*YamlDoc) ArrayCount ¶
ArrayCount counts the number of elements in a YAML array at a path.
func (*YamlDoc) ArrayCountP ¶
ArrayCountP counts the number of elements in a YAML array at a path using dot notation.
func (*YamlDoc) ArrayElement ¶
ArrayElement attempts to access an element by an index from a YAML array at a path.
func (*YamlDoc) ArrayElementP ¶
ArrayElementP attempts to access an element by an index from a YAML array at a path using dot notation.
func (*YamlDoc) ArrayI ¶
ArrayI creates a new YAML array within an array at an index. Returns an error if the element is not an array or the index is out of bounds.
func (*YamlDoc) ArrayInsert ¶
ArrayInsert attempts to insert an element at a specified index into a YAML array at a path.
func (*YamlDoc) ArrayInsertP ¶
ArrayInsertP attempts to insert an element at a specified index into a YAML array at a path using dot notation.
func (*YamlDoc) ArrayOfSize ¶
ArrayOfSize creates a new YAML array of a particular size at a path. Returns an error if the path contains a collision with a non object type.
func (*YamlDoc) ArrayOfSizeI ¶
ArrayOfSizeI creates a new YAML array of a particular size within an array at an index. Returns an error if the element is not an array or the index is out of bounds.
func (*YamlDoc) ArrayOfSizeP ¶
ArrayOfSizeP creates a new YAML array of a particular size at a path using dot notation. Returns an error if the path contains a collision with a non object type.
func (*YamlDoc) ArrayP ¶
ArrayP creates a new YAML array at a path using dot notation. Returns an error if the path contains a collision with a non object type.
func (*YamlDoc) ArrayRemove ¶
ArrayRemove attempts to remove an element identified by an index from a YAML array at a path.
func (*YamlDoc) ArrayRemoveP ¶
ArrayRemoveP attempts to remove an element identified by an index from a YAML array at a path using dot notation.
func (*YamlDoc) BytesIndent ¶
BytesIndent marshals an element to a YAML []byte blob formatted with a specified indent. Since YAML inherently supports indentation, this function allows you to set the indentation level.
func (*YamlDoc) BytesWithoutCommentKeys ¶ added in v0.1.25
BytesWithoutCommentKeys returns the YAML bytes of this document with all $comment$ keys removed.
func (*YamlDoc) Children ¶
Children returns a slice of all children of an array element. This also works for objects; however, the children returned for an object will be in a random order, and you lose the names of the returned objects this way. If the underlying container value isn't an array or map, nil is returned.
func (*YamlDoc) ChildrenMap ¶
ChildrenMap returns a map of all the children of an object element. If the underlying value isn't an object then an empty map is returned.
func (*YamlDoc) Data ¶
func (c *YamlDoc) Data() interface{}
Data returns the underlying node of the target element in the YAML structure.
func (*YamlDoc) DataOrdered ¶ added in v0.1.25
func (c *YamlDoc) DataOrdered() interface{}
DataOrdered returns the underlying data using *orderedmap.OrderedMap[string, interface{}] for mapping nodes, preserving the key order from the YAML source.
func (*YamlDoc) DeleteCommentKeysForPath ¶ added in v0.1.25
DeleteCommentKeysForPath removes all $comment$ sibling keys associated with the specified data path.
func (*YamlDoc) ExtractCommentsToKeys ¶ added in v0.1.25
ExtractCommentsToKeys walks the YAML document tree and converts native HeadComment/LineComment/FootComment on nodes into $comment$ sibling keys in the parent mapping. Clears the original node comments.
func (*YamlDoc) Flatten ¶
Flatten a YAML array or object into an object of key/value pairs for each field, where the key is the full path of the structured field in dot path notation matching the spec for the method Path.
Returns an error if the target is not a YAML object or array.
func (*YamlDoc) FlattenIncludeEmpty ¶
FlattenIncludeEmpty a YAML array or object into an object of key/value pairs for each field, just as Flatten, but includes empty arrays and objects, where the key is the full path of the structured field in dot path notation matching the spec for the method Path.
Returns an error if the target is not a YAML object or array.
func (*YamlDoc) GetCommentKeys ¶ added in v0.1.25
GetCommentKeys returns all $comment$ keys and their values from the parent map of the specified path. The path identifies a data key; this method returns any $comment$head$KEY, $comment$line$KEY, $comment$foot$KEY siblings.
func (*YamlDoc) GetComments ¶
func (*YamlDoc) GetDocComment ¶ added in v0.1.25
GetDocComment returns the document-level header comment and whether it exists.
func (*YamlDoc) Index ¶
Index attempts to find and return an element within a YAML array by an index.
func (*YamlDoc) InjectCommentsFromKeys ¶ added in v0.1.25
InjectCommentsFromKeys walks the YAML document and converts $comment$ sibling keys back into HeadComment/LineComment/FootComment on the corresponding data nodes. Removes the $comment$ keys from the document.
func (*YamlDoc) IsEmptyDoc ¶
func (*YamlDoc) MarshalJSON ¶
MarshalJSON returns the JSON encoding of this container.
func (*YamlDoc) MarshalJSONWithoutCommentKeys ¶ added in v0.1.25
MarshalJSONWithoutCommentKeys returns the JSON encoding of this document with all $comment$ keys removed.
func (*YamlDoc) MarshalYAML ¶
MarshalYAML returns the YAML encoding of this container.
func (*YamlDoc) Merge ¶
Merge merges a source object into an existing destination object. When a collision is found within the merged structures (both a source and destination object contain the same non-object keys), the result will be a sequence containing both values, where values that are already sequences will be expanded into the resulting sequence.
It is possible to merge structures with different collision behaviours with MergeFn.
func (*YamlDoc) MergeDoc ¶
MergeDoc merges a source document into the destination document using kyaml's merge2 strategy. This merge preserves comments and follows Kubernetes strategic merge patch semantics. Unlike Merge, MergeDoc uses kustomize's merge2 implementation which better handles YAML metadata like comments, anchors, and preserves field order where possible.
func (*YamlDoc) MergeDocP ¶
MergeDocP merges a source document into the destination at a specific path using dot notation. The path specifies where in the destination document the merge should occur.
func (*YamlDoc) MergeFn ¶
func (c *YamlDoc) MergeFn(source *YamlDoc, collisionFn func(destination, source interface{}) interface{}) error
MergeFn merges two objects using a provided function to resolve collisions.
The collision function receives two interface{} arguments, destination (the original object) and source (the object being merged into the destination). Whichever value is returned becomes the new value in the destination object at the location of the collision.
func (*YamlDoc) Object ¶
Object creates a new YAML object at a target path. Returns an error if the path contains a collision with a non object type.
func (*YamlDoc) ObjectI ¶
ObjectI creates a new YAML object at an array index. Returns an error if the object is not an array or the index is out of bounds.
func (*YamlDoc) ObjectP ¶
ObjectP creates a new YAML object at a target path using dot notation. Returns an error if the path contains a collision with a non object type.
func (*YamlDoc) Path ¶
Path searches the YAML structure following a path in dot notation, segments of this path are searched according to the same rules as Search.
func (*YamlDoc) Set ¶
Set attempts to set the value of a field located by a hierarchy of field names.
func (*YamlDoc) SetComment ¶
func (*YamlDoc) SetCommentKey ¶ added in v0.1.25
SetCommentKey sets a $comment$ sibling key for the specified data path. commentType should be "head", "line", or "foot".
func (*YamlDoc) SetDocComment ¶ added in v0.1.25
SetDocComment sets the document-level header comment key ($comment$head$) on a document.
func (*YamlDoc) SetDocExpandP ¶ added in v0.1.25
SetDocExpandP is like SetDocP but expands arrays on demand.
func (*YamlDoc) SetDocP ¶
SetDocP sets the value of a field to a YamlDoc at a path using dot notation.
func (*YamlDoc) SetExpand ¶ added in v0.1.25
SetExpand is like Set but expands arrays on demand when an index is at or beyond the current length. Missing elements up to the target index are filled with null nodes.
func (*YamlDoc) SetExpandP ¶ added in v0.1.25
SetExpandP is like SetP but expands arrays on demand.
func (*YamlDoc) SetYAMLPointer ¶
SetYAMLPointer parses a YAML pointer path and sets the leaf to a value. Returns an error if the pointer could not be resolved due to missing fields.
func (*YamlDoc) StringIndent ¶
StringIndent marshals an element to a YAML string formatted with indents.
func (*YamlDoc) StripCommentKeys ¶ added in v0.1.25
StripCommentKeys returns a deep copy of this document with all $comment$ keys removed from mapping nodes. Useful before JSON schema validation or other operations that don't understand comment keys.