describer.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package registry
  2. // Description of an entry
  3. type Description struct {
  4. Name string `json:"name"`
  5. Desc string `json:"description"`
  6. Tags []string `json:"categories"`
  7. //InputType
  8. Inputs []DescType `json:"inputs"`
  9. Output DescType `json:"output"`
  10. Extra map[string]interface{} `json:"extra"`
  11. }
  12. //EDescriber helper to batch set properties
  13. type EDescriber []*Entry
  14. // Describer returns a batch of entries for easy manipulation
  15. func Describer(params ...interface{}) EDescriber {
  16. ret := EDescriber{}
  17. for _, el := range params {
  18. switch v := el.(type) {
  19. case EDescriber:
  20. ret = append(ret, v...)
  21. case *Entry:
  22. ret = append(ret, v)
  23. }
  24. }
  25. return ret
  26. }
  27. // Description set node description
  28. func (d EDescriber) Description(m string) EDescriber {
  29. for _, e := range d {
  30. if e.Description == nil {
  31. continue
  32. }
  33. e.Description.Desc = m
  34. }
  35. return d
  36. }
  37. //Tags set categories of the group
  38. func (d EDescriber) Tags(tags ...string) EDescriber {
  39. for _, e := range d {
  40. if e.Description == nil {
  41. continue
  42. }
  43. e.Description.Tags = tags
  44. }
  45. return d
  46. }
  47. // Inputs describe inputs
  48. func (d EDescriber) Inputs(inputs ...string) EDescriber {
  49. for _, e := range d {
  50. if e.Description == nil {
  51. continue
  52. }
  53. for i, dstr := range inputs {
  54. if i >= len(e.Description.Inputs) { // do nothing
  55. break // next entry
  56. }
  57. curDesc := e.Description.Inputs[i]
  58. e.Description.Inputs[i] = DescType{curDesc.Type, dstr}
  59. }
  60. }
  61. return d
  62. }
  63. // Output describe the output
  64. func (d EDescriber) Output(output string) EDescriber {
  65. for _, e := range d {
  66. if e.Description == nil {
  67. continue
  68. }
  69. e.Description.Output = DescType{e.Description.Output.Type, output}
  70. }
  71. return d
  72. }
  73. // Extra set extras of the group
  74. func (d EDescriber) Extra(name string, value interface{}) EDescriber {
  75. for _, e := range d {
  76. if e.Description == nil {
  77. continue
  78. }
  79. e.Description.Extra[name] = value
  80. }
  81. return d
  82. }