yaml

package
v1.223.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package yaml provides YAML parsing, caching, and utility functions for Atmos.

This package contains YAML-specific functionality including:

  • Parsing and unmarshaling YAML with custom tag processing
  • Content-aware caching for parsed YAML documents
  • Position tracking for provenance
  • Output formatting and highlighting

The custom tag processing uses the function registry from pkg/function to handle tags like !env, !exec, !terraform.output, etc.

Example usage:

data, err := yaml.UnmarshalYAML[map[string]any](content)
if err != nil {
    return err
}

Index

Constants

View Source
const (
	TypeString = "string"
	TypeInt    = "int"
	TypeBool   = "bool"
	TypeFloat  = "float"
	TypeNull   = "null"
	// TypeYAML treats the value as a raw YAML/yq literal (e.g. `[1,2,3]`,
	// `{a: 1}`, or an unquoted scalar), inserted verbatim.
	TypeYAML = "yaml"
)

Value type names accepted by SetWithType / SetFileWithType. These let CLI callers coerce a string argument into a typed YAML scalar (or a raw YAML literal) instead of always writing a quoted string.

View Source
const DefaultIndent = 2

DefaultIndent is the default indentation for YAML output.

Variables

View Source
var (
	// ErrNilAtmosConfig is returned when atmosConfig is nil.
	ErrNilAtmosConfig = errors.New("atmosConfig cannot be nil")

	// ErrIncludeInvalidArguments is returned when !include has invalid arguments.
	ErrIncludeInvalidArguments = errors.New("invalid number of arguments in the !include function")

	// ErrIncludeFileNotFound is returned when !include references a non-existent file.
	ErrIncludeFileNotFound = errors.New("the !include function references a file that does not exist")

	// ErrIncludeAbsPath is returned when converting to absolute path fails.
	ErrIncludeAbsPath = errors.New("failed to convert the file path to an absolute path in the !include function")

	// ErrIncludeProcessFailed is returned when processing stack manifest fails.
	ErrIncludeProcessFailed = errors.New("failed to process the stack manifest with the !include function")

	// ErrInvalidYAMLFunction is returned when a YAML function has invalid syntax.
	ErrInvalidYAMLFunction = errors.New("invalid Atmos YAML function")

	// ErrInvalidYAMLExpression is returned when a dot-path or yq expression cannot be parsed or evaluated.
	ErrInvalidYAMLExpression = errors.New("invalid YAML path or expression")

	// ErrYAMLPathNotFound is returned when a requested path does not exist in the document.
	ErrYAMLPathNotFound = errors.New("YAML path not found")

	// ErrYAMLUpdateFailed is returned when an edit operation fails to produce a valid document.
	ErrYAMLUpdateFailed = errors.New("failed to update YAML")

	// ErrYAMLAnchorAltered is returned when an edit would alter or expand a YAML anchor or alias,
	// which the strict editing contract forbids.
	ErrYAMLAnchorAltered = errors.New("edit would alter or expand a YAML anchor or alias")

	// ErrYAMLDuplicateAnchor is returned when a document defines the same anchor name more than
	// once. Aliases before and after the redefinition resolve to different values, so the anchor
	// guard cannot safely verify an edit; the duplicates must be renamed first.
	ErrYAMLDuplicateAnchor = errors.New("duplicate YAML anchor definition")

	// ErrYAMLMultiDocUnsupported is returned when the editor is given a stream containing more
	// than one YAML document. Edits would silently apply to every document, so multi-document
	// files are rejected explicitly.
	ErrYAMLMultiDocUnsupported = errors.New("multi-document YAML streams are not supported by the YAML editor")

	// ErrParseYAML is returned when YAML content cannot be parsed.
	ErrParseYAML = errors.New("failed to parse YAML")

	// ErrReadFile is returned when a file cannot be read.
	ErrReadFile = errors.New("failed to read file")

	// ErrWriteFile is returned when a file cannot be written.
	ErrWriteFile = errors.New("failed to write file")
)

Functions

func ConvertToYAMLPreservingDelimiters added in v1.208.0

func ConvertToYAMLPreservingDelimiters(data any, delimiters []string, opts ...u.YAMLOptions) (string, error)

ConvertToYAMLPreservingDelimiters converts data to YAML while ensuring that custom template delimiter characters are preserved literally in the output. When custom delimiters contain single-quote characters, the default yaml.v3 encoder may use single-quoted style for certain values (like those starting with '!'), which escapes internal single quotes by doubling them. This breaks template processing because the delimiter pattern is altered. This function forces double-quoted YAML style for affected scalar values to preserve delimiters.

func Delete added in v1.223.0

func Delete(content []byte, path string) ([]byte, error)

Delete removes the value at a dot-notation (or raw yq) path via yq's `del`. Deleting from an empty document is a no-op, matching yq's del semantics for a missing path.

func DeleteFile added in v1.223.0

func DeleteFile(filePath, path string) (bool, error)

DeleteFile removes the value at path in a YAML file. It returns whether a value was present and removed, letting callers distinguish an actual deletion from a no-op when the path was already absent.

func DelimiterConflictsWithYAMLQuoting added in v1.208.0

func DelimiterConflictsWithYAMLQuoting(delimiters []string) bool

DelimiterConflictsWithYAMLQuoting checks if any custom delimiter contains a single-quote character that would conflict with YAML's single-quoted string escaping.

func DisplayPath added in v1.223.0

func DisplayPath(file string) string

DisplayPath returns file relative to the current working directory for user-facing display, falling back to the absolute path if the relative form can't be computed (e.g. a different volume on Windows). Callers still read/write the original path passed in; this only affects what's echoed back to the user.

func DotPathToYqPath added in v1.223.0

func DotPathToYqPath(dotPath string) (string, error)

DotPathToYqPath converts a user-facing dot-notation path into a yq path expression. Dot-notation is the default addressing syntax for the Atmos config/stack/vendor editors.

Examples:

vars.region                       -> .vars.region
sources[0].version                -> .sources[0].version
components.terraform.vpc.vars.cidr -> .components.terraform.vpc.vars.cidr
metadata."weird.key"              -> .metadata.["weird.key"]

Keys that are not simple identifiers are emitted using yq's quoted `.["..."]` form so embedded dots and symbols are preserved literally.

func EnsureDoubleQuotedForDelimiterSafety added in v1.208.0

func EnsureDoubleQuotedForDelimiterSafety(node *goyaml.Node)

EnsureDoubleQuotedForDelimiterSafety recursively walks a yaml.Node tree and forces double-quoted style for scalar nodes whose values contain single-quote characters. This prevents YAML's single-quote escaping (two consecutive single quotes) from interfering with template delimiters that contain single quotes.

func Eval added in v1.223.0

func Eval(content []byte, expr string) ([]byte, error)

Eval evaluates an arbitrary yq expression against raw YAML bytes and returns the modified document. It is the escape hatch for power users who want full yq syntax; the strict anchor guard still applies so a raw expression cannot silently rewrite shared anchors.

func EvalFile added in v1.223.0

func EvalFile(filePath, expr string) error

EvalFile evaluates a yq expression against a file and writes the result back atomically, applying the strict anchor guard.

func Format added in v1.223.0

func Format(content []byte) ([]byte, error)

Format normalizes YAML formatting (indentation, spacing) by round-tripping the document through the editor's encoder with an identity expression, while preserving comments and anchors. This powers a `format`/`fmt` capability.

func FormatFile added in v1.223.0

func FormatFile(filePath string) error

FormatFile normalizes a YAML file's formatting in place.

func Get added in v1.223.0

func Get(content []byte, path string) (string, error)

Get reads the value at a dot-notation (or raw yq) path and returns it as a trimmed string. Missing or null paths return ErrYAMLPathNotFound.

func GetFile added in v1.223.0

func GetFile(filePath, path string) (string, error)

GetFile reads a YAML file and returns the value at path.

func GetTyped added in v1.223.0

func GetTyped[T any](content []byte, path string) (T, error)

GetTyped reads the value at a path and decodes it into T.

func HasPosition

func HasPosition(positions PositionMap, path string) bool

HasPosition checks if a position exists for a specific JSONPath.

func Query added in v1.223.0

func Query(content []byte, expr string) (string, error)

Query evaluates a read-only yq expression (e.g. a select() filter) against raw YAML bytes and returns the trimmed result. Unlike Get, it does not treat the input as a dot-path, so callers can use full yq syntax. A "null" result is reported as ErrYAMLPathNotFound.

func QueryFile added in v1.223.0

func QueryFile(filePath, expr string) (string, error)

QueryFile evaluates a read-only yq expression against a file.

func QuotePathSegment added in v1.223.0

func QuotePathSegment(key string) string

QuotePathSegment renders a literal map key as a dot-path segment, quoting it as "key" when it is not a simple identifier so embedded dots and brackets are parsed literally (e.g. a component named "vpc.prod" or "foo[0]") instead of as nested path syntax. Simple identifiers are returned unchanged.

func Set added in v1.223.0

func Set(content []byte, path, value string) ([]byte, error)

Set assigns a string value at a dot-notation (or raw yq) path and returns the modified document, preserving comments/anchors/formatting. The value is written as a YAML string scalar. Use SetRaw for non-string (typed) values.

func SetFile added in v1.223.0

func SetFile(filePath, path, value string) error

SetFile sets a string value at path in a YAML file, writing the result back atomically while preserving the original file mode.

func SetFileRaw added in v1.223.0

func SetFileRaw(filePath, path, rhs string) error

SetFileRaw sets a raw (typed) value at path in a YAML file.

func SetFileWithType added in v1.223.0

func SetFileWithType(filePath, path, value, valueType string) (bool, error)

SetFileWithType assigns a typed value at path in a YAML file (atomic write). It returns whether the path was newly created (true) as opposed to already existing and being updated (false), letting callers distinguish the two for user-facing messaging.

func SetRaw added in v1.223.0

func SetRaw(content []byte, path, rhs string) ([]byte, error)

SetRaw assigns a raw yq right-hand-side expression at a path. The rhs is inserted verbatim, so callers may pass typed literals (`true`, `42`, `[1,2,3]`) or yq expressions. Comments/anchors/formatting are preserved and the strict anchor guard applies.

func SetWithType added in v1.223.0

func SetWithType(content []byte, path, value, valueType string) ([]byte, error)

SetWithType assigns value at path, coercing it according to valueType, and returns the modified document. See the Type* constants for valid types.

Types

type LongString

type LongString string

LongString is a string type that encodes as a YAML folded scalar (>). This is used to wrap long strings across multiple lines for better readability.

func (LongString) MarshalYAML

func (s LongString) MarshalYAML() (interface{}, error)

MarshalYAML implements yaml.Marshaler to encode as a folded scalar.

type Options

type Options struct {
	Indent int
}

Options configures YAML encoding behavior.

type PathEntry added in v1.223.0

type PathEntry struct {
	Path  string
	Type  string
	Value string
}

PathEntry describes an addressable YAML value discovered while flattening a document into Atmos dot-notation paths.

func ListPathEntries added in v1.223.0

func ListPathEntries(content []byte) ([]PathEntry, error)

ListPathEntries parses a YAML document and returns all addressable child values as sorted Atmos dot-notation paths. Mapping keys are quoted with the same rules used by the config/stack/vendor editors.

type Position

type Position struct {
	Line   int // 1-indexed line number.
	Column int // 1-indexed column number.
}

Position represents a line and column position in a YAML file.

func GetPosition

func GetPosition(positions PositionMap, path string) Position

GetPosition gets the position for a specific JSONPath from the position map. Returns Position{0, 0} if not found.

type PositionMap

type PositionMap map[string]Position

PositionMap maps JSONPath-style paths to their positions in a YAML file.

func ExtractPositions

func ExtractPositions(node *goyaml.Node, enabled bool) PositionMap

ExtractPositions extracts line/column positions from a YAML node tree. Returns a map of JSONPath -> Position for all values in the YAML. If enabled is false, returns an empty map immediately (zero overhead).

Directories

Path Synopsis
Package expand provides YAML key delimiter expansion for yaml.Node trees.
Package expand provides YAML key delimiter expansion for yaml.Node trees.

Jump to

Keyboard shortcuts

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