funcs

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2021 License: MIT Imports: 6 Imported by: 1

README

funcs

GoDoc Build Status codecov Go Report Card GitHub release LICENSE

Function call by its name in golang

Get started

Install
go get github.com/hslam/funcs
Import
import "github.com/hslam/funcs"
Usage

First create an instance of the funcs:

Funcs:=funcs.New()

Then register your Struct:

type Service struct {
}
func (s *Service) Method(params ...interface{}) error {
    //to do
    return nil
}
Funcs.Register(new(Service))

And then call your function by name.

Function's Name Format : "StructName.MethodName"

if err := Funcs.Call("Service.Method", params...);err != nil {
    log.Fatalln("Call Service.Method error: ", err)
}

Logging.

Funcs.SetLog(true)

if a function has 2 input parameters ,You can get the function's first input parameter and second input parameter.

Funcs.GetFuncIn("Service.Method",0)
Funcs.GetFuncIn("Service.Method",1)
//and so on
Example
package main

import (
	"errors"
	"fmt"
	"github.com/hslam/funcs"
	"log"
)

//ArithRequest is the first input parameter.
type ArithRequest struct {
	A int
	B int
}

//ArithResponse is the second input parameter.
type ArithResponse struct {
	Quo int // quotient
	Rem int // remainder
}

//Arith is the Service struct.
type Arith struct {
}

//Divide is the Arith's Method.
func (a *Arith) Divide(req *ArithRequest, res *ArithResponse) error {
	if req.B == 0 {
		return errors.New("divide by zero")
	}
	res.Quo = req.A / req.B
	res.Rem = req.A % req.B
	return nil
}

func main() {
	Funcs := funcs.New()
	Funcs.SetLog(true)

	//Funcs.RegisterName("Arith",new(Arith))
	Funcs.Register(new(Arith))

	f := Funcs.GetFunc("Arith.Divide")
	fmt.Printf("num of args : %d\n", f.NumIn())

	//req := &ArithRequest{A: 9, B: 2}
	req := Funcs.GetFuncIn("Arith.Divide", 0).(*ArithRequest)
	req.A = 9
	req.B = 2

	//res := &ArithResponse{}
	res := Funcs.GetFuncIn("Arith.Divide", 1).(*ArithResponse)

	if err := Funcs.Call("Arith.Divide", req, res); err != nil {
		log.Fatalln("Call Arith.Divide error: ", err)
		return
	}
	fmt.Printf("%d / %d, quo is %d, rem is %d\n", req.A, req.B, res.Quo, res.Rem)
}
Output
[funcs] 2020/01/09 10:12:19.065121 StructName:Arith,NumMethod:1
[funcs] 2020/01/09 10:12:19.065246 MethodIndex:0,CallName:Arith.Divide,NumIn:2,NumOut:1
num of args : 2
9 / 2, quo is 4, rem is 1
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

funcs was written by Meng Huang.

Documentation

Overview

Package funcs implements function call by its name.

Index

Constants

View Source
const LogPrefix = "funcs"

LogPrefix is the prefix of log.

Variables

View Source
var (
	//ZeroValue is the Value of zero
	ZeroValue = Value{}
	//ErrNumParams is the error of params number.
	ErrNumParams = errors.New("The number of params is not adapted")
	//ErrObject is the error of nil.
	ErrObject = errors.New("The object is nil")
	//DefalutFuncs is the defalut Funcs.
	DefalutFuncs = New()
)

Functions

func Call

func Call(name string, params ...interface{}) (err error)

Call calls the function with the input arguments. For example, Call("v",arg1,arg2) represents the Go call v(arg1,arg2). Call panics if v's Kind is not Func. As in Go, each input argument must be assignable to the type of the function's corresponding input parameter.

func GetFuncIn

func GetFuncIn(name string, i int) interface{}

GetFuncIn returns index'th input parameter by name and index in the DefalutFuncs.

func ReflectValueOf

func ReflectValueOf(param interface{}) reflect.Value

ReflectValueOf returns a new reflect.Value.

func Register

func Register(obj interface{}) (err error)

Register publishes the set of struct's methods in the DefalutFuncs. If the function has a context.Context parameter, the context.Context must be the first parameter of the function.

func RegisterName

func RegisterName(name string, obj interface{}) error

RegisterName is like Register but uses the provided name for the type instead of the struct's concrete type.

func Services added in v1.0.1

func Services() []string

Services returns registered services.

func SetLog

func SetLog(enable bool)

SetLog enables Log in the DefalutFuncs.

func ValueCall

func ValueCall(name string, in ...Value) (err error)

ValueCall calls the function with the Value of input arguments.

Types

type Func

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

Func defines the struct of func.

func GetFunc

func GetFunc(name string) (F *Func)

GetFunc returns Func by name in the DefalutFuncs.

func (*Func) Call

func (f *Func) Call(params ...interface{}) (err error)

Call calls the function with the input arguments.

func (*Func) GetIn

func (f *Func) GetIn(i int) interface{}

GetIn returns index'th input parameter by index.

func (*Func) GetValueIn

func (f *Func) GetValueIn(i int) Value

GetValueIn returns the Value of index'th input parameter by index.

func (*Func) NumCalls

func (f *Func) NumCalls() (n int64)

NumCalls returns the number of calls.

func (*Func) NumIn

func (f *Func) NumIn() int

NumIn returns the number of input parameter.

func (*Func) NumOut

func (f *Func) NumOut() int

NumOut returns the number of output parameter.

func (*Func) ValueCall

func (f *Func) ValueCall(in ...Value) (err error)

ValueCall calls the function with the Value of input arguments.

func (*Func) WithContext added in v1.0.1

func (f *Func) WithContext() bool

WithContext returns whether calling with context.

type Funcs

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

Funcs defines the struct of Funcs.

func New

func New() *Funcs

New returns a new blank Funcs instance.

func (*Funcs) Call

func (f *Funcs) Call(name string, params ...interface{}) (err error)

Call calls the function with the input arguments. For example, Call("v",arg1,arg2) represents the Go call v(arg1,arg2). Call panics if v's Kind is not Func. As in Go, each input argument must be assignable to the type of the function's corresponding input parameter.

func (*Funcs) GetFunc

func (f *Funcs) GetFunc(name string) (F *Func)

GetFunc returns Func by name in the Funcs.

func (*Funcs) GetFuncIn

func (f *Funcs) GetFuncIn(name string, i int) interface{}

GetFuncIn returns index'th input parameter by name and index in the Funcs.

func (*Funcs) GetFuncValueIn

func (f *Funcs) GetFuncValueIn(name string, i int) Value

GetFuncValueIn returns the Value of index'th input parameter by name and index in the Funcs.

func (*Funcs) Register

func (f *Funcs) Register(obj interface{}) (err error)

Register publishes the set of struct's methods in the Funcs. If the function has a context.Context parameter, the context.Context must be the first parameter of the function.

func (*Funcs) RegisterName

func (f *Funcs) RegisterName(name string, obj interface{}) (err error)

RegisterName is like Register but uses the provided name for the type instead of the struct's concrete type.

func (*Funcs) Services added in v1.0.1

func (f *Funcs) Services() []string

Services returns registered services.

func (*Funcs) SetLog

func (f *Funcs) SetLog(enable bool)

SetLog enables Log in the Funcs.

func (*Funcs) ValueCall

func (f *Funcs) ValueCall(name string, in ...Value) (err error)

ValueCall calls the function with the Value of input arguments.

type Value

type Value reflect.Value

Value is the reflection interface to a Go value.

func GetFuncValueIn

func GetFuncValueIn(name string, i int) Value

GetFuncValueIn returns the Value of index'th input parameter by name and index in the DefalutFuncs.

func ValueOf

func ValueOf(param interface{}) Value

ValueOf returns a new Value.

func (Value) Interface

func (v Value) Interface() (i interface{})

Interface returns v's current value as an interface{}.

func (Value) Kind

func (v Value) Kind() reflect.Kind

Kind returns v's Kind. If v is the zero Value (IsValid returns false), Kind returns Invalid.

Jump to

Keyboard shortcuts

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