From 7d6a7cfa16da01eb607ee5f613a64646a94ed09b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B3=88=E4=BB=81=E5=AE=87?= Date: Mon, 3 Aug 2020 10:34:53 +0800 Subject: [PATCH] refactor: rearrange control flow in method loadField --- envconf.go | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/envconf.go b/envconf.go index f856bf9..ce38c6a 100644 --- a/envconf.go +++ b/envconf.go @@ -38,50 +38,47 @@ func (l *loader) useKey(name string) error { } func (l *loader) loadField(name string, out *reflect.Value) { + kind := out.Kind() + if kind == reflect.Struct { + l.loadStruct(name, out) + return + } + + if err := l.useKey(name); err != nil { + panic(err) + } + switch out.Kind() { case reflect.String: - if err := l.useKey(name); err != nil { - panic(err) - } l.loadString(name, out) + return case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - if err := l.useKey(name); err != nil { - panic(err) - } l.loadInt(name, out) + return case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - if err := l.useKey(name); err != nil { - panic(err) - } l.loadUint(name, out) + return case reflect.Bool: - if err := l.useKey(name); err != nil { - panic(err) - } l.loadBool(name, out) - - case reflect.Struct: - l.loadStruct(name, out) + return case reflect.Slice: sliceType := out.Type() switch sliceType { case stringSliceType: - if err := l.useKey(name); err != nil { - panic(err) - } l.loadStringSlice(name, out) + return default: panic(fmt.Errorf("slice type %v on %v is not supported", sliceType, name))