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 ¶
- func GetDocumentKeys(r io.Reader) ([][]string, error)
- func IsEmptyYAML(r io.Reader) (bool, error)
- func Merge(inputs [][]byte) (*bytes.Buffer, error)
- func SplitDocuments(r io.Reader) ([][]byte, error)
- func SplitDocumentsDecEnc(r io.Reader) ([][]byte, error)
- func SplitDocumentsText(r io.Reader) ([][]byte, error)
- type Patcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDocumentKeys ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 (*Patcher) MergedPatchContent ¶
MergedPatchContent reads a YAML file and, if it exists, its patch file, then merges them and returns it serialized.
func (*Patcher) PrependedPatchContent ¶
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.