Documentation ¶
Overview ¶
Package ita provides a clean API for dynamically calling structs methods
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( //ErrMethodNotFound is returned when there is no method found ErrMethodNotFound = errors.New("ita: method not found") //ErrZeroValue is returend when the vales is not valid ErrZeroValue = errors.New("ita: zero value argument") //ErrTooManyArgs is returned when arguments required are more than those passed to the method ErrTooManyArgs = errors.New("ita: the method has more arguments than the ones provided") //ErrTooFewArgs is returned when arguments required are less than those supplied to the method ErrTooFewArgs = errors.New("ita: you have provided more arguments than required by the method") )
Functions ¶
This section is empty.
Types ¶
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result stores the returned values from dynamically callling struct methods
func (*Result) First ¶
First returns the first result returned by caling a method
For instance if you have method Foo
func (s *MyStruct)Foo()( a string, err error){...}
First will return a but in interface form, meaning you will have to manualy cast the result to string. For automatic casting, please see Map, and MapTo
Example ¶
var rst []reflect.Value out := []int{1, 2, 3} for _, o := range out { rst = append(rst, reflect.ValueOf(o)) } result := &Result{} result.Set(rst) fmt.Println(result.First())
Output: 1 true
func (*Result) Get ¶
Get retrieves the result by Index, It will not panic if the index is out of range. instead it returns a nil value and ok is set to false.
Example ¶
var rst []reflect.Value out := []int{1, 2, 3} for _, o := range out { rst = append(rst, reflect.ValueOf(o)) } result := &Result{} result.Set(rst) fmt.Println(result.Get(0)) fmt.Println(result.Get(5))
Output: 1 true <nil> false
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
Struct wraps the struct that we want to call its methods
Example ¶
package main import ( "fmt" ) type Hello struct{} func (h *Hello) Say() string { return "hello" } func (h *Hello) Who(say string) string { return say } func main() { hell := New(&Hello{}) who := hell.Call("Who", "gernest") say := hell.Call("Say") first, _ := say.GetResults().First() second, _ := who.GetResults().First() fmt.Println(first, second) }
Output: hello gernest
func New ¶
func New(v interface{}) *Struct
New wraps v into a Struct and returns the struct ready for plumbing.
func (*Struct) Call ¶
Call calls method by nameand passing any arguments to the unserlying struct. This supports variadic methods and also methods that requires no arguments.
func (*Struct) Error ¶
Error returns the most recent error encountered while calling Call(). Be wise to remember to check this just incase to be sure errors are under control.
func (*Struct) GetResults ¶
GetResults returns the results of the last Call.