Skip to content

Commit

Permalink
Merge pull request #17 from drewstinnett/chore-interface-to-any
Browse files Browse the repository at this point in the history
Removing interface and adding any
  • Loading branch information
drewstinnett authored Feb 24, 2024
2 parents b0f02fc + 414ca59 commit 3c9868a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion formats/gotemplate/gotemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
15 changes: 8 additions & 7 deletions formats/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
36 changes: 24 additions & 12 deletions formats/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion formats/plain/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion formats/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion formats/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion formats/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
28 changes: 14 additions & 14 deletions gout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3c9868a

Please sign in to comment.