lit

package
v0.0.0-...-7e8a755 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: BSD-2-Clause Imports: 9 Imported by: 12

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

Constants

This section is empty.

Variables

View Source
var (
	ErrKey     = cor.StrError("expect key name")
	ErrKeySep  = cor.StrError("expect key separator")
	ErrKeyVal  = cor.StrError("expect key value")
	ErrUnknown = cor.StrError("unknown literal")
)
View Source
var (
	Nil      = Null(typ.Any)
	False    = Bool(false)
	True     = Bool(true)
	ZeroUUID = UUID([16]byte{})
	ZeroTime = Time(time.Time{})
	ZeroSpan = Span(0)
)
View Source
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.

View Source
var ErrIdxBounds = cor.StrError("idx out of bounds")
View Source
var ErrUnconv = cor.StrError("cannot convert literal")

ErrUnconv is the default conversion error.

View Source
var ReadPath = typ.ReadPath

Functions

func Comp

func Comp(a, b Lit) (less, same, ok bool)

Comp returns whether a is less or the same as b and whether both types marked as ordered.

func Equal

func Equal(a, b Lit) bool

Equal returns whether the literals a and b are strictly equal.

func Equiv

func Equiv(a, b Lit) bool

Equiv returns whether a and b are equivalent, that is if they are either equal or comparable.

func Less

func Less(a, b Lit) (res, ok bool)

Less returns whether a is less than b and whether both types are marked as ordered.

Types

type Any

type Any struct{ Lit }

Any represents a non-null, any-typed literal.

func (Any) Some

func (a Any) Some() Lit

func (Any) Typ

func (a Any) Typ() typ.Type

type AnyProxy

type AnyProxy struct {
	Val reflect.Value
	Lit
}

func (*AnyProxy) Assign

func (p *AnyProxy) Assign(l Lit) error

func (*AnyProxy) New

func (p *AnyProxy) New() Proxy

func (*AnyProxy) Ptr

func (p *AnyProxy) Ptr() interface{}

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 BitsInt

type BitsInt struct {
	Type typ.Type
	Int
}

BitsInt represents a bits int constant

func (BitsInt) Typ

func (c BitsInt) Typ() typ.Type

type Bool

type Bool bool

func (*Bool) Assign

func (v *Bool) Assign(l Lit) error

func (Bool) IsZero

func (v Bool) IsZero() bool

func (Bool) MarshalJSON

func (v Bool) MarshalJSON() ([]byte, error)

func (*Bool) New

func (v *Bool) New() Proxy

func (Bool) Num

func (v Bool) Num() float64

func (*Bool) Ptr

func (v *Bool) Ptr() interface{}

func (Bool) String

func (v Bool) String() string

func (Bool) Typ

func (v Bool) Typ() typ.Type

func (Bool) Val

func (v Bool) Val() interface{}

func (Bool) WriteBfr

func (v Bool) WriteBfr(b *bfr.Ctx) error

type Char

type Char string

func (Char) Char

func (v Char) Char() string

func (Char) IsZero

func (v Char) IsZero() bool

func (Char) Len

func (v Char) Len() int

func (Char) MarshalJSON

func (v Char) MarshalJSON() ([]byte, error)

func (Char) String

func (v Char) String() string

func (Char) Typ

func (v Char) Typ() typ.Type

func (Char) Val

func (v Char) Val() interface{}

func (Char) WriteBfr

func (v Char) WriteBfr(b *bfr.Ctx) error

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

type Dict struct {
	Elem typ.Type
	List []Keyed
}

Dict is a generic container implementing the dict type.

func MakeDict

func MakeDict(t typ.Type) (*Dict, error)

MakeDict returns a new abstract dict literal with the given type or an error.

func MakeDictCap

func MakeDictCap(t typ.Type, cap int) (*Dict, error)

MakeDictCap returns a new abstract dict literal with the given type and cap or an error.

func (*Dict) Assign

func (d *Dict) Assign(l Lit) error

func (*Dict) Element

func (d *Dict) Element() (Proxy, error)

func (*Dict) IsZero

func (d *Dict) IsZero() bool

func (*Dict) IterKey

func (d *Dict) IterKey(it func(string, Lit) error) error

func (*Dict) Key

func (d *Dict) Key(k string) (Lit, error)

func (*Dict) Keys

func (d *Dict) Keys() []string

func (*Dict) Len

func (d *Dict) Len() int

func (*Dict) MarshalJSON

func (d *Dict) MarshalJSON() ([]byte, error)

func (*Dict) New

func (d *Dict) New() Proxy

func (*Dict) Ptr

func (d *Dict) Ptr() interface{}

func (*Dict) SetKey

func (d *Dict) SetKey(k string, el Lit) (_ Keyer, err error)

func (*Dict) String

func (d *Dict) String() string

func (*Dict) Typ

func (d *Dict) Typ() typ.Type

func (*Dict) UnmarshalJSON

func (d *Dict) UnmarshalJSON(b []byte) error

func (*Dict) WriteBfr

func (d *Dict) WriteBfr(b *bfr.Ctx) error

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 EnumStr

type EnumStr struct {
	Type typ.Type
	Str
}

EnumStr represents a enum str constant

func (EnumStr) Typ

func (c EnumStr) Typ() typ.Type

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

type Indexer interface {
	Lit
	Idxr
	// Len returns the number of contained elements.
	Len() int
}

Indexer is the common interface for container literals with elements accessible by index.

type Int

type Int int64

func (*Int) Assign

func (v *Int) Assign(l Lit) error

func (Int) IsZero

func (v Int) IsZero() bool

func (Int) MarshalJSON

func (v Int) MarshalJSON() ([]byte, error)

func (*Int) New

func (v *Int) New() Proxy

func (Int) Num

func (v Int) Num() float64

func (*Int) Ptr

func (v *Int) Ptr() interface{}

func (Int) String

func (v Int) String() string

func (Int) Typ

func (v Int) Typ() typ.Type

func (Int) Val

func (v Int) Val() interface{}

func (Int) WriteBfr

func (v Int) WriteBfr(b *bfr.Ctx) error

type Keyed

type Keyed struct {
	Key string
	Lit
}

Keyed is a key associated with a literal.

type Keyer

type Keyer interface {
	Lit
	Keyr
	// Len returns the number of contained elements.
	Len() int
}

Keyer is the common interface for container literals with elements accessible by key.

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

type List struct {
	Elem typ.Type
	Data []Lit
}

List is a generic container implementing the indexer type.

func MakeList

func MakeList(t typ.Type, len int) (*List, error)

MakeList returns a new abstract list literal with the given type and len or an error.

func MakeListCap

func MakeListCap(t typ.Type, len, cap int) (*List, error)

MakeListCap returns a new abstract list literal with the given type, len and cap or an error.

func (*List) Append

func (l *List) Append(ls ...Lit) (_ Appender, err error)

func (*List) Assign

func (l *List) Assign(val Lit) error

func (*List) Element

func (l *List) Element() (Proxy, error)

func (*List) Idx

func (l *List) Idx(i int) (Lit, error)

func (*List) IsZero

func (l *List) IsZero() bool

func (*List) IterIdx

func (l *List) IterIdx(it func(int, Lit) error) error

func (*List) Len

func (l *List) Len() int

func (*List) MarshalJSON

func (l *List) MarshalJSON() ([]byte, error)

func (*List) New

func (l *List) New() Proxy

func (*List) Ptr

func (l *List) Ptr() interface{}

func (*List) SetIdx

func (l *List) SetIdx(i int, el Lit) (_ Indexer, err error)

func (*List) String

func (l *List) String() string

func (*List) Typ

func (l *List) Typ() typ.Type

func (*List) UnmarshalJSON

func (l *List) UnmarshalJSON(b []byte) error

func (*List) WriteBfr

func (l *List) WriteBfr(b *bfr.Ctx) error

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

func Convert(l Lit, dst typ.Type, cmp typ.Cmp) (_ Lit, err error)

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

func Deopt(l Lit) Lit

Deopt returns the wrapped literal if l is an optional literal, otherwise it returns l as-is.

func Parse

func Parse(a *lex.Tree) (Lit, error)

Parse parses the syntax tree a and returns a literal or an error.

func Read

func Read(r io.Reader) (Lit, error)

Read reads and parses from r and returns a literal or an error.

func Select

func Select(l Lit, path string) (Lit, error)

Select reads path and returns the selected literal from within the container l or an error.

func SelectIdx

func SelectIdx(l Lit, idx int) (Lit, error)

func SelectKey

func SelectKey(l Lit, key string) (Lit, error)

func SelectPath

func SelectPath(l Lit, p Path) (Lit, error)

SelectPath returns the literal selected by path p from within l or an error.

func SetPath

func SetPath(l Lit, p Path, el Lit, create bool) (Lit, error)

SetPath sets literal l at path p to el or returns an error. If create is true it tries to create or resize intermediate containers so they can hold el.

func Zero

func Zero(t typ.Type) Lit

Zero returns the zero literal for the given type t.

type MarkBits

type MarkBits interface{ Bits() map[string]int64 }

MarkBits is a marker interface. When implemented on an unsigned integer indicates a bits type.

type MarkEnum

type MarkEnum interface{ Enums() map[string]int64 }

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 Null

type Null typ.Type

Null represents a typed zero value.

func (Null) IsZero

func (n Null) IsZero() bool

func (Null) MarshalJSON

func (n Null) MarshalJSON() ([]byte, error)

func (Null) Some

func (n Null) Some() Lit

func (Null) String

func (n Null) String() string

func (Null) Typ

func (n Null) Typ() typ.Type

func (Null) WriteBfr

func (n Null) WriteBfr(b *bfr.Ctx) error

type Num

type Num float64

func (Num) IsZero

func (v Num) IsZero() bool

func (Num) MarshalJSON

func (v Num) MarshalJSON() ([]byte, error)

func (Num) Num

func (v Num) Num() float64

func (Num) String

func (v Num) String() string

func (Num) Typ

func (v Num) Typ() typ.Type

func (Num) Val

func (v Num) Val() interface{}

func (Num) WriteBfr

func (v Num) WriteBfr(b *bfr.Ctx) error

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 Opter

type Opter interface {
	Lit
	// Some returns the wrapped literal or nil
	Some() Lit
}

Opter is the interface for literals with an optional type.

type Path

type Path = typ.Path

type PathSeg

type PathSeg = typ.PathSeg

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

type Proxy interface {
	Lit
	Proxr
}

Proxy is the common interface for proxies and some adapter pointers that can be assigned to.

func ZeroProxy

func ZeroProxy(tt typ.Type) (res Proxy)

ZeroProxy returns an assignable zero literal for the given type t.

type Raw

type Raw []byte

func (*Raw) Assign

func (v *Raw) Assign(l Lit) error

func (Raw) Char

func (v Raw) Char() string

func (Raw) IsZero

func (v Raw) IsZero() bool

func (Raw) Len

func (v Raw) Len() int

func (Raw) MarshalJSON

func (v Raw) MarshalJSON() ([]byte, error)

func (*Raw) New

func (v *Raw) New() Proxy

func (*Raw) Ptr

func (v *Raw) Ptr() interface{}

func (Raw) String

func (v Raw) String() string

func (Raw) Typ

func (v Raw) Typ() typ.Type

func (Raw) Val

func (v Raw) Val() interface{}

func (Raw) WriteBfr

func (v Raw) WriteBfr(b *bfr.Ctx) error

type Real

type Real float64

func (*Real) Assign

func (v *Real) Assign(l Lit) error

func (Real) IsZero

func (v Real) IsZero() bool

func (Real) MarshalJSON

func (v Real) MarshalJSON() ([]byte, error)

func (*Real) New

func (v *Real) New() Proxy

func (Real) Num

func (v Real) Num() float64

func (*Real) Ptr

func (v *Real) Ptr() interface{}

func (Real) String

func (v Real) String() string

func (Real) Typ

func (v Real) Typ() typ.Type

func (Real) Val

func (v Real) Val() interface{}

func (Real) WriteBfr

func (v Real) WriteBfr(b *bfr.Ctx) error

type Rec

type Rec struct {
	Type typ.Type
	Dict
}

func MakeRec

func MakeRec(t typ.Type) (*Rec, error)

MakeRec return a new abstract record literal with the given type or an error.

func RecFromKeyed

func RecFromKeyed(list []Keyed) *Rec

RecFromKeyed creates a new abstract record literal from the given list of keyed literals.

func (*Rec) Assign

func (a *Rec) Assign(l Lit) error

func (*Rec) Idx

func (a *Rec) Idx(i int) (Lit, error)

func (*Rec) IsZero

func (a *Rec) IsZero() bool

func (*Rec) IterIdx

func (a *Rec) IterIdx(it func(int, Lit) error) error

func (*Rec) Key

func (a *Rec) Key(key string) (Lit, error)

func (*Rec) MarshalJSON

func (a *Rec) MarshalJSON() ([]byte, error)

func (*Rec) New

func (a *Rec) New() Proxy

func (*Rec) Ptr

func (a *Rec) Ptr() interface{}

func (*Rec) SetIdx

func (a *Rec) SetIdx(i int, el Lit) (Indexer, error)

func (*Rec) SetKey

func (a *Rec) SetKey(key string, el Lit) (Keyer, error)

func (*Rec) String

func (a *Rec) String() string

func (*Rec) Typ

func (a *Rec) Typ() typ.Type

func (*Rec) WriteBfr

func (a *Rec) WriteBfr(b *bfr.Ctx) error

type Record

type Record interface {
	Lit
	Idxr
	Keyr
	// Len returns the number of fields.
	Len() int
}

Record is the interface for record literals.

type Some

type Some struct{ Lit }

Some represents non-null option.

func (Some) Some

func (s Some) Some() Lit

func (Some) Typ

func (s Some) Typ() typ.Type

type SomeProxy

type SomeProxy struct{ Proxy }

SomeProxy represents non-null assignable option.

func (SomeProxy) Some

func (s SomeProxy) Some() Lit

func (SomeProxy) Typ

func (s SomeProxy) Typ() typ.Type

type Span

type Span time.Duration

func (*Span) Assign

func (v *Span) Assign(l Lit) error

func (Span) Char

func (v Span) Char() string

func (Span) IsZero

func (v Span) IsZero() bool

func (Span) MarshalJSON

func (v Span) MarshalJSON() ([]byte, error)

func (*Span) New

func (v *Span) New() Proxy

func (Span) Num

func (v Span) Num() float64

func (*Span) Ptr

func (v *Span) Ptr() interface{}

func (Span) Seconds

func (v Span) Seconds() float64

func (Span) String

func (v Span) String() string

func (Span) Typ

func (v Span) Typ() typ.Type

func (*Span) UnmarshalText

func (v *Span) UnmarshalText(b []byte) error

func (Span) Val

func (v Span) Val() interface{}

func (Span) WriteBfr

func (v Span) WriteBfr(b *bfr.Ctx) error

type Str

type Str string

func (*Str) Assign

func (v *Str) Assign(l Lit) error

func (Str) Char

func (v Str) Char() string

func (Str) IsZero

func (v Str) IsZero() bool

func (Str) Len

func (v Str) Len() int

func (Str) MarshalJSON

func (v Str) MarshalJSON() ([]byte, error)

func (*Str) New

func (v *Str) New() Proxy

func (*Str) Ptr

func (v *Str) Ptr() interface{}

func (Str) String

func (v Str) String() string

func (Str) Typ

func (v Str) Typ() typ.Type

func (Str) Val

func (v Str) Val() interface{}

func (Str) WriteBfr

func (v Str) WriteBfr(b *bfr.Ctx) error

type Time

type Time time.Time

func (*Time) Assign

func (v *Time) Assign(l Lit) error

func (Time) Char

func (v Time) Char() string

func (Time) IsZero

func (v Time) IsZero() bool

func (Time) MarshalJSON

func (v Time) MarshalJSON() ([]byte, error)

func (*Time) New

func (v *Time) New() Proxy

func (Time) Num

func (v Time) Num() float64

func (*Time) Ptr

func (v *Time) Ptr() interface{}

func (Time) String

func (v Time) String() string

func (Time) Typ

func (v Time) Typ() typ.Type

func (Time) Val

func (v Time) Val() interface{}

func (Time) WriteBfr

func (v Time) WriteBfr(b *bfr.Ctx) error

type TypProxy

type TypProxy struct {
	*typ.Type
}

func (TypProxy) Assign

func (p TypProxy) Assign(l Lit) error

func (TypProxy) New

func (p TypProxy) New() Proxy

func (TypProxy) Ptr

func (p TypProxy) Ptr() interface{}

type UUID

type UUID [16]byte

func (*UUID) Assign

func (v *UUID) Assign(l Lit) error

func (UUID) Char

func (v UUID) Char() string

func (UUID) IsZero

func (v UUID) IsZero() bool

func (UUID) MarshalJSON

func (v UUID) MarshalJSON() ([]byte, error)

func (*UUID) New

func (v *UUID) New() Proxy

func (*UUID) Ptr

func (v *UUID) Ptr() interface{}

func (UUID) String

func (v UUID) String() string

func (UUID) Typ

func (v UUID) Typ() typ.Type

func (*UUID) UnmarshalText

func (v *UUID) UnmarshalText(b []byte) (err error)

func (UUID) Val

func (v UUID) Val() interface{}

func (UUID) WriteBfr

func (v UUID) WriteBfr(b *bfr.Ctx) error

Jump to

Keyboard shortcuts

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