csyaml

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 12 Imported by: 3

Documentation

Overview

Package merge implements a deep-merge over multiple YAML documents, preserving key order and rejecting invalid documents.

Maps are deep-merged; sequences and scalars are replaced by later inputs. Type mismatches result in an error.

Adapted from https://github.com/uber-go/config/tree/master/internal/merge

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDocumentKeys

func GetDocumentKeys(r io.Reader) ([][]string, error)

GetDocumentKeys reads all YAML documents from r and for each one returns a slice of its top-level keys, in order.

Non-mapping documents yield an empty slice. Duplicate keys are not allowed and return an error.

func IsEmptyYAML

func IsEmptyYAML(r io.Reader) (bool, error)

IsEmptyYAML reads one or more YAML documents from r and returns true if they are all empty or contain only comments. It will reports errors if the input is not valid YAML.

func Merge

func Merge(inputs [][]byte) (*bytes.Buffer, error)

Merge reads each YAML source in inputs, merges them in order (later sources override earlier), and returns the result as a bytes.Buffer. Always runs in strict mode: type mismatches or duplicate keys cause an error.

func SplitDocuments

func SplitDocuments(r io.Reader) ([][]byte, error)

SplitDocuments returns a slice of byte slices, each representing a YAML document.

Since preserving formatting and comments is important but the existing go packages all have some issue, this function attempts two strategies: one that decodes and re-encodes the YAML content, and another that simply splits the input text. If both methods return the same number of documents, we assume the text-based function is sufficient. It retains comments and formatting better. Otherwise, the round-trip version is used. It retains comments but the formatting may be off. The semantics of the document will still be the same but if it contains parsing errors, they may refer to a wrong line or column.

This function returns reading errors but any parsing errors are ignored and trigger the text-based splitting method.

func SplitDocumentsDecEnc

func SplitDocumentsDecEnc(r io.Reader) ([][]byte, error)

SplitDocumentsDecEnc splits documents from reader and returns them as re-encoded []byte slices, preserving comments but not exact original whitespace. It returns an error if any document cannot be decoded or re-encoded.

func SplitDocumentsText

func SplitDocumentsText(r io.Reader) ([][]byte, error)

SplitDocumentsText splits a YAML input stream into separate documents by looking for the `---` separator. No encoding or decoding is performed; the input is treated as raw text. Comments and whitespace are preserved. Malformed documents are returned as-is.

Types

type Patcher

type Patcher struct {
	BaseFilePath  string
	PatchFilePath string
	// contains filtered or unexported fields
}

func NewPatcher

func NewPatcher(filePath string, suffix string) *Patcher

func (*Patcher) MergedPatchContent

func (p *Patcher) MergedPatchContent() ([]byte, error)

MergedPatchContent reads a YAML file and, if it exists, its patch file, then merges them and returns it serialized.

func (*Patcher) PrependedPatchContent

func (p *Patcher) PrependedPatchContent() ([]byte, error)

PrependedPatchContent collates the base .yaml file with the .yaml.patch, by putting the content of the patch BEFORE the base document. The result is a multi-document YAML in all cases, even if the base and patch files are single documents.

func (*Patcher) SetQuiet

func (p *Patcher) SetQuiet(quiet bool)

SetQuiet sets the quiet flag, which will log as DEBUG_LEVEL instead of INFO.

Jump to

Keyboard shortcuts

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