Skip to content

Commit

Permalink
support simple multi line #5
Browse files Browse the repository at this point in the history
  • Loading branch information
wenerme committed May 6, 2021
1 parent cd47650 commit 6a51767
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ami/conn_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Conn) loop(ctx context.Context) (err error) {

// send pending message
msg := async.msg
log.Sugar().With("id", async.id, "type", msg.Type, "name", msg.Name).Debug("send message")
//log.Sugar().With("id", async.id, "type", msg.Type, "name", msg.Name).Debug("send message")

err = msg.Write(c.conn)

Expand Down
2 changes: 1 addition & 1 deletion ami/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (c *Conn) connect(conn net.Conn) (err error) {
}
log.Info("login success")
}
log.Sugar().Debug("do conn check ping")
//log.Sugar().Debug("do conn check ping")
// be ready
_, err = c.Request(amimodels.PingAction{})
return
Expand Down
37 changes: 32 additions & 5 deletions ami/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,49 @@ func (m *Message) Read(r *bufio.Reader) (err error) {
return errors.Errorf("invalid message type: %q", sp[0])
}

var stack [][]string
for {
// may contain BOM
line, err = r.ReadString('\n')
if err != nil {
return err
}
line = strings.TrimSuffix(line, "\r\n")
if len(line) == 0 {
if line == "\r\n" {
break
}
sp = strings.SplitN(line, ":", 2)
if len(sp) != 2 {
return errors.Errorf("invalid attr line read(%v: %v): %q", m.Type, m.Name, line)

switch {
case len(sp) == 2 && strings.HasSuffix(line, "\r\n"):
// valid line
stack = append(stack, sp)
case len(stack) == 0 && len(sp) == 2:
// first line
stack = append(stack, sp)
case len(stack) != 0:
// continue line
stack = append(stack, []string{"", line})
}
m.Attributes[sp[0]] = strings.TrimSpace(sp[1])
}

var k, v string
for _, pair := range stack {
switch {
case pair[0] != "" && k != "":
m.Attributes[k] = strings.TrimSuffix(v, "\r\n")
k = ""
v = ""
fallthrough
case pair[0] != "":
k = pair[0]
v = strings.TrimLeft(pair[1], " ")
case pair[0] == "":
v += pair[1]
}
}
if k != "" {
m.Attributes[k] = strings.TrimSuffix(v, "\r\n")
}
return
}
func (m *Message) Write(w io.Writer) (err error) {
Expand Down
49 changes: 42 additions & 7 deletions ami/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/stretchr/testify/assert"
amimodels "github.com/wenerme/astgo/ami/models"
"io"
"sort"
"strings"
"testing"
)

Expand All @@ -14,21 +16,54 @@ func TestMsgIO(t *testing.T) {
msg *Message
out string
}{
{msg: &Message{
Type: MessageTypeEvent,
Name: "FullyBooted",
Attributes: map[string]interface{}{
"Uptime": "1234",
{
msg: &Message{
Type: MessageTypeEvent,
Name: "Name",
Attributes: map[string]interface{}{
"Text": "12\r\n34",
"More": "Yes",
},
},
// more after multi line
out: "Event: Name\r\nText: 12\r\n34\r\nMore: Yes\r\n\r\n",
},
{
msg: &Message{
Type: MessageTypeEvent,
Name: "Name",
Attributes: map[string]interface{}{
"Text": "12\r\n34",
},
},
// simple multi line
out: "Event: Name\r\nText: 12\r\n34\r\n\r\n",
},

{
msg: &Message{
Type: MessageTypeEvent,
Name: "FullyBooted",
Attributes: map[string]interface{}{
"Uptime": "1234",
},
},
out: "Event: FullyBooted\r\nUptime: 1234\r\n\r\n",
},
out: "Event: FullyBooted\r\nUptime: 1234\r\n\r\n"},
{msg: MustConvertToMessage(amimodels.FullyBootedEvent{
Uptime: "1234",
}),
out: "Event: FullyBooted\r\nUptime: 1234\r\n\r\n"},
} {
msg := test.msg
assert.Equal(t, test.out, msg.Format())

// ignore order
exp := strings.Split(test.out, "\r\n")
act := strings.Split(msg.Format(), "\r\n")
sort.Strings(exp)
sort.Strings(act)
assert.Equal(t, exp, act)

r := bufio.NewReader(bytes.NewReader([]byte(test.out)))
rm := &Message{}
assert.NoError(t, rm.Read(r))
Expand Down

0 comments on commit 6a51767

Please sign in to comment.