Skip to content

Commit

Permalink
Add tests for NewInput function
Browse files Browse the repository at this point in the history
  • Loading branch information
Milad Abbasi committed Jan 12, 2021
1 parent ac7a474 commit cdd0f1a
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 17 deletions.
12 changes: 8 additions & 4 deletions errors.go
Expand Up @@ -39,16 +39,20 @@ const (
// An InvalidInputError describes an invalid argument passed to Into function
// The argument must be a non-nil struct pointer
type InvalidInputError struct {
Type reflect.Type
Value reflect.Value
}

func (e *InvalidInputError) Error() string {
msg := "gonfig: invalid input: "
var t reflect.Type

if e.Type == nil {
msg += "nil"
} else if e.Type.Kind() != reflect.Ptr {
if e.Value.IsValid() {
t = e.Value.Type()
}

if t == nil {
msg += "<nil>"
} else if t.Kind() != reflect.Ptr {
msg += "non-pointer type"
} else if e.Value.IsNil() {
msg += "nil pointer"
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Expand Up @@ -4,6 +4,9 @@ go 1.15

require (
github.com/BurntSushi/toml v0.3.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/joho/godotenv v1.3.0
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
15 changes: 15 additions & 0 deletions go.sum
@@ -1,7 +1,22 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 10 additions & 10 deletions input.go
Expand Up @@ -39,7 +39,7 @@ type Field struct {
func NewInput(i interface{}) (*Input, error) {
v := reflect.ValueOf(i)

if err := checkInput(v); err != nil {
if err := validateInput(v); err != nil {
return nil, err
}

Expand All @@ -52,30 +52,30 @@ func NewInput(i interface{}) (*Input, error) {
Tags: &ConfigTags{},
}

if err := in.traverseFiled(&f); err != nil {
if err := in.traverseField(&f); err != nil {
return nil, err
}

return &in, nil
}

// checkInput checks for a non-nil struct pointer
func checkInput(v reflect.Value) error {
if v.Type() == nil ||
// validateInput checks for a non-nil struct pointer
func validateInput(v reflect.Value) error {
if !v.IsValid() ||
v.Type() == nil ||
v.Type().Kind() != reflect.Ptr ||
v.IsNil() ||
v.Type().Elem().Kind() != reflect.Struct {
return &InvalidInputError{
Type: v.Type(),
Value: v,
}
}

return nil
}

// traverseFiled recursively traverse all fields and collect their information
func (in *Input) traverseFiled(f *Field) error {
// traverseField recursively traverse all fields and collect their information
func (in *Input) traverseField(f *Field) error {
if !f.Value.CanSet() || f.Tags.Ignore {
return nil
}
Expand All @@ -95,7 +95,7 @@ func (in *Input) traverseFiled(f *Field) error {
Path: append(f.Path, f.Value.Type().Field(i).Name),
}

if err := in.traverseFiled(&nestedField); err != nil {
if err := in.traverseField(&nestedField); err != nil {
return err
}
}
Expand All @@ -110,7 +110,7 @@ func (in *Input) traverseFiled(f *Field) error {
Path: f.Path,
}

return in.traverseFiled(&pointedField)
return in.traverseField(&pointedField)

case reflect.Slice, reflect.Array:
switch f.Value.Type().Elem().Kind() {
Expand Down

0 comments on commit cdd0f1a

Please sign in to comment.