call

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: MIT Imports: 6 Imported by: 3

README

call

License Sonar Coverage GitHub Workflow Status Go Report Card Go PKG Web

Call dependency injection library based on registry arguments and functions.

go get github.com/rytsh/call

Usage

First get new registry and add own functions and arguments.

Also you can add arguments in directly function.

Registry add function and arguments not in order you can add argument later.

// create registry
reg := call.NewReg().
    AddArgument("a", 6).
    AddArgument("b", 2).
    AddFunction("divide", func(a, b int) (int, error) {
        if b == 0 {
            return 0, fmt.Errorf("divide by zero")
        }

        return a / b, nil
    }, "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>

It is possible to call directly with arguments also callable as variadic.

returns, err := reg.CallWithArgs("divide", "a", "b")

If function's argument length and type is not match, it will return error.

Check documentation for more details.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func OptionGetIndex

func OptionGetIndex(v []reflect.Value, args ...string) ([]reflect.Value, error)

OptionGetIndex returns value by index from slice, array or map.

func OptionVariadic

func OptionVariadic(v []reflect.Value, _ ...string) ([]reflect.Value, error)

OptionVariadic returns variadic value, value should be slice,array or map. If map type it turns into slice of values.

Types

type Func

type Func struct {
	Args []string
	Fn   reflect.Value
}

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 OptionFunc struct {
	Name string
	Fn   func([]reflect.Value, ...string) ([]reflect.Value, error)
}

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) AddOption

func (o *Options) AddOption(name string, fn func([]reflect.Value, ...string) ([]reflect.Value, error)) Option

func (*Options) GetDelimeter

func (o *Options) GetDelimeter() string

GetDelimeter returns delimeter for options.

func (*Options) GetOption

func (o *Options) GetOption(name string) (func([]reflect.Value, ...string) ([]reflect.Value, error), bool)

func (*Options) ParseOption

func (o *Options) ParseOption(name string) []string

ParseOptions parses options from string with delimeter.

func (*Options) VisitOptions

func (o *Options) VisitOptions(arg string, v any) ([]reflect.Value, error)

type Reg

type Reg struct {
	Option
	// contains filtered or unexported fields
}

Reg is a registry for functions and arguments.

func NewReg

func NewReg(optionFuncs ...OptionFunc) *Reg

NewReg creates new registry.

func (*Reg) AddArgument

func (r *Reg) AddArgument(name string, v any) *Reg

AddArgument adds argument to registry with name.

If name includes delimeter, it will not add options.

func (*Reg) AddFunction

func (r *Reg) AddFunction(name string, fn any, args ...string) *Reg

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) Call

func (r *Reg) Call(name string) ([]any, error)

Call calls function with name and uses already registered arguments.

func (*Reg) CallWithArgs

func (r *Reg) CallWithArgs(name string, args ...string) ([]any, error)

CallWithArgs calls function with name and arguments.

func (*Reg) DeleteArgument added in v0.1.1

func (r *Reg) DeleteArgument(name string) *Reg

DeleteArgument deletes argument with name.

func (*Reg) DeleteFunction added in v0.2.0

func (r *Reg) DeleteFunction(name string) *Reg

DeleteFunction removes function with name.

func (*Reg) GetArgument added in v0.1.1

func (r *Reg) GetArgument(name string) (any, bool)

GetArgument returns argument with name.

func (*Reg) GetArgumentNames added in v0.1.1

func (r *Reg) GetArgumentNames() []string

GetArgumentNames returns all argument names.

func (*Reg) GetFunction added in v0.1.1

func (r *Reg) GetFunction(name string) (Func, bool)

GetFunction returns function with name.

func (*Reg) GetFunctionNames added in v0.1.1

func (r *Reg) GetFunctionNames() []string

GetFunctionNames returns all function names.

Jump to

Keyboard shortcuts

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