schema.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2022 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 schema
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/pkg/def"
  19. )
  20. type Info struct {
  21. Type def.SchemaType `json:"type"`
  22. Name string `json:"name"`
  23. Content string `json:"content"`
  24. FilePath string `json:"file"`
  25. SoPath string `json:"soFile"`
  26. }
  27. func (i *Info) InstallScript() string {
  28. marshal, err := json.Marshal(i)
  29. if err != nil {
  30. return ""
  31. }
  32. return string(marshal)
  33. }
  34. func (i *Info) Validate() error {
  35. if i.Name == "" {
  36. return fmt.Errorf("name is required")
  37. }
  38. if i.Content != "" && i.FilePath != "" {
  39. return fmt.Errorf("cannot specify both content and file")
  40. }
  41. switch i.Type {
  42. case def.PROTOBUF:
  43. if i.Content == "" && i.FilePath == "" {
  44. return fmt.Errorf("must specify content or file")
  45. }
  46. case def.CUSTOM:
  47. if i.SoPath == "" {
  48. return fmt.Errorf("soFile is required")
  49. }
  50. default:
  51. return fmt.Errorf("unsupported type: %s", i.Type)
  52. }
  53. return nil
  54. }
  55. var schemaExt = map[def.SchemaType]string{
  56. def.PROTOBUF: ".proto",
  57. }