Skip to content

Commit

Permalink
Mqtt: publish pointer values (#13741)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored May 4, 2024
1 parent bf58479 commit 3794b3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 5 additions & 2 deletions server/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ func (m *MQTT) publishComplex(topic string, retained bool, payload interface{})
}

case reflect.Pointer:
if reflect.ValueOf(payload).IsNil() {
payload = nil
if !reflect.ValueOf(payload).IsNil() {
m.publishComplex(topic, retained, reflect.Indirect(reflect.ValueOf(payload)).Interface())
return
}

payload = nil
fallthrough

default:
Expand Down
20 changes: 15 additions & 5 deletions server/mqtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,25 @@ func TestPublishTypes(t *testing.T) {
}{
Foo: "bar",
})
require.Len(t, topics, 1)
assert.Equal(t, `test/foo`, topics[0], "struct mismatch")
assert.Equal(t, `bar`, payloads[0], "struct mismatch")
assert.Equal(t, []string{"test/foo"}, topics, "struct mismatch")
assert.Equal(t, []string{"bar"}, payloads, "struct mismatch")
reset()

i := 1
m.publish("test", false, struct {
Foo, Bar *int
}{
Foo: &i,
Bar: nil,
})
assert.Equal(t, []string{"test/foo", "test/bar"}, topics, "pointer mismatch")
assert.Equal(t, []string{"1", ""}, payloads, "pointer mismatch")
reset()

slice := []int{10, 20}
m.publish("test", false, slice)
require.Len(t, topics, 3)
assert.Equal(t, []string{`test`, `test/1`, `test/2`}, topics, "slice mismatch")
assert.Equal(t, []string{`2`, `10`, `20`}, payloads, "slice mismatch")
assert.Equal(t, []string{"test", "test/1", "test/2"}, topics, "slice mismatch")
assert.Equal(t, []string{"2", "10", "20"}, payloads, "slice mismatch")
reset()
}

0 comments on commit 3794b3f

Please sign in to comment.