compare

package
v0.0.57 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2021 License: Apache-2.0, NCSA Imports: 6 Imported by: 9

Documentation

Overview

Package compare implements comparisons between Kythe values.

Index

Constants

This section is empty.

Variables

View Source
var (
	ByVNameSignature = By(func(x interface{}) interface{} {
		return x.(*spb.VName).GetSignature()
	})
	ByVNameCorpus = By(func(x interface{}) interface{} {
		return x.(*spb.VName).GetCorpus()
	})
	ByVNameRoot = By(func(x interface{}) interface{} {
		return x.(*spb.VName).GetRoot()
	})
	ByVNamePath = By(func(x interface{}) interface{} {
		return x.(*spb.VName).GetPath()
	})
	ByVNameLanguage = By(func(x interface{}) interface{} {
		return x.(*spb.VName).GetLanguage()
	})
)

Options for comparing components of *spb.VName protobuf messages.

View Source
var (
	ByEntrySource = And(By(func(x interface{}) interface{} {
		return x.(*spb.Entry).GetSource()
	}), With(func(a, b interface{}) Order { return VNames(a.(*spb.VName), b.(*spb.VName)) }))
	ByEntryEdgeKind = By(func(x interface{}) interface{} { return x.(*spb.Entry).GetEdgeKind() })
	ByEntryFactName = By(func(x interface{}) interface{} { return x.(*spb.Entry).GetFactName() })
	ByEntryTarget   = And(By(func(x interface{}) interface{} {
		return x.(*spb.Entry).GetTarget()
	}), With(func(a, b interface{}) Order { return VNames(a.(*spb.VName), b.(*spb.VName)) }))
)

Options for comparing components of *spb.Entry protobuf messages.

Functions

func EntriesEqual

func EntriesEqual(e1, e2 *spb.Entry) bool

EntriesEqual reports whether e1 and e2 are equivalent, including their fact values (if any).

func ProtoDiff added in v0.0.30

func ProtoDiff(x, y interface{}, opts ...cmp.Option) string

ProtoDiff returns a human-readable report of the differences between two values, ensuring that any proto.Message values are compared correctly with proto.Equal.

See github.com/google/go-cmp/cmp for more details.

func VNamesEqual

func VNamesEqual(v1, v2 *spb.VName) bool

VNamesEqual reports whether v1 and v2 are equal.

Types

type By

type By func(interface{}) interface{}

By is an Option that transforms a value before performing a comparison. It should work on both sides of the comparison equivalently.

type ByEntries

type ByEntries []*spb.Entry

ByEntries is a min-heap of entries, ordered by Entries.

func (ByEntries) Len

func (s ByEntries) Len() int

Implement the sort.Interface

func (ByEntries) Less

func (s ByEntries) Less(i, j int) bool

func (*ByEntries) Pop

func (s *ByEntries) Pop() interface{}

Pop implements part of the heap.Interface

func (*ByEntries) Push

func (s *ByEntries) Push(v interface{})

Push implements part of the heap.Interface

func (ByEntries) Swap

func (s ByEntries) Swap(i, j int)

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option changes the behavior of the generic comparisons.

func And

func And(opt ...Option) Option

And is an Option that unions other Options.

func Reversed

func Reversed() Option

Reversed returns an Option that reverses the resulting Order from a comparison.

type Order

type Order int

An Order represents an ordering relationship between values.

const (
	LT Order = -1 // lhs < rhs
	EQ Order = 0  // lhs == rhs
	GT Order = 1  // lhs > rhs
)

LT, EQ, and GT are the standard values for an Order.

func Bools added in v0.0.51

func Bools(a, b bool) Order

Bools returns LT if !a && b, EQ if a == b, or GT if a && !b.

func Bytes

func Bytes(s, t []byte) Order

Bytes returns LT if s < t, EQ if s == t, or GT if s > t.

func Compare

func Compare(a, b interface{}, opts ...Option) (o Order)

Compare returns the Order between two arbitrary values of the same type.

Only the following types are currently supported:

{string, int, int32, []byte, bool}.

Options may be provided to change the semantics of the comparison. Other types may be compared if an appropriate By Option transforms the values into a supported type or a With Option is provided for the types given.

Note: this function panics if a and b are different types

func Entries

func Entries(e1, e2 *spb.Entry) Order

Entries reports whether e1 is LT, GT, or EQ to e2 in entry order, ignoring fact values (if any).

The ordering for entries is defined by lexicographic comparison of [source, edge kind, fact name, target].

func Ints

func Ints(a, b int) Order

Ints returns LT if a < b, EQ if a == b, or GT if a > b.

func Seq

func Seq(a, b interface{}, opts ...Option) Order

Seq sequences comparisons on the same two values over different Options. If len(opts) == 0, Seq merely returns Compare(a, b). For len(opts) > 0, Seq returns Compare(a, b, opts[0]).AndThen(a, b, opts[1])....AndThen(a, b, opts[len(opts)-1]).

func Strings

func Strings(s, t string) Order

Strings returns LT if s < t, EQ if s == t, or GT if s > t.

func ToOrder

func ToOrder(c int) Order

ToOrder returns LT if c < 0, EQ if c == 0, or GT if c > 0.

func VNames

func VNames(v1, v2 *spb.VName) Order

VNames returns LT if v1 precedes v2, EQ if v1 and v2 are equal, or GT if v1 follows v2, in standard order. The ordering for VNames is defined by lexicographic comparison of [signature, corpus, root, path, language].

func ValueEntries

func ValueEntries(e1, e2 *spb.Entry) Order

ValueEntries reports whether e1 is LT, GT, or EQ to e2 in entry order, including fact values (if any).

func (Order) AndThen

func (o Order) AndThen(a, b interface{}, opts ...Option) Order

AndThen returns o if o != EQ. Otherwise, the Order between a and b is determined and returned. AndThen can be used to chain comparisons.

Examples:

Compare(a, b, By(someField)).AndThen(a, b, By(someOtherField))

Entries(e1, e2).AndThen(e1.FactValue, e2.FactValue)

func (Order) Reverse

func (o Order) Reverse() Order

Reverse reverses the Order: LT -> GT, GT -> LT; EQ -> EQ.

func (Order) String

func (o Order) String() string

type With

type With func(a, b interface{}) Order

With is an Option that provides a custom comparison function for the final values being compared. Only the last With Option passed to Compare will be honored.

Jump to

Keyboard shortcuts

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