funcMeta.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package plugins
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/common"
  5. "io/ioutil"
  6. "path"
  7. "strings"
  8. )
  9. type (
  10. fileFunc struct {
  11. Name string `json:"name"`
  12. Example string `json:"example"`
  13. Hint *fileLanguage `json:"hint"`
  14. }
  15. fileFuncs struct {
  16. About *fileAbout `json:"about"`
  17. FiFuncs []*fileFunc `json:"functions"`
  18. }
  19. uiFunc struct {
  20. Name string `json:"name"`
  21. Example string `json:"example"`
  22. Hint *language `json:"hint"`
  23. }
  24. uiFuncs struct {
  25. About *about `json:"about"`
  26. UiFuncs []*uiFunc `json:"functions"`
  27. }
  28. )
  29. func isInternalFunc(fiName string) bool {
  30. internal := []string{`accumulateWordCount.json`, `countPlusOne.json`, `echo.json`, `internal.json`}
  31. for _, v := range internal {
  32. if v == fiName {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. func newUiFuncs(fi *fileFuncs) *uiFuncs {
  39. if nil == fi {
  40. return nil
  41. }
  42. uis := new(uiFuncs)
  43. uis.About = newAbout(fi.About)
  44. for _, v := range fi.FiFuncs {
  45. ui := new(uiFunc)
  46. ui.Name = v.Name
  47. ui.Example = v.Example
  48. ui.Hint = newLanguage(v.Hint)
  49. uis.UiFuncs = append(uis.UiFuncs, ui)
  50. }
  51. return uis
  52. }
  53. var g_funcMetadata []*uiFuncs
  54. func (m *Manager) readFuncMetaDir() error {
  55. confDir, err := common.GetConfLoc()
  56. if nil != err {
  57. return err
  58. }
  59. dir := path.Join(confDir, "functions")
  60. files, err := ioutil.ReadDir(dir)
  61. if nil != err {
  62. return err
  63. }
  64. for _, file := range files {
  65. fname := file.Name()
  66. if !strings.HasSuffix(fname, ".json") {
  67. continue
  68. }
  69. filePath := path.Join(dir, fname)
  70. fis := new(fileFuncs)
  71. err = common.ReadJsonUnmarshal(filePath, fis)
  72. if nil != err {
  73. return fmt.Errorf("fname:%s err:%v", fname, err)
  74. }
  75. if nil == fis.About {
  76. return fmt.Errorf("not found about of %s", filePath)
  77. } else if isInternalFunc(fname) {
  78. fis.About.Installed = true
  79. } else {
  80. _, fis.About.Installed = m.registry.Get(FUNCTION, strings.TrimSuffix(fname, `.json`))
  81. }
  82. common.Log.Infof("funcMeta file : %s", fname)
  83. g_funcMetadata = append(g_funcMetadata, newUiFuncs(fis))
  84. }
  85. return nil
  86. }
  87. func (m *Manager) readFuncMetaFile(filePath string) error {
  88. fiName := path.Base(filePath)
  89. fis := new(fileFuncs)
  90. err := common.ReadJsonUnmarshal(filePath, fis)
  91. if nil != err {
  92. return fmt.Errorf("filePath:%s err:%v", filePath, err)
  93. }
  94. if nil == fis.About {
  95. return fmt.Errorf("not found about of %s", filePath)
  96. } else if isInternalFunc(fiName) {
  97. fis.About.Installed = true
  98. } else {
  99. _, fis.About.Installed = m.registry.Get(FUNCTION, strings.TrimSuffix(fiName, `.json`))
  100. }
  101. g_funcMetadata = append(g_funcMetadata, newUiFuncs(fis))
  102. common.Log.Infof("funcMeta file : %s", fiName)
  103. return nil
  104. }
  105. func GetFunctions() []*uiFuncs {
  106. return g_funcMetadata
  107. }