diff

package
v0.40.4 Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: Apache-2.0 Imports: 13 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ADD = "+   "
	DEL = "-   "
)

Variables

View Source
var AsyncPanicErr = errors.New("async panic")

Functions

func Apply

func Apply(ctx context.Context, nbf *types.NomsBinFormat, root types.Value, patch Patch) (types.Value, error)

Apply applies a Patch (list of diffs) to a graph. It fulfills the following contract:

Given 2 Noms graphs: a1 and a2:
  ApplyPatch(a1, Diff(a1, a2)) == a2

This is useful for IncrementalUpdate() and possibly other problems. See updater.go for more information.

This function uses a patchStack to maintain state of the graph as it cycles through the diffs in a patch, applying them to 'root' one by one. Because the Difference objects in the patch can be sorted according to their path, each one is applied in order. When done in combination with the stack, this enables all Differences that change a particular node to be applied to that node before it gets assigned back to it's parent.

func Diff

func Diff(ctx context.Context, v1, v2 types.Value, dChan chan<- Difference, leftRight bool, descFunc ShouldDescFunc) error

Diff traverses two graphs simultaneously looking for differences. It returns two channels: a DiffReceiveChan that the caller can use to iterate over the diffs in the graph and a StopSendChanel that a caller can use to signal the Diff function to stop processing. Diff returns the Differences in depth-first first order. A 'diff' is defined as one of the following conditions:

  • a Value is Added or Removed from a node in the graph
  • the type of a Value has changed in the graph
  • a primitive (i.e. Bool, Float, String, Ref or Blob) Value has changed.

A Difference is not returned when a non-primitive value has been modified. For example, a struct field has been changed from one Value of type Employee to another. Those modifications are accounted for by the Differences described above at a lower point in the graph.

If leftRight is true then the left-right diff is used for ordered sequences - see Diff vs DiffLeftRight in Set and Map.

Note: the function sends messages on diffChan and checks whether stopChan has been closed to know if it needs to terminate diffing early. To function properly it needs to be executed concurrently with code that reads values from diffChan. The following is a typical invocation of Diff():

dChan := make(chan Difference)
sChan := make(chan struct{})
go func() {
    d.Diff(s3, s4, dChan, sChan, leftRight)
    close(dChan)
}()
for dif := range dChan {
    <some code>
}

func DiffMapRange

func DiffMapRange(ctx context.Context, m1, m2 types.Map, start types.Value, inRange types.ValueInRange, dChan chan<- Difference, leftRight bool, descFunc ShouldDescFunc) error

func PrintDiff

func PrintDiff(ctx context.Context, w io.Writer, v1, v2 types.Value, leftRight bool) (err error)

PrintDiff writes a textual reprensentation of the diff from |v1| to |v2| to |w|. If |leftRight| is true then the left-right diff is used for ordered sequences - see Diff vs DiffLeftRight in Set and Map.

func ShouldDescend

func ShouldDescend(v1, v2 types.Value) bool

shouldDescend returns true, if Value is not primitive or is a Ref.

func Summary

func Summary(ctx context.Context, vr1 types.ValueReader, vr2 types.ValueReader, value1, value2 types.Value)

Summary prints a summary of the diff between two values to stdout.

Types

type Difference

type Difference struct {
	// Path to the Value that has changed
	Path types.Path
	// ChangeType indicates the type of diff: modified, added, deleted
	ChangeType types.DiffChangeType
	// OldValue is Value before the change, can be nil if Value was added
	OldValue types.Value
	// NewValue is Value after the change, can be nil if Value was removed
	NewValue types.Value
	// NewKeyValue is used for when elements are added to diffs with a
	// non-primitive key. The new key must available when the map gets updated.
	NewKeyValue types.Value
	// KeyValue holds the key associated with a changed map value
	KeyValue types.Value
}

Difference represents a "diff" between two Noms graphs.

func (Difference) IsEmpty

func (dif Difference) IsEmpty() bool

type Patch

type Patch []Difference

Patch is a list of difference objects that can be applied to a graph using ApplyPatch(). Patch implements a sort order that is useful for applying the patch in an efficient way.

type PatchSort

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

func (PatchSort) Len

func (ps PatchSort) Len() int

func (PatchSort) Less

func (ps PatchSort) Less(i, j int) (bool, error)

func (PatchSort) Swap

func (ps PatchSort) Swap(i, j int)

type ShouldDescFunc

type ShouldDescFunc func(v1, v2 types.Value) bool

Jump to

Keyboard shortcuts

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