compare

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultDiffOptions = DiffOptions{
	IgnoreSeqOrder: false,
}
View Source
var DefaultOutputOptions = FormatOptions{
	Plain:    false,
	Silent:   false,
	Metadata: false,
}

Functions

This section is empty.

Types

type Diff

type Diff struct {
	// contains filtered or unexported fields
}

func (*Diff) Format

func (d *Diff) Format(opts FormatOptions) string

type DiffOptions

type DiffOptions struct {
	// IgnoreSeqOrder, when true, treats arrays as equal regardless of the order of their items.
	// For instance, the arrays [1, 2] and [2, 1] will be considered equal.
	IgnoreSeqOrder bool
}

DiffOptions specifies options for customizing the behavior of the comparison.

type DocDiffs

type DocDiffs []*Diff

func (DocDiffs) Format

func (d DocDiffs) Format(opts FormatOptions) string

func (DocDiffs) Len

func (a DocDiffs) Len() int

func (DocDiffs) Less

func (a DocDiffs) Less(i, j int) bool

func (DocDiffs) Swap

func (a DocDiffs) Swap(i, j int)

type FileDiffs

type FileDiffs []DocDiffs

func Compare

func Compare(left []byte, right []byte, comments bool, opts DiffOptions) (FileDiffs, error)

Compare compares two yaml files provided as bytes and returns the differences as FileDiffs, or an error if there's an issue parsing the files.

Example
left := []byte(`
name: Alice
city:  New York
items:
	- one
	- two
`)

right := []byte(`
name: Bob
value: 990
items:
	- one
	- three
`)

diffs, err := Compare(left, right, false, DefaultDiffOptions)
if err != nil {
	panic(err)
}

output := diffs.Format(FormatOptions{
	Plain:    true,
	Silent:   false,
	Metadata: false,
})
fmt.Println(output)
Output:

~ name: Alice -> Bob
- city: New York
+ value: 990
~ items[1]: two -> three

func CompareAst

func CompareAst(left *ast.File, right *ast.File, opts DiffOptions) FileDiffs

CompareAst compares two yaml documents represented as ASTs and returns the differences as FileDiffs.

func CompareFile

func CompareFile(leftFile string, rightFile string, comments bool, opts DiffOptions) (FileDiffs, error)

CompareFile compares two yaml files specified by file paths and returns the differences as FileDiffs, or an error if there's an issue reading or parsing the files.

func (FileDiffs) Format

func (d FileDiffs) Format(opts FormatOptions) string

func (FileDiffs) HasDiff

func (d FileDiffs) HasDiff() bool

type FormatOptions

type FormatOptions struct {
	// Plain disables colored output when set to true.
	Plain bool

	// Silent suppresses the display of values when set to true.
	Silent bool

	// Metadata includes additional metadata, such as line numbers or types, when set to true.
	Metadata bool
}

FormatOptions specifies options for formatting the output of the comparison.

Jump to

Keyboard shortcuts

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