structural

package
v0.5.17 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2021 License: BSD-3-Clause Imports: 8 Imported by: 4

README

structural

Structural diff, merge, patch, pick, and mask helpers for CUE.

MVS will make using cue modules easier.

Index

support for both Cue and Go vals/structs

  1. Usage
  2. equal
  3. diff
  4. merge
  5. patch
  6. pick
  7. mask
  8. Developing

Usage

structural is most easily used with cue modules, see https://github.com/hofstadter-io/mvs.

Initialize a cue module
mvs init cue github.com/<namespace>/<project>
Add structural to your cue.mods file:
require github.com/hofstadter-io/structural v0.0.3
Update your dependencies:
mvs vendor
After creating example.cue (see below) run the following:
cue eval example.cue
cue export example.cue

Look for a "same" field == true

example.cue
import "github.com/hofstadter-io/structural"

A :: {
	a: "a"
	b: "b"
	N: {x: "x", y: "y"}
}
B :: {
	b: "b"
	c: "c"
	N: {x: "x", z: "z"}
}

diff: {
  same: (ex & an) != _|_
  ex: (structural.Diff & {Orig: A, New: B}).Result
  an: {
    "[]": {
      removed: {
        a: "a"
      }
      added: {
        c: "c"
      }
    }
    "[\"N\"]": {
      removed: {
        y: "y"
      }
      added: {
        z: "z"
      }
    }
  }
}

merge: {
  same: (ex & an) != _|_
  ex: (structural.Merge & {Orig: A, New: B}).Result
  an: {
    a: "a"
    b: "b"
    c: "c"
    N: {
      x: "x"
      y: "y"
      z: "z"
    }
  }
}

Diff

Diff :: {
	// Arguments
	Orig: {...}
	New:  {...}

	Result: {...}
}
A :: {
	a: "a"
	b: "b"
	N: {x: "x", y: "y"}
}
Z :: {
	a: "a"
	b: "b"
	N: "N"
}
x: (structural.Diff & {Orig: A, New: Z}).Result
x: {
	changed: {
	  N: {
	    from: {
	      x: "x"
	      y: "y"
	    }
	    to: "N"
	  }
	}
}

Merge

Merge :: {
	// Arguments
	Orig: {...}
	New:  {...}

	Result: {...}
A :: {
	a: "a"
	b: "b"
	N: {x: "x", y: "y"}
}
B :: {
	b: "b"
	c: "c"
	N: {x: "x", z: "z"}
}
x: (structural.Merge & {Orig: A, New: B}).Result
x: {
  a: "a"
  b: "b"
  c: "c"
  N: {
    x: "x"
    y: "y"
    z: "z"
  }
}

Patch

Patch :: {
	// Arguments
	Orig: {...}
	Diff: {...}

	Result: {...}
}
x: (structural.Patch & {Orig: {a: "a", b: "b", y: "y", N: {x: "x"}},
												 Diff: {inplace: {N: {changed: {x: {from: "x", to: "xx"}}}}, changed: {y: {from: "y", to: "yy"}}, removed: {b: "b"}, added: {z: "z"}}}).Result
x: {a: "a", y: "yy", N: {x: "xx"}, z: "z"}

Pick

Pick :: {
	// Arguments
  Orig: {...}
  Pick: {...}

  Result: {...}
}
X:: {
	a: "a"
	b: "b"
	N: {x: "x", y: "y"}
	l: [1, 2, 3, 4, 5]
}
x: (structural.Pick & {Orig: X, Pick: { b: string, N: {x: string}, l: [1, 1, 3] }}).Result
x: {
  b: "b"
  N: {
    x: "x"
  }
  l: [1, 3]
}

Mask

Mask :: {
	// Arguments
  Orig: {...}
  Mask: {...}

  Result: {...}
}
X:: {
	a: "a"
	b: "b"
	N: {x: "x", y: "y"}
	l: [1, 2, 3, 4, 5]
}
x: (structural.Mask & {Orig: X, Mask: { b: string, N: {x: string}, l: [1, 1, 3] }}).Result
x: {
	a: "a"
	N: {
		y: "y"
	}
	l: [2, 4, 5]
}

Developing

There isn't much special, you just need cue installed.

Running tests
cue test

See the test directory for more specifics and examples.

This runs the test command in test_tool.cue which is nothing more than "cue export test/*.cue"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CueDiff

func CueDiff(sorig, snew string) (string, error)

func CueMask

func CueMask(sorig, smask string) (string, error)

func CueMerge

func CueMerge(sorig, snew string) (string, error)

func CueQuery

func CueQuery(squery, sdata string) (string, error)

func Diff

func Diff(theirs, ours interface{}) (interface{}, error)

func DiffCue

func DiffCue(theirs, ours cue.Value) (cue.Value, error)

func DiffGo

func DiffGo(theirs, ours interface{}) (interface{}, error)

func DiffValues added in v0.5.4

func DiffValues(orig, next cue.Value) (cue.Value, error)

func ExprFromValue

func ExprFromValue(v cue.Value) *pvExpr

func FindByAttrs

func FindByAttrs(s cue.Value, attrs []string, attrsK map[string][]string, attrsKV map[string]map[string]string) ([]cue.Value, error)

FindByAttrs finds values in s if 1) attrs : it has @attr(...) 2) attrsK : it has @attr(key,...) [it must have all keys in the passed list, not just one] 3) attrsKV : it has @attr(key=value,...) [it must have all key=value in the passed map, not just one of them]

func FindByAttrsAny added in v0.5.7

func FindByAttrsAny(s cue.Value, attrs []string, attrsK map[string][]string, attrsKV map[string]map[string]string) ([]cue.Value, error)

FindByAttrsAll finds values in s like FindByAttrs, but any instead of all

func MaskValues added in v0.5.4

func MaskValues(orig, mask cue.Value) (cue.Value, error)

func Merge

func Merge(orig, last interface{}) (cue.Value, error)

func MergeValues added in v0.5.1

func MergeValues(orig, update cue.Value) (cue.Value, error)

func NewpvList

func NewpvList() *pvList

func NewpvStruct

func NewpvStruct() *pvStruct

func Pick

func Pick(orig, pick interface{}) (cue.Value, error)

func PickValues added in v0.5.4

func PickValues(orig, pick cue.Value) (val cue.Value, err error)

func QueryValues added in v0.5.4

func QueryValues(orig, query cue.Value) (cue.Value, error)

func RunDiffFromArgs added in v0.5.4

func RunDiffFromArgs(orig, next string, entrypoints []string) error

func RunMaskFromArgs added in v0.5.4

func RunMaskFromArgs(orig, mask string, entrypoints []string) error

func RunMergeFromArgs added in v0.5.4

func RunMergeFromArgs(orig, update string, entrypoints []string) error

func RunPickFromArgs added in v0.5.4

func RunPickFromArgs(orig, pick string, entrypoints []string) error

func RunQueryFromArgs added in v0.5.4

func RunQueryFromArgs(orig, expr string, entrypoints []string) error

Types

type DiffOp

type DiffOp struct {
	Op        string
	Path      string
	Value     interface{}
	PrevValue interface{}
}

Jump to

Keyboard shortcuts

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