ext_inferer_custom.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. //go:build schema || !core
  15. package schema
  16. import (
  17. "encoding/json"
  18. "fmt"
  19. "plugin"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/internal/pkg/def"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. "github.com/lf-edge/ekuiper/pkg/message"
  25. )
  26. func init() {
  27. inferes[message.FormatCustom] = InferCustom
  28. }
  29. func InferCustom(schemaFile string, messageName string) (ast.StreamFields, error) {
  30. conf.Log.Infof("Load custom schema from file %s, for symbol Get%s", schemaFile, messageName)
  31. ffs, err := GetSchemaFile(def.CUSTOM, schemaFile)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if ffs.SoFile == "" {
  36. return nil, fmt.Errorf("no so file found for custom schema %s", messageName)
  37. }
  38. sp, err := plugin.Open(ffs.SoFile)
  39. if err != nil {
  40. conf.Log.Errorf(fmt.Sprintf("custom schema file %s open error: %v", ffs.SoFile, err))
  41. return nil, fmt.Errorf("cannot open %s: %v", ffs.SoFile, err)
  42. }
  43. nf, err := sp.Lookup("Get" + messageName)
  44. if err != nil {
  45. conf.Log.Warnf(fmt.Sprintf("cannot find schemaId %s, please check if it is exported: Get%v", messageName, err))
  46. return nil, nil
  47. }
  48. nff, ok := nf.(func() interface{})
  49. if !ok {
  50. conf.Log.Errorf("exported symbol Get%s is not func to return interface{}", messageName)
  51. return nil, fmt.Errorf("load custom schema %s, message %s error", ffs.SoFile, messageName)
  52. }
  53. mc, ok := nff().(message.SchemaProvider)
  54. if ok {
  55. sj := mc.GetSchemaJson()
  56. var result ast.StreamFields
  57. err := json.Unmarshal(cast.StringToBytes(sj), &result)
  58. if err != nil {
  59. return nil, fmt.Errorf("invalid schema json %s: %v", sj, err)
  60. }
  61. return result, nil
  62. } else {
  63. return nil, fmt.Errorf("get schema converter failed, exported symbol %s is not type of message.Converter", messageName)
  64. }
  65. }