entry.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package registry
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. //DescType type Description
  7. type DescType struct {
  8. Type string `json:"type"`
  9. Name string `json:"name"`
  10. }
  11. // Entry contains a function description params etc
  12. type Entry struct {
  13. registry *R
  14. fn interface{}
  15. Inputs []reflect.Type
  16. Output reflect.Type
  17. Description *Description
  18. err error
  19. }
  20. // NewEntry creates and describes a New Entry
  21. func NewEntry(r *R, fn interface{}) *Entry {
  22. e := &Entry{registry: r, fn: fn}
  23. fntyp := reflect.TypeOf(e.fn)
  24. if fntyp.Kind() != reflect.Func {
  25. e.err = ErrNotAFunc
  26. return e
  27. }
  28. if fntyp.NumOut() == 0 {
  29. e.err = ErrOutput
  30. return e
  31. }
  32. fnTyp := reflect.TypeOf(e.fn)
  33. nInputs := fnTyp.NumIn()
  34. Inputs := make([]DescType, nInputs)
  35. for i := 0; i < nInputs; i++ {
  36. Inputs[i] = DescType{fmt.Sprint(fnTyp.In(i)), ""}
  37. e.Inputs = append(e.Inputs, fnTyp.In(i)) // ?
  38. }
  39. Output := DescType{fmt.Sprint(fnTyp.Out(0)), ""}
  40. e.Output = fnTyp.Out(0) // ?
  41. e.Description = &Description{
  42. Tags: []string{"generic"},
  43. Inputs: Inputs,
  44. Output: Output,
  45. Extra: map[string]interface{}{},
  46. }
  47. return e
  48. }
  49. // Describer return a description builder
  50. func (e *Entry) Describer() EDescriber {
  51. return Describer(e)
  52. }
  53. // Err returns error of the entry if any
  54. func (e *Entry) Err() error {
  55. return e.err
  56. }