tnglib

package module
v0.0.0-...-d33de98 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 17 Imported by: 0

README

tnglib

Modules for Tengo scripting language.

Implemented modules

So far, the following modules has been implemented.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedConfigFormat = errors.New("unsupported configuration file format")

ErrUnsupportedConfigFormat return unsupported config file format

Functions

func AllModuleNames

func AllModuleNames() []string

AllModuleNames return all registered modules

func ArgIToBool

func ArgIToBool(idx int, args ...tengo.Object) (bool, error)

ArgIToBool convert tengo function call arguments to boolean. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgIToByteSlice

func ArgIToByteSlice(idx int, args ...tengo.Object) ([]byte, error)

ArgIToByteSlice convert tengo function call arguments to []byte. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgIToDuration

func ArgIToDuration(idx int, args ...tengo.Object) (time.Duration, error, error)

ArgIToDuration convert i to duriation err1 -> conversion error err2 -> argument mismatch

func ArgIToInt

func ArgIToInt(idx int, args ...tengo.Object) (int, error)

ArgIToInt convert tengo function call arguments to int. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgIToInt64

func ArgIToInt64(idx int, args ...tengo.Object) (int64, error)

ArgIToInt64 convert tengo function call arguments to string. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgIToMap

func ArgIToMap(idx int, args ...tengo.Object) (map[string]interface{}, error)

ArgIToMap convert argument to context value

func ArgIToString

func ArgIToString(idx int, args ...tengo.Object) (string, error)

ArgIToString convert tengo function call arguments to string. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgIToTime

func ArgIToTime(idx int, args ...tengo.Object) (time.Time, error)

ArgIToTime convert argument to context value

func ArgToBool

func ArgToBool(args ...tengo.Object) (bool, error)

ArgToBool convert tengo function call arguments to boolean. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgToByteSlice

func ArgToByteSlice(args ...tengo.Object) ([]byte, error)

ArgToByteSlice convert tengo function call arguments to []byte. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgToDuration

func ArgToDuration(args ...tengo.Object) (time.Duration, error, error)

ArgToDuration convert i to duriation err1 -> conversion error err2 -> argument mismatch

func ArgToInt

func ArgToInt(args ...tengo.Object) (int, error)

ArgToInt convert tengo function call arguments to int. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgToString

func ArgToString(args ...tengo.Object) (string, error)

ArgToString convert tengo function call arguments to string. If the argument count is not equal to one, it will return ErrWrongNumArguments

func ArgsToStrings

func ArgsToStrings(minArg int, args ...tengo.Object) ([]string, error)

ArgsToStrings convert tengo function call arguments to string slice. If the argument count is less than minArg, it will return ErrWrongNumArguments

func BoolObject

func BoolObject(flag bool) tengo.Object

func FuncABRE

func FuncABRE(fn func(bool) error) tengo.CallableFunc

func FuncACRE

func FuncACRE(fn func(context.Context) error) tengo.CallableFunc

FuncACRE transform a function of 'func(ctx) error' signature into CallableFunc type.

func FuncADRE

func FuncADRE(fn func(time.Duration) error) tengo.CallableFunc

func FuncAIRI

func FuncAIRI(fn func(any) any) tengo.CallableFunc

FuncAIRI transform a function of 'func(any) any' signature into CallableFunc type.

func FuncAISARSAE

func FuncAISARSAE(fn func(int, string, interface{}) (string, []interface{}, error)) tengo.CallableFunc

func FuncAISRS

func FuncAISRS(fn func(int, string) string) tengo.CallableFunc

FuncAISRS transform a function of 'func(int, string) string' signature into CallableFunc type.

func FuncARSsE

func FuncARSsE(fn func() ([]string, error)) tengo.CallableFunc

FuncARSsE transform a function of 'func() ([]string, error)' signature into CallableFunc type.

func FuncARYs

func FuncARYs(fn func() []byte) tengo.CallableFunc

FuncARYs transform a function of 'func() []byte' signature into CallableFunc type.

func FuncASARSAE

func FuncASARSAE(fn func(string, interface{}) (string, []interface{}, error)) tengo.CallableFunc

func FuncASRI

func FuncASRI(fn func(string) int) tengo.CallableFunc

FuncASRI transform a function of 'func(string) int' signature into CallableFunc type.

func FuncASVRSAE

func FuncASVRSAE(fn func(string, ...interface{}) (string, []interface{}, error)) tengo.CallableFunc

func FuncATBR

func FuncATBR(fn func() (time.Time, bool)) tengo.CallableFunc

FuncATBR transform a function of 'func() (time.Time, bool)' signature into CallableFunc type.

func FuncAWARE

func FuncAWARE(fn func(w io.Writer, data any) error) tengo.CallableFunc

FuncAWARE convert any function(io.Writer, interface{}) error to tengo.CallableFunc

func FuncAWAREs

func FuncAWAREs(fn func(w io.Writer, data any) error) tengo.CallableFunc

FuncAWAREs convert any function(io.Writer, interface{}) error to tengo.CallableFunc

func FuncAYIR

func FuncAYIR(fn func([]byte, int)) tengo.CallableFunc

FuncAYIR transform a function of 'func([]byte, int)' signature into CallableFunc type.

func GetModuleMap

func GetModuleMap(names ...string) *tengo.ModuleMap

GetModuleMap returns the module map that includes all modules for the given module names. If module does not exist, it will panic.

func LoadConfig

func LoadConfig[T any, P Ptr[T]](filename string) (*T, error)

LoadConfig load configuration from given file. Supported formats: json

func LoadConfigTo

func LoadConfigTo[T any, P Ptr[T]](filename string, out *T) error

LoadConfigTo load configuration from given file. Supported formats: json, hjson

func LookupLoop

func LookupLoop[K comparable, V any](key K, keys []K, values []V) (V, bool)

LookupLoop implement switch-case like lookup table functionality.

func MapGet

func MapGet[T any](o tengo.Object, key string, defVal T, fn func(tengo.Object) (T, bool)) T

MapGet return value with given key in case object is map. If object is not map or value does not exists, it will return defaultV

func MapToObject

func MapToObject(v map[string]interface{}) (tengo.Object, error)

MapToObject convert map[string]interface{} to tengo.Object

func ObjectMapToMap

func ObjectMapToMap(v map[string]tengo.Object) map[string]interface{}

ObjectMapToMap converts map[string]tengo.Object to map[string]interface{}

func ObjectToStrings

func ObjectToStrings(obj tengo.Object) ([]string, error)

ObjectToStrings convert tengo.Object to []string

func RegisterModule

func RegisterModule(name string, mod map[string]tengo.Object)

RegisterModule with given name

func StringsToObject

func StringsToObject(items []string) tengo.Object

StringsToObject convert []string to tengo.Object

func Ternary

func Ternary[T any](cond bool, ifTrue T, ifFalse T) T

Ternary condition in one line, for simple assignment.

func ToObject

func ToObject(o tengo.Object) (tengo.Object, bool)

ToObject simply return argument and true

func ValueGet

func ValueGet[T any](o tengo.Object, defVal T, fn func(tengo.Object) (T, bool)) T

ValueGet return underlying object value or defValue if object is not convertible to T

func WrapError

func WrapError(err error) tengo.Object

WrapError convert error to tengo object or true if err is nil.

Types

type Context

type Context struct {
	tengo.ImmutableMap
	Ctx context.Context
}

Context is context.Context wrapper which is accessible from tengo

func ArgIToContext

func ArgIToContext(idx int, args ...tengo.Object) (*Context, error)

ArgIToContext convert argument to context value

func ArgToContext

func ArgToContext(args ...tengo.Object) (*Context, error)

ArgToContext convert argument to context value

func NewContext

func NewContext(ctx context.Context) *Context

NewContext creates scriptable context.Context

func (*Context) Copy

func (c *Context) Copy() tengo.Object

Copy returns a copy of the type.

func (*Context) Equals

func (c *Context) Equals(x tengo.Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Context) IsFalsy

func (c *Context) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Context) String

func (c *Context) String() string

String return string representation of the context

func (*Context) TypeName

func (c *Context) TypeName() string

TypeName return context

type InterfaceImpl

type InterfaceImpl struct {
	Name string
}

InterfaceImpl for wraping interface

func (*InterfaceImpl) BinaryOp

func (o *InterfaceImpl) BinaryOp(_ token.Token, _ tengo.Object) (tengo.Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*InterfaceImpl) CanCall

func (o *InterfaceImpl) CanCall() bool

CanCall returns whether the tengo.Object can be Called.

func (*InterfaceImpl) CanIterate

func (o *InterfaceImpl) CanIterate() bool

CanIterate returns whether the tengo.Object can be Iterated.

func (*InterfaceImpl) IndexGet

func (o *InterfaceImpl) IndexGet(_ tengo.Object) (res tengo.Object, err error)

IndexGet returns an element at a given index.

func (*InterfaceImpl) IndexSet

func (o *InterfaceImpl) IndexSet(_, _ tengo.Object) (err error)

IndexSet sets an element at a given index.

func (*InterfaceImpl) Iterate

func (o *InterfaceImpl) Iterate() tengo.Iterator

Iterate returns an iterator.

func (*InterfaceImpl) String

func (o *InterfaceImpl) String() string

func (*InterfaceImpl) TypeName

func (o *InterfaceImpl) TypeName() string

TypeName returns the name of the type.

type IoFunc

type IoFunc struct {
	Fn *tengo.UserFunction
}

IoFunc wrapper

func NewIoFunc

func NewIoFunc(name string, obj tengo.Object) (*IoFunc, bool)

NewIoFunc from tengo Object

func (*IoFunc) Read

func (i *IoFunc) Read(data []byte) (int, error)

Reader implement io.Reader

func (*IoFunc) Set

func (i *IoFunc) Set(name string, o tengo.Object) bool

Set user function with given name (if exist in the object)

func (*IoFunc) Write

func (i *IoFunc) Write(data []byte) (int, error)

Write implement io.Writer

type Ptr

type Ptr[T any] interface {
	*T
}

Ptr for pointer constraints

type Reader

type Reader struct {
	InterfaceImpl
	Value io.Reader
}

Reader represents a user function.

func (*Reader) Call

func (o *Reader) Call(args ...tengo.Object) (tengo.Object, error)

Call invokes a user function.

func (*Reader) Copy

func (o *Reader) Copy() tengo.Object

Copy returns a copy of the type.

func (*Reader) Equals

func (o *Reader) Equals(v tengo.Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Reader) IsFalsy

func (o *Reader) IsFalsy() bool

CanCall returns whether the Object can be Called.

type Span

type Span struct {
	time.Duration
}

Span stores duration for json decode/encode

func MustTimeSpan

func MustTimeSpan(s string) Span

MustTimeSpan convert string to Span

func TimeSpan

func TimeSpan(d time.Duration) Span

TimeSpan convert duration to Span

func (Span) IsNegative

func (d Span) IsNegative() bool

IsNegative return true if duration is less than 0

func (Span) IsPositive

func (d Span) IsPositive() bool

IsPositive return true if duration is greater than 0

func (Span) IsZero

func (d Span) IsZero() bool

IsZero return true if duration is zero

func (Span) MarshalJSON

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

MarshalJSON converts duration to string

func (*Span) UnmarshalJSON

func (d *Span) UnmarshalJSON(data []byte) error

UnmarshalJSON convert duration stream to time.Duration

type Writer

type Writer struct {
	InterfaceImpl
	Value io.Writer
}

Writer represents a user function.

func (*Writer) Call

func (o *Writer) Call(args ...tengo.Object) (tengo.Object, error)

Call invokes a user function.

func (*Writer) Copy

func (o *Writer) Copy() tengo.Object

Copy returns a copy of the type.

func (*Writer) Equals

func (o *Writer) Equals(v tengo.Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Writer) IsFalsy

func (o *Writer) IsFalsy() bool

CanCall returns whether the Object can be Called.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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