obj_diff

package
v0.0.0-...-57273a9 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2019 License: Apache-2.0 Imports: 3 Imported by: 2

Documentation

Overview

Copyright (c) Walmart Inc.

This source code is licensed under the Apache 2.0 license found in the LICENSE file in the root directory of this source tree.

Copyright (c) Walmart Inc.

This source code is licensed under the Apache 2.0 license found in the LICENSE file in the root directory of this source tree.

Copyright (c) Walmart Inc.

This source code is licensed under the Apache 2.0 license found in the LICENSE file in the root directory of this source tree.

Copyright (c) Walmart Inc.

This source code is licensed under the Apache 2.0 license found in the LICENSE file in the root directory of this source tree.

Copyright (c) Walmart Inc.

This source code is licensed under the Apache 2.0 license found in the LICENSE file in the root directory of this source tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyReflectValue

func CopyReflectValue(oldVal reflect.Value) (newVal reflect.Value)

Reflectively and recursively makes a copy of a reflect.Value.

func CopyValueReflectively

func CopyValueReflectively(oldValue interface{}) interface{}

Reflectively make a copy of an object. This uses reflection to traverse an object and create a copy.

Types

type ChangeSet

type ChangeSet struct {
	BaseType reflect.Type
	Changes  []Change
}

A ChangeSet represents the result of a diff as a set of Changes against a Base Type.

func Diff

func Diff(obj1 interface{}, obj2 interface{}) (*ChangeSet, error)

Computes the change set between two objects, both objects must have the same type. This returns a ChangeSet on success and an error on failure.

func (*ChangeSet) AddPathAddition

func (cs *ChangeSet) AddPathAddition(ctx []PathElement, newValue reflect.Value)

Add an addition to this change set.

func (*ChangeSet) AddPathChange

func (cs *ChangeSet) AddPathChange(ctx []PathElement, oldValue reflect.Value, newValue reflect.Value)

Add a change to this change set.

func (*ChangeSet) AddPathDeletion

func (cs *ChangeSet) AddPathDeletion(ctx []PathElement, oldValue reflect.Value)

Add a delete to this change set.

func (ChangeSet) Equals

func (cs ChangeSet) Equals(other ChangeSet) bool

Compares this ChangeSet against another ChangeSet, returns true if the are the same. This is currently used only in the testing framework, but could have other uses.

func (ChangeSet) Patch

func (cs ChangeSet) Patch(obj interface{}) (err error)

Patch an object (in place/by reference) with the Changes within this ChangeSet. Panics if obj is not settable or does not match the BaseType.

func (ChangeSet) String

func (cs ChangeSet) String() string

type ObjectPath

type ObjectPath struct {
	reflect.Value

	Path []PathElement
	// contains filtered or unexported fields
}

An Object path provides a way to step through a given []PathElement as it relates to object given at creation and take actions at each point along the way. At any given point the ObjectPath can be used as a reflect.Value at the current point in the traversal of path.

func NewObjectPathWithConfig

func NewObjectPathWithConfig(root reflect.Value, path []PathElement, config ObjectPathConfig) *ObjectPath

Creates an Object path, if root is not writable then mutating actions on the resulting ObjectPath will panic. The path is a list of PathElements to follow when traversing root. Finally config contains options and actions that ObjectPath can take for you automatically.

func (*ObjectPath) Append

func (op *ObjectPath) Append(newVal reflect.Value)

Appends to the current Slice a newValue. Panics if newValue is not assignable to the Slice type.

func (*ObjectPath) AppendNew

func (op *ObjectPath) AppendNew(newType reflect.Type)

Appends to the current Slice a new object of newType. Panics if newType is not assignable to the Slice type.

func (*ObjectPath) CreateIfMissing

func (op *ObjectPath) CreateIfMissing()

Create the next object in the path if it does not currently exist.

func (*ObjectPath) Delete

func (op *ObjectPath) Delete()

Delete the object at the current point in the path. Delete is only supported for Map, Slice, and Ptr; panics otherwise.

func (*ObjectPath) GetField

func (op *ObjectPath) GetField() reflect.Value

Retrieve the next from a Struct type. Panics if the current object is not a Struct.

func (*ObjectPath) GetIndex

func (op *ObjectPath) GetIndex() reflect.Value

Retrieves the next index. Panics if the current object is not an Array or Slice.

func (*ObjectPath) GetMapValue

func (op *ObjectPath) GetMapValue() reflect.Value

Retrieves the value for the next key. Panics if the current object is not a Map.

func (*ObjectPath) InBounds

func (op *ObjectPath) InBounds() bool

Returns true if the next index exists in the current Slice or Array. Panics if current element is not a Slice or Array.

func (*ObjectPath) IsPointer

func (op *ObjectPath) IsPointer() bool

Returns true if the current PathElement is a pointer.

func (ObjectPath) LastVal

func (op ObjectPath) LastVal() reflect.Value

Retrieve the last value of this ObjectPath.

func (*ObjectPath) NeedsAppend

func (op *ObjectPath) NeedsAppend() bool

Returns true if the next index is at the end of a Slice. This means that an append will be successful at this point.

func (*ObjectPath) Next

func (op *ObjectPath) Next() (hasNext bool)

Advance to the next object in the path. Returns true if there are further elements in the path, and false otherwise.

func (ObjectPath) PathElem

func (op ObjectPath) PathElem() PathElement

Retrieve the current Path Element.

func (*ObjectPath) Set

func (op *ObjectPath) Set(newVal reflect.Value)

Set the current value to newValue. Panics if newValue is not assignable to the current value.

func (*ObjectPath) SetMapValue

func (op *ObjectPath) SetMapValue(newValue reflect.Value)

Sets the next map key to the given value. Panics if the newValue is not assignable to the Map value.

func (*ObjectPath) SetMapValueToNew

func (op *ObjectPath) SetMapValueToNew(newType reflect.Type)

Sets the next map key to the value of a new reflect.Type. Panics if newType is not assignable to the Map value.

func (*ObjectPath) SetToNew

func (op *ObjectPath) SetToNew(newType reflect.Type)

Set the current value to an instance of newType. Panics if newType is not assignable to the current value.

type ObjectPathConfig

type ObjectPathConfig struct {
	// If the path would traverse into an object (struct, map,
	// array, slice, pointer) that does not exist, create it.
	CreateMissingObjects bool
	// If the path traverses into an invalid Map key or Slice
	// index, create the object that should be there.
	CreateMissingValues bool
}

Notes

Bugs

  • Renaming of Map keys results in a deletion and addition.

  • Lists with different orders but the same elements will generate changes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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