Refactor pkg/stremformatter with custom constructors instead of passing a boolean

Signed-off-by: Antonio Murdaca <me@runcom.ninja>
This commit is contained in:
Antonio Murdaca 2015-05-12 20:18:54 +02:00
parent 88a6b39bd3
commit cc22fd7990
2 changed files with 41 additions and 16 deletions

View file

@ -12,8 +12,14 @@ type StreamFormatter struct {
json bool json bool
} }
func NewStreamFormatter(json bool) *StreamFormatter { // NewStreamFormatter returns a simple StreamFormatter
return &StreamFormatter{json} func NewStreamFormatter() *StreamFormatter {
return &StreamFormatter{}
}
// NewJSONStreamFormatter returns a StreamFormatter configured to stream json
func NewJSONStreamFormatter() *StreamFormatter {
return &StreamFormatter{true}
} }
const streamNewline = "\r\n" const streamNewline = "\r\n"
@ -62,7 +68,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
progress = &jsonmessage.JSONProgress{} progress = &jsonmessage.JSONProgress{}
} }
if sf.json { if sf.json {
b, err := json.Marshal(&jsonmessage.JSONMessage{ b, err := json.Marshal(&jsonmessage.JSONMessage{
Status: action, Status: action,
ProgressMessage: progress.String(), ProgressMessage: progress.String(),
@ -81,10 +86,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
return []byte(action + " " + progress.String() + endl) return []byte(action + " " + progress.String() + endl)
} }
func (sf *StreamFormatter) Json() bool {
return sf.json
}
type StdoutFormater struct { type StdoutFormater struct {
io.Writer io.Writer
*StreamFormatter *StreamFormatter

View file

@ -10,31 +10,55 @@ import (
) )
func TestFormatStream(t *testing.T) { func TestFormatStream(t *testing.T) {
sf := NewStreamFormatter(true) sf := NewStreamFormatter()
res := sf.FormatStream("stream")
if string(res) != "stream"+"\r" {
t.Fatalf("%q", res)
}
}
func TestFormatJSONStatus(t *testing.T) {
sf := NewStreamFormatter()
res := sf.FormatStatus("ID", "%s%d", "a", 1)
if string(res) != "a1\r\n" {
t.Fatalf("%q", res)
}
}
func TestFormatSimpleError(t *testing.T) {
sf := NewStreamFormatter()
res := sf.FormatError(errors.New("Error for formatter"))
if string(res) != "Error: Error for formatter\r\n" {
t.Fatalf("%q", res)
}
}
func TestJSONFormatStream(t *testing.T) {
sf := NewJSONStreamFormatter()
res := sf.FormatStream("stream") res := sf.FormatStream("stream")
if string(res) != `{"stream":"stream"}`+"\r\n" { if string(res) != `{"stream":"stream"}`+"\r\n" {
t.Fatalf("%q", res) t.Fatalf("%q", res)
} }
} }
func TestFormatStatus(t *testing.T) { func TestJSONFormatStatus(t *testing.T) {
sf := NewStreamFormatter(true) sf := NewJSONStreamFormatter()
res := sf.FormatStatus("ID", "%s%d", "a", 1) res := sf.FormatStatus("ID", "%s%d", "a", 1)
if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" { if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" {
t.Fatalf("%q", res) t.Fatalf("%q", res)
} }
} }
func TestFormatSimpleError(t *testing.T) { func TestJSONFormatSimpleError(t *testing.T) {
sf := NewStreamFormatter(true) sf := NewJSONStreamFormatter()
res := sf.FormatError(errors.New("Error for formatter")) res := sf.FormatError(errors.New("Error for formatter"))
if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" { if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" {
t.Fatalf("%q", res) t.Fatalf("%q", res)
} }
} }
func TestFormatJSONError(t *testing.T) { func TestJSONFormatJSONError(t *testing.T) {
sf := NewStreamFormatter(true) sf := NewJSONStreamFormatter()
err := &jsonmessage.JSONError{Code: 50, Message: "Json error"} err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
res := sf.FormatError(err) res := sf.FormatError(err)
if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" { if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" {
@ -42,8 +66,8 @@ func TestFormatJSONError(t *testing.T) {
} }
} }
func TestFormatProgress(t *testing.T) { func TestJSONFormatProgress(t *testing.T) {
sf := NewStreamFormatter(true) sf := NewJSONStreamFormatter()
progress := &jsonmessage.JSONProgress{ progress := &jsonmessage.JSONProgress{
Current: 15, Current: 15,
Total: 30, Total: 30,