utl

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: 4

Documentation

Overview

Package utl provides helpers to extend the xelf language and some common library function specs.

Index

Constants

This section is empty.

Variables

View Source
var StrLib = Lazy(Fmap{
	"str_contains":     strings.Contains,
	"str_contains_any": strings.ContainsAny,
	"str_prefix":       strings.HasPrefix,
	"str_suffix":       strings.HasSuffix,
	"str_upper":        strings.ToUpper,
	"str_lower":        strings.ToLower,
	"str_trim":         strings.TrimSpace,
})
View Source
var TimeLib = Lazy(Fmap{
	"time_sub":       time.Time.Sub,
	"time_add_date":  time.Time.AddDate,
	"time_add_days":  timeAddDays,
	"time_year":      time.Time.Year,
	"time_month":     time.Time.Month,
	"time_weekday":   time.Time.Weekday,
	"time_yearday":   time.Time.YearDay,
	"time_format":    time.Time.Format,
	"time_date_long": timeDateLong,
	"time_day_start": DayStart,
	"time_day_end":   DayEnd,
})
View Source
var ZeroKeyer = OffsetKeyer(0)

ZeroKeyer is an index keyer without offset.

Functions

func ApplyDelta

func ApplyDelta(o lit.Keyer, d *lit.Dict) error

func DayEnd

func DayEnd(t time.Time) time.Time

func DayStart

func DayStart(t time.Time) time.Time

func DynPrepper

func DynPrepper(p *exp.Prog, env exp.Env, n *exp.Tag) (lit.Lit, error)

DynPrepper resolves args using c and env and returns a literal or an error. Empty args return a untyped null literal. Multiple args are resolved as dyn expression.

func ListPrepper

func ListPrepper(p *exp.Prog, env exp.Env, n *exp.Tag) (lit.Lit, error)

ListPrepper resolves args using c and env and returns a list or an error.

func MergeDeltas

func MergeDeltas(a, b *lit.Dict) error

func MustReflectFmap

func MustReflectFmap(m Fmap) exp.LookupFunc

MustReflectFmap reflects m and returns a lookup function or panics.

func NewDelta

func NewDelta(a, b lit.Record) (*lit.Dict, error)

func ParseTags

func ParseTags(p *exp.Prog, env exp.Env, els []exp.El, v interface{}, rules TagRules) error

ParseTags parses args as tags and sets them to v using rules or returns an error.

func PathSetter

func PathSetter(n Node, key string, el lit.Lit) error

PathSetter sets el to n using key as path or returns an error.

func ReflectFmap

func ReflectFmap(m Fmap) (exp.LookupFunc, error)

ReflectFmap reflects m and returns a lookup function or an error.

func ReflectFunc

func ReflectFunc(name string, val interface{}, names ...string) (*exp.Spec, error)

ReflectFunc reflects val and returns a function literal or an error. The names are optionally and associated to the arguments by index.

Types

type Fmap

type Fmap = map[string]interface{}

Fmap is a go template.FuncMap compatible map type alias

type IdxKeyer

type IdxKeyer = func(n Node, idx int) string

IdxKeyer returns a key for an unnamed tag at idx.

func OffsetKeyer

func OffsetKeyer(offset int) IdxKeyer

OffsetKeyer returns an index keyer that looks up a field at the index plus the offset.

type KeyPrepper

type KeyPrepper = func(p *exp.Prog, env exp.Env, n *exp.Tag) (lit.Lit, error)

KeyPrepper resolves els and returns a literal for key or an error.

func BitsPrepper

func BitsPrepper(consts []typ.Const) KeyPrepper

BitsPrepper returns a key prepper that tries to resolve a bits constant.

type KeyRule

type KeyRule struct {
	KeyPrepper
	KeySetter
}

KeyRule is a configurable helper for assigning tags to nodes.

type KeySetter

type KeySetter = func(n Node, key string, l lit.Lit) error

KeySetter sets l to node with key or returns an error.

func BitsSetter

func BitsSetter(key string) KeySetter

BitsSetter returns a key setter that tries to add to a node bits field with key.

func ExtraMapSetter

func ExtraMapSetter(mapkey string) KeySetter

ExtraMapSetter returns a key setter that tries to add to a node map field with key.

type LazyFmap

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

LazyFmap reflects a fmap on first use and is protected by a mutex

func Lazy

func Lazy(fmap Fmap) *LazyFmap

Lazy returns a LazyFmap for fmap

func (*LazyFmap) Lookup

func (m *LazyFmap) Lookup() exp.LookupFunc

Lookup returns the same fmap lookup function, it is created once on the first call

type Node

type Node interface {
	lit.Record
	Ptr() interface{}
	Assign(lit.Lit) error
}

Node is an interface for assignable object literals.

func GetNode

func GetNode(val interface{}) (Node, error)

GenNode returns a node for val or an error. It tries to proxy if val is not a Node.

type NodeResolver

type NodeResolver struct {
	NodeRules
	// contains filtered or unexported fields
}

NodeResolver is a form resolver that returns nodes of a specific type.

func NewNodeResolver

func NewNodeResolver(rules NodeRules, v interface{}) *NodeResolver

NewNodeResolver returns a node resolver using rules and returning new nodes based on v.

func (*NodeResolver) Eval

func (r *NodeResolver) Eval(p *exp.Prog, env exp.Env, c *exp.Call, h typ.Type) (exp.El, error)

func (*NodeResolver) Resl

func (r *NodeResolver) Resl(p *exp.Prog, env exp.Env, c *exp.Call, h typ.Type) (exp.El, error)

type NodeRules

type NodeRules struct {
	Tags TagRules
	Tail KeyRule
}

NodeRules is a configurable helper for assigning tags and tail elements to nodes.

type ReflectBody

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

ReflectBody is a function resolver wrapping a reflected go function.

func (*ReflectBody) Eval

func (f *ReflectBody) Eval(p *exp.Prog, env exp.Env, c *exp.Call, h typ.Type) (exp.El, error)

func (*ReflectBody) Resl

func (f *ReflectBody) Resl(p *exp.Prog, env exp.Env, c *exp.Call, h typ.Type) (exp.El, error)

type TagRules

type TagRules struct {
	// Rules holds optional per key rules
	Rules map[string]KeyRule
	// IdxKeyer will map unnamed tags to a key, when null unnamed tags result in an error
	IdxKeyer
	// KeyRule holds optional default rules.
	// If neither specific nor default rules are found DynPrepper and PathSetter are used.
	KeyRule
}

TagRules is a configurable helper for assigning tags to nodes.

func (*TagRules) Resolve

func (tr *TagRules) Resolve(p *exp.Prog, env exp.Env, tags []*exp.Tag, node Node) (err error)

Resolve resolves tags using c and env and assigns them to node or returns an error

func (*TagRules) ResolveTag

func (tr *TagRules) ResolveTag(p *exp.Prog, env exp.Env, tag *exp.Tag, idx int, node Node) (err error)

ResolveTag resolves tag using c and env and assigns them to node or returns an error

func (TagRules) WithOffset

func (tr TagRules) WithOffset(off int) *TagRules

WithOffset return a with an offset keyer.

Jump to

Keyboard shortcuts

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