jsondiff

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package jsondiff makes "100% compatible with GitHub" mechanical. It compares a Githome response against a recorded GitHub response (the golden), enforcing structure, types, and nullability while ignoring values that legitimately vary between instances (ids, timestamps, hosts, rate-limit counters).

The asymmetry is deliberate: every key GitHub sends must be present with the same type and nullability, and Githome must not send keys GitHub does not. Values are compared only where they carry contract meaning (a resource label, a URL path), never where they are instance-specific.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertCompatible

func AssertCompatible(t testing.TB, want, got []byte, opt Options)

AssertCompatible decodes want and got and fails t with every difference. It also runs a blunt host-leak backstop on the raw got bytes.

Types

type Diff

type Diff struct {
	Path string
	Kind Kind
	Want string
	Got  string
}

Diff is one structural or value difference found by Compare.

func Compare

func Compare(want, got any, opt Options) []Diff

Compare reports every difference of got against want (the golden).

func (Diff) String

func (d Diff) String() string

type Kind

type Kind int

Kind classifies a single difference.

const (
	MissingKey     Kind = iota // present in want, absent in got
	ExtraKey                   // present in got, absent in want
	TypeMismatch               // different JSON types
	Nullability                // one is null, the other is not
	ValueMismatch              // values that must match do not
	LengthMismatch             // arrays of different length
)

The kinds of difference Compare can report.

func (Kind) String

func (k Kind) String() string

type Options

type Options struct {
	// Hosts are rewritten to the sentinel "HOST" before comparing URL strings, so
	// a Githome link compares equal to the captured github.com link by path.
	Hosts []string
	// IgnoreValueKeys names keys whose value varies between instances; presence
	// and type are still enforced, only the value is skipped.
	IgnoreValueKeys map[string]bool
}

Options controls normalization during comparison.

func Default

func Default(testHost string) Options

Default returns Options that rewrite the upstream hosts plus the given test host, and ignore the value of instance-specific keys.

Jump to

Keyboard shortcuts

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