Skip to content

Commit 58a8ed8

Browse files
committedApr 2, 2014
eliminate interface{} from public api aside from trace formatting
Change-Id: I7f7def6ff2cb8dab246f6a299c861e364d3f35f0 Signed-off-by: Seth Hoenig <seth.a.hoenig@gmail.com>
1 parent 5769c81 commit 58a8ed8

File tree

4 files changed

+41
-55
lines changed

4 files changed

+41
-55
lines changed
 

‎client.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,9 @@ func (c *MqttClient) disconnect() {
253253
// and content to the specified topic.
254254
// Returns a read only channel used to track
255255
// the delivery of the message.
256-
func (c *MqttClient) Publish(qos QoS, topic string, message interface{}) <-chan Receipt {
257-
var pub *Message
258-
switch msg := message.(type) {
259-
case string:
260-
pub = newPublishMsg(qos, topic, []byte(msg))
261-
case []byte:
262-
pub = newPublishMsg(qos, topic, msg)
263-
}
264-
256+
func (c *MqttClient) Publish(qos QoS, topic string, payload []byte) <-chan Receipt {
257+
pub := newPublishMsg(qos, topic, payload)
265258
r := make(chan Receipt, 1)
266-
267259
c.trace_v(CLI, "sending publish message, topic: %s", topic)
268260

269261
select {

‎fvt_client_test.go

+36-36
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Test_Publish_1(t *testing.T) {
105105
t.Fatalf("Error on MqttClient.Start(): ", err)
106106
}
107107

108-
c.Publish(QOS_ZERO, "/test/Publish", "Publish qo0")
108+
c.Publish(QOS_ZERO, "/test/Publish", []byte("Publish qo0"))
109109

110110
c.Disconnect(250)
111111
}
@@ -122,8 +122,8 @@ func Test_Publish_2(t *testing.T) {
122122
t.Fatalf("Error on MqttClient.Start(): ", err)
123123
}
124124

125-
c.Publish(QOS_ZERO, "/test/Publish", "Publish1 qos0")
126-
c.Publish(QOS_ZERO, "/test/Publish", "Publish2 qos0")
125+
c.Publish(QOS_ZERO, "/test/Publish", []byte("Publish1 qos0"))
126+
c.Publish(QOS_ZERO, "/test/Publish", []byte("Publish2 qos0"))
127127

128128
c.Disconnect(250)
129129
}
@@ -140,9 +140,9 @@ func Test_Publish_3(t *testing.T) {
140140
t.Fatalf("Error on MqttClient.Start(): ", err)
141141
}
142142

143-
c.Publish(QOS_ZERO, "/test/Publish", "Publish1 qos0")
144-
c.Publish(QOS_ONE, "/test/Publish", "Publish2 qos1")
145-
c.Publish(QOS_TWO, "/test/Publish", "Publish2 qos2")
143+
c.Publish(QOS_ZERO, "/test/Publish", []byte("Publish1 qos0"))
144+
c.Publish(QOS_ONE, "/test/Publish", []byte("Publish2 qos1"))
145+
c.Publish(QOS_TWO, "/test/Publish", []byte("Publish2 qos2"))
146146

147147
c.Disconnect(250)
148148
}
@@ -178,7 +178,7 @@ func Test_Subscribe(t *testing.T) {
178178
t.Fatalf("Error on MqttClient.Start(): ", err)
179179
}
180180

181-
p.Publish(QOS_ZERO, "/test/sub", "Publish qos0")
181+
p.Publish(QOS_ZERO, "/test/sub", []byte("Publish qos0"))
182182

183183
p.Disconnect(250)
184184
s.Disconnect(250)
@@ -334,15 +334,15 @@ func Test_p0s0(t *testing.T) {
334334
if err != nil {
335335
t.Fatalf("Error on MqttClient.Start(): ", err)
336336
}
337-
receipt = p.Publish(QOS_ZERO, topic, "p0s0 payload 1")
337+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s0 payload 1"))
338338
<-receipt
339-
receipt = p.Publish(QOS_ZERO, topic, "p0s0 payload 2")
339+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s0 payload 2"))
340340
<-receipt
341341

342342
wait(choke)
343343
wait(choke)
344344

345-
receipt = p.Publish(QOS_ZERO, topic, "p0s0 payload 3")
345+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s0 payload 3"))
346346
<-receipt
347347
wait(choke)
348348

@@ -394,14 +394,14 @@ func Test_p0s1(t *testing.T) {
394394
if err != nil {
395395
t.Fatalf("Error on MqttClient.Start(): ", err)
396396
}
397-
receipt = p.Publish(QOS_ZERO, topic, "p0s1 payload 1")
397+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s1 payload 1"))
398398
<-receipt
399-
receipt = p.Publish(QOS_ZERO, topic, "p0s1 payload 2")
399+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s1 payload 2"))
400400
<-receipt
401401
wait(choke)
402402
wait(choke)
403403

404-
receipt = p.Publish(QOS_ZERO, topic, "p0s1 payload 3")
404+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s1 payload 3"))
405405
<-receipt
406406
wait(choke)
407407

@@ -453,14 +453,14 @@ func Test_p0s2(t *testing.T) {
453453
if err != nil {
454454
t.Fatalf("Error on MqttClient.Start(): ", err)
455455
}
456-
receipt = p.Publish(QOS_ZERO, topic, "p0s2 payload 1")
456+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s2 payload 1"))
457457
<-receipt
458-
receipt = p.Publish(QOS_ZERO, topic, "p0s2 payload 2")
458+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s2 payload 2"))
459459
<-receipt
460460
wait(choke)
461461
wait(choke)
462462

463-
receipt = p.Publish(QOS_ZERO, topic, "p0s2 payload 3")
463+
receipt = p.Publish(QOS_ZERO, topic, []byte("p0s2 payload 3"))
464464
<-receipt
465465
wait(choke)
466466

@@ -512,14 +512,14 @@ func Test_p1s0(t *testing.T) {
512512
if err != nil {
513513
t.Fatalf("Error on MqttClient.Start(): ", err)
514514
}
515-
receipt = p.Publish(QOS_ONE, topic, "p1s0 payload 1")
515+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s0 payload 1"))
516516
<-receipt
517-
receipt = p.Publish(QOS_ONE, topic, "p1s0 payload 2")
517+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s0 payload 2"))
518518
<-receipt
519519
wait(choke)
520520
wait(choke)
521521

522-
receipt = p.Publish(QOS_ONE, topic, "p1s0 payload 3")
522+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s0 payload 3"))
523523
<-receipt
524524
wait(choke)
525525

@@ -571,14 +571,14 @@ func Test_p1s1(t *testing.T) {
571571
if err != nil {
572572
t.Fatalf("Error on MqttClient.Start(): ", err)
573573
}
574-
receipt = p.Publish(QOS_ONE, topic, "p1s1 payload 1")
574+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s1 payload 1"))
575575
<-receipt
576-
receipt = p.Publish(QOS_ONE, topic, "p1s1 payload 2")
576+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s1 payload 2"))
577577
<-receipt
578578
wait(choke)
579579
wait(choke)
580580

581-
receipt = p.Publish(QOS_ONE, topic, "p1s1 payload 3")
581+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s1 payload 3"))
582582
<-receipt
583583
wait(choke)
584584

@@ -629,14 +629,14 @@ func Test_p1s2(t *testing.T) {
629629
if err != nil {
630630
t.Fatalf("Error on MqttClient.Start(): ", err)
631631
}
632-
receipt = p.Publish(QOS_ONE, topic, "p1s2 payload 1")
632+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s2 payload 1"))
633633
<-receipt
634-
receipt = p.Publish(QOS_ONE, topic, "p1s2 payload 2")
634+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s2 payload 2"))
635635
<-receipt
636636
wait(choke)
637637
wait(choke)
638638

639-
receipt = p.Publish(QOS_ONE, topic, "p1s2 payload 3")
639+
receipt = p.Publish(QOS_ONE, topic, []byte("p1s2 payload 3"))
640640
<-receipt
641641
wait(choke)
642642

@@ -688,12 +688,12 @@ func Test_p2s0(t *testing.T) {
688688
if err != nil {
689689
t.Fatalf("Error on MqttClient.Start(): ", err)
690690
}
691-
p.Publish(QOS_TWO, topic, "p2s0 payload 1")
692-
p.Publish(QOS_TWO, topic, "p2s0 payload 2")
691+
p.Publish(QOS_TWO, topic, []byte("p2s0 payload 1"))
692+
p.Publish(QOS_TWO, topic, []byte("p2s0 payload 2"))
693693
wait(choke)
694694
wait(choke)
695695

696-
p.Publish(QOS_TWO, topic, "p2s0 payload 3")
696+
p.Publish(QOS_TWO, topic, []byte("p2s0 payload 3"))
697697
wait(choke)
698698

699699
p.Disconnect(250)
@@ -744,14 +744,14 @@ func Test_p2s1(t *testing.T) {
744744
if err != nil {
745745
t.Fatalf("Error on MqttClient.Start(): ", err)
746746
}
747-
receipt = p.Publish(QOS_TWO, topic, "p2s1 payload 1")
747+
receipt = p.Publish(QOS_TWO, topic, []byte("p2s1 payload 1"))
748748
<-receipt
749-
receipt = p.Publish(QOS_TWO, topic, "p2s1 payload 2")
749+
receipt = p.Publish(QOS_TWO, topic, []byte("p2s1 payload 2"))
750750
<-receipt
751751
wait(choke)
752752
wait(choke)
753753

754-
receipt = p.Publish(QOS_TWO, topic, "p2s1 payload 3")
754+
receipt = p.Publish(QOS_TWO, topic, []byte("p2s1 payload 3"))
755755
<-receipt
756756
wait(choke)
757757

@@ -803,14 +803,14 @@ func Test_p2s2(t *testing.T) {
803803
if err != nil {
804804
t.Fatalf("Error on MqttClient.Start(): ", err)
805805
}
806-
receipt = p.Publish(QOS_TWO, topic, "p2s2 payload 1")
806+
receipt = p.Publish(QOS_TWO, topic, []byte("p2s2 payload 1"))
807807
<-receipt
808-
receipt = p.Publish(QOS_TWO, topic, "p2s2 payload 2")
808+
receipt = p.Publish(QOS_TWO, topic, []byte("p2s2 payload 2"))
809809
<-receipt
810810
wait(choke)
811811
wait(choke)
812812

813-
receipt = p.Publish(QOS_TWO, topic, "p2s2 payload 3")
813+
receipt = p.Publish(QOS_TWO, topic, []byte("p2s2 payload 3"))
814814
<-receipt
815815
wait(choke)
816816

@@ -865,7 +865,7 @@ func Test_PublishMessage(t *testing.T) {
865865
}
866866

867867
text := "pubmsg payload"
868-
m := NewMessage(text)
868+
m := NewMessage([]byte(text))
869869
p.PublishMessage(topic, m)
870870
p.PublishMessage(topic, m)
871871
wait(choke)
@@ -985,7 +985,7 @@ func Test_Cleanstore(t *testing.T) {
985985
t.Fatalf("Error on MqttClient.Start(): ", err)
986986
}
987987

988-
m := NewMessage("test message")
988+
m := NewMessage([]byte("test message"))
989989
p.PublishMessage(topic, m)
990990
p.PublishMessage(topic, m)
991991
p.PublishMessage(topic, m)

‎message.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,12 @@ type sendable struct {
4040
// Create a default PUBLISH Message with the specified payload
4141
// If message == nil, create a zero length message
4242
// Defaults: QoS=1, Retained=False
43-
func NewMessage(message interface{}) *Message {
43+
func NewMessage(message []byte) *Message {
4444
m := newMsg(PUBLISH, false, QOS_ONE, false)
45-
4645
if message == nil {
4746
m.appendPayloadField([]byte{})
4847
} else {
49-
switch msg := message.(type) {
50-
case string:
51-
m.appendPayloadField([]byte(msg))
52-
case []byte:
53-
m.appendPayloadField(msg)
54-
}
48+
m.appendPayloadField(message)
5549
}
5650
return m
5751
}

‎unit_message_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ func Test_newUnsubscribeMessage_unsb3(t *testing.T) {
693693
}
694694

695695
func Test_NewMessage(t *testing.T) {
696-
m := NewMessage("test message")
696+
m := NewMessage([]byte("test message"))
697697

698698
exp := []byte{
699699
/* msg type */

0 commit comments

Comments
 (0)
Please sign in to comment.