diff

package
v0.0.0-...-e5fa29d Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: Apache-2.0 Imports: 10 Imported by: 7

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func Apply

func Apply(root types.Value, patch Patch) types.Value

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(v1, v2 types.Value, dChan chan<- Difference, stopChan chan struct{}, leftRight bool)

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, Number, 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 PrintDiff

func PrintDiff(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 Summary

func Summary(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
}

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.

func (Patch) Len

func (r Patch) Len() int

func (Patch) Less

func (r Patch) Less(i, j int) bool

func (Patch) Swap

func (r Patch) Swap(i, j int)

Jump to

Keyboard shortcuts

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