diff --git a/mqttdevice/mqttdevice.go b/mqttdevice/mqttdevice.go index df9801b..5a5af8a 100644 --- a/mqttdevice/mqttdevice.go +++ b/mqttdevice/mqttdevice.go @@ -1,6 +1,7 @@ package mqttdevice import ( + "encoding/json" "fmt" "github.com/cyrilix/robocar-base/types" MQTT "github.com/eclipse/paho.mqtt.golang" @@ -107,8 +108,12 @@ func NewMqttValue(v interface{}) MqttValue { case MqttValue: return val default: - log.Printf("invalid mqtt value: %v", val) - return nil + jsonValue, err := json.Marshal(v) + if err != nil { + log.Printf("unable to mashall to json value '%v': %v", v, err) + return nil + } + return jsonValue } } diff --git a/mqttdevice/mqttdevice_test.go b/mqttdevice/mqttdevice_test.go index b6cc020..195208c 100644 --- a/mqttdevice/mqttdevice_test.go +++ b/mqttdevice/mqttdevice_test.go @@ -66,14 +66,14 @@ func TestNewMqttValue(t *testing.T) { {[]byte("test bytes"), []byte("test bytes")}, {struct { - content string - }{"invalid"}, nil}, + Content string + }{"other"}, []byte(`{"Content":"other"}`)}, } for _, c := range cases { val := NewMqttValue(c.value) 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)) } } }