load.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 static
  15. import (
  16. "fmt"
  17. "plugin"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/pkg/message"
  20. )
  21. func LoadStaticConverter(soFile string, messageName string) (message.Converter, error) {
  22. sp, err := plugin.Open(soFile)
  23. if err != nil {
  24. conf.Log.Errorf(fmt.Sprintf("custom schema file %s open error: %v", soFile, err))
  25. return nil, fmt.Errorf("cannot open %s: %v", soFile, err)
  26. }
  27. nf, err := sp.Lookup("Get" + messageName)
  28. if err != nil {
  29. conf.Log.Warnf(fmt.Sprintf("cannot find schemaId %s, please check if it is exported: Get%v", messageName, err))
  30. return nil, nil
  31. }
  32. nff, ok := nf.(func() interface{})
  33. if !ok {
  34. conf.Log.Errorf("exported symbol Get%s is not func to return interface{}", messageName)
  35. return nil, fmt.Errorf("load static converter %s, message %s error", soFile, messageName)
  36. }
  37. mc, ok := nff().(message.Converter)
  38. if ok {
  39. return mc, nil
  40. } else {
  41. return nil, fmt.Errorf("get schema converter failed, exported symbol %s is not type of message.Converter", messageName)
  42. }
  43. }