Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/rytsh/call" ) func divide(a, b int) (int, error) { if b == 0 { return 0, fmt.Errorf("divide by zero") } return a / b, nil } func main() { // create registry reg := call.NewReg(). AddArgument("a", 6). AddArgument("b", 2). AddFunction("", divide, "a", "b") // call function returns, err := reg.Call("divide") if err != nil { fmt.Println(err) return } fmt.Println(returns[0], returns[1]) }
Output: 3 <nil>
Example (NewOption) ¶
package main import ( "fmt" "reflect" "strings" "github.com/rytsh/call" ) func main() { myOption := call.OptionFunc{ Name: "addExtraValue", Fn: func(args []reflect.Value, options ...string) ([]reflect.Value, error) { // do something with options return []reflect.Value{ reflect.ValueOf(fmt.Sprintf("%s+%s", args[0].Interface(), strings.Join(options, "+"))), }, nil }, } // create registry reg := call.NewReg(myOption). AddArgument("arg", "check"). AddFunction("print", func(v string) string { return v }) // call function returns, err := reg.CallWithArgs("print", "arg:addExtraValue=1,2,3") if err != nil { fmt.Println(err) return } fmt.Println(returns[0]) }
Output: check+1+2+3
Example (Options) ¶
package main import ( "fmt" "github.com/rytsh/call" ) func main() { // create registry reg := call.NewReg(). AddArgument("a", []int{2, 5}). AddFunction("sum", func(x ...int) int { sum := 0 for _, v := range x { sum += v } return sum }) // call function returns, err := reg.CallWithArgs("sum", "a:...") if err != nil { fmt.Println(err) return } fmt.Println(returns[0]) // call function with a's elements returns, err = reg.CallWithArgs("sum", "a:index=0,1") if err != nil { fmt.Println(err) return } fmt.Println(returns[0]) reg.AddArgument("b", map[string]int{"a": 2, "b": 5}) // call function with b's elements returns, err = reg.CallWithArgs("sum", "b:...") if err != nil { fmt.Println(err) return } fmt.Println(returns[0]) // call function with b's element returns, err = reg.CallWithArgs("sum", "b:index=b") if err != nil { fmt.Println(err) return } fmt.Println(returns[0]) }
Output: 7 7 7 5
Example (Simple) ¶
package main import ( "fmt" "github.com/rytsh/call" ) func main() { // create registry reg := call.NewReg(). AddFunction("hababam", func() string { return "hababam" }) // call function returns, err := reg.Call("hababam") if err != nil { fmt.Println(err) return } fmt.Println(returns[0].(string)) }
Output: hababam
Example (Variadic) ¶
package main import ( "fmt" "github.com/rytsh/call" ) func main() { // create registry reg := call.NewReg(). AddArgument("a", 6). AddArgument("b", 2). AddFunction("sum", func(x ...int) int { sum := 0 for _, v := range x { sum += v } return sum }) // call function returns, err := reg.CallWithArgs("sum", "a", "b") if err != nil { fmt.Println(err) return } fmt.Println(returns[0]) }
Output: 8
Index ¶
- func OptionGetIndex(v []reflect.Value, args ...string) ([]reflect.Value, error)
- func OptionVariadic(v []reflect.Value, _ ...string) ([]reflect.Value, error)
- type Func
- type Option
- type OptionFunc
- type Options
- func (o *Options) AddOption(name string, fn func([]reflect.Value, ...string) ([]reflect.Value, error)) Option
- func (o *Options) GetDelimeter() string
- func (o *Options) GetOption(name string) (func([]reflect.Value, ...string) ([]reflect.Value, error), bool)
- func (o *Options) ParseOption(name string) []string
- func (o *Options) VisitOptions(arg string, v any) ([]reflect.Value, error)
- type Reg
- func (r *Reg) AddArgument(name string, v any) *Reg
- func (r *Reg) AddFunction(name string, fn any, args ...string) *Reg
- func (r *Reg) Call(name string) ([]any, error)
- func (r *Reg) CallWithArgs(name string, args ...string) ([]any, error)
- func (r *Reg) DeleteArgument(name string) *Reg
- func (r *Reg) DeleteFunction(name string) *Reg
- func (r *Reg) GetArgument(name string) (any, bool)
- func (r *Reg) GetArgumentNames() []string
- func (r *Reg) GetFunction(name string) (Func, bool)
- func (r *Reg) GetFunctionNames() []string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OptionGetIndex ¶
OptionGetIndex returns value by index from slice, array or map.
Types ¶
type Option ¶
type Option interface { GetDelimeter() string AddOption(name string, fn func([]reflect.Value, ...string) ([]reflect.Value, error)) Option GetOption(name string) (func([]reflect.Value, ...string) ([]reflect.Value, error), bool) VisitOptions(arg string, v any) ([]reflect.Value, error) }
func NewOptions ¶
func NewOptions() Option
type OptionFunc ¶ added in v0.2.0
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options is enable to modify arguments when calling functions.
Use options like this first value name after that `:` seperated and pass option arguments with `=`.
`hababam:option1=1,2,3;option2=value2`.
func (*Options) GetDelimeter ¶
GetDelimeter returns delimeter for options.
func (*Options) ParseOption ¶
ParseOptions parses options from string with delimeter.
type Reg ¶
type Reg struct { Option // contains filtered or unexported fields }
Reg is a registry for functions and arguments.
func (*Reg) AddArgument ¶
AddArgument adds argument to registry with name.
If name includes delimeter, it will not add options.
func (*Reg) AddFunction ¶
AddFunction adds function to registry with name.
If name is empty, function name will be used. Argument must be a function, otherwise it will panic.
func (*Reg) CallWithArgs ¶
CallWithArgs calls function with name and arguments.
func (*Reg) DeleteArgument ¶ added in v0.1.1
DeleteArgument deletes argument with name.
func (*Reg) DeleteFunction ¶ added in v0.2.0
DeleteFunction removes function with name.
func (*Reg) GetArgument ¶ added in v0.1.1
GetArgument returns argument with name.
func (*Reg) GetArgumentNames ¶ added in v0.1.1
GetArgumentNames returns all argument names.
func (*Reg) GetFunction ¶ added in v0.1.1
GetFunction returns function with name.
func (*Reg) GetFunctionNames ¶ added in v0.1.1
GetFunctionNames returns all function names.