plugin.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package plugin
  15. import "encoding/json"
  16. type PluginType int
  17. const (
  18. SOURCE PluginType = iota
  19. SINK
  20. FUNCTION
  21. PORTABLE
  22. WASM
  23. )
  24. var PluginTypes = []string{"sources", "sinks", "functions", "portable", "wasm"}
  25. var PluginTypeMap = map[string]PluginType{
  26. "sources": SOURCE,
  27. "sinks": SINK,
  28. "functions": FUNCTION,
  29. "portable": PORTABLE,
  30. "wasm": WASM,
  31. }
  32. type Plugin interface {
  33. GetName() string
  34. GetFile() string
  35. GetShellParas() []string
  36. GetSymbols() []string
  37. SetName(n string)
  38. GetInstallScripts() []byte
  39. }
  40. // IOPlugin Unify model. Flat all properties for each kind.
  41. type IOPlugin struct {
  42. Name string `json:"name"`
  43. File string `json:"file"`
  44. ShellParas []string `json:"shellParas"`
  45. }
  46. func (p *IOPlugin) GetName() string {
  47. return p.Name
  48. }
  49. func (p *IOPlugin) GetFile() string {
  50. return p.File
  51. }
  52. func (p *IOPlugin) GetShellParas() []string {
  53. return p.ShellParas
  54. }
  55. func (p *IOPlugin) GetSymbols() []string {
  56. return nil
  57. }
  58. func (p *IOPlugin) SetName(n string) {
  59. p.Name = n
  60. }
  61. func (p *IOPlugin) GetInstallScripts() []byte {
  62. marshal, err := json.Marshal(p)
  63. if err != nil {
  64. return nil
  65. }
  66. return marshal
  67. }
  68. func NewPluginByType(t PluginType) Plugin {
  69. switch t {
  70. case FUNCTION:
  71. return &FuncPlugin{}
  72. case WASM:
  73. return &FuncPlugin{}
  74. default:
  75. return &IOPlugin{}
  76. }
  77. }
  78. type FuncPlugin struct {
  79. IOPlugin
  80. // Optional, if not specified, a default element with the same name of the file will be registered
  81. Functions []string `json:"functions"`
  82. }
  83. func (fp *FuncPlugin) GetSymbols() []string {
  84. return fp.Functions
  85. }
  86. type EXTENSION_TYPE int
  87. const (
  88. NONE_EXTENSION EXTENSION_TYPE = iota
  89. INTERNAL
  90. NATIVE_EXTENSION
  91. PORTABLE_EXTENSION
  92. SERVICE_EXTENSION
  93. WASM_EXTENSION
  94. )