types

package
v0.15.3 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFieldNotFound must be returned by Document implementations, when calling the GetByField method and
	// the field wasn't found in the document.
	ErrFieldNotFound = errors.New("field not found")
	// ErrValueNotFound must be returned by Array implementations, when calling the GetByIndex method and
	// the index wasn't found in the array.
	ErrValueNotFound = errors.New("value not found")
)

Functions

func As added in v0.15.0

func As[T any](v Value) T

func Fields

func Fields(d Document) ([]string, error)

Fields returns a list of all the fields at the root of the document sorted lexicographically.

func Is added in v0.15.0

func Is[T any](v Value) (T, bool)

func IsEqual

func IsEqual(v, other Value) (bool, error)

IsEqual returns true if v is equal to the given value.

func IsGreaterThan

func IsGreaterThan(v, other Value) (bool, error)

IsGreaterThan returns true if v is greather than the given value.

func IsGreaterThanOrEqual

func IsGreaterThanOrEqual(v, other Value) (bool, error)

IsGreaterThanOrEqual returns true if v is greather than or equal to the given value.

func IsLesserThan

func IsLesserThan(v, other Value) (bool, error)

IsLesserThan returns true if v is lesser than the given value.

func IsLesserThanOrEqual

func IsLesserThanOrEqual(v, other Value) (bool, error)

IsLesserThanOrEqual returns true if v is lesser than or equal to the given value.

func IsNotEqual

func IsNotEqual(v, other Value) (bool, error)

IsNotEqual returns true if v is not equal to the given value.

func IsNull added in v0.15.1

func IsNull(v Value) bool

func IsTruthy

func IsTruthy(v Value) (bool, error)

IsTruthy returns whether v is not equal to the zero value of its type.

func IsZeroValue

func IsZeroValue(v Value) (bool, error)

IsZeroValue indicates if the value data is the zero value for the value type. This function doesn't perform any allocation.

func MarshalTextIndent added in v0.14.0

func MarshalTextIndent(v Value, prefix, indent string) ([]byte, error)

Types

type Array

type Array interface {
	// Iterate goes through all the values of the array and calls the given function by passing each one of them.
	// If the given function returns an error, the iteration stops.
	Iterate(fn func(i int, value Value) error) error
	// GetByIndex returns a value by index of the array.
	GetByIndex(i int) (Value, error)

	// MarshalJSON implements the json.Marshaler interface.
	// It returns a JSON representation of the array.
	MarshalJSON() ([]byte, error)
}

An Array contains a set of values.

type Document

type Document interface {
	// Iterate goes through all the fields of the document and calls the given function by passing each one of them.
	// If the given function returns an error, the iteration stops.
	Iterate(fn func(field string, value Value) error) error
	// GetByField returns a value by field name.
	// Must return ErrFieldNotFound if the field doesn't exist.
	GetByField(field string) (Value, error)

	// MarshalJSON implements the json.Marshaler interface.
	// It returns a JSON representation of the document.
	MarshalJSON() ([]byte, error)
}

A Document represents a group of key value pairs.

type Value

type Value interface {
	Type() ValueType
	V() any
	String() string
	MarshalJSON() ([]byte, error)
	MarshalText() ([]byte, error)
}

func Add

func Add(v1, v2 Value) (res Value, err error)

Add u to v and return the result. Only numeric values and booleans can be added together.

func BitwiseAnd

func BitwiseAnd(v1, v2 Value) (res Value, err error)

BitwiseAnd calculates v & u and returns the result. Only numeric values and booleans can be calculated together. If both v and u are integers, the result will be an integer.

func BitwiseOr

func BitwiseOr(v1, v2 Value) (res Value, err error)

BitwiseOr calculates v | u and returns the result. Only numeric values and booleans can be calculated together. If both v and u are integers, the result will be an integer.

func BitwiseXor

func BitwiseXor(v1, v2 Value) (res Value, err error)

BitwiseXor calculates v ^ u and returns the result. Only numeric values and booleans can be calculated together. If both v and u are integers, the result will be an integer.

func Div

func Div(v1, v2 Value) (res Value, err error)

Div calculates v / u and returns the result. Only numeric values and booleans can be calculated together. If both v and u are integers, the result will be an integer.

func Mod

func Mod(v1, v2 Value) (res Value, err error)

Mod calculates v / u and returns the result. Only numeric values and booleans can be calculated together. If both v and u are integers, the result will be an integer.

func Mul

func Mul(v1, v2 Value) (res Value, err error)

Mul calculates v * u and returns the result. Only numeric values and booleans can be calculated together.

func NewArrayValue

func NewArrayValue(a Array) Value

NewArrayValue returns a value of type Array.

func NewBlobValue

func NewBlobValue(x []byte) Value

NewBlobValue encodes x and returns a value.

func NewBoolValue

func NewBoolValue(x bool) Value

NewBoolValue encodes x and returns a value.

func NewDocumentValue

func NewDocumentValue(d Document) Value

NewDocumentValue returns a value of type Document.

func NewDoubleValue

func NewDoubleValue(x float64) Value

NewDoubleValue encodes x and returns a value.

func NewIntegerValue

func NewIntegerValue(x int64) Value

NewIntegerValue encodes x and returns a value whose type depends on the magnitude of x.

func NewNullValue

func NewNullValue() Value

NewNullValue returns a Null value.

func NewTextValue

func NewTextValue(x string) Value

NewTextValue encodes x and returns a value.

func NewValueWith

func NewValueWith[T any](t ValueType, v T) Value

NewValueWith creates a value with the given type and value.

func Sub

func Sub(v1, v2 Value) (res Value, err error)

Sub calculates v - u and returns the result. Only numeric values and booleans can be calculated together.

type ValueType

type ValueType uint8

ValueType represents a value type supported by the database.

const (
	// AnyValue denotes the absence of type
	AnyValue ValueType = 0x00

	NullValue ValueType = 0x05

	BooleanValue ValueType = 0x10

	IntegerValue ValueType = 0x20

	DoubleValue ValueType = 0xD0

	TextValue ValueType = 0xDA

	BlobValue ValueType = 0xE0

	ArrayValue ValueType = 0xE6

	DocumentValue ValueType = 0xF0
)

List of supported value types.

func (ValueType) IsAny

func (t ValueType) IsAny() bool

IsAny returns whether this is type is Any or a real type

func (ValueType) IsNumber

func (t ValueType) IsNumber() bool

IsNumber returns true if t is either an integer of a float.

func (ValueType) String

func (t ValueType) String() string

Jump to

Keyboard shortcuts

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