Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Jan 19, 2021
2 parents 125cf61 + 81824d7 commit 5a62fea
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
@@ -0,0 +1 @@
github: [belak]
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Expand Up @@ -12,17 +12,20 @@ jobs:

steps:
- name: Set up Go
uses: actions/setup-go@v2-beta
uses: actions/setup-go@v2
with:
go-version: '^1.7'

- name: Check out code
uses: actions/checkout@9a3a9ade
uses: actions/checkout@v2
with:
submodules: true

- name: Clean up extra files
run: rm ./testcases/*.go

- name: Download deps
run: go mod download

# This is disabled until is properly respects the golangci_lint_version
#- name: Run golangci-lint
Expand All @@ -32,9 +35,6 @@ jobs:
# with:
# golangci_lint_version: 'v1.27.0'

- name: Download deps
run: go mod download

- name: Run tests
run: go test -race -v ./...

Expand Down
30 changes: 19 additions & 11 deletions conn.go
Expand Up @@ -101,16 +101,24 @@ func NewReader(r io.Reader) *Reader {
}

// ReadMessage returns the next message from the stream or an error.
func (r *Reader) ReadMessage() (*Message, error) {
line, err := r.reader.ReadString('\n')
if err != nil {
return nil, err
// It ignores empty messages.
func (r *Reader) ReadMessage() (msg *Message, err error) {
// It's valid for a message to be empty. Clients should ignore these,
// so we do to be good citizens.
err = ErrZeroLengthMessage
for err == ErrZeroLengthMessage {
var line string
line, err = r.reader.ReadString('\n')
if err != nil {
return nil, err
}

if r.DebugCallback != nil {
r.DebugCallback(line)
}

// Parse the message from our line
msg, err = ParseMessage(line)
}

if r.DebugCallback != nil {
r.DebugCallback(line)
}

// Parse the message from our line
return ParseMessage(line)
return msg, err
}
6 changes: 6 additions & 0 deletions conn_test.go
Expand Up @@ -123,6 +123,12 @@ func TestConn(t *testing.T) {
_, err := c.ReadMessage()
assert.Equal(t, ErrMissingDataAfterPrefix, err)

// Ensure empty messages are ignored
m = MustParseMessage("001 test_nick")
rwc.server.WriteString("\r\n" + m.String() + "\r\n")
m2 = testReadMessage(t, c)
assert.EqualValues(t, m, m2, "Message returned by client did not match input")

// This is an odd one... if there wasn't any output, it'll hit
// EOF, so we expect an error here so we can test an error
// condition.
Expand Down
9 changes: 9 additions & 0 deletions parser.go
Expand Up @@ -315,6 +315,15 @@ func ParseMessage(line string) (*Message, error) {
return c, nil
}

// Param returns the i'th argument in the Message or an empty string
// if the requested arg does not exist
func (m *Message) Param(i int) string {
if i < 0 || i >= len(m.Params) {
return ""
}
return m.Params[i]
}

// Trailing returns the last argument in the Message or an empty string
// if there are no args.
func (m *Message) Trailing() string {
Expand Down
9 changes: 9 additions & 0 deletions parser_test.go
Expand Up @@ -68,6 +68,15 @@ func TestMustParseMessage(t *testing.T) {
}, "Got unexpected panic")
}

func TestMessageParam(t *testing.T) {
t.Parallel()

m := MustParseMessage("PING :test")
assert.Equal(t, m.Param(0), "test")
assert.Equal(t, m.Param(-1), "")
assert.Equal(t, m.Param(2), "")
}

func TestMessageTrailing(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 5a62fea

Please sign in to comment.