Skip to content

Commit ea55af8

Browse files
committed
Simplified Formatter interface and package implementation
1 parent 56f5e6b commit ea55af8

File tree

5 files changed

+45
-115
lines changed

5 files changed

+45
-115
lines changed

common/formatter/examples_test.go

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ func ExampleJSONFormatter_Print() {
5959
example.Field3.Inner2 = 10.432412
6060

6161
var jf formatter.JSONFormatter
62-
jf.Print(example)
62+
fmt.Println(jf.Format(example))
6363

6464
var example2 float64 = 3.14
65-
jf.Print(example2)
65+
fmt.Println(jf.Format(example2))
6666

6767
// Output:
68-
// {"field1":"test","field2":10,"field3":{"inner1":"inner test","inner2":10.432412}}
69-
//
68+
// {"field1":"test","field2":10,"field3":{"inner1":"inner test","inner2":10.432412}} <nil>
69+
// float64 ignored
7070
}
7171

7272
func ExampleJSONFormatter_Format() {
@@ -85,25 +85,14 @@ func ExampleJSONFormatter_Format() {
8585
example.Field3.Inner2 = 10.432412
8686

8787
var jf formatter.JSONFormatter
88-
var result string
89-
result, err := jf.Format(example)
90-
if err != nil {
91-
fmt.Println("ERROR:", err)
92-
} else {
93-
fmt.Println("RESULT:", result)
94-
}
88+
fmt.Println(jf.Format(example))
9589

9690
var example2 float32 = 3.14
97-
result, err = jf.Format(example2)
98-
if err != nil {
99-
fmt.Println("ERROR:", err)
100-
} else if result == "" {
101-
fmt.Println("RESULT: <empty string>")
102-
}
91+
fmt.Println(jf.Format(example2))
10392

10493
// Output:
105-
// RESULT: {"field1":"test","field2":10,"field3":{"inner1":"inner test","inner2":10.432412}}
106-
// RESULT: <empty string>
94+
// {"field1":"test","field2":10,"field3":{"inner1":"inner test","inner2":10.432412}} <nil>
95+
// float32 ignored
10796
}
10897

10998
func ExampleJSONFormatter_Print_debug() {
@@ -112,58 +101,39 @@ func ExampleJSONFormatter_Print_debug() {
112101
jf := formatter.JSONFormatter{
113102
Debug: false,
114103
}
115-
//using struct
116-
jf.Print(valid)
104+
// using struct
105+
fmt.Println(jf.Format(valid))
117106

118-
//using string (invalid sine it's not a struct or a map)
119-
jf.Print(invalid)
107+
// using string (invalid sine it's not a struct or a map)
108+
fmt.Println(jf.Format(invalid))
120109

121110
jf.Debug = true
122-
jf.Print(valid)
123-
jf.Print(invalid)
111+
fmt.Println(jf.Format(valid))
112+
fmt.Println(jf.Format(invalid))
124113

125-
//using map
114+
// using map
126115
newValue := make(map[string]int)
127116
newValue["value2"] = 10
128117

129-
jf.Print(newValue)
118+
fmt.Println(jf.Format(newValue))
119+
130120
// Output:
131-
// {"value":20}
132-
// {"value":20}
133-
// Only structs and maps values are accepted
134-
// {"value2":10}
121+
// {"value":20} <nil>
122+
// string ignored
123+
// {"value":20} <nil>
124+
// string ignored
125+
// {"value2":10} <nil>
135126
}
136127

137128
func ExampleFormat() {
138129
formatter.SetFormatter("text")
139-
result, err := formatter.Format(TestStruct{5})
140-
if err == nil {
141-
fmt.Println("RESULT: ", result)
142-
} else {
143-
fmt.Println("ERROR: ", err)
144-
}
145-
130+
fmt.Println(formatter.Format(TestStruct{5}))
146131
formatter.SetFormatter("json")
147-
result, err = formatter.Format(TestStruct{10})
148-
if err == nil {
149-
fmt.Println("RESULT: ", result)
150-
} else {
151-
fmt.Println("ERROR: ", err)
152-
}
153-
result, err = formatter.Format(5)
154-
if err == nil {
155-
if result == "" {
156-
result = "<empty string>"
157-
fmt.Println("RESULT: ", result)
158-
} else {
159-
fmt.Println("unexpected RESULT: ", result)
160-
}
161-
} else {
162-
fmt.Sprintln("ERROR: ", err)
163-
}
132+
fmt.Println(formatter.Format(TestStruct{10}))
133+
fmt.Println(formatter.Format(5))
164134

165135
// Output:
166-
// RESULT: VALUE = 5
167-
// RESULT: {"value":10}
168-
// RESULT: <empty string>
136+
// VALUE = 5 <nil>
137+
// {"value":10} <nil>
138+
// int ignored
169139
}

common/formatter/formatter.go

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ package formatter
3232
import (
3333
"errors"
3434
"fmt"
35-
"strings"
3635

3736
"github.com/sirupsen/logrus"
3837
)
3938

4039
// Formatter interface represents a generic formatter. It allows to print and format Messages.
4140
type Formatter interface {
4241
Format(interface{}) (string, error) // Format formats a parameter if possible, otherwise it returns an error.
43-
Print(interface{}) error // Print just prints specified parameter, returns error if it is not parsable.
4442
}
4543

4644
// PrintFunc represents a function used to print formatted data.
@@ -49,8 +47,6 @@ type PrintFunc func(Formatter, interface{}) error
4947
var formatters map[string]Formatter
5048
var defaultFormatter Formatter
5149

52-
var printFunc PrintFunc
53-
5450
var logger *logrus.Logger
5551

5652
var debug bool
@@ -60,8 +56,6 @@ func init() {
6056
AddCustomFormatter("text", &TextFormatter{})
6157
AddCustomFormatter("json", &JSONFormatter{})
6258
defaultFormatter = formatters["text"]
63-
64-
printFunc = defaultPrintFunc
6559
}
6660

6761
// SetFormatter sets the defaults format to the one specified, if valid. Otherwise it returns an error.
@@ -111,28 +105,10 @@ func Format(msg interface{}) (string, error) {
111105

112106
// Print prints a message formatted using a Formatter specified by SetFormatter(...) function.
113107
func Print(msg interface{}) error {
114-
if defaultFormatter == nil {
115-
return errors.New("No formatter set")
116-
}
117-
return defaultFormatter.Print(msg)
118-
}
119-
120-
// defaultPrintFunc is the base function of all Print methods of Formatters.
121-
//
122-
// It can be used for an unified implementation.
123-
func defaultPrintFunc(f Formatter, msg interface{}) error {
124-
val, err := f.Format(msg)
125-
if val != "" {
126-
if err == nil {
127-
fmt.Println(strings.TrimSpace(val))
128-
} else {
129-
fmt.Println(strings.TrimSpace(err.Error()))
130-
}
108+
output, err := defaultFormatter.Format(msg)
109+
if err != nil {
110+
return err
131111
}
132-
return err
133-
}
134-
135-
// SetPrintFunc changes the actual print function with a specified one.
136-
func SetPrintFunc(p PrintFunc) {
137-
printFunc = p
112+
fmt.Println(output)
113+
return nil
138114
}

common/formatter/json.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,23 @@ package formatter
3131

3232
import "encoding/json"
3333
import "reflect"
34-
import "errors"
34+
3535
import "fmt"
3636

37-
//JSONFormatter represents a Printer and Formatter of JSON objects.
37+
// JSONFormatter is a Formatter that output JSON objects.
38+
// Intermediate results or interactive messages are ignored.
3839
type JSONFormatter struct {
39-
Debug bool //if false, errors are not shown. Unparsable inputs are skipped. Otherwise an error message is shown.
40+
Debug bool // if false, errors are not shown. Unparsable inputs are skipped. Otherwise an error message is shown.
4041
}
4142

42-
// Format formaats a message into a JSON object.
43-
//
44-
// It ignores Header and Footer fields of the message.
43+
// Format implements Formatter interface
4544
func (jf JSONFormatter) Format(msg interface{}) (string, error) {
46-
msgType := reflect.TypeOf(msg).Kind().String()
47-
if msgType == "struct" ||
48-
msgType == "map" {
45+
t := reflect.TypeOf(msg).Kind().String()
46+
switch t {
47+
case "struct", "map":
4948
ret, err := json.Marshal(msg)
5049
return string(ret), err
51-
} else if jf.Debug {
52-
return fmt.Sprint(msg), errors.New("Only structs and maps values are accepted")
50+
default:
51+
return "", fmt.Errorf("%s ignored", t)
5352
}
54-
return "", nil
55-
}
56-
57-
// Print prints a JSON object.
58-
func (jf JSONFormatter) Print(msg interface{}) error {
59-
return printFunc(jf, msg)
6053
}

common/formatter/print_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ func ExamplePrint() {
6262

6363
formatter.SetFormatter("json")
6464
formatter.Print(example)
65-
fmt.Println() //to separe outputs
6665
formatter.SetFormatter("text")
6766
formatter.Print(example)
6867
// Output:
6968
// {"field1":"test","field2":10,"field3":{"inner1":"inner test","inner2":10.432412}}
70-
//
7169
// Field1: test
7270
// Field2: 10
7371
// Field3.Inner1: inner test

common/formatter/text.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,10 @@ package formatter
3131

3232
import "fmt"
3333

34-
//TextFormatter represents a Printer and Formatter of Text.
34+
// TextFormatter represents a Formatter for a text console
3535
type TextFormatter struct{}
3636

37-
// Format formaats a message into a JSON object.
38-
//
39-
// It ignores Header and Footer fields of the message.
37+
// Format implements Formatter interface
4038
func (tp TextFormatter) Format(msg interface{}) (string, error) {
4139
return fmt.Sprintf("%s", msg), nil
4240
}
43-
44-
// Print prints a JSON object.
45-
func (tp TextFormatter) Print(msg interface{}) error {
46-
return printFunc(tp, msg)
47-
}

0 commit comments

Comments
 (0)