entry.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. outTyp := fntyp.Out(0)
  33. if outTyp.Kind() == reflect.Func {
  34. outTyp.Out(0)
  35. }
  36. fnTyp := reflect.TypeOf(e.fn)
  37. nInputs := fnTyp.NumIn()
  38. // Experimental
  39. offs := 0
  40. if fnTyp.NumIn() > 0 && fnTyp.In(0).String() == "*flow.Flow" {
  41. nInputs--
  42. offs = 1
  43. }
  44. ///
  45. Inputs := make([]DescType, nInputs)
  46. for i := 0; i < nInputs; i++ {
  47. inTyp := fnTyp.In(i + offs)
  48. Inputs[i] = DescType{fmt.Sprint(inTyp), ""}
  49. e.Inputs = append(e.Inputs, inTyp) // ?
  50. }
  51. Output := DescType{fmt.Sprint(outTyp), ""}
  52. e.Output = outTyp // ?
  53. e.Description = &Description{
  54. Tags: []string{"generic"},
  55. Inputs: Inputs,
  56. Output: Output,
  57. Extra: map[string]interface{}{},
  58. }
  59. return e
  60. }
  61. // Describer return a description builder
  62. func (e *Entry) Describer() EDescriber {
  63. return Describer(e)
  64. }
  65. // Err returns error of the entry if any
  66. func (e *Entry) Err() error {
  67. return e.err
  68. }