Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit b92b966

Browse files
author
Mike Trinkala
committed
Merge pull request #847 from mozilla-services/fileoutput-null-payload
Fileoutput null payload
2 parents 9219ed0 + 124fe87 commit b92b966

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
Bug Handling
55
------------
66

7+
* FileOutput no longer panics when using `format = "text"` and payload is nil
8+
(issue #843).
9+
710
* Fix SandboxDecoder pass-through case so decoders that only use write_message
811
and not inject_message will emit messages correctly (issue #844).
912

plugins/file/file_output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (o *FileOutput) handleMessage(pack *PipelinePack, outBytes *[]byte) (err er
267267
err = fmt.Errorf("Can't encode to JSON: %s", err)
268268
}
269269
case "text":
270-
*outBytes = append(*outBytes, *pack.Message.Payload...)
270+
*outBytes = append(*outBytes, pack.Message.GetPayload()...)
271271
*outBytes = append(*outBytes, NEWLINE)
272272
case "protobufstream":
273273
if err = ProtobufEncodeMessage(pack, &*outBytes); err != nil {

plugins/file/file_output_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"os"
3030
"path/filepath"
3131
"runtime"
32+
"strings"
3233
"sync"
3334
"time"
3435
)
@@ -68,13 +69,15 @@ func FileOutputSpec(c gs.Context) {
6869
outData := make([]byte, 0, 20)
6970

7071
c.Specify("by default", func() {
71-
fileOutput.handleMessage(pack, &outData)
72+
err = fileOutput.handleMessage(pack, &outData)
73+
c.Expect(err, gs.IsNil)
7274
c.Expect(toString(&outData), gs.Equals, *msg.Payload+"\n")
7375
})
7476

7577
c.Specify("w/ a prepended timestamp when specified", func() {
7678
fileOutput.prefix_ts = true
77-
fileOutput.handleMessage(pack, &outData)
79+
err = fileOutput.handleMessage(pack, &outData)
80+
c.Expect(err, gs.IsNil)
7881
// Test will fail if date flips btn handleMessage call and
7982
// todayStr calculation... should be extremely rare.
8083
todayStr := time.Now().Format("[2006/Jan/02:")
@@ -83,6 +86,27 @@ func FileOutputSpec(c gs.Context) {
8386
c.Expect(strContents, pipeline_ts.StringContains, payload)
8487
c.Expect(strContents, pipeline_ts.StringStartsWith, todayStr)
8588
})
89+
90+
c.Specify("even when payload is nil", func() {
91+
pack.Message.Payload = nil
92+
err = fileOutput.handleMessage(pack, &outData)
93+
c.Expect(err, gs.IsNil)
94+
strContents := toString(&outData)
95+
c.Expect(strContents, gs.Equals, "\n")
96+
})
97+
98+
c.Specify("payload is nil and with a timestamp", func() {
99+
pack.Message.Payload = nil
100+
fileOutput.prefix_ts = true
101+
err = fileOutput.handleMessage(pack, &outData)
102+
c.Expect(err, gs.IsNil)
103+
// Test will fail if date flips btn handleMessage call and
104+
// todayStr calculation... should be extremely rare.
105+
todayStr := time.Now().Format("[2006/Jan/02:")
106+
strContents := toString(&outData)
107+
c.Expect(strings.HasPrefix(strContents, todayStr), gs.IsTrue)
108+
c.Expect(strings.HasSuffix(strContents, " \n"), gs.IsTrue)
109+
})
86110
})
87111

88112
c.Specify("correctly formats JSON output", func() {

0 commit comments

Comments
 (0)