calculation

package
v0.0.0-...-ad801e6 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareValue

func CompareValue(v1, v2 ValueUnion) (int, error)

CompareValue is a convience method that compares two value unions. If the two values have different field types, the result is undefined and the method always returns -1. Otherwise, the corresponding values are compared, and the method returns * -1 if v1 < v2 * 0 if v1 == v2 * 1 if v1 > v2

func MustCompareValue

func MustCompareValue(v1, v2 ValueUnion) int

MustCompareValue compares two value unions, and panics if it encounters an error.

func MustReverseCompareValue

func MustReverseCompareValue(v1, v2 ValueUnion) int

MustReverseCompareValue reverse compares two value unions, and panics if it encounters an error.

Types

type FieldValueToValueFn

type FieldValueToValueFn func(v *field.ValueUnion) ValueUnion

FieldValueToValueFn converts a field value to a calculation value union.

func AsValueFn

AsValueFn returns a function that converts a field value union with the given field type to a calculation value union.

func AsValueFns

func AsValueFns(fieldTypes field.OptionalTypeArray) ([]FieldValueToValueFn, error)

AsValueFns returns a list of value conversion functions for the given list of field types.

type NewResultArrayFn

type NewResultArrayFn func() (ResultArray, error)

NewResultArrayFn creates a new result array.

type NewResultArrayFromValueTypesFn

type NewResultArrayFromValueTypesFn func(valueTypes field.OptionalTypeArray) (ResultArray, error)

NewResultArrayFromValueTypesFn creates a new result array based on the field value types.

type Op

type Op int

Op represents a calculation operator.

const (
	UnknownOp Op = iota
	Count
	Sum
	Avg
	Min
	Max
)

A list of supported calculation operators.

func (Op) AllowedTypes

func (f Op) AllowedTypes() (field.ValueTypeSet, error)

AllowedTypes returns a list of value types that are allowed for the given calculation operator.

func (Op) IsValid

func (f Op) IsValid() bool

IsValid returns true if a calculation operator is valid.

func (Op) MustNewResult

func (f Op) MustNewResult(t field.ValueType) Result

MustNewResult creates a new result based on the operator and possibly the given field value type, and panics if an error is encountered.

func (Op) NewResult

func (f Op) NewResult(t field.ValueType) (Result, error)

NewResult creates a new result based on the operator and possibly the given field value type.

func (Op) RequiresField

func (f Op) RequiresField() bool

RequiresField returns true if the calculation may be performed against a field that differs from the set of grouping fields.

func (Op) String

func (f Op) String() string

String returns the string representation of the calculation operator.

func (*Op) ToOptionalProto

func (f *Op) ToOptionalProto() (servicepb.OptionalCalculationOp, error)

ToOptionalProto converts the calculation op to an optional calculation op protobuf message.

func (Op) ToProto

func (f Op) ToProto() (servicepb.Calculation_Op, error)

ToProto converts the calculation op to a calculation op protobuf message.

func (*Op) UnmarshalJSON

func (f *Op) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a JSON object as a calculation operator.

type Result

type Result interface {
	// New creates a new result with the same result type.
	New() Result

	// Add adds a value to the result.
	// TODO(xichen): Perhaps make this API take a `*ValueUnion` instead to save copy time.
	Add(v ValueUnion)

	// MergeInPlace merges the current result with the other result in place.
	MergeInPlace(other Result) error

	// Value returns the result value.
	Value() ValueUnion

	// MarshalJSON marshals the result as a JSON object.
	MarshalJSON() ([]byte, error)
}

Result represents a merge-able calculation result. In simple cases this can be simply a single numeric value or a bytes value. However, there are also cases where calculation of a result requires calculations of other intermediate values, which eventually get transformed into the final result (e.g., the average value of a field).

func NewAvgResult

func NewAvgResult() Result

NewAvgResult creates a new average result.

func NewCountResult

func NewCountResult() Result

NewCountResult creates a new count result.

func NewMaxBytesResult

func NewMaxBytesResult() Result

NewMaxBytesResult creates a new maximum bytes result.

func NewMaxNumberResult

func NewMaxNumberResult() Result

NewMaxNumberResult creates a new maximum number result.

func NewMinBytesResult

func NewMinBytesResult() Result

NewMinBytesResult creates a new minimum bytes result.

func NewMinNumberResult

func NewMinNumberResult() Result

NewMinNumberResult creates a new minimum number result.

func NewSumResult

func NewSumResult() Result

NewSumResult creates a new sum result.

type ResultArray

type ResultArray []Result

ResultArray is an array of calculation result.

func (ResultArray) MergeInPlace

func (arr ResultArray) MergeInPlace(other ResultArray)

MergeInPlace merges the other result array into the current array in place. Precondition: len(arr) == len(other).

func (ResultArray) New

func (arr ResultArray) New() ResultArray

New creates a new result array where each result is created anew from the existing result in the corresponding slot.

func (ResultArray) ToProto

func (arr ResultArray) ToProto() []servicepb.CalculationValue

ToProto converts a result array to a value array protobuf message.

type ValueCompareFn

type ValueCompareFn func(v1, v2 ValueUnion) int

ValueCompareFn compares two value unions.

type ValueType

type ValueType int

ValueType is the type of a calculation value.

const (
	NumberType ValueType = iota
	BytesType
)

A list of supported value types.

type ValueUnion

type ValueUnion struct {
	Type      ValueType
	NumberVal float64
	BytesVal  xbytes.Bytes
}

ValueUnion is a value union.

func NewBytesUnion

func NewBytesUnion(v xbytes.Bytes) ValueUnion

NewBytesUnion creates a new string union.

func NewNumberUnion

func NewNumberUnion(v float64) ValueUnion

NewNumberUnion creates a new number union.

func NewValueFromProto

func NewValueFromProto(pbValue servicepb.CalculationValue) (ValueUnion, error)

NewValueFromProto creates a value from protobuf message.

func (ValueUnion) MarshalJSON

func (u ValueUnion) MarshalJSON() ([]byte, error)

MarshalJSON marshals value as a JSON object.

func (ValueUnion) ToProto

ToProto converts a value to a calculation value protobuf message.

type Values

type Values []ValueUnion

Values is a list of calculation values.

func NewValuesFromProto

func NewValuesFromProto(pbValues []servicepb.CalculationValue) (Values, error)

NewValuesFromProto creates a list of calculation values from protobuf message.

Jump to

Keyboard shortcuts

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