-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement CSV encoding of log messages.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package csv | ||
|
||
import ( | ||
"encoding/csv" | ||
"errors" | ||
"io" | ||
) | ||
|
||
// Produces a list of fields making up a record. | ||
type Recorder interface { | ||
Record() []string | ||
} | ||
|
||
// An Encoder writes CSV records to an output stream. | ||
type Encoder struct { | ||
w *csv.Writer | ||
} | ||
|
||
// NewEncoder returns a new encoder that writes to w. | ||
func NewEncoder(w io.Writer) *Encoder { | ||
return &Encoder{w: csv.NewWriter(w)} | ||
} | ||
|
||
// Encode writes a CSV record representing v to the stream followed by a | ||
// newline character. Value given must implement the Recorder interface. | ||
func (enc *Encoder) Encode(v interface{}) (err error) { | ||
record, ok := v.(Recorder) | ||
if !ok { | ||
return errors.New("value does not satisfy Recorder interface") | ||
} | ||
|
||
err = enc.w.Write(record.Record()) | ||
enc.w.Flush() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters