inferer.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2022-2023 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. "strings"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. )
  21. type inferer func(schemaFileName string, SchemaMessageName string) (ast.StreamFields, error)
  22. // init once and read only
  23. var inferes = map[string]inferer{}
  24. func InferFromSchemaFile(schemaType string, schemaId string) (ast.StreamFields, error) {
  25. if c, ok := inferes[schemaType]; ok {
  26. r := strings.Split(schemaId, ".")
  27. if len(r) != 2 {
  28. return nil, fmt.Errorf("invalid schemaId: %s", schemaId)
  29. }
  30. // mock result for testing
  31. if conf.IsTesting {
  32. return ast.StreamFields{
  33. {
  34. Name: "field1",
  35. FieldType: &ast.BasicType{
  36. Type: ast.BIGINT,
  37. },
  38. },
  39. {
  40. Name: "field2",
  41. FieldType: &ast.BasicType{
  42. Type: ast.STRINGS,
  43. },
  44. },
  45. }, nil
  46. }
  47. return c(r[0], r[1])
  48. } else {
  49. return nil, nil
  50. }
  51. }