inferer.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/pkg/ast"
  18. "github.com/lf-edge/ekuiper/pkg/message"
  19. "strings"
  20. )
  21. type inferer func(schemaFileName string, SchemaMessageName string) (ast.StreamFields, error)
  22. var ( // init once and read only
  23. inferes = map[string]inferer{
  24. message.FormatCustom: InferCustom,
  25. }
  26. )
  27. func InferFromSchemaFile(schemaType string, schemaId string) (ast.StreamFields, error) {
  28. r := strings.Split(schemaId, ".")
  29. if len(r) != 2 {
  30. return nil, fmt.Errorf("invalid schemaId: %s", schemaId)
  31. }
  32. if c, ok := inferes[schemaType]; ok {
  33. return c(r[0], r[1])
  34. } else {
  35. return nil, fmt.Errorf("unsupported type: %s", schemaType)
  36. }
  37. }