Skip to content

Commit 05cb9cf

Browse files
committed
Added convenient Sprint method
1 parent d9be801 commit 05cb9cf

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

colprint.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"math"
1111
"sort"
1212
"github.com/ryanuber/columnize"
13+
"bytes"
1314
)
1415

1516
const TagName = "colprint"
@@ -27,6 +28,12 @@ type Config struct {
2728
// FloatPrecision represents the precision used when printing floats.
2829
FloatPrecision *int
2930
}
31+
// Sprint is a convenience method for creating a string from a struct or slice of structs using default config
32+
func Sprint(s interface{}) (string, error) {
33+
buf := new(bytes.Buffer)
34+
err := Fprint(buf, s)
35+
return buf.String(), err
36+
}
3037

3138
// Print prints a struct or slice of structs to stdout using default config
3239
func Print(s interface{}) error {

colprint_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (s *UnitTests) TestFPrint_PointerArg() {
183183
})
184184
}
185185

186-
func (s *UnitTests) TestPrint_WithComposition() {
186+
func (s *UnitTests) TestSprint_WithComposition() {
187187
type A struct {
188188
Name string `colprint:"Name,1"`
189189
}
@@ -197,8 +197,9 @@ func (s *UnitTests) TestPrint_WithComposition() {
197197
*B `colprint:"=>"`
198198
Description string `colprint:"Desc,1"`
199199
}
200-
201-
s.NoError(Print(C{B: &B{Date: "29.03.2017", A: A{Name: "Kari Nordmann"}}, Description:"desc"}))
200+
val, err := Sprint(C{B: &B{Date: "29.03.2017", A: A{Name: "Kari Nordmann"}}, Description:"desc"})
201+
s.NoError(err)
202+
s.NotEmpty(val)
202203
}
203204

204205
func (s *UnitTests) TestPrint_CompositionWithErrors() {

0 commit comments

Comments
 (0)