types

package
v0.0.0-...-f5d25ad Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package types provides a type system for the add engine. Values have a type, and types are described by type descriptors. Types define operations on their values, such as comparison.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Bool is the Bool type. Bools are comparable with true>false. The name of
	// this type is "Bool".
	Bool = BoolType{
		// contains filtered or unexported fields
	}
)
View Source
var (
	// Date is the date type. Dates are comparable. A date that is later than
	// another date is considered larger. The name of this type is "Date".
	Date = DateType{
		// contains filtered or unexported fields
	}
)
View Source
var (
	// Function is the function type. Functions are not comparable. The name of
	// this type is "Function".
	Function = FunctionType{
		// contains filtered or unexported fields
	}
)
View Source
var (
	// Integer is the date type. Integers are comparable. The name of this type
	// is "Integer".
	Integer = IntegerType{
		// contains filtered or unexported fields
	}
)
View Source
var (
	// Real is the date type. Reals are comparable. The name of this type
	// is "Real".
	Real = RealType{
		// contains filtered or unexported fields
	}
)
View Source
var (
	// String is the string type. Strings are comparable. Comparison is done
	// lexicographically. The name of this type is "String".
	String = StringType{
		// contains filtered or unexported fields
	}
)

Functions

This section is empty.

Types

type ArithmeticAdder

type ArithmeticAdder interface {
	Add(Value, Value) (Value, error)
}

ArithmeticAdder wraps the arithmetic add operation, usually represented by a '+'. The actual addition is defined and must be documented by the implementing type.

type ArithmeticDivider

type ArithmeticDivider interface {
	Div(Value, Value) (Value, error)
}

ArithmeticDivider wraps the arithmetic div operation, usually represented by a '/'. The actual division is defined and must be documented by the implementing type.

type ArithmeticExponentiator

type ArithmeticExponentiator interface {
	Pow(Value, Value) (Value, error)
}

ArithmeticExponentiator wraps the arithmetic pow operation, usually represented by a '**'. The actual exponentiation is defined and must be documented by the implementing type.

type ArithmeticModulator

type ArithmeticModulator interface {
	Mod(Value, Value) (Value, error)
}

ArithmeticModulator wraps the arithmetic mod operation, usually represented by a '%'. The actual modulation is defined and must be documented by the implementing type.

type ArithmeticMultiplicator

type ArithmeticMultiplicator interface {
	Mul(Value, Value) (Value, error)
}

ArithmeticMultiplicator wraps the arithmetic mul operation, usually represented by a '*'. The actual multiplication is defined and must be documented by the implementing type.

type ArithmeticSubtractor

type ArithmeticSubtractor interface {
	Sub(Value, Value) (Value, error)
}

ArithmeticSubtractor wraps the arithmetic sub operation, usually represented by a '-'. The actual subtraction is defined and must be documented by the implementing type.

type BoolType

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

BoolType is a basic type. Values of this type describe a boolean value, either true or false.

func (BoolType) Compare

func (t BoolType) Compare(left, right Value) (int, error)

Compare compares two bool values. For this to succeed, both values must be of type BoolValue and be not nil. The bool value true is considered larger than false. This method will return 1 if left>right, 0 if left==right, and -1 if left<right.

func (BoolType) Deserialize

func (BoolType) Deserialize(data []byte) (Value, error)

Deserialize can deserialize a single byte to a bool value. If the length of the given data is not 1, an error will be returned. A zero byte will be deserialized to the value false, while everything else will be deserialized to the value true.

func (BoolType) Name

func (t BoolType) Name() string

func (BoolType) Serialize

func (t BoolType) Serialize(v Value) ([]byte, error)

Serialize can serialize bool values to a single byte. false will be serialized to 0x00, while true will be serialized to 0x01.

func (BoolType) String

func (t BoolType) String() string

type BoolValue

type BoolValue struct {

	// Value is the underlying primitive value.
	Value bool
	// contains filtered or unexported fields
}

BoolValue is a value of type Bool. The boolean value is held as a primitive Go bool.

func NewBool

func NewBool(v bool) BoolValue

NewBool creates a new value of type Bool.

func (BoolValue) Is

func (v BoolValue) Is(t Type) bool

Is checks whether this value is of the given type.

func (BoolValue) IsNull

func (v BoolValue) IsNull() bool

func (BoolValue) String

func (v BoolValue) String() string

String returns "true" or "false", depending on the value of this bool value.

func (BoolValue) Type

func (v BoolValue) Type() Type

type Caster

type Caster interface {
	Cast(Value) (Value, error)
}

Caster wraps the Cast method, which is used to transform the input value into an output value. Types can implement this interface. E.g. if the type String implements Caster, any value passed into the Cast method should be attempted to be cast to String, or an error should be returned.

type Comparator

type Comparator interface {
	// Compare compares the given to values left and right as follows. -1 if
	// left<right, 0 if left==right, 1 if left>right. However, NULL<any, so if
	// the left value is NULL, and is comparable to the right value (same type),
	// this will return -1 and no error. NULL~NULL is undefined.
	Compare(left, right Value) (int, error)
}

Comparator is the interface that wraps the basic compare method. The compare method compares the left and right value as follows. -1 if left<right, 0 if left==right, 1 if left>right. What exectly is considered to be <, ==, > is up to the implementation. By definition, the NULL value is smaller than any other value. When comparing NULL to another NULL value, and both NULLs have the same type, the result is undefined, however, no error must be returned.

type DateType

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

DateType is a comparable type.

func (DateType) Compare

func (t DateType) Compare(left, right Value) (int, error)

Compare compares two date values. For this to succeed, both values must be of type DateValue and be not nil. A date later than another date is considered larger. This method will return 1 if left>right, 0 if left==right, and -1 if left<right.

func (DateType) Name

func (t DateType) Name() string

func (DateType) String

func (t DateType) String() string

type DateValue

type DateValue struct {

	// Value is the underlying primitive value.
	Value time.Time
	// contains filtered or unexported fields
}

DateValue is a value of type Date.

func NewDate

func NewDate(v time.Time) DateValue

NewDate creates a new value of type Date.

func (DateValue) Is

func (v DateValue) Is(t Type) bool

Is checks whether this value is of the given type.

func (DateValue) IsNull

func (v DateValue) IsNull() bool

func (DateValue) String

func (v DateValue) String() string

func (DateValue) Type

func (v DateValue) Type() Type

type Error

type Error string

Error is a sentinel error.

func ErrCannotCast

func ErrCannotCast(from, to Type) Error

ErrCannotCast returns an error that indicates, that a case from the given from type to the given to type cannot be performed.

func ErrDataSizeMismatch

func ErrDataSizeMismatch(expectedSize, gotSize int) Error

ErrDataSizeMismatch returns an error that indicates, that data which had an unexpected size was passed in. This will be useful for functions that expect fixed-size data.

func ErrTypeMismatch

func ErrTypeMismatch(expected, got Type) Error

ErrTypeMismatch returns an error that indicates a type mismatch, and includes the expected and the actual type.

func (Error) Error

func (e Error) Error() string

type FunctionType

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

FunctionType is a non-comparable type.

func (FunctionType) Name

func (t FunctionType) Name() string

func (FunctionType) String

func (t FunctionType) String() string

type FunctionValue

type FunctionValue struct {
	Name string
	Args []Value
	// contains filtered or unexported fields
}

FunctionValue is a value of type Function. This can not be called, it is simply a shell that holds a function name and the arguments, that were used in the SQL statement. It is the engine's responsibility, to execute the appropriate code to make this function call happen.

func NewFunction

func NewFunction(name string, args ...Value) FunctionValue

NewFunction creates a new value of type Function.

func (FunctionValue) Is

func (v FunctionValue) Is(t Type) bool

Is checks whether this value is of the given type.

func (FunctionValue) IsNull

func (v FunctionValue) IsNull() bool

func (FunctionValue) String

func (v FunctionValue) String() string

func (FunctionValue) Type

func (v FunctionValue) Type() Type

type IntegerType

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

IntegerType is a comparable type.

func (IntegerType) Add

func (t IntegerType) Add(left, right Value) (Value, error)

Add adds the left and right value, producing a new integer value. This only works, if left and right are of type integer.

func (IntegerType) Compare

func (t IntegerType) Compare(left, right Value) (int, error)

Compare compares two date values. For this to succeed, both values must be of type IntegerValue and be not nil. A date later than another date is considered larger. This method will return 1 if left>right, 0 if left==right, and -1 if left<right.

func (IntegerType) Div

func (t IntegerType) Div(left, right Value) (Value, error)

Div divides the left by the right value, producing a new real value. This only works, if left and right are of type integer.

func (IntegerType) Mod

func (t IntegerType) Mod(left, right Value) (Value, error)

Mod modulates the left and right value, producing a new integer value. This only works, if left and right are of type integer.

func (IntegerType) Mul

func (t IntegerType) Mul(left, right Value) (Value, error)

Mul multiplicates the left and right value, producing a new integer value. This only works, if left and right are of type integer.

func (IntegerType) Name

func (t IntegerType) Name() string

func (IntegerType) Pow

func (t IntegerType) Pow(left, right Value) (Value, error)

Pow exponentiates the left and right value, producing a new integer value. This only works, if left and right are of type integer.

func (IntegerType) String

func (t IntegerType) String() string

func (IntegerType) Sub

func (t IntegerType) Sub(left, right Value) (Value, error)

Sub subtracts the right from the left value, producing a new integer value. This only works, if left and right are of type integer.

type IntegerValue

type IntegerValue struct {

	// Value is the underlying primitive value.
	Value int64
	// contains filtered or unexported fields
}

IntegerValue is a value of type Integer.

func NewInteger

func NewInteger(v int64) IntegerValue

NewInteger creates a new value of type Integer.

func (IntegerValue) Is

func (v IntegerValue) Is(t Type) bool

Is checks whether this value is of the given type.

func (IntegerValue) IsNull

func (v IntegerValue) IsNull() bool

func (IntegerValue) String

func (v IntegerValue) String() string

func (IntegerValue) Type

func (v IntegerValue) Type() Type

type IsNuller

type IsNuller interface {
	IsNull() bool
}

IsNuller wraps the method IsNull, which determines whether the value is a null value or not.

type IsTyper

type IsTyper interface {
	Is(Type) bool
}

IsTyper wraps the basic method Is, which can check the type of a value.

type NullValue

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

NullValue is a value of type null. It has no actual value.

func NewNull

func NewNull(t Type) NullValue

NewNull creates a new value of type null, with the given type.

func (NullValue) Is

func (v NullValue) Is(t Type) bool

Is checks whether this value is of the given type.

func (NullValue) IsNull

func (v NullValue) IsNull() bool

func (NullValue) String

func (v NullValue) String() string

String "NULL", appended to the type of this value in parenthesis.

func (NullValue) Type

func (v NullValue) Type() Type

type RealType

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

RealType is a comparable type.

func (RealType) Add

func (t RealType) Add(left, right Value) (Value, error)

Add adds the left and right value, producing a new real value. This only works, if left and right are of type real.

func (RealType) Compare

func (t RealType) Compare(left, right Value) (int, error)

Compare compares two real values. For this to succeed, both values must be of type RealValue and be not nil.

func (RealType) Div

func (t RealType) Div(left, right Value) (Value, error)

Div divides the left by the right value, producing a new real value. This only works, if left and right are of type real.

func (RealType) Mul

func (t RealType) Mul(left, right Value) (Value, error)

Mul multiplicates the left and right value, producing a new real value. This only works, if left and right are of type real.

func (RealType) Name

func (t RealType) Name() string

func (RealType) Pow

func (t RealType) Pow(left, right Value) (Value, error)

Pow exponentiates the left and right value, producing a new real value. This only works, if left and right are of type real.

func (RealType) String

func (t RealType) String() string

func (RealType) Sub

func (t RealType) Sub(left, right Value) (Value, error)

Sub subtracts the right from the left value, producing a new real value. This only works, if left and right are of type real.

type RealValue

type RealValue struct {

	// Value is the underlying primitive value.
	Value float64
	// contains filtered or unexported fields
}

RealValue is a value of type Real.

func NewReal

func NewReal(v float64) RealValue

NewReal creates a new value of type Real.

func (RealValue) Is

func (v RealValue) Is(t Type) bool

Is checks whether this value is of the given type.

func (RealValue) IsNull

func (v RealValue) IsNull() bool

func (RealValue) String

func (v RealValue) String() string

func (RealValue) Type

func (v RealValue) Type() Type

type Serializer

type Serializer interface {
	Serialize(Value) ([]byte, error)
	Deserialize([]byte) (Value, error)
}

Serializer wraps two basic methods for two-way serializing values. Which values can be serialized and deserialized, is up to the implementing object. It must be documented, what can and can not be serialized and deserialized.

type StringType

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

StringType is a comparable type.

func (StringType) Add

func (t StringType) Add(left, right Value) (Value, error)

Add concatenates the left and right right value. This only works, if left and right are string values.

func (StringType) Cast

func (StringType) Cast(v Value) (Value, error)

Cast attempts to cast the given value to a String. This is done by returning a string representing the string value of the given value.

func (StringType) Compare

func (t StringType) Compare(left, right Value) (int, error)

Compare for the String is defined as the lexicographical comparison of the two underlying primitive values. This method will return 1 if left>right, 0 if left==right, and -1 if left<right.

func (StringType) Deserialize

func (t StringType) Deserialize(data []byte) (Value, error)

Deserialize reads the data size from the first 4 passed-in bytes, and then converts the rest of the bytes to a string leveraging the Go runtime.

func (StringType) Name

func (t StringType) Name() string

func (StringType) Serialize

func (t StringType) Serialize(v Value) ([]byte, error)

Serialize serializes the internal string value as 4-byte-framed byte sequence.

func (StringType) String

func (t StringType) String() string

type StringValue

type StringValue struct {

	// Value is the underlying primitive value.
	Value string
	// contains filtered or unexported fields
}

StringValue is a value of type String.

func NewString

func NewString(v string) StringValue

NewString creates a new value of type String.

func (StringValue) Is

func (v StringValue) Is(t Type) bool

Is checks whether this value is of the given type.

func (StringValue) IsNull

func (v StringValue) IsNull() bool

func (StringValue) String

func (v StringValue) String() string

func (StringValue) Type

func (v StringValue) Type() Type

type Type

type Type interface {
	Name() string
	fmt.Stringer
}

Type is a data type that consists of a type descriptor and a comparator. The comparator forces types to define relations between two values of this type. A type is only expected to be able to handle values of its own type.

func ByIndicator

func ByIndicator(indicator TypeIndicator) Type

ByIndicator accepts a type indicator and returns the corresponding type. If the returned type is nil, the type indicator is unknown.

type TypeIndicator

type TypeIndicator uint8

TypeIndicator is a type that is used to serialize types. Each indicator corresponds to a type.

const (
	TypeIndicatorUnknown TypeIndicator = iota
	TypeIndicatorBool
	TypeIndicatorDate
	TypeIndicatorInteger
	TypeIndicatorReal
	TypeIndicatorString
)

Known type indicators corresponding to known types.

func (TypeIndicator) String

func (i TypeIndicator) String() string

type Value

type Value interface {
	Type() Type
	IsTyper
	IsNuller
	fmt.Stringer
}

Value describes an object that can be used as a value in the engine. A value has a type, which can be used to compare this value to another one.

Jump to

Keyboard shortcuts

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