Skip to content

Commit 9c29b9d

Browse files
icholybelak
authored andcommitted
Add Param method to Message type
1 parent 94490a0 commit 9c29b9d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

parser.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ func ParseMessage(line string) (*Message, error) {
315315
return c, nil
316316
}
317317

318+
// Param returns the i'th argument in the Message or an empty string
319+
// if the requested arg does not exist
320+
func (m *Message) Param(i int) string {
321+
if i < 0 || i >= len(m.Params) {
322+
return ""
323+
}
324+
return m.Params[i]
325+
}
326+
318327
// Trailing returns the last argument in the Message or an empty string
319328
// if there are no args
320329
func (m *Message) Trailing() string {

parser_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ func TestMustParseMessage(t *testing.T) {
6868
}, "Got unexpected panic")
6969
}
7070

71+
func TestMessageParam(t *testing.T) {
72+
t.Parallel()
73+
74+
m := MustParseMessage("PING :test")
75+
assert.Equal(t, m.Param(0), "test")
76+
assert.Equal(t, m.Param(-1), "")
77+
assert.Equal(t, m.Param(2), "")
78+
}
79+
7180
func TestMessageTrailing(t *testing.T) {
7281
t.Parallel()
7382

0 commit comments

Comments
 (0)