package registry import ( "fmt" "reflect" ) // Description of an entry type Description struct { Name string `json:"name"` Tags []string `json:"categories"` Inputs []string `json:"inputs"` InputDesc map[int]string `json:"inputsDesc"` Output string `json:"output"` OutputDesc string `json:"outputDesc"` Extra map[string]interface{} `json:"extra"` } // 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) Inputs := []string{} nInputs := fnTyp.NumIn() for i := 0; i < nInputs; i++ { e.Inputs = append(e.Inputs, fnTyp.In(i)) Inputs = append(Inputs, fmt.Sprint(fnTyp.In(i))) } Output := fmt.Sprint(fnTyp.Out(0)) e.Output = fnTyp.Out(0) e.Description = &Description{ Tags: []string{"generic"}, Inputs: Inputs, Output: Output, InputDesc: map[int]string{}, Extra: map[string]interface{}{}, } return e } // Err returns error of the entry if any func (e *Entry) Err() error { return e.err } //Tags of the entry func (e *Entry) Tags(cat ...string) *Entry { if e.err != nil { return e } e.Description.Tags = cat return e } // DescInput description for Input func (e *Entry) DescInput(i int, desc string) *Entry { if e.err != nil { return e } e.Description.InputDesc[i] = desc return e } // DescOutput description for Input func (e *Entry) DescOutput(desc string) *Entry { if e.err != nil { return e } e.Description.OutputDesc = desc return e } // Extra information on entry func (e *Entry) Extra(name string, extra interface{}) *Entry { if e.err != nil { return e } e.Description.Extra[name] = extra return e } //Batch helper to batch set properties type Batch []*Entry //Tags set categories of the group func (eg Batch) Tags(cat ...string) Batch { for _, e := range eg { e.Tags(cat...) } return eg } // Extra set extras of the group func (eg Batch) Extra(name string, value interface{}) Batch { for _, e := range eg { e.Extra(name, value) } return eg }