1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package registry
- import (
- "fmt"
- "reflect"
- )
- //DescType type Description
- type DescType struct {
- Type string `json:"type"`
- Name string `json:"name"`
- }
- // Entry contains a function description params etc
- type Entry struct {
- registry *R
- fn interface{}
- Inputs []reflect.Type
- Output reflect.Type
- Description *Description
- err error
- }
- // NewEntry creates and describes a New Entry
- func NewEntry(r *R, fn interface{}) *Entry {
- e := &Entry{registry: r, fn: fn}
- fntyp := reflect.TypeOf(e.fn)
- if fntyp.Kind() != reflect.Func {
- e.err = ErrNotAFunc
- return e
- }
- if fntyp.NumOut() == 0 {
- e.err = ErrOutput
- return e
- }
- fnTyp := reflect.TypeOf(e.fn)
- nInputs := fnTyp.NumIn()
- Inputs := make([]DescType, nInputs)
- for i := 0; i < nInputs; i++ {
- Inputs[i] = DescType{fmt.Sprint(fnTyp.In(i)), ""}
- e.Inputs = append(e.Inputs, fnTyp.In(i)) // ?
- }
- Output := DescType{fmt.Sprint(fnTyp.Out(0)), ""}
- e.Output = fnTyp.Out(0) // ?
- e.Description = &Description{
- Tags: []string{"generic"},
- Inputs: Inputs,
- Output: Output,
- Extra: map[string]interface{}{},
- }
- return e
- }
- // Describer return a description builder
- func (e *Entry) Describer() EDescriber {
- return Describer(e)
- }
- // Err returns error of the entry if any
- func (e *Entry) Err() error {
- return e.err
- }
|