123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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
- }
|