Skip to content

Commit

Permalink
feat: support multiline message
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 22, 2021
1 parent 28d7d08 commit 3bdf6e5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
34 changes: 31 additions & 3 deletions store/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ type Record struct {
Latency time.Duration
}

func UnescapeMessage(s string) string {
for _, x := range []struct {
From string
To string
}{
{`\t`, "\t"},
{`\n`, "\n"},
{`\\`, `\`},
} {
s = strings.ReplaceAll(s, x.From, x.To)
}
return s
}

func ParseRecord(s string) (Record, error) {
var r Record
var timestamp string
Expand All @@ -34,7 +48,7 @@ func ParseRecord(s string) (Record, error) {
return Record{}, err
}
target = ss[3]
r.Message = ss[4]
r.Message = UnescapeMessage(ss[4])

r.CheckedAt, err = time.Parse(time.RFC3339, timestamp)
if err != nil {
Expand All @@ -61,18 +75,32 @@ func (r Record) Sanitize() Record {
CheckedAt: r.CheckedAt,
Target: r.Target,
Status: r.Status,
Message: strings.ReplaceAll(strings.ReplaceAll(r.Message, "\t", " "), "\n", " "),
Message: strings.Trim(r.Message, "\r\n"),
Latency: r.Latency,
}
}

func EscapeMessage(s string) string {
for _, x := range []struct {
From string
To string
}{
{`\`, `\\`},
{"\t", `\t`},
{"\n", `\n`},
} {
s = strings.ReplaceAll(s, x.From, x.To)
}
return s
}

func (r Record) String() string {
return strings.Join([]string{
r.CheckedAt.Format(time.RFC3339),
r.Status.String(),
fmt.Sprintf("%.3f", float64(r.Latency.Microseconds())/1000),
r.Target.String(),
r.Message,
EscapeMessage(r.Message),
}, "\t")
}

Expand Down
46 changes: 46 additions & 0 deletions store/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,49 @@ func TestRecord(t *testing.T) {
}
}
}

func TestUnescapeMessage(t *testing.T) {
tests := []struct {
Input string
Output string
}{
{`hello world`, `hello world`},
{`"hello"world`, `"hello"world`},
{`hello\tworld`, "hello\tworld"},
{`hello\nworld`, "hello\nworld"},
{`hello\r\nworld`, "hello\\r\nworld"},
{`\\hello\\world\\\\\n`, "\\hello\\world\\\\\n"},
{`\n`, "\n"},
{``, ""},
}

for _, tt := range tests {
got := store.UnescapeMessage(tt.Input)
if got != tt.Output {
t.Errorf("%#v: unexpected result\nexpected: %#v\n but got: %#v", tt.Input, tt.Output, got)
}
}
}

func TestEscapeMessage(t *testing.T) {
tests := []struct {
Input string
Output string
}{
{`hello world`, `hello world`},
{`"hello"world`, `"hello"world`},
{"hello\tworld", `hello\tworld`},
{"\thello\tworld\t", `\thello\tworld\t`},
{"\n\nhello\nworld\n", `\n\nhello\nworld\n`},
{`\n\t\\`, `\\n\\t\\\\`},
{"\n", `\n`},
{"", ``},
}

for _, tt := range tests {
got := store.EscapeMessage(tt.Input)
if got != tt.Output {
t.Errorf("%#v: unexpected result\nexpected: %#v\n but got: %#v", tt.Input, tt.Output, got)
}
}
}

0 comments on commit 3bdf6e5

Please sign in to comment.