Skip to content

Commit

Permalink
fix bug with *struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bardia-key committed Aug 9, 2020
1 parent 564a4cf commit 78a2ad3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func configFilePath(app string) (string, error) {

func configFileType(config interface{}) (string, error) {
t := reflect.TypeOf(config)
if t.Kind() == reflect.Ptr {
// Extract the underlying object
t = t.Elem()
}

if t.Kind() != reflect.Struct {
return "", errors.New("config must be a struct")
}
Expand Down
9 changes: 9 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func TestConfigFileType(t *testing.T) {
assert.Equal(t, cftXML, cft)
})

t.Run("detects *struct", func(t *testing.T) {
type J struct {
A string `json:"a"`
}
cft, err := configFileType(&J{})
require.NoError(t, err)
assert.Equal(t, cftJSON, cft)
})

t.Run("errors on non-struct ", func(t *testing.T) {
_, err := configFileType("qeubar")
assert.Error(t, err)
Expand Down

0 comments on commit 78a2ad3

Please sign in to comment.