Skip to content

Commit

Permalink
return error on invalid Feed() input, add tests to pass coverage, go …
Browse files Browse the repository at this point in the history
…mod tidy
  • Loading branch information
mc12d committed Nov 12, 2022
1 parent 7da1eb7 commit 7441424
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.11

require (
github.com/BurntSushi/toml v0.4.1
github.com/golobby/cast v1.3.0 // indirect
github.com/golobby/cast v1.3.0
github.com/golobby/dotenv v1.3.1
github.com/golobby/env/v2 v2.2.0
github.com/stretchr/testify v1.7.0
Expand Down
5 changes: 3 additions & 2 deletions pkg/feeder/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package feeder

import (
"fmt"
"github.com/golobby/cast"
"reflect"
"unsafe"

"github.com/golobby/cast"
)

// Default is a feeder.
Expand All @@ -23,7 +24,7 @@ func (f Default) Feed(structure interface{}) error {
}
}

return nil
return fmt.Errorf("default: input is not a struct pointer: %v --> %T", structure, structure)
}

// fillStruct sets a reflected struct fields with the default value.
Expand Down
29 changes: 28 additions & 1 deletion pkg/feeder/default_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package feeder_test

import (
"testing"

"github.com/golobby/config/v3/pkg/feeder"
"github.com/stretchr/testify/assert"
"testing"
)

func TestDefault_Feed(t *testing.T) {
Expand Down Expand Up @@ -37,3 +38,29 @@ func TestDefault_Feed_With_Invalid_Struct_It_Should_Fail(t *testing.T) {
err := f.Feed(&c)
assert.Error(t, err)
}

func TestDefault_Feed_With_Invalid_Inner_Struct_It_Should_Fail(t *testing.T) {
type HttpConf struct {
RequestTimeoutMs float64 `default:"string"`
}
type App struct {
Name string `env:"APP_NAME" default:"string"`
Http *HttpConf
}
c := App{
Http: &HttpConf{},
}

f := feeder.Default{}
err := f.Feed(&c)
assert.Error(t, err)

}

func TestDefault_Feed_With_Non_Struct_Type_Should_Fail(t *testing.T) {
c := 42
f := feeder.Default{}

err := f.Feed(c)
assert.Error(t, err)
}

0 comments on commit 7441424

Please sign in to comment.