Skip to content

Commit

Permalink
removed cast panic in external conf parse; test naming improve
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmashko committed Sep 14, 2023
1 parent 8baec6c commit d1cd1ba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
14 changes: 9 additions & 5 deletions external.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package envconf

import (
"fmt"
"log"
"strings"
"unicode"
)
Expand Down Expand Up @@ -41,8 +40,9 @@ func (c *externalConfig) Unmarshal(v interface{}) error {
return err
}
c.data = make(map[string]interface{})
c.fillMap(c.s, mp)
log.Printf("ext data: %#v", c.data)
if err = c.fillMap(c.s, mp); err != nil {
return err
}
return nil
}

Expand All @@ -55,7 +55,7 @@ func (c *externalConfig) get(f field) (interface{}, bool) {
return v, ok
}

func (c *externalConfig) fillMap(s *structType, src map[string]interface{}) {
func (c *externalConfig) fillMap(s *structType, src map[string]interface{}) error {
for k, v := range src {
f, ok := c.findField(k, s)
if !ok {
Expand All @@ -66,14 +66,18 @@ func (c *externalConfig) fillMap(s *structType, src map[string]interface{}) {
if ok {
st, ok := f.(*structType)
if !ok {
panic(fmt.Sprintf("unable to cast field %s of type %s to struct", fullname(f), f.structField().Type))
return &Error{
Message: fmt.Sprintf("unable to cast %s to struct", f.structField().Type),
FieldName: fullname(f),
}
}
c.fillMap(st, mp)
continue
}

c.data[fullname(f)] = v
}
return nil
}

func (c *externalConfig) findField(key string, s *structType) (field, bool) {
Expand Down
2 changes: 0 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ type JsonConfig struct {
data []byte
}

// DEPRECATED: Do not use this external type
// It will be moved to the separate package
func NewJsonConfig() *JsonConfig {
return &JsonConfig{}
}
Expand Down
72 changes: 35 additions & 37 deletions json_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package envconf
package envconf_test

import "testing"
import (
"testing"

func TestSimpleExternalJsonConfigOK(t *testing.T) {
"github.com/antonmashko/envconf"
)

func TestJsonConfig_SimpleExternalJsonConfig_OK(t *testing.T) {
json := `{"foo":"bar"}`
tc := struct {
Foo string `default:"fail"`
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.Foo != "bar" {
t.Errorf("incorrect value was set. %#v", tc.Foo)
}
}

func TestSimpleExternalJsonConfigFieldWithUnderscoreOK(t *testing.T) {
func TestJsonConfig_SimpleExternalFieldWithUnderscore_OK(t *testing.T) {
json := `{"foo_bar":"foo_bar"}`
tc := struct {
FooBar string `json:"foo_bar" default:"fail"`
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.FooBar != "foo_bar" {
t.Errorf("incorrect value was set. %#v", tc.FooBar)
}
}

func TestNestedStructExternalJsonConfigOK(t *testing.T) {
func TestJsonConfig_NestedStructExternal_OK(t *testing.T) {
json := `{
"foo": {
"bar": {
Expand All @@ -47,17 +51,17 @@ func TestNestedStructExternalJsonConfigOK(t *testing.T) {
}
}
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.Foo.Bar.FooBar != "foo_bar" {
t.Errorf("incorrect value was set. %#v", tc.Foo)
}
}

func TestNestedStructExternalJsonConfigFieldWithUnderscoreOK(t *testing.T) {
func TestJsonConfig_NestedStructExternalFieldWithUnderscore_OK(t *testing.T) {
json := `{
"foo_bar": {
"foo_bar": "foo_bar"
Expand All @@ -68,17 +72,17 @@ func TestNestedStructExternalJsonConfigFieldWithUnderscoreOK(t *testing.T) {
FooBar string `json:"foo_bar" default:"fail"`
} `json:"foo_bar"`
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.FooBar.FooBar != "foo_bar" {
t.Errorf("incorrect value was set. %#v", tc.FooBar)
}
}

func TestSliceJsonConfigOK(t *testing.T) {
func TestJsonConfig_Slice_OK(t *testing.T) {
json := `{
"foo": [
1
Expand All @@ -87,17 +91,17 @@ func TestSliceJsonConfigOK(t *testing.T) {
tc := struct {
Foo []int
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if len(tc.Foo) != 1 || tc.Foo[0] != 1 {
t.Errorf("incorrect value was set. %#v", tc.Foo)
}
}

func TestSliceJsonConfigFloatOk(t *testing.T) {
func TestJsonConfig_SliceFloat_Ok(t *testing.T) {
json := `{
"foo": [
1.1
Expand All @@ -106,9 +110,9 @@ func TestSliceJsonConfigFloatOk(t *testing.T) {
tc := struct {
Foo []float32
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if len(tc.Foo) != 1 || tc.Foo[0] != 1.1 {
Expand All @@ -131,9 +135,9 @@ func TestJsonConfig_PropertyCamelCase_Ok(t *testing.T) {
}
}
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.Foo.Bar.FooBar != "foo_bar" {
Expand All @@ -156,9 +160,9 @@ func TestJsonConfig_CaseSensitive_Ok(t *testing.T) {
ABC int
}
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.AbC != 1 || tc.Abc != 2 || tc.ABC.ABC != 3 {
Expand All @@ -172,9 +176,9 @@ func TestJsonConfig_NonExistJsonValueDefaultUse_Ok(t *testing.T) {
Foo int `json:"foo"`
Bar int `json:"bar" default:"5"`
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.Foo != 2 || tc.Bar != 5 {
Expand All @@ -187,9 +191,9 @@ func TestJsonConfig_NonExistConfigValue_Ok(t *testing.T) {
tc := struct {
Foo int `json:"foo"`
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
if err := envconf.ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
}
if tc.Foo != 2 {
Expand All @@ -198,20 +202,14 @@ func TestJsonConfig_NonExistConfigValue_Ok(t *testing.T) {
}

func TestJsonConfig_PanicOnIncorrectType_Err(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.Fail()
}
}()
json := `{"foo":2, "bar":{"abc":3}}`
tc := struct {
Foo int `json:"foo"`
Bar int `json:"bar"`
}{}
jconf := NewJsonConfig()
jconf := envconf.NewJsonConfig()
jconf.Read([]byte(json))
if err := ParseWithExternal(&tc, jconf); err != nil {
t.Errorf("failed to external parse. err=%s", err)
if err := envconf.ParseWithExternal(&tc, jconf); err == nil {
t.Errorf("expected error but got nil")
}
t.Fail()
}
2 changes: 0 additions & 2 deletions type_primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package envconf
import (
"errors"
"fmt"
"log"
"net"
"net/url"
"reflect"
Expand Down Expand Up @@ -94,7 +93,6 @@ func (t *primitiveType) define() error {
if !ok {
continue
}
log.Printf("set from i: %s val_t: %T f_t: %s", fullname(t), val, t.p.t.String())
return setFromInterface(t.v, val)
case DefaultValue:
v = t.def
Expand Down

0 comments on commit d1cd1ba

Please sign in to comment.