1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-29 07:47:22 +00:00

add a new x/reflex sub-package for reflection common functions - may be improved in the near future

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-03-06 17:11:56 +02:00
parent fc2f8f4776
commit 410e5eae83
7 changed files with 224 additions and 0 deletions

44
x/reflex/func.go Normal file
View File

@@ -0,0 +1,44 @@
package reflex
import "reflect"
// IsFunc reports whether the "kindable" is a type of function.
func IsFunc(typ interface{ Kind() reflect.Kind }) bool {
return typ.Kind() == reflect.Func
}
// FuncParam holds the properties of function input or output.
type FuncParam struct {
Index int
Type reflect.Type
}
// LookupInputs returns the index and type of each function's input argument.
// Panics if "fn" is not a type of Func.
func LookupInputs(fn reflect.Type) []FuncParam {
n := fn.NumIn()
params := make([]FuncParam, 0, n)
for i := 0; i < n; i++ {
in := fn.In(i)
params = append(params, FuncParam{
Index: i,
Type: in,
})
}
return params
}
// LookupOutputs returns the index and type of each function's output argument.
// Panics if "fn" is not a type of Func.
func LookupOutputs(fn reflect.Type) []FuncParam {
n := fn.NumOut()
params := make([]FuncParam, 0, n)
for i := 0; i < n; i++ {
out := fn.Out(i)
params = append(params, FuncParam{
Index: i,
Type: out,
})
}
return params
}