connect_selector.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package conf
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type ConSelector struct {
  7. ConnSelectorStr string
  8. Type string // mqtt edgex
  9. CfgKey string // config key
  10. }
  11. func (c *ConSelector) Init() error {
  12. conTypeSel := strings.SplitN(c.ConnSelectorStr, ".", 2)
  13. if len(conTypeSel) != 2 {
  14. return fmt.Errorf("not a valid connection selector : %s", c.ConnSelectorStr)
  15. }
  16. c.Type = strings.ToLower(conTypeSel[0])
  17. c.CfgKey = conTypeSel[1]
  18. return nil
  19. }
  20. func (c *ConSelector) ReadCfgFromYaml() (props map[string]interface{}, err error) {
  21. yamlOps, err := NewConfigOperatorFromConnectionYaml(c.Type)
  22. if err != nil {
  23. return nil, err
  24. }
  25. cfg := yamlOps.CopyConfContent()
  26. if len(cfg) == 0 {
  27. return nil, fmt.Errorf("fail to parse yaml for connection Type %s", c.Type)
  28. } else {
  29. if cons, found := cfg[strings.ToLower(c.CfgKey)]; found {
  30. props = cons
  31. } else if cons, found := cfg[c.CfgKey]; found {
  32. props = cons
  33. } else {
  34. return nil, fmt.Errorf("not found connection Type and Selector: %s.%s", c.Type, c.CfgKey)
  35. }
  36. }
  37. jsonPath := "sources/" + c.Type + ".json"
  38. if c.Type == "mqtt" {
  39. jsonPath = "mqtt_source.json"
  40. }
  41. err = CorrectsConfigKeysByJson(props, jsonPath)
  42. return props, err
  43. }