Documentation
¶
Overview ¶
Package lit provides code for working with literals.
The Lit interface defines the common behaviour of all literals. There are additional interfaces for each base type: Opter, Numer, Charer, Idxer, Keyer, Appender and Assignable.
The go implementation provides adapter and proxy types that implement those interfaces.
adapters adapt go values of a limited set of types as literals proxies proxy uncommon, nested or custom go values as literals using reflection
Literal can be read from and written as JSON or a xelf extension with four new syntax features:
single quoted strings: 'no need to escape a double quote _"_' raw multi-line strings: `\r\a\w \s\t\r\i\n\g` optional commas (as in whitespace): [1 2], {'a': 1 'b' :2} simple dict key notation: {foo:1 'with space': 23 "json key": 42}
Literals without context default to their base type. Base types in typed context are automatically converted to the context. Type expressions can otherwise be used to convert literals to a specific type. For example:
(real 1) (int 1e6) (time "2018-11-16T23:52:20") (span "7:07:40") ((rec id:uuid name:str?) ["68986386-46ac-47f5-bf47-198ab20e594b" "foo"])
The type usually applies to the whole literal and defines all nested fields types. Which works for all fields except any typed fields, those can not fully be represented in the JSON literal format and need explicit conversion.
(list|@prod {}) (list|any (float 23) (span '7h'))
Index ¶
- Variables
- func Comp(a, b Lit) (less, same, ok bool)
- func Equal(a, b Lit) bool
- func Equiv(a, b Lit) bool
- func Less(a, b Lit) (res, ok bool)
- type Any
- type AnyProxy
- type Appender
- type BitsInt
- type Bool
- func (v *Bool) Assign(l Lit) error
- func (v Bool) IsZero() bool
- func (v Bool) MarshalJSON() ([]byte, error)
- func (v *Bool) New() Proxy
- func (v Bool) Num() float64
- func (v *Bool) Ptr() interface{}
- func (v Bool) String() string
- func (v Bool) Typ() typ.Type
- func (v Bool) Val() interface{}
- func (v Bool) WriteBfr(b *bfr.Ctx) error
- type Char
- type Character
- type Dict
- func (d *Dict) Assign(l Lit) error
- func (d *Dict) Element() (Proxy, error)
- func (d *Dict) IsZero() bool
- func (d *Dict) IterKey(it func(string, Lit) error) error
- func (d *Dict) Key(k string) (Lit, error)
- func (d *Dict) Keys() []string
- func (d *Dict) Len() int
- func (d *Dict) MarshalJSON() ([]byte, error)
- func (d *Dict) New() Proxy
- func (d *Dict) Ptr() interface{}
- func (d *Dict) SetKey(k string, el Lit) (_ Keyer, err error)
- func (d *Dict) String() string
- func (d *Dict) Typ() typ.Type
- func (d *Dict) UnmarshalJSON(b []byte) error
- func (d *Dict) WriteBfr(b *bfr.Ctx) error
- type Dictionary
- type EnumStr
- type Idxr
- type Indexer
- type Int
- func (v *Int) Assign(l Lit) error
- func (v Int) IsZero() bool
- func (v Int) MarshalJSON() ([]byte, error)
- func (v *Int) New() Proxy
- func (v Int) Num() float64
- func (v *Int) Ptr() interface{}
- func (v Int) String() string
- func (v Int) Typ() typ.Type
- func (v Int) Val() interface{}
- func (v Int) WriteBfr(b *bfr.Ctx) error
- type Keyed
- type Keyer
- type Keyr
- type List
- func (l *List) Append(ls ...Lit) (_ Appender, err error)
- func (l *List) Assign(val Lit) error
- func (l *List) Element() (Proxy, error)
- func (l *List) Idx(i int) (Lit, error)
- func (l *List) IsZero() bool
- func (l *List) IterIdx(it func(int, Lit) error) error
- func (l *List) Len() int
- func (l *List) MarshalJSON() ([]byte, error)
- func (l *List) New() Proxy
- func (l *List) Ptr() interface{}
- func (l *List) SetIdx(i int, el Lit) (_ Indexer, err error)
- func (l *List) String() string
- func (l *List) Typ() typ.Type
- func (l *List) UnmarshalJSON(b []byte) error
- func (l *List) WriteBfr(b *bfr.Ctx) error
- type Lit
- func Convert(l Lit, dst typ.Type, cmp typ.Cmp) (_ Lit, err error)
- func Deopt(l Lit) Lit
- func Parse(a *lex.Tree) (Lit, error)
- func Read(r io.Reader) (Lit, error)
- func Select(l Lit, path string) (Lit, error)
- func SelectIdx(l Lit, idx int) (Lit, error)
- func SelectKey(l Lit, key string) (Lit, error)
- func SelectPath(l Lit, p Path) (Lit, error)
- func SetPath(l Lit, p Path, el Lit, create bool) (Lit, error)
- func Zero(t typ.Type) Lit
- type MarkBits
- type MarkEnum
- type MarkSpan
- type Null
- type Num
- type Numeric
- type Opter
- type Path
- type PathSeg
- type Proxr
- type Proxy
- type Raw
- func (v *Raw) Assign(l Lit) error
- func (v Raw) Char() string
- func (v Raw) IsZero() bool
- func (v Raw) Len() int
- func (v Raw) MarshalJSON() ([]byte, error)
- func (v *Raw) New() Proxy
- func (v *Raw) Ptr() interface{}
- func (v Raw) String() string
- func (v Raw) Typ() typ.Type
- func (v Raw) Val() interface{}
- func (v Raw) WriteBfr(b *bfr.Ctx) error
- type Real
- func (v *Real) Assign(l Lit) error
- func (v Real) IsZero() bool
- func (v Real) MarshalJSON() ([]byte, error)
- func (v *Real) New() Proxy
- func (v Real) Num() float64
- func (v *Real) Ptr() interface{}
- func (v Real) String() string
- func (v Real) Typ() typ.Type
- func (v Real) Val() interface{}
- func (v Real) WriteBfr(b *bfr.Ctx) error
- type Rec
- func (a *Rec) Assign(l Lit) error
- func (a *Rec) Idx(i int) (Lit, error)
- func (a *Rec) IsZero() bool
- func (a *Rec) IterIdx(it func(int, Lit) error) error
- func (a *Rec) Key(key string) (Lit, error)
- func (a *Rec) MarshalJSON() ([]byte, error)
- func (a *Rec) New() Proxy
- func (a *Rec) Ptr() interface{}
- func (a *Rec) SetIdx(i int, el Lit) (Indexer, error)
- func (a *Rec) SetKey(key string, el Lit) (Keyer, error)
- func (a *Rec) String() string
- func (a *Rec) Typ() typ.Type
- func (a *Rec) WriteBfr(b *bfr.Ctx) error
- type Record
- type Some
- type SomeProxy
- type Span
- func (v *Span) Assign(l Lit) error
- func (v Span) Char() string
- func (v Span) IsZero() bool
- func (v Span) MarshalJSON() ([]byte, error)
- func (v *Span) New() Proxy
- func (v Span) Num() float64
- func (v *Span) Ptr() interface{}
- func (v Span) Seconds() float64
- func (v Span) String() string
- func (v Span) Typ() typ.Type
- func (v *Span) UnmarshalText(b []byte) error
- func (v Span) Val() interface{}
- func (v Span) WriteBfr(b *bfr.Ctx) error
- type Str
- func (v *Str) Assign(l Lit) error
- func (v Str) Char() string
- func (v Str) IsZero() bool
- func (v Str) Len() int
- func (v Str) MarshalJSON() ([]byte, error)
- func (v *Str) New() Proxy
- func (v *Str) Ptr() interface{}
- func (v Str) String() string
- func (v Str) Typ() typ.Type
- func (v Str) Val() interface{}
- func (v Str) WriteBfr(b *bfr.Ctx) error
- type Time
- func (v *Time) Assign(l Lit) error
- func (v Time) Char() string
- func (v Time) IsZero() bool
- func (v Time) MarshalJSON() ([]byte, error)
- func (v *Time) New() Proxy
- func (v Time) Num() float64
- func (v *Time) Ptr() interface{}
- func (v Time) String() string
- func (v Time) Typ() typ.Type
- func (v Time) Val() interface{}
- func (v Time) WriteBfr(b *bfr.Ctx) error
- type TypProxy
- type UUID
- func (v *UUID) Assign(l Lit) error
- func (v UUID) Char() string
- func (v UUID) IsZero() bool
- func (v UUID) MarshalJSON() ([]byte, error)
- func (v *UUID) New() Proxy
- func (v *UUID) Ptr() interface{}
- func (v UUID) String() string
- func (v UUID) Typ() typ.Type
- func (v *UUID) UnmarshalText(b []byte) (err error)
- func (v UUID) Val() interface{}
- func (v UUID) WriteBfr(b *bfr.Ctx) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrKey = cor.StrError("expect key name") ErrKeySep = cor.StrError("expect key separator") ErrKeyVal = cor.StrError("expect key value") ErrUnknown = cor.StrError("unknown literal") )
var ( Nil = Null(typ.Any) False = Bool(false) True = Bool(true) ZeroUUID = UUID([16]byte{}) ZeroTime = Time(time.Time{}) ZeroSpan = Span(0) )
var BreakIter = cor.StrError("break iter")
BreakIter is a special error value that can be returned from iterators. It indicates that the iteration should be stopped even though no actual failure occurred.
var ErrIdxBounds = cor.StrError("idx out of bounds")
var ErrUnconv = cor.StrError("cannot convert literal")
ErrUnconv is the default conversion error.
var ReadPath = typ.ReadPath
Functions ¶
func Comp ¶
Comp returns whether a is less or the same as b and whether both types marked as ordered.
Types ¶
type Appender ¶
type Appender interface { Indexer // Append appends the given literals and returns a new appender or an error Append(...Lit) (Appender, error) // Element returns a newly created proxy of the element type or an error. Element() (Proxy, error) }
Appender is the common interface for list literals.
type Character ¶
type Character interface { Lit // Char returns the character format of the literal as string. Char() string // Val returns the simple go value representing this literal. // The type is either string, []byte, [16]byte, time.Time or time.Duration. Val() interface{} }
Character is the common interface for character literals.
type Dict ¶
Dict is a generic container implementing the dict type.
func MakeDictCap ¶
MakeDictCap returns a new abstract dict literal with the given type and cap or an error.
func (*Dict) MarshalJSON ¶
func (*Dict) UnmarshalJSON ¶
type Dictionary ¶
type Dictionary interface { Keyer // Element returns a newly created proxy of the element type or an error. Element() (Proxy, error) }
Dictionary is the interface for dict literals.
type Idxr ¶
type Idxr interface { // Idx returns the literal of the element at idx or an error. Idx(idx int) (Lit, error) // SetIdx sets the element value at idx to l and returns the indexer or an error. SetIdx(idx int, l Lit) (Indexer, error) // IterIdx iterates over elements, calling iter with the elements index and literal value. // If iter returns an error the iteration is aborted. IterIdx(iter func(int, Lit) error) error }
Idxr is encapsulates the extra method-set of indexer literals. It is used only for easier interface composition before Go 1.13.
type Indexer ¶
Indexer is the common interface for container literals with elements accessible by index.
type Keyr ¶
type Keyr interface { // Keys returns a string slice of all keys. Keys() []string // Key returns the literal of the element with key key or an error. Key(key string) (Lit, error) // SetKey sets the elements value with key to l and returns the keyer or an error. SetKey(key string, l Lit) (Keyer, error) // IterKey iterates over elements, calling iter with the elements key and literal value. // If iter returns an error the iteration is aborted. IterKey(iter func(string, Lit) error) error }
Keyr is encapsulates the extra method-set of keyer literals. It is only used for easier interface composition before Go 1.13.
type List ¶
List is a generic container implementing the indexer type.
func MakeList ¶
MakeList returns a new abstract list literal with the given type and len or an error.
func MakeListCap ¶
MakeListCap returns a new abstract list literal with the given type, len and cap or an error.
func (*List) MarshalJSON ¶
func (*List) UnmarshalJSON ¶
type Lit ¶
type Lit interface { // Typ returns the defined type of the literal. Typ() typ.Type // IsZero returns whether the literal value is the zero value. IsZero() bool // WriteBfr writes to a bfr ctx either as strict JSON or xelf representation. WriteBfr(*bfr.Ctx) error // String returns the xelf representation as string. String() string // MarshalJSON returns the JSON representation as bytes. MarshalJSON() ([]byte, error) }
Lit is the common interface for all literal adapters. A nil Lit represents an absent value.
func Convert ¶
Convert converts l to the dst type and returns the result or an error. Cmp is used if not CmpNone, otherwise Compare is called with the type of l and dst.
func Deopt ¶
Deopt returns the wrapped literal if l is an optional literal, otherwise it returns l as-is.
func Select ¶
Select reads path and returns the selected literal from within the container l or an error.
func SelectPath ¶
SelectPath returns the literal selected by path p from within l or an error.
type MarkBits ¶
MarkBits is a marker interface. When implemented on an unsigned integer indicates a bits type.
type MarkEnum ¶
MarkEnum is a marker interface. When implemented on a string or integer indicates an enum type.
type MarkSpan ¶
type MarkSpan interface{ Seconds() float64 }
MarkSpan is a marker interface. When implemented on an int64 indicates a span type.
type Numeric ¶
type Numeric interface { Lit // Num returns the numeric value of the literal as float64. Num() float64 // Val returns the simple go value representing this literal. // The type is either bool, int64, float64, time.Time or time.Duration Val() interface{} }
Numeric is the common interface for numeric literals.
type Proxr ¶
type Proxr interface { // New returns a zero instance of the proxy literal. New() Proxy // Ptr returns a pointer to the underlying go value as interface. Ptr() interface{} // Assign assigns the value of the given literal or returns an error. // The literal must be valid literal of the same type. Assign(Lit) error }
Proxr is the encapsulates the extra method-set of proxy literals. It is used only for easier interface composition before Go 1.13.
type Proxy ¶
Proxy is the common interface for proxies and some adapter pointers that can be assigned to.
type Rec ¶
func RecFromKeyed ¶
RecFromKeyed creates a new abstract record literal from the given list of keyed literals.