entry.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package registry
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. // Description of an entry
  7. type Description struct {
  8. Name string `json:"name"`
  9. Tags []string `json:"categories"`
  10. Inputs []string `json:"inputs"`
  11. InputDesc map[int]string `json:"inputsDesc"`
  12. Output string `json:"output"`
  13. OutputDesc string `json:"outputDesc"`
  14. Extra map[string]interface{} `json:"extra"`
  15. }
  16. // Entry contains a function description params etc
  17. type Entry struct {
  18. registry *R
  19. fn interface{}
  20. Inputs []reflect.Type
  21. Output reflect.Type
  22. Description *Description
  23. err error
  24. }
  25. // NewEntry creates and describes a New Entry
  26. func NewEntry(r *R, fn interface{}) *Entry {
  27. e := &Entry{registry: r, fn: fn}
  28. fntyp := reflect.TypeOf(e.fn)
  29. if fntyp.Kind() != reflect.Func {
  30. e.err = ErrNotAFunc
  31. return e
  32. }
  33. if fntyp.NumOut() == 0 {
  34. e.err = ErrOutput
  35. return e
  36. }
  37. fnTyp := reflect.TypeOf(e.fn)
  38. Inputs := []string{}
  39. nInputs := fnTyp.NumIn()
  40. for i := 0; i < nInputs; i++ {
  41. e.Inputs = append(e.Inputs, fnTyp.In(i))
  42. Inputs = append(Inputs, fmt.Sprint(fnTyp.In(i)))
  43. }
  44. Output := fmt.Sprint(fnTyp.Out(0))
  45. e.Output = fnTyp.Out(0)
  46. e.Description = &Description{
  47. Tags: []string{"generic"},
  48. Inputs: Inputs,
  49. Output: Output,
  50. InputDesc: map[int]string{},
  51. Extra: map[string]interface{}{},
  52. }
  53. return e
  54. }
  55. // Err returns error of the entry if any
  56. func (e *Entry) Err() error {
  57. return e.err
  58. }
  59. //Tags of the entry
  60. func (e *Entry) Tags(cat ...string) *Entry {
  61. if e.err != nil {
  62. return e
  63. }
  64. e.Description.Tags = cat
  65. return e
  66. }
  67. // DescInput description for Input
  68. func (e *Entry) DescInput(i int, desc string) *Entry {
  69. if e.err != nil {
  70. return e
  71. }
  72. e.Description.InputDesc[i] = desc
  73. return e
  74. }
  75. // DescOutput description for Input
  76. func (e *Entry) DescOutput(desc string) *Entry {
  77. if e.err != nil {
  78. return e
  79. }
  80. e.Description.OutputDesc = desc
  81. return e
  82. }
  83. // Extra information on entry
  84. func (e *Entry) Extra(name string, extra interface{}) *Entry {
  85. if e.err != nil {
  86. return e
  87. }
  88. e.Description.Extra[name] = extra
  89. return e
  90. }
  91. //Batch helper to batch set properties
  92. type Batch []*Entry
  93. //Tags set categories of the group
  94. func (eg Batch) Tags(cat ...string) Batch {
  95. for _, e := range eg {
  96. e.Tags(cat...)
  97. }
  98. return eg
  99. }
  100. // Extra set extras of the group
  101. func (eg Batch) Extra(name string, value interface{}) Batch {
  102. for _, e := range eg {
  103. e.Extra(name, value)
  104. }
  105. return eg
  106. }