values

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BoolWrapper = &wrapperFuncs[bool]{
	copy: copyValue[bool],
	marshal: func(value bool) ([]byte, error) {
		if value {
			return []byte{1}, nil
		}
		return []byte{0}, nil
	},
	unmarshal: func(data []byte) (bool, error) {
		if len(data) < 1 {
			return false, encoding.ErrNotEnoughData
		}
		return data[0] == 1, nil
	},
}

BoolWrapper defines un/marshalling functions for boolean fields.

View Source
var BytesWrapper = &wrapperFuncs[[]byte]{
	copy:      func(v []byte) []byte { u := make([]byte, len(v)); copy(u, v); return u },
	marshal:   oldMarshal(encoding.MarshalBytes),
	unmarshal: encoding.UnmarshalBytes,
}

BytesWrapper defines un/marshalling functions for byte slice fields.

View Source
var ErrNotEnoughData = errors.BadRequest.With("not enough data")
View Source
var ErrOverflow = errors.BadRequest.With("overflow")
View Source
var HashWrapper = &wrapperFuncs[[32]byte]{
	copy:      copyValue[[32]byte],
	marshal:   oldMarshalPtr(encoding.MarshalHash),
	unmarshal: encoding.UnmarshalHash,
}

HashWrapper defines un/marshalling functions for hash fields.

View Source
var IntWrapper = &wrapperFuncs[int64]{
	copy:      copyValue[int64],
	marshal:   oldMarshal(encoding.MarshalInt),
	unmarshal: encoding.UnmarshalInt,
}

IntWrapper defines un/marshalling functions for int fields.

View Source
var RawWrapper = &wrapperFuncs[[]byte]{
	copy:      func(v []byte) []byte { u := make([]byte, len(v)); copy(u, v); return u },
	marshal:   func(value []byte) ([]byte, error) { return value, nil },
	unmarshal: func(data []byte) ([]byte, error) { return data, nil },
}

RawWrapper defines un/marshalling functions for raw byte slice fields.

View Source
var StringWrapper = &wrapperFuncs[string]{
	copy:      copyValue[string],
	marshal:   oldMarshal(encoding.MarshalString),
	unmarshal: encoding.UnmarshalString,
}

StringWrapper defines un/marshalling functions for string fields.

View Source
var TxidWrapper = &wrapperFuncs[*url.TxID]{
	copy:      copyValue[*url.TxID],
	marshal:   marshalAsString[*url.TxID],
	unmarshal: unmarshalFromString(url.ParseTxID),
}

TxidWrapper defines un/marshalling functions for txid fields.

View Source
var UintWrapper = &wrapperFuncs[uint64]{
	copy:      copyValue[uint64],
	marshal:   oldMarshal(encoding.MarshalUint),
	unmarshal: encoding.UnmarshalUint,
}

UintWrapper defines un/marshalling functions for uint fields.

View Source
var UrlWrapper = &wrapperFuncs[*url.URL]{
	copy:      copyValue[*url.URL],
	marshal:   marshalAsString[*url.URL],
	unmarshal: unmarshalFromString(url.Parse),
}

UrlWrapper defines un/marshalling functions for url fields.

Functions

func Commit

func Commit[T database.Record](lastErr *error, v T)

Commit commits the record, if it is non-nil.

func CompareBigInt added in v1.3.0

func CompareBigInt(u, v *big.Int) int

func CompareBool added in v1.3.0

func CompareBool(u, v bool) int

func CompareBytes added in v1.3.0

func CompareBytes(u, v []byte) int

func CompareDuration added in v1.3.0

func CompareDuration(u, v time.Duration) int

func CompareFloat added in v1.3.0

func CompareFloat(u, v float64) int

func CompareHash

func CompareHash(u, v [32]byte) int

func CompareInt added in v1.3.0

func CompareInt(u, v int64) int

func CompareString added in v1.2.0

func CompareString(u, v string) int

func CompareTime added in v1.3.0

func CompareTime(u, v time.Time) int

func CompareTxid

func CompareTxid(u, v *url.TxID) int

func CompareUint

func CompareUint(u, v uint64) int

func CompareUrl

func CompareUrl(u, v *url.URL) int

func GetOrCreate

func GetOrCreate[Container any, Record any](c Container, ptr *Record, create func(Container) Record) Record

GetOrCreate retrieves an existing record or creates, records, and assigns a new record to the field.

func GetOrCreateMap

func GetOrCreateMap[Container any, Record any, Key keyType[MapKey], MapKey comparable](c Container, ptr *map[MapKey]Record, key Key, create func(Container, Key) Record) Record

GetOrCreateMap retrieves an existing record or creates, records, and assigns a new record to the map.

func IsDirty

func IsDirty[T database.Record](v T) bool

IsDirty returns true if the record is non-nil and is dirty.

func MapKeyBytes

func MapKeyBytes(v []byte) [32]byte

func MapKeyUrl

func MapKeyUrl(v *url.URL) [32]byte

func ParseString

func ParseString(s string) (string, error)

func Resolve

func Resolve[T any](r database.Record, key *database.Key) (T, error)

Resolve resolves the value for the given key.

func Struct

func Struct[T any, PT ptrBinaryValue[T]]() encodableValue[PT]

Struct returns an encodable value for the given encodable struct-type.

func Union

func Union[T encoding.BinaryValue](unmarshal func([]byte) (T, error)) encodableValue[T]

Union returns an encodable value for the given encodable union type.

func UnionFactory

func UnionFactory[T encoding.BinaryValue](unmarshal func([]byte) (T, error)) func() encodableValue[T]

UnionFactory curries Union.

func Walk

func Walk[T database.Record](lastErr *error, v T, opts database.WalkOptions, fn database.WalkFunc)

Walk walks the record if it is non-nil, unless opts.Modified is set and the record is unmodified.

func WalkComposite

func WalkComposite[T database.Record](v T, opts database.WalkOptions, fn database.WalkFunc) (skip bool, err error)

func WalkField

func WalkField[T database.Record](lastErr *error, v T, make func() T, opts database.WalkOptions, fn database.WalkFunc)

WalkField walks an attribute. If opts.Modified is set, WalkField walks the record if it is non-nil and modified. Otherwise, WalkField will walk the existing record or create (but not retain) a new record and walk that.

func WalkMap

func WalkMap[Record database.Record, Key keyType[MapKey], MapKey comparable](lastErr *error, m map[MapKey]Record, make func(Key) Record, getKeys func() ([]Key, error), opts database.WalkOptions, fn database.WalkFunc)

WalkField walks a parameterized attribute. If opts.Modified is set, WalkField iterates over the existing records, walking any that are modified. If opts.Modified is not set and getKeys is nil, WalkField does nothing. Otherwise, WalkField calls getKeys, iterates over the result, and walks all the corresponding records. If a record exists in the map, WalkField uses that; otherwise it creates (but does not retain) a new record and walks that.

func Wrapped

func Wrapped[T any](funcs *wrapperFuncs[T]) encodableValue[T]

Wrapped returns an encodable value for the given type using the given wrapper functions.

func WrappedFactory

func WrappedFactory[T any](funcs *wrapperFuncs[T]) func() encodableValue[T]

WrappedFactory curries Wrapped.

Types

type Counted

type Counted[T any] interface {
	database.Record
	Get(int) (T, error)
	Put(T) error
	Count() (int, error)
	GetAll() ([]T, error)
	Last() (int, T, error)
	Overwrite([]T) error
}

Counted records an insertion-ordered list of values as separate records plus a record for the count.

func NewCounted

func NewCounted[T any](_ any, store database.Store, key *database.Key, new func() encodableValue[T]) Counted[T]

NewCounted returns a new counted using the given encodable value type.

type List

type List[T any] interface {
	Value[[]T]
	Add(...T) error
}

List records an unordered list of values as a single record.

func NewList

func NewList[T any](_ any, store database.Store, key *database.Key, encoder encodableValue[T]) List[T]

NewList returns a new list using the given encoder and comparison.

type RecordStore

type RecordStore struct {
	Record database.Record
}

RecordStore implements database.Store using a database.Record.

func (RecordStore) GetValue

func (s RecordStore) GetValue(key *database.Key, dst database.Value) error

GetValue implements record.Store.

func (RecordStore) PutValue

func (s RecordStore) PutValue(key *database.Key, src database.Value) error

PutValue implements database.Store.

func (RecordStore) Unwrap

func (s RecordStore) Unwrap() database.Record

type Set

type Set[T any] interface {
	List[T]
	Remove(T) error
	Index(T) (int, error)
	Find(T) (T, error)
}

Set records an ordered list of values as a single record.

func NewSet

func NewSet[T any](_ any, store database.Store, key *database.Key, encoder encodableValue[T], cmp func(u, v T) int) Set[T]

NewSet returns a new set using the given encoder and comparison.

type Value

type Value[T any] interface {
	database.Record
	Get() (T, error)
	GetAs(any) error
	Put(T) error
}

Value records a value.

func NewValue

func NewValue[T any](_ any, store database.Store, key *database.Key, allowMissing bool, ev encodableValue[T]) Value[T]

NewValue returns a new value using the given encodable value.

Jump to

Keyboard shortcuts

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