From 414ca59732c6b499f5651130d5f5f3d5ed661a8a Mon Sep 17 00:00:00 2001 From: Drew Stinnett Date: Sat, 24 Feb 2024 17:06:37 -0500 Subject: [PATCH] Removing interface and adding any --- formats/gotemplate/gotemplate.go | 2 +- formats/json/json.go | 15 ++++++------- formats/json/json_test.go | 36 +++++++++++++++++++++----------- formats/plain/plain.go | 2 +- formats/registry.go | 2 +- formats/xml/xml.go | 2 +- formats/yaml/yaml.go | 2 +- gout.go | 28 ++++++++++++------------- 8 files changed, 51 insertions(+), 38 deletions(-) diff --git a/formats/gotemplate/gotemplate.go b/formats/gotemplate/gotemplate.go index 3024178..0babda0 100644 --- a/formats/gotemplate/gotemplate.go +++ b/formats/gotemplate/gotemplate.go @@ -18,7 +18,7 @@ type Formatter struct { } // Format satisfies the formats.Format interface -func (w Formatter) Format(v interface{}) ([]byte, error) { +func (w Formatter) Format(v any) ([]byte, error) { if w.Template == "" { return nil, errors.New("no Template set for gotemplate") } diff --git a/formats/json/json.go b/formats/json/json.go index dae838d..8d61766 100644 --- a/formats/json/json.go +++ b/formats/json/json.go @@ -10,15 +10,16 @@ import ( ) // Formatter holds the base json stuff -type Formatter struct{} +type Formatter struct { + indent bool +} -// Format satiesfiles the formats.Formatter interface -func (w Formatter) Format(v interface{}) ([]byte, error) { - var i any - if i == nil { - return ujson.Marshal(v) +// Format satisfies the formats.Formatter interface +func (w Formatter) Format(v any) ([]byte, error) { + if w.indent { + return ujson.MarshalIndent(v, "", " ") } - return ujson.MarshalIndent(v, "", " ") + return ujson.Marshal(v) } func init() { diff --git a/formats/json/json_test.go b/formats/json/json_test.go index b77ab74..7f25dc9 100644 --- a/formats/json/json_test.go +++ b/formats/json/json_test.go @@ -2,21 +2,33 @@ package json import ( "testing" + + "github.com/drewstinnett/gout/v2/formats" ) func TestJSONFormatter(t *testing.T) { - f := Formatter{} - got, err := f.Format(struct { - Foo string + for desc, tt := range map[string]struct { + given formats.Formatter + expect string }{ - Foo: "bar", - }) - if err != nil { - t.Fatalf("got an unexpected error: %v", err) - } - expect := `{"Foo":"bar"}` - gotS := string(got) - if expect != gotS { - t.Fatalf("expected: %v, but got: %v", expect, got) + "default": { + given: Formatter{}, + expect: `{"Foo":"bar"}`, + }, + "indented": { + given: Formatter{ + indent: true, + }, + expect: "{\n \"Foo\": \"bar\"\n}", + }, + } { + got, err := tt.given.Format(struct{ Foo string }{Foo: "bar"}) + if err != nil { + t.Fatalf("%v: got an unexpected error: %v", desc, err) + } + gotS := string(got) + if tt.expect != gotS { + t.Fatalf("%v: expected: %v, but got: %v", desc, tt.expect, gotS) + } } } diff --git a/formats/plain/plain.go b/formats/plain/plain.go index 54f31d0..57493f7 100644 --- a/formats/plain/plain.go +++ b/formats/plain/plain.go @@ -13,7 +13,7 @@ import ( type Formatter struct{} // Format satisfies the formats.Formatter interface -func (w Formatter) Format(v interface{}) ([]byte, error) { +func (w Formatter) Format(v any) ([]byte, error) { return []byte(fmt.Sprintf("%+v\n", v)), nil } diff --git a/formats/registry.go b/formats/registry.go index d51c902..003c9aa 100644 --- a/formats/registry.go +++ b/formats/registry.go @@ -30,5 +30,5 @@ func Names() []string { // Formatter interface that defines how a thing can be formatted for output type Formatter interface { - Format(interface{}) ([]byte, error) + Format(any) ([]byte, error) } diff --git a/formats/xml/xml.go b/formats/xml/xml.go index 6ed7f72..0705762 100644 --- a/formats/xml/xml.go +++ b/formats/xml/xml.go @@ -13,7 +13,7 @@ import ( type Formatter struct{} // Format satisfies the formats.Formatter interface -func (w Formatter) Format(v interface{}) ([]byte, error) { +func (w Formatter) Format(v any) ([]byte, error) { return uxml.Marshal(v) } diff --git a/formats/yaml/yaml.go b/formats/yaml/yaml.go index 28ea159..29067fc 100644 --- a/formats/yaml/yaml.go +++ b/formats/yaml/yaml.go @@ -12,7 +12,7 @@ import ( type Formatter struct{} // Format satisfies the formats.Formatter interface -func (w Formatter) Format(v interface{}) ([]byte, error) { +func (w Formatter) Format(v any) ([]byte, error) { return uyaml.Marshal(v) } diff --git a/gout.go b/gout.go index 5c40a4a..c30e8ee 100644 --- a/gout.go +++ b/gout.go @@ -73,11 +73,11 @@ func (g *Gout) SetFormatterString(s string) error { return fmt.Errorf("unknown formatter name: %v", s) } -// Print print an interface using the given Formatter and io.Writer -func Print(v interface{}) (err error) { return gi.Print(v) } +// Print print an item using the built in Gout instance +func Print(v any) (err error) { return gi.Print(v) } -// Print prints the output on a custom Gout instance -func (g *Gout) Print(v interface{}) (err error) { +// Print print an item from a custom Gout instance +func (g *Gout) Print(v any) (err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("panic while attempting to format: %v", r) @@ -92,12 +92,12 @@ func (g *Gout) Print(v interface{}) (err error) { return err } -// PrintMulti useful when wanting to print multiple interfaces to a single +// PrintMulti useful when wanting to print multiple items to a single // serialized item -func PrintMulti(v ...interface{}) (err error) { return gi.PrintMulti(v) } +func PrintMulti(v ...any) (err error) { return gi.PrintMulti(v) } // PrintMulti prints multiple items on a custom gout instance -func (g *Gout) PrintMulti(v ...interface{}) (err error) { +func (g *Gout) PrintMulti(v ...any) (err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("panic while attempting to format: %v", r) @@ -113,23 +113,23 @@ func (g *Gout) PrintMulti(v ...interface{}) (err error) { return err } -// MustPrint print an interface and panic if there is any sort of error -func MustPrint(v interface{}) { gi.MustPrint(v) } +// MustPrint print an item and panic if there is any sort of error +func MustPrint(v any) { gi.MustPrint(v) } // MustPrint outputs data on a custom Gout instance -func (g *Gout) MustPrint(v interface{}) { +func (g *Gout) MustPrint(v any) { err := g.Print(v) if err != nil { panic(err) } } -// MustPrintMulti print an multiple interfaces and panic if there is any sort of +// MustPrintMulti print an multiple items and panic if there is any sort of // error -func MustPrintMulti(v ...interface{}) { gi.MustPrintMulti(v) } +func MustPrintMulti(v ...any) { gi.MustPrintMulti(v) } // MustPrintMulti prints multiple items with a custom Gout instance -func (g *Gout) MustPrintMulti(v ...interface{}) { +func (g *Gout) MustPrintMulti(v ...any) { err := g.PrintMulti(v) if err != nil { panic(err) @@ -168,7 +168,7 @@ func New(opts ...Option) *Gout { return g } -func (g *Gout) itemizedFormatter(v ...interface{}) ([]byte, error) { +func (g *Gout) itemizedFormatter(v ...any) ([]byte, error) { var buf bytes.Buffer for _, item := range v { bi, err := g.formatter.Format(item)