diffnest

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatJSON = "json"
	FormatYAML = "yaml"
	FormatTOML = "toml"
)

Format constants.

Variables

View Source
var (
	ErrInvalidArgs         = errors.New("expected 2 files")
	ErrIncompatibleOptions = errors.New("--show-all and -C options are incompatible: context lines are only meaningful when showing only differences")
)
View Source
var (
	ErrUnsupportedFormat = errors.New("unsupported format")
)

Errors.

Functions

func DetectFormatFromFilename

func DetectFormatFromFilename(filename string) string

DetectFormatFromFilename detects the format from filename extension.

func HasDifferences

func HasDifferences(results []*DiffResult) bool

HasDifferences checks if there are any differences in the results.

Types

type ArrayDiffStrategy

type ArrayDiffStrategy int

ArrayDiffStrategy defines how to compare arrays.

const (
	ArrayStrategyIndex ArrayDiffStrategy = iota // Compare by index
	ArrayStrategyValue                          // Find best matching
)

type Command

type Command struct {
	// Flags
	ShowAll          bool
	IgnoreZeroValues bool
	IgnoreEmpty      bool
	IgnoreKeyCase    bool
	IgnoreValueCase  bool
	ArrayStrategy    string
	OutputFormat     string
	Format1          string
	Format2          string
	Verbose          bool
	Help             bool
	ContextLines     int

	// Arguments
	File1 string
	File2 string
	// contains filtered or unexported fields
}

Command represents the CLI command configuration.

func NewCommand

func NewCommand(name string, errorHandling flag.ErrorHandling) *Command

NewCommand creates a new Command instance.

func (*Command) GetDiffOptions

func (c *Command) GetDiffOptions() DiffOptions

GetDiffOptions returns DiffOptions based on command flags.

func (*Command) GetFormat1

func (c *Command) GetFormat1() string

GetFormat1 returns the format for file1, auto-detecting if necessary.

func (*Command) GetFormat2

func (c *Command) GetFormat2() string

GetFormat2 returns the format for file2, auto-detecting if necessary.

func (*Command) GetFormatter

func (c *Command) GetFormatter() Formatter

GetFormatter returns the appropriate formatter based on command flags.

func (*Command) Parse

func (c *Command) Parse(args []string) error

Parse parses command line arguments.

func (*Command) SetOutput

func (c *Command) SetOutput(w io.Writer)

SetOutput sets the output destination for error messages.

func (*Command) Usage

func (c *Command) Usage(w io.Writer)

Usage prints usage information.

type Controller

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

Controller handles the core diff logic.

func NewController

func NewController(reader1, reader2 io.Reader, format1, format2 string, diffOpts DiffOptions, formatter Formatter, writer io.Writer) *Controller

NewController creates a new Controller.

func (*Controller) Run

func (c *Controller) Run() (bool, error)

Run executes the diff process and returns whether differences were found.

type DataType

type DataType int

DataType represents the type of structured data.

const (
	TypeNull DataType = iota
	TypeBool
	TypeNumber
	TypeString
	TypeArray
	TypeObject
)

type DiffEngine

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

DiffEngine computes differences between structures.

func NewDiffEngine

func NewDiffEngine(options DiffOptions) *DiffEngine

NewDiffEngine creates a new diff engine.

func (*DiffEngine) Compare

func (e *DiffEngine) Compare(a, b *StructuredData) *DiffResult

Compare compares two structured data.

type DiffMeta

type DiffMeta struct {
	DiffCount int // Size of the difference
	Note      string
}

DiffMeta contains additional diff information.

type DiffOptions

type DiffOptions struct {
	IgnoreEmptyFields bool
	IgnoreZeroValues  bool
	IgnoreKeyCase     bool
	IgnoreValueCase   bool
	ArrayDiffStrategy ArrayDiffStrategy
}

DiffOptions contains options for diff computation.

type DiffResult

type DiffResult struct {
	Status   DiffStatus
	Path     []string // Path to this element
	From     *StructuredData
	To       *StructuredData
	Children []*DiffResult // For nested structures
	Meta     *DiffMeta
}

DiffResult represents the result of comparing two structures.

func Compare

func Compare(docsA, docsB []*StructuredData, options DiffOptions) []*DiffResult

Compare compares multiple documents and finds optimal pairings.

type DiffStatus

type DiffStatus int

DiffStatus represents the status of a diff.

const (
	StatusSame DiffStatus = iota
	StatusModified
	StatusAdded
	StatusDeleted
)

type Formatter

type Formatter interface {
	Format(w io.Writer, results []*DiffResult) error
}

Formatter interface for different output formats.

type JSONParser

type JSONParser struct{}

JSONParser implements Parser for JSON.

func (*JSONParser) Format

func (p *JSONParser) Format() string

func (*JSONParser) Parse

func (p *JSONParser) Parse(reader io.Reader) ([]*StructuredData, error)

type JSONPatchFormatter

type JSONPatchFormatter struct{}

JSONPatchFormatter implements RFC 6902 JSON Patch format.

func (*JSONPatchFormatter) Format

func (f *JSONPatchFormatter) Format(w io.Writer, results []*DiffResult) error

Format formats diff results as JSON Patch.

type Location

type Location struct {
	Line   int
	Column int
}

Location represents position in source file.

type Metadata

type Metadata struct {
	Format      string      // "json", "yaml", "toml"
	Location    *Location   // Position in source file
	Comments    []string    // Comments (YAML/TOML)
	StringStyle StringStyle // Style of string representation (for YAML)
}

Metadata contains format-specific information.

type Parser

type Parser interface {
	Parse(reader io.Reader) ([]*StructuredData, error)
	Format() string
}

Parser interface for different formats.

type StringStyle

type StringStyle int

StringStyle represents YAML string representation style.

const (
	StringStyleUnknown StringStyle = iota
	StringStyleQuoted              // "string" or 'string'
	StringStyleLiteral             // |
	StringStyleFolded              // >
	StringStylePlain               // no quotes
)

type StructuredData

type StructuredData struct {
	Type     DataType
	Value    any                        // Actual value for primitives
	Children map[string]*StructuredData // For objects
	Elements []*StructuredData          // For arrays
	Meta     *Metadata                  // Format-specific metadata
}

StructuredData represents format-agnostic structured data.

func ParseWithFormat

func ParseWithFormat(reader io.Reader, format string) ([]*StructuredData, error)

ParseWithFormat parses content from reader with specified format.

type UnifiedFormatter

type UnifiedFormatter struct {
	ShowOnlyDiff bool
	Verbose      bool
	ContextLines int
}

UnifiedFormatter implements unified diff format.

func (*UnifiedFormatter) Format

func (f *UnifiedFormatter) Format(w io.Writer, results []*DiffResult) error

Format formats diff results.

type YAMLParser

type YAMLParser struct{}

YAMLParser implements Parser for YAML.

func (*YAMLParser) Format

func (p *YAMLParser) Format() string

func (*YAMLParser) Parse

func (p *YAMLParser) Parse(reader io.Reader) ([]*StructuredData, error)

Jump to

Keyboard shortcuts

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