schema.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/pkg/def"
  18. "github.com/lf-edge/ekuiper/internal/schema/protobuf"
  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. }
  26. var (
  27. schemaExt = map[def.SchemaType]string{
  28. def.PROTOBUF: ".proto",
  29. }
  30. )
  31. // Converter converts bytes & map or []map according to the schema
  32. type Converter interface {
  33. Encode(d interface{}) ([]byte, error)
  34. Decode(b []byte) (interface{}, error)
  35. }
  36. func GetOrCreateSchema(t def.SchemaType, schemaFile string, schemaId string) (Converter, error) {
  37. switch t {
  38. case def.PROTOBUF:
  39. fileName, err := getSchemaFile(t, schemaFile)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return protobuf.NewConverter(schemaId, fileName)
  44. default:
  45. return nil, fmt.Errorf("unsupported schema type: %s", t)
  46. }
  47. }