Skip to content

Commit

Permalink
fix(edgex): convert the optional configure to map[string]string
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong <huangjy@emqx.io>
  • Loading branch information
ngjaying committed Mar 2, 2022
1 parent 805fdd9 commit cf0d7b7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion etc/connections/connection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ edgex:
port: 1883
type: mqtt
optional:
ClientId: "client1"
KeepAlive: "50"

zeroMsgBus: #connection key
protocol: tcp
Expand Down
2 changes: 1 addition & 1 deletion etc/sources/edgex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mqtt_conf: #Conf_key
topic: events
type: mqtt
optional:
ClientId: "client1"
KeepAlive: "50"

share_conf: #Conf_key
protocol: tcp
Expand Down
12 changes: 7 additions & 5 deletions internal/topo/connection/clients/edgex/client_edgex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func TestEdgex_CfgValidate(t *testing.T) {
"server": "127.0.0.1",
"port": 1883,
"type": "mqtt",
"optional": map[string]string{
"ClientId": "client1",
"Username": "user1",
"optional": map[string]interface{}{
"ClientId": "client1",
"Username": "user1",
"KeepAlive": 500,
},
},
wantErr: false,
Expand All @@ -57,8 +58,9 @@ func TestEdgex_CfgValidate(t *testing.T) {
},
Type: "mqtt",
Optional: map[string]string{
"ClientId": "client1",
"Username": "user1",
"ClientId": "client1",
"Username": "user1",
"KeepAlive": "500",
},
},
},
Expand Down
15 changes: 15 additions & 0 deletions internal/topo/connection/clients/edgex/edgex.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ func (es *EdgexClient) CfgValidate(props map[string]interface{}) error {
Optional: nil,
}

if o, ok := props["optional"]; ok {
switch ot := o.(type) {
case map[string]string:
c.Optional = ot
case map[string]interface{}:
c.Optional = make(map[string]string)
for k, v := range ot {
c.Optional[k] = fmt.Sprintf("%v", v)
}
default:
return fmt.Errorf("invalid optional config %v, must be a map", o)
}
delete(props, "optional")
}

err := cast.MapToStruct(props, c)
if err != nil {
return fmt.Errorf("map config map to struct fail with error: %v", err)
Expand Down

0 comments on commit cf0d7b7

Please sign in to comment.