connect_selector.go 1.2 KB

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