diff

package module
v4.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CREATE represents when an element has been added
	CREATE = "create"
	// UPDATE represents when an element has been updated
	UPDATE = "update"
	// DELETE represents when an element has been removed
	DELETE = "delete"
)

Variables

View Source
var (
	// ErrTypeMismatch Compared types do not match
	ErrTypeMismatch = NewError("types do not match")
	// ErrInvalidChangeType The specified change values are not unsupported
	ErrInvalidChangeType = NewError("change type must be one of 'create' or 'delete'")
)

Functions

func AllowTypeMismatch

func AllowTypeMismatch(enabled bool) func(d *Differ) error

AllowTypeMismatch changed behaviour to report value as "updated" when its type has changed instead of error

func AreType

func AreType(a, b reflect.Value, types ...reflect.Type) bool

func Changed

func Changed(a, b interface{}) bool

Changed returns true if both values differ

func ConvertCompatibleTypes

func ConvertCompatibleTypes() func(d *Differ) error

ConvertTypes enables values that are convertible to the target type to be converted when patching

func CustomValueDiffers

func CustomValueDiffers(vd ...ValueDiffer) func(d *Differ) error

CustomValueDiffers allows you to register custom differs for specific types

func DisableStructValues

func DisableStructValues() func(d *Differ) error

DisableStructValues disables populating a separate change for each item in a struct, where the struct is being compared to a nil value

func DiscardComplexOrigin

func DiscardComplexOrigin() func(d *Differ) error

DiscardComplexOrigin - by default, we are now keeping the complex struct associated with a create entry. This allows us to fix the merge to new object issue of not having enough change log details when allocating new objects. This however is a trade off of memory size and complexity vs correctness which is often only necessary when embedding structs in slices and arrays. It memory constrained environments, it may be desirable to turn this feature off however from a computational perspective, keeping the complex origin is actually quite cheap so, make sure you're extremely clear on the pitfalls of turning this off prior to doing so.

func Filter

func Filter(f FilterFunc) func(d *Differ) error

Filter allows you to determine which fields the differ descends into

func FlattenEmbeddedStructs

func FlattenEmbeddedStructs() func(d *Differ) error

FlattenEmbeddedStructs determines whether fields of embedded structs should behave as if they are directly under the parent

func SliceOrdering

func SliceOrdering(enabled bool) func(d *Differ) error

SliceOrdering determines whether the ordering of items in a slice results in a change

func StructMapKeySupport

func StructMapKeySupport() func(d *Differ) error

StructMapKeySupport - Changelog paths do not provided structured object values for maps that contain complex keys (such as other structs). You must enable this support via an option and it then uses msgpack to encode path elements that are structs. If you don't have this on, and try to patch, your apply will fail for that element.

func TagName

func TagName(tag string) func(d *Differ) error

TagName sets the tag name to use when getting field names and options

Types

type Change

type Change struct {
	Type string      `json:"type"`
	Path []string    `json:"path"`
	From interface{} `json:"from"`
	To   interface{} `json:"to"`
	// contains filtered or unexported fields
}

Change stores information about a changed item

type ChangeValue

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

ChangeValue is a specialized struct for monitoring patching

func NewChangeValue

func NewChangeValue(d *Differ, c Change, target interface{}) (ret *ChangeValue)

NewChangeValue idiomatic constructor (also invokes render)

func (*ChangeValue) AddError

func (c *ChangeValue) AddError(err error) *ChangeValue

AddError appends errors to this change value

func (*ChangeValue) ClearFlag

func (c *ChangeValue) ClearFlag(flag PatchFlags)

ClearFlag removes just a single flag

func (*ChangeValue) HasFlag

func (c *ChangeValue) HasFlag(flag PatchFlags) bool

HasFlag indicates if a flag is set on the node. returns false if node is bad

func (ChangeValue) Index

func (c ChangeValue) Index(i int) reflect.Value

Index echo for index

func (*ChangeValue) IsValid

func (c *ChangeValue) IsValid() bool

IsValid echo for is valid

func (ChangeValue) Len

func (c ChangeValue) Len() int

Len echo for len

func (ChangeValue) NewArrayElement

func (c ChangeValue) NewArrayElement() reflect.Value

NewArrayElement gives us a dynamically typed new element

func (ChangeValue) NewElement

func (c ChangeValue) NewElement() reflect.Value

Instance a new element of type for target. Taking the copy of the complex origin avoids the 'lack of data' issue present when allocating complex structs with slices and arrays

func (ChangeValue) ParentIndex

func (c ChangeValue) ParentIndex(i int) (ret reflect.Value)

ParentIndex - get us the parent version, nil safe

func (ChangeValue) ParentKind

func (c ChangeValue) ParentKind() reflect.Kind

ParentKind - helps keep us nil safe

func (ChangeValue) ParentLen

func (c ChangeValue) ParentLen() (ret int)

ParentLen is a nil safe parent length check

func (*ChangeValue) ParentSet

func (c *ChangeValue) ParentSet(value reflect.Value, convertCompatibleTypes bool)

ParentSet - nil safe parent set

func (*ChangeValue) Set

func (c *ChangeValue) Set(value reflect.Value, convertCompatibleTypes bool)

Set echos reflect set

func (*ChangeValue) SetFlag

func (c *ChangeValue) SetFlag(flag PatchFlags)

Sets a flag on the node and saves the change

type Changelog

type Changelog []Change

Changelog stores a list of changed items

func Diff

func Diff(a, b interface{}, opts ...func(d *Differ) error) (Changelog, error)

Diff returns a changelog of all mutated values from both

func StructValues

func StructValues(t string, path []string, s interface{}) (Changelog, error)

StructValues gets all values from a struct values are stored as "created" or "deleted" entries in the changelog, depending on the change type specified

func (*Changelog) Add

func (cl *Changelog) Add(t string, path []string, ftco ...interface{})

func (*Changelog) Filter

func (cl *Changelog) Filter(path []string) Changelog

Filter filter changes based on path. Paths may contain valid regexp to match items

func (*Changelog) FilterOut

func (cl *Changelog) FilterOut(path []string) Changelog

FilterOut filter out the changes based on path. Paths may contain valid regexp to match items

type Comparative

type Comparative struct {
	A, B *reflect.Value
}

Comparative ...

type ComparativeList

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

ComparativeList : stores indexed comparative

func NewComparativeList

func NewComparativeList() *ComparativeList

NewComparativeList : returns a new comparative list

type DiffError

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

our own version of an error, which can wrap others

func NewError

func NewError(message string, causes ...error) *DiffError

NewError just give me a plain error

func NewErrorf

func NewErrorf(format string, messages ...interface{}) *DiffError

NewErrorf just give me a plain error with formatting

func (DiffError) Error

func (s DiffError) Error() string

Error implements the error interface

func (*DiffError) Unwrap

func (s *DiffError) Unwrap() error

Unwrap implement 1.13 unwrap feature for compatibility

func (*DiffError) WithCause

func (s *DiffError) WithCause(err error) *DiffError

AppendCause appends a new cause error to the chain

type DiffFunc

type DiffFunc func([]string, reflect.Value, reflect.Value, interface{}) error

DiffFunc represents the built-in diff functions

type DiffType

type DiffType uint8

DiffType represents an enum with all the supported diff types

const (
	UNSUPPORTED DiffType = iota
	STRUCT
	SLICE
	ARRAY
	STRING
	BOOL
	INT
	UINT
	FLOAT
	MAP
	PTR
	INTERFACE
)

func (DiffType) String

func (t DiffType) String() string

type Differ

type Differ struct {
	TagName             string
	SliceOrdering       bool
	DisableStructValues bool

	AllowTypeMismatch      bool
	DiscardParent          bool
	StructMapKeys          bool
	FlattenEmbeddedStructs bool
	ConvertCompatibleTypes bool
	Filter                 FilterFunc
	// contains filtered or unexported fields
}

Differ a configurable diff instance

func NewDiffer

func NewDiffer(opts ...func(d *Differ) error) (*Differ, error)

NewDiffer creates a new configurable diffing object

func (*Differ) Diff

func (d *Differ) Diff(a, b interface{}) (Changelog, error)

Diff returns a changelog of all mutated values from both

func (*Differ) Merge

func (d *Differ) Merge(original interface{}, changed interface{}, target interface{}) (PatchLog, error)

Merge is a convenience function that diffs, the original and changed items and merges said changes with target all in one call.

func (*Differ) Patch

func (d *Differ) Patch(cl Changelog, target interface{}) (ret PatchLog)

Patch... the missing feature.

type FilterFunc

type FilterFunc func(path []string, parent reflect.Type, field reflect.StructField) bool

FilterFunc is a function that determines whether to descend into a struct field. parent is the struct being examined and field is a field on that struct. path is the path to the field from the root of the diff.

type PatchFlags

type PatchFlags uint32

Not strictly necessary but might be nice in some cases

const (
	OptionCreate PatchFlags = 1 << iota
	OptionNoCreate
	OptionOmitUnequal
	OptionImmutable
	FlagInvalidTarget
	FlagApplied
	FlagFailed
	FlagCreated
	FlagIgnored
	FlagDeleted
	FlagUpdated
	FlagParentSetApplied
	FlagParentSetFailed
)

type PatchLog

type PatchLog []PatchLogEntry

func Merge

func Merge(original interface{}, changed interface{}, target interface{}) (PatchLog, error)

func Patch

func Patch(cl Changelog, target interface{}) (ret PatchLog)

func (PatchLog) Applied

func (p PatchLog) Applied() bool

Applied - returns true if all change log entries were actually

applied, regardless of if any errors were encountered

func (PatchLog) ErrorCount

func (p PatchLog) ErrorCount() (ret uint)

ErrorCount -- counts the number of errors encountered while patching

func (PatchLog) HasErrors

func (p PatchLog) HasErrors() (ret bool)

HasErrors - indicates if a patch log contains any errors

type PatchLogEntry

type PatchLogEntry struct {
	Path   []string    `json:"path"`
	From   interface{} `json:"from"`
	To     interface{} `json:"to"`
	Flags  PatchFlags  `json:"flags"`
	Errors error       `json:"errors"`
}

PatchLogEntry defines how a DiffLog entry was applied

func NewPatchLogEntry

func NewPatchLogEntry(cv *ChangeValue) PatchLogEntry

NewPatchLogEntry converts our complicated reflection based struct to a simpler format for the consumer

func (PatchLogEntry) HasFlag

func (p PatchLogEntry) HasFlag(flag PatchFlags) bool

HasFlag - convenience function for users

type ValueDiffer

type ValueDiffer interface {
	Match(a, b reflect.Value) bool
	Diff(dt DiffType, df DiffFunc, cl *Changelog, path []string, a, b reflect.Value, parent interface{}) error
	InsertParentDiffer(dfunc func(path []string, a, b reflect.Value, p interface{}) error)
}

ValueDiffer is an interface for custom differs

Jump to

Keyboard shortcuts

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