Skip to content

Commit 5a92b8c

Browse files
tfogoskriptble
authored andcommitted
Implement Msg.String()
Change-Id: I36f7d44d2dc66fa7e51f9f05d13f3767c800fd34
1 parent 8e600a4 commit 5a92b8c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

x/network/wiremessage/msg.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package wiremessage
88

99
import (
1010
"errors"
11+
"fmt"
1112

1213
"go.mongodb.org/mongo-driver/bson"
1314
"go.mongodb.org/mongo-driver/x/bsonx"
@@ -63,7 +64,10 @@ func (m Msg) AppendWireMessage(b []byte) ([]byte, error) {
6364

6465
// String implements the fmt.Stringer interface.
6566
func (m Msg) String() string {
66-
panic("not implemented")
67+
return fmt.Sprintf(
68+
`OP_MSG{MsgHeader: %v, FlagBits: %d, Sections: %v, Checksum: %d}`,
69+
m.MsgHeader, m.FlagBits, m.Sections, m.Checksum,
70+
)
6771
}
6872

6973
// Len implements the WireMessage interface.

x/network/wiremessage/msg_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,28 @@ func TestMsg(t *testing.T) {
106106
})
107107
}
108108
})
109+
110+
t.Run("WireMessageToString", func(t *testing.T) {
111+
msgHeader := Header{
112+
MessageLength: 33,
113+
RequestID: 0,
114+
ResponseTo: 0,
115+
OpCode: 2013,
116+
}
117+
118+
msg := Msg{
119+
MsgHeader: msgHeader,
120+
FlagBits: 0,
121+
Sections: oneSection(t),
122+
}
123+
124+
var msgString string
125+
126+
msgString = "OP_MSG{MsgHeader: Header{MessageLength: 33, RequestID: 0, ResponseTo: 0, OpCode: OP_MSG}, " +
127+
"FlagBits: 0, Sections: [{0 {\"x\": {\"$numberInt\":\"5\"}}}], Checksum: 0}"
128+
129+
if msg.String() != msgString {
130+
t.Errorf("Did not get expected string. got:\n%s\n\nexpected:\n%s", msg.String(), msgString)
131+
}
132+
})
109133
}

0 commit comments

Comments
 (0)