Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage: support implementsInterface() for non-struct types #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ type Specification struct {
Property string `envconfig:"inner"`
PropertyWithDefault string `default:"fuzzybydefault"`
} `envconfig:"outer"`
AfterNested string
DecodeStruct HonorDecodeInStruct `envconfig:"honor"`
Datetime time.Time
MapField map[string]string `default:"one:two,three:four"`
UrlValue CustomURL
UrlPointer *CustomURL
AfterNested string
DecodeStruct HonorDecodeInStruct `envconfig:"honor"`
Datetime time.Time
MapField map[string]string `default:"one:two,three:four"`
UrlValue CustomURL
UrlPointer *CustomURL
NamedSliceValue NamedSlice
CustomSliceValue SliceWithUnmarshal
}

type Embedded struct {
Expand All @@ -86,6 +88,14 @@ type EmbeddedButIgnored struct {
SecondEmbeddedButIgnored string
}

type NamedSlice []int8

type SliceWithUnmarshal []int8

func (cs *SliceWithUnmarshal) UnmarshalText(text []byte) error {
return nil
}

func TestProcess(t *testing.T) {
var s Specification
os.Clearenv()
Expand Down
2 changes: 2 additions & 0 deletions testdata/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ ENV_CONFIG_DATETIME=
ENV_CONFIG_MAPFIELD=
ENV_CONFIG_URLVALUE=
ENV_CONFIG_URLPOINTER=
ENV_CONFIG_NAMEDSLICEVALUE=
ENV_CONFIG_CUSTOMSLICEVALUE=
10 changes: 10 additions & 0 deletions testdata/default_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,13 @@ ENV_CONFIG_URLPOINTER
..[type]........CustomURL
..[default].....
..[required]....
ENV_CONFIG_NAMEDSLICEVALUE
..[description].
..[type]........Comma-separated.list.of.Integer
..[default].....
..[required]....
ENV_CONFIG_CUSTOMSLICEVALUE
..[description].
..[type]........SliceWithUnmarshal
..[default].....
..[required]....
2 changes: 2 additions & 0 deletions testdata/default_table.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ ENV_CONFIG_DATETIME..............................Time...........................
ENV_CONFIG_MAPFIELD..............................Comma-separated.list.of.String:String.pairs.....one:two,three:four................
ENV_CONFIG_URLVALUE..............................CustomURL.........................................................................
ENV_CONFIG_URLPOINTER............................CustomURL.........................................................................
ENV_CONFIG_NAMEDSLICEVALUE.......................Comma-separated.list.of.Integer...................................................
ENV_CONFIG_CUSTOMSLICEVALUE......................SliceWithUnmarshal................................................................
2 changes: 2 additions & 0 deletions testdata/fault.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@
{.Key}
{.Key}
{.Key}
{.Key}
{.Key}
7 changes: 4 additions & 3 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func implementsInterface(t reflect.Type) bool {

// toTypeDescription converts Go types into a human readable description
func toTypeDescription(t reflect.Type) string {
if implementsInterface(t) && t.Name() != "" {
return t.Name()
}

switch t.Kind() {
case reflect.Array, reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
Expand All @@ -71,9 +75,6 @@ func toTypeDescription(t reflect.Type) string {
case reflect.Ptr:
return toTypeDescription(t.Elem())
case reflect.Struct:
if implementsInterface(t) && t.Name() != "" {
return t.Name()
}
return ""
case reflect.String:
name := t.Name()
Expand Down