[MqttValue] Return json content as default value
This commit is contained in:
parent
4932ffbf31
commit
33511a015a
@ -1,6 +1,7 @@
|
|||||||
package mqttdevice
|
package mqttdevice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/cyrilix/robocar-base/types"
|
"github.com/cyrilix/robocar-base/types"
|
||||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
@ -107,8 +108,12 @@ func NewMqttValue(v interface{}) MqttValue {
|
|||||||
case MqttValue:
|
case MqttValue:
|
||||||
return val
|
return val
|
||||||
default:
|
default:
|
||||||
log.Printf("invalid mqtt value: %v", val)
|
jsonValue, err := json.Marshal(v)
|
||||||
return nil
|
if err != nil {
|
||||||
|
log.Printf("unable to mashall to json value '%v': %v", v, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return jsonValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,14 +66,14 @@ func TestNewMqttValue(t *testing.T) {
|
|||||||
{[]byte("test bytes"), []byte("test bytes")},
|
{[]byte("test bytes"), []byte("test bytes")},
|
||||||
|
|
||||||
{struct {
|
{struct {
|
||||||
content string
|
Content string
|
||||||
}{"invalid"}, nil},
|
}{"other"}, []byte(`{"Content":"other"}`)},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
val := NewMqttValue(c.value)
|
val := NewMqttValue(c.value)
|
||||||
if string(val) != string(c.expected) {
|
if string(val) != string(c.expected) {
|
||||||
t.Errorf("NewMqttValue(%v): %v, wants %v", c.value, val, c.expected)
|
t.Errorf("NewMqttValue(%v): %v, wants %v", c.value, string(val), string(c.expected))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user