changes

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Changes package provides functionality for tracking changes to objects.

Index

Constants

This section is empty.

Variables

View Source
var (
	// PrefixEmpty is the empty prefix for root level field changes.
	PrefixEmpty = NewPrefix(FieldNameEmpty)
)

Functions

func IntoKey added in v0.5.0

func IntoKey(i interface{}) string

IntoKey converts an interface to a string key, uses KeyForChanges if the interface implements Key.

func WithPrintNameAndKey added in v0.5.1

func WithPrintNameAndKey(printNameAndKey func(name, key string) string) func(*DefaultFormatter)

WithPrintNameAndKey sets the function used for printing a prefix as name with key.

Types

type ChainedLogger

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

ChainedLogger implements Logger interface using an inner change logger. Multiple change loggers are chained together by prepending prefixes.

func NewChainedLogger

func NewChainedLogger(prefix Prefix, inner Logger) *ChainedLogger

NewChainedLogger creates a new instance of ChainedLogger.

func (*ChainedLogger) Append

func (c *ChainedLogger) Append(change Change)

Append appends a change to the change logger.

func (*ChainedLogger) ToString

func (c *ChainedLogger) ToString() []string

ToString converts the change logger to a slice of human readable strings.

type Change

type Change struct {
	Prefix    []Prefix  `json:"prefix,omitempty"`
	FieldName string    `json:"field_name,omitempty"`
	Operation Operation `json:"operation,omitempty"`
	OldValue  string    `json:"old_value,omitempty"`
	NewValue  string    `json:"new_value,omitempty"`
	Key       string    `json:"key,omitempty"`
}

Change represents a mutation applied to an object.

type DefaultFormatter

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

DefaultFormatter provides basic change formatting functionality.

func NewDefaultFormatter added in v0.5.1

func NewDefaultFormatter(options ...func(*DefaultFormatter)) *DefaultFormatter

NewDefaultFormatter creates a new instance of DefaultFormatter.

func (*DefaultFormatter) Format

func (f *DefaultFormatter) Format(c *Change) string

Format formats a change to a human readable string.

type DefaultLogger

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

DefaultLogger provides basic change logging functionality.

func NewDefaultLogger

func NewDefaultLogger(prefix Prefix) *DefaultLogger

NewDefaultLogger creates a new instance of DefaultChangeLogger.

func (*DefaultLogger) Append

func (c *DefaultLogger) Append(change Change)

Append appends a change to the change logger.

func (*DefaultLogger) ToString

func (c *DefaultLogger) ToString() (result []string)

ToString converts the change logger to a slice of human readable strings.

type FieldName added in v0.5.0

type FieldName string

FieldName defines a type for the field name enums used in prefixes.

var (
	// FieldNameEmpty is the empty prefix name for root level field changes.
	FieldNameEmpty FieldName = ""
)

type Formatter

type Formatter interface {
	Format(c *Change) string
}

Formatter defines an interface for formatting changes to human readable string.

type Key added in v0.5.0

type Key interface {
	KeyForChanges() string
}

Key interface can be implemented for types so that the resulting string is used as the key in the prefix.

type Logger

type Logger interface {
	Append(change Change)
	ToString() []string
}

Logger defines an interface for logging changes.

type Operation

type Operation string

Operation defines a type used for change operation enums.

const (
	// OperationAdded is used for slice appends and map inserts.
	OperationAdded Operation = "added"
	// OperationRemoved is used for slice removals and map deletes.
	OperationRemoved Operation = "removed"
	// OperationUpdated is used for field updates.
	OperationUpdated Operation = "updated"
	// OperationSet is used for field sets.
	// Setting a whole slice/map, or setting a field which was previously unset.
	OperationSet Operation = "set"
	// OperationCleared is used for setting a field's value to its zero value.
	OperationCleared Operation = "cleared"
)

type Prefix

type Prefix struct {
	Name FieldName
	Key  string
}

Prefix defines a type used for change prefix enums. Prefixes are used to identify the object's field that was changed. If a field is a map or slice, the key is also included in the prefix.

For example, if a FieldName "Foo" was added to a map with key "bar", the prefix would be {Name: "Foo", Key: "bar"}, and the default change logger would print "Foo[bar] added with value 'value'".

func NewPrefix added in v0.5.0

func NewPrefix(name FieldName) Prefix

NewPrefix creates a new instance of Prefix using name only.

func NewPrefixWithKey added in v0.5.0

func NewPrefixWithKey(name FieldName, key string) Prefix

NewPrefixWithKey creates a new instance of Prefix using name and key.

Jump to

Keyboard shortcuts

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