123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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
- }
- outTyp := fntyp.Out(0)
- if outTyp.Kind() == reflect.Func {
- outTyp.Out(0)
- }
- fnTyp := reflect.TypeOf(e.fn)
- nInputs := fnTyp.NumIn()
- // Experimental
- offs := 0
- if fnTyp.NumIn() > 0 && fnTyp.In(0).String() == "*flow.Flow" {
- nInputs--
- offs = 1
- }
- ///
- Inputs := make([]DescType, nInputs)
- for i := 0; i < nInputs; i++ {
- inTyp := fnTyp.In(i + offs)
- Inputs[i] = DescType{fmt.Sprint(inTyp), ""}
- e.Inputs = append(e.Inputs, inTyp) // ?
- }
- Output := DescType{fmt.Sprint(outTyp), ""}
- e.Output = outTyp // ?
- 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
- }
|