generic

package
v0.24.4-1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package generic provides implementations of Value for common types. It also provides iterators which generate generic values from common slice types.

Index

Constants

This section is empty.

Variables

View Source
var (
	True  = BoolOf(true)
	False = BoolOf(false)
	Zero  = FloatOf(0.0)
	Empty = StringOf("")
)

Functions

func Ancestry

func Ancestry(k *Kind) (ret []string)

Ancestor list, root towards the start; the name of this kind at the end.

func Base

func Base(k *Kind) string

func IsUnknown

func IsUnknown(e error) bool

func ListIt

func ListIt(v Value) (ret *sliceIt)

panics if the value isnt a list

func SliceIt

func SliceIt(size int, next func(int) (Value, error)) *sliceIt

func UnknownField

func UnknownField(target, field string) error

func UnknownName

func UnknownName(name string) error

func UnknownObject

func UnknownObject(o string) error

func UnknownResponse

func UnknownResponse(v string) error

func UnknownVariable

func UnknownVariable(v string) error

Types

type Aspect

type Aspect struct {
	Name   string // matches the field name
	Traits []string
}

type EmptyStream

type EmptyStream bool

func (EmptyStream) GetNext

func (EmptyStream) GetNext() (Value, error)

func (EmptyStream) HasNext

func (EmptyStream) HasNext() bool

func (EmptyStream) Remaining

func (EmptyStream) Remaining() int

type Field

type Field struct {
	Name     string
	Affinity affine.Affinity
	Type     string // ex. kind for text types
}

type Iterator

type Iterator interface {
	// HasNext returns true if the iterator can be safely advanced.
	HasNext() bool
	// GetNext returns the next value in the stream and advances the iterator.
	GetNext() (Value, error)
}

Iterator provides a way to iterate over a stream of values. The underlying implementation and values returned depends on the stream.

type Kind

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

func NewKind

func NewKind(name string, parent *Kind, fields []Field) *Kind

NewKind -

func NewKindWithTraits

func NewKindWithTraits(name string, parent *Kind, fields []Field, aspects []Aspect) *Kind

when kinds are created via this method, the traits of any aspect fields act like separate boolean fields; without it, only the aspect text field itself exists. ex. if the list of fields contains a "colour" aspect with traits "red", "blue", "green" then the returned kind will respond to "colour", "red", "blue", and "green"; with NewKind() it would respond only to "colour", the r,b,g fields wouldn't exist. its a bit of leaky abstraction because boolean traits are used only by objects.

func (*Kind) Field

func (k *Kind) Field(i int) (ret Field)

panics if out of range

func (*Kind) FieldIndex

func (k *Kind) FieldIndex(n string) (ret int)

searches for the field which handles the passed field; for traits, it returns the index of its associated aspect. returns -1 if no matching field was found

func (*Kind) Implements

func (k *Kind) Implements(name string) (okay bool)

func (*Kind) Name

func (k *Kind) Name() (ret string)

func (*Kind) NewRecord

func (k *Kind) NewRecord() *Record

func (*Kind) NumField

func (k *Kind) NumField() int

func (*Kind) Parent

func (k *Kind) Parent() (ret *Kind)

type Kinds

type Kinds interface {
	GetKindByName(n string) (*Kind, error)
}

Kinds database this isnt used by package generic, but its a common enough interface for tests and the runtime

type Overflow

type Overflow struct {
	Index, Bounds int
}

func (Overflow) Error

func (e Overflow) Error() string

type PanicValue

type PanicValue struct{}

PanicValue is a PanicValue where every method panics except type and affinity.

func (PanicValue) Affinity

func (n PanicValue) Affinity() affine.Affinity

Affinity returns a blank string.

func (PanicValue) Appends

func (n PanicValue) Appends(Value) error

Append panics

func (PanicValue) Bool

func (n PanicValue) Bool() bool

Bool panics

func (PanicValue) FieldByName

func (n PanicValue) FieldByName(string) (Value, error)

FieldByName panics

func (PanicValue) Float

func (n PanicValue) Float() float64

Float panics

func (PanicValue) Floats

func (Floats PanicValue) Floats() []float64

Floats panics

func (PanicValue) Index

func (n PanicValue) Index(int) Value

Index panics

func (PanicValue) Int

func (n PanicValue) Int() int

Int panics

func (PanicValue) Len

func (n PanicValue) Len() int

Len panics

func (PanicValue) Record

func (n PanicValue) Record() *Record

Record panics

func (PanicValue) Records

func (n PanicValue) Records() []*Record

Records panics

func (PanicValue) SetFieldByName

func (n PanicValue) SetFieldByName(string, Value) error

SetFieldByName panics

func (PanicValue) SetIndex

func (n PanicValue) SetIndex(int, Value) error

SetIndex panics

func (PanicValue) Slice

func (n PanicValue) Slice(i, j int) (Value, error)

Slice panics

func (PanicValue) Splice

func (n PanicValue) Splice(start, end int, add Value) (Value, error)

Splice panics

func (PanicValue) String

func (n PanicValue) String() string

String panics

func (PanicValue) Strings

func (n PanicValue) Strings() []string

Strings panics

func (PanicValue) Type

func (n PanicValue) Type() string

Type returns a blank string.

type Record

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

Record - provides low level access to named field/values pairs. The fields of a record are defined by its kind.

func NewAnonymousRecord

func NewAnonymousRecord(fields []Field) *Record

a record without a named kind

func (*Record) GetIndexedField

func (d *Record) GetIndexedField(i int) (ret Value, err error)

GetIndexedField panics if out of range. note: traits are never indexed fields ( although their aspect is ) fix? GetIndexedField writes defaults into the record if there was no value.

func (*Record) GetNamedField

func (d *Record) GetNamedField(field string) (ret Value, err error)

GetNamedField picks a value or trait from this record.

func (*Record) HasValue

func (d *Record) HasValue(i int) (ret bool)

return whether the indexed field has ever been written to.

func (*Record) Kind

func (d *Record) Kind() *Kind

func (*Record) SetIndexedField

func (d *Record) SetIndexedField(i int, val Value) (err error)

SetIndexedField - note this doesn't handle trait translation. Unlike the Value interface, this doesnt panic and it doesnt copy values.

func (*Record) SetNamedField

func (d *Record) SetNamedField(field string, val Value) (err error)

SetNamedField - pokes the passed value into the record. Unlike the Value interface, this doesnt panic and it doesnt copy values.

func (*Record) Type

func (d *Record) Type() string

type Underflow

type Underflow struct {
	Index, Bounds int
}

func (Underflow) Error

func (e Underflow) Error() string

type Unknown

type Unknown struct {
	Target, Field string
}

error for GetField, SetField

func (Unknown) Error

func (e Unknown) Error() (ret string)

func (Unknown) IsUnknownField

func (e Unknown) IsUnknownField() bool

type Value

type Value interface {
	// identifies the general category of the value.
	Affinity() affine.Affinity
	// identifies how the passed value is represented internally.
	// ex. a number can be represented as float or as an int,
	// a record might be one of several different kinds,
	// text might represent an object id of a specific kind, an aspect, trait, or other value.
	Type() string
	// return this value as a bool, or panic if the value isn't a bool.
	Bool() bool
	// return this value as a float, or panic if the value isn't a number.
	Float() float64
	// return this value as an int, or panic if the value isn't a number.
	Int() int
	// return this value as a string; it doesn't panic.
	// for non-string values, similar to package reflect, it returns a string of the form "<type value>".
	String() string
	// return this value as a record, or panic if the value isn't a record.
	// warning: can return a nil.
	Record() *Record
	// return this value as a slice of floats, or panic if this isn't a float slice.
	// note: while primitive values support both ints and floats, slices can only be floats.
	Floats() []float64
	// return this value as a slice of strings, or panic if this isn't a string slice.
	Strings() []string
	// return this value as a slice of records, or panic if not a record slice.
	// note: every value in the returned slice is expected to be record of this value's Type().
	Records() []*Record
	// return the nth element of this value, where 0 is the first value.
	// panics if this isn't a slice.
	Index(int) Value
	// the number of elements in the value.
	// panics if this cant have a length: isnt a slice or a string.
	Len() int
	// return a value representing a field inside this record.
	// if the field holds a record or a slice, the returned value shares its memory with the named field.
	// errors if the field doesn't exist.
	// panics if this isn't a record.
	FieldByName(string) (Value, error)
	// writes a *copy* of the passed value into a record.
	// errors if the field doesn't exist or if its affinity cant support the passed value.
	// panics if this isn't a record.
	SetFieldByName(string, Value) error
	// writes a *copy* of the passed value into a slice
	// panics if this isn't a slice, if the index is out of range, or if the affinities are mismatched.
	// errors if the types are mismatched
	SetIndex(int, Value) error
	// adds a *copy* of a value, or a copy of a slice of values, to the end of this slice.
	// panics if this value isn't an appropriate kind of slice; errors on subtype.
	// In golang, this is a package level function, presumably to mirror the built-in append()
	Appends(Value) error
	// return a *copy* of this slice and its values containing the first index up to (and not including) the second index.
	// panics if this value isn't a slice.
	Slice(i, j int) (Value, error)
	// cut elements out of this slice from start to end,
	// adding copies of the passed additional elements (if any) at the start of the cut point.
	// Returns the cut elements, or an error if the start and end indices are bad.
	// panics if this value isn't a slice, or if additional element(s) are of an incompatible affinity.
	Splice(start, end int, add Value) (Value, error)
}

Value represents any one of Tapestry's built in types.

func BoolFrom

func BoolFrom(v bool, subtype string) Value

func BoolOf

func BoolOf(v bool) Value

func CompactNumbers

func CompactNumbers(it Iterator, vals []float64) (ret Value, err error)

func CompactTexts

func CompactTexts(it Iterator, vals []string) (ret Value, err error)

func CopyValue

func CopyValue(val Value) (ret Value)

CopyValue: create a new value from a snapshot of the passed value panics on error because it assumes all values should be copyable

func FloatFrom

func FloatFrom(v float64, subtype string) Value

func FloatOf

func FloatOf(v float64) Value

func FloatsFrom

func FloatsFrom(vs []float64, subtype string) (ret Value)

func FloatsOf

func FloatsOf(vs []float64) Value

func IntFrom

func IntFrom(v int, subtype string) Value

func IntOf

func IntOf(v int) Value

func NewDefaultValue

func NewDefaultValue(aff affine.Affinity, cls string) (ret Value, err error)

NewDefaultValue generates a zero value for the specified affinity Record values (and lists) are nil.

func RecordFrom

func RecordFrom(subtype string) Value

returns a nil record of the specified type

func RecordOf

func RecordOf(v *Record) Value

func RecordsFrom

func RecordsFrom(vs []*Record, subtype string) (ret Value)

func StringFrom

func StringFrom(v string, subtype string) Value

func StringOf

func StringOf(v string) Value

func StringsFrom

func StringsFrom(vs []string, subtype string) (ret Value)

func StringsOf

func StringsOf(vs []string) Value

Jump to

Keyboard shortcuts

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