types

package
v0.0.0-...-20810c9 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Access

type Access struct {
	Base     *Type
	Path     []PathNode
	Nodes    []AccessNode
	Cache    []Value
	CacheRef []bool
	CanSet   bool
	CanGet   bool
	CanRef   bool
}

Access to value down a path starting from a given type.

func (Access) Get

func (a Access) Get(source Value) Value

Gets a copy of a value in the access path from the source.

func (Access) Last

func (a Access) Last() *AccessNode

The last node in the access path. If there is no path then nil is returned.

func (Access) Ref

func (a Access) Ref(source Value) Value

Gets a reference to a value in the access path from the source. A reference points to an addressable value meaning changing the value directly is possible.

func (Access) Set

func (a Access) Set(source Ptr, value Value) bool

Sets a value to a given source in the access path.

func (Access) Type

func (a Access) Type() *Type

The return type for the access.

type AccessNode

type AccessNode struct {
	Prop *Prop
	Key  Value
	Type *Type
	Args []Value
	Get  GetFn
	Ref  RefFn
	Set  SetFn
}

A node along an access path with the necessary values.

type Collection

type Collection interface {
	// The number of items in the collection.
	Len() int
	// Gets the value of the item in the collection with the given key
	Get(key Value) (Value, bool)
	// Sets the value of the item in the collection with the given key
	Set(key Value, value Value) bool
	// Adds the value to the collection
	Add(key Value, value Value) bool
	// Returns true if a value exists in the collection with the given key
	Contains(key Value) bool
	// Returns an iterator for the collection.
	Iterator() Iterator
}

Something that can iterate or fetch values from a collection of key-values.

type CollectionDef

type CollectionDef struct {
	// The key type.
	Key *Type
	// The value type.
	Value *Type
	// Returns a collection given a value.
	Collect func(value Value) Collection
}

A definition for a type which holds key-values

type GetArgsFn

type GetArgsFn = func(source Value, args []Value) Value

A get function that can accept args. Source is

type GetFn

type GetFn = func(source Value) Value

A get function that returns a copy of a value.

type Incrementor

type Incrementor[V constraints.Ordered] struct {
	// contains filtered or unexported fields
}

Auto-incrementing value generator.

func NewIncrementor

func NewIncrementor[V constraints.Ordered](start V, increment V) Incrementor[V]

Returns a new incrementor with the given initial value and how much to increment by on each Get() call.

func (Incrementor[V]) Get

func (i Incrementor[V]) Get() V

Returns an auto-incrementing value.

type Iterator

type Iterator interface {
	// Returns true if there is a next key & value
	HasNext() bool
	// Returns the next key and value
	Next() (value Value, key Value)
	// Removes the last key and value returned by Next and returns true.
	// If they cannot be removed or Next() was never called then false is returned.
	Remove() bool
	// Sets the value for the last key returned by Next and returns true.
	// If it cannot be set or Next() was never called then false is returned.
	Set(value Value) bool
	// Resets the iterator to the beginning of the key-values.
	Reset()
}

Iterates over values and keys.

type Meta

type Meta int

Metadata stored on a type/prop

func NewMeta

func NewMeta(name string) Meta

Creates a new Meta with the given name.

func (Meta) GetName

func (m Meta) GetName() string

func (Meta) String

func (m Meta) String() string

type NameMap

type NameMap[V any] struct {
	Values []V
	// contains filtered or unexported fields
}

A map of named values. This structure is useful when the most common operation is iteration where adding & removing are less common.

func NewNameMap

func NewNameMap[V any](getName func(V) string, capacity int, insensitive bool, ordered bool) NameMap[V]

Creates a new named map optionally having the names be case insensitive and optionally retaining insertion order when remove is performed.

func Props

func Props(props []Prop) NameMap[*Prop]

Returns a NameMap given a list of props.

func Types

func Types() NameMap[*Type]

Returns all defined types.

func (*NameMap[V]) Add

func (m *NameMap[V]) Add(value V) bool

Adds a named value to the map and returns true on success. False is returned if a value with the given name already exists.

func (*NameMap[V]) Clear

func (m *NameMap[V]) Clear()

Clears the map of all values.

func (NameMap[V]) Get

func (m NameMap[V]) Get(name string) (V, bool)

Gets a value with the name from the map and whether it exists at all.

func (NameMap[V]) Has

func (m NameMap[V]) Has(name string) bool

Returns whether this map has a value with the given name.

func (NameMap[V]) IsValid

func (m NameMap[V]) IsValid() bool

Returns whether the internal state of the map is valid. The internal state of the map can be invalid if the Values was manipulated directly or if any of the names of the values has changed and a Rebuild or Update was not called.

func (NameMap[V]) Len

func (m NameMap[V]) Len() int

Returns the number of items in the map.

func (*NameMap[V]) Rebuild

func (m *NameMap[V]) Rebuild()

Handles the value being renamed given the old name. If the old name does not exist a rebuild is performed.

func (*NameMap[V]) Remove

func (m *NameMap[V]) Remove(value V) bool

Removes the value from the map.

func (*NameMap[V]) RemoveName

func (m *NameMap[V]) RemoveName(name string) bool

Removes the value with the name from the map.

func (*NameMap[V]) Rename

func (m *NameMap[V]) Rename(oldName string)

Handles the value being renamed given the old name. If the old name does not exist a rebuild is performed.

func (*NameMap[V]) Set

func (m *NameMap[V]) Set(value V)

Sets the named value to the map whether it exists or not. If it exists already the existing value is replaced.

type PathNode

type PathNode struct {
	Token string
	Key   Value
	Args  []Value
}

A path node represents a path starting with a base value and ending with a desired value.

func NewPathFromTokens

func NewPathFromTokens(tokens []string) []PathNode

Creates a new simple path from a stream of tokens.

type Prop

type Prop struct {
	// The name of the property
	Name string
	// The type of the prop
	Type *Type
	// A function to return a copy of a prop value on a source value.
	Get GetArgsFn
	// A function to return a pointer to a prop value from a source value.
	Ref RefFn
	// A function to set a prop value on a source value.
	Set SetFn
	// A virtual prop is one that can be get or set but should be transferred.
	Virtual bool
	// The default value for a property. Why is this here?
	Default any
	// Metadata on the Prop
	Meta map[Meta]Value
}

A property on a type

func (Prop) GetName

func (p Prop) GetName() string

func (Prop) IsReadOnly

func (p Prop) IsReadOnly() bool

type Ptr

type Ptr = any

A value for a type but its explicitly an addressable value.

type RefFn

type RefFn = func(source Value) Value

A ref function. Source is always a ptr and so is the returned value.

type SetFn

type SetFn = func(source Value, value Value) bool

A set function. Source is always a ptr and value is not.

type Type

type Type struct {
	ID   uint16
	Name string
	Def  *TypeDef
}

A lightweight type with an ID (auto-incrementing starting at 0) and a name. A definition can be attached to it later.

func New

func New(name string) *Type

Creates a new type with the given name.

func (*Type) Access

func (t *Type) Access(path []PathNode) (Access, error)

Starting with a type and path of props/keys return something that will allow us to set & get a value.

func (*Type) Define

func (t *Type) Define(def TypeDef)

Sets the definition for the type.

func (Type) New

func (t Type) New() Value

Returns a new instance of the given type.

func (Type) Prop

func (t Type) Prop(name string) *Prop

Gets a prop with the given name.

type TypeDef

type TypeDef struct {
	// Creates a value of this type if possible.
	Create func() Value
	// If the type supports it this value is convertible to a string.
	ToString func(Value) string
	// If the type supports it this value is convertible from a string.
	FromString func(x string) (Value, error)
	// The properties on this type.
	Props NameMap[*Prop]
	// A definition if this type holds a collection of key-values.
	Collection *CollectionDef
	// Metadata on the Type
	Meta map[Meta]Value
}

A definition for a type.

type TypedValue

type TypedValue struct {
	Value Value
	Type  *Type
}

A value with given defined type.

type Value

type Value = any

A value for a type (void* in C/C++)

Jump to

Keyboard shortcuts

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