From 94194823e6111c4f76010d1643710fdf277280c7 Mon Sep 17 00:00:00 2001 From: Yuki Iwanaga Date: Wed, 11 Apr 2018 20:02:22 +0900 Subject: [PATCH 1/4] Add specs --- defaults_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/defaults_test.go b/defaults_test.go index 2d238fc..a5f24ff 100644 --- a/defaults_test.go +++ b/defaults_test.go @@ -117,7 +117,7 @@ func TestInit(t *testing.T) { } if err := Set(sample); err != nil { - t.Fatalf("it should return an error: %v", err) + t.Fatalf("it should not return an error: %v", err) } if err := Set(1); err == nil { @@ -272,6 +272,22 @@ func TestInit(t *testing.T) { if len(sample.SliceWithJSON) == 0 || sample.SliceWithJSON[0] != "foo" { t.Errorf("it should initialize slice with json") } + + t.Run("invalid json", func(t *testing.T) { + if err := Set(&struct { + I []int `default:"[!]"` + }{}); err == nil { + t.Errorf("it should return error") + } + + if err := Set(&struct { + S struct { + I []int `default:"[!]"` + } + }{}); err == nil { + t.Errorf("it should return error") + } + }) }) t.Run("Setter interface", func(t *testing.T) { From c94070b34a49ba3d5d923c88a2b672fbb58673d5 Mon Sep 17 00:00:00 2001 From: Yuki Iwanaga Date: Wed, 11 Apr 2018 20:42:58 +0900 Subject: [PATCH 2/4] Fix --- defaults.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/defaults.go b/defaults.go index 9a1acfc..e740646 100644 --- a/defaults.go +++ b/defaults.go @@ -33,20 +33,22 @@ func Set(ptr interface{}) error { for i := 0; i < t.NumField(); i++ { if defaultVal := t.Field(i).Tag.Get(fieldName); defaultVal != "-" { - setField(v.Field(i), defaultVal) + if err := setField(v.Field(i), defaultVal); err != nil { + return err + } } } return nil } -func setField(field reflect.Value, defaultVal string) { +func setField(field reflect.Value, defaultVal string) error { if !field.CanSet() { - return + return nil } if !shouldInitializeField(field.Kind(), defaultVal) { - return + return nil } if isInitialValue(field) { @@ -116,20 +118,27 @@ func setField(field reflect.Value, defaultVal string) { ref := reflect.New(field.Type()) ref.Elem().Set(reflect.MakeSlice(field.Type(), 0, 0)) if defaultVal != "" && defaultVal != "[]" { - json.Unmarshal([]byte(defaultVal), ref.Interface()) + println(defaultVal) + if err := json.Unmarshal([]byte(defaultVal), ref.Interface()); err != nil { + return err + } } field.Set(ref.Elem().Convert(field.Type())) case reflect.Map: ref := reflect.New(field.Type()) ref.Elem().Set(reflect.MakeMap(field.Type())) if defaultVal != "" && defaultVal != "{}" { - json.Unmarshal([]byte(defaultVal), ref.Interface()) + if err := json.Unmarshal([]byte(defaultVal), ref.Interface()); err != nil { + return err + } } field.Set(ref.Elem().Convert(field.Type())) case reflect.Struct: ref := reflect.New(field.Type()) if defaultVal != "" && defaultVal != "{}" { - json.Unmarshal([]byte(defaultVal), ref.Interface()) + if err := json.Unmarshal([]byte(defaultVal), ref.Interface()); err != nil { + return err + } } field.Set(ref.Elem()) case reflect.Ptr: @@ -141,13 +150,17 @@ func setField(field reflect.Value, defaultVal string) { case reflect.Ptr: setField(field.Elem(), defaultVal) callSetter(field.Interface()) - default: + case reflect.Struct: ref := reflect.New(field.Type()) ref.Elem().Set(field) - Set(ref.Interface()) + if err := Set(ref.Interface()); err != nil { + return err + } callSetter(ref.Interface()) field.Set(ref.Elem()) } + + return nil } func isInitialValue(field reflect.Value) bool { From 1424daba586f19e8ea0be5b3d299ceb56c37991c Mon Sep 17 00:00:00 2001 From: Yuki Iwanaga Date: Wed, 11 Apr 2018 20:47:43 +0900 Subject: [PATCH 3/4] Tweak --- defaults.go | 1 - defaults_test.go | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/defaults.go b/defaults.go index e740646..1882ac9 100644 --- a/defaults.go +++ b/defaults.go @@ -118,7 +118,6 @@ func setField(field reflect.Value, defaultVal string) error { ref := reflect.New(field.Type()) ref.Elem().Set(reflect.MakeSlice(field.Type(), 0, 0)) if defaultVal != "" && defaultVal != "[]" { - println(defaultVal) if err := json.Unmarshal([]byte(defaultVal), ref.Interface()); err != nil { return err } diff --git a/defaults_test.go b/defaults_test.go index a5f24ff..c949db4 100644 --- a/defaults_test.go +++ b/defaults_test.go @@ -280,6 +280,20 @@ func TestInit(t *testing.T) { t.Errorf("it should return error") } + if err := Set(&struct { + I map[string]int `default:"{1}"` + }{}); err == nil { + t.Errorf("it should return error") + } + + if err := Set(&struct { + S struct { + I []int + } `default:"{!}"` + }{}); err == nil { + t.Errorf("it should return error") + } + if err := Set(&struct { S struct { I []int `default:"[!]"` From b5182688d973eb302fedeb614160efbd966e8b71 Mon Sep 17 00:00:00 2001 From: Yuki Iwanaga Date: Wed, 11 Apr 2018 20:51:13 +0900 Subject: [PATCH 4/4] Add more spec --- defaults_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/defaults_test.go b/defaults_test.go index c949db4..8c7b897 100644 --- a/defaults_test.go +++ b/defaults_test.go @@ -120,7 +120,12 @@ func TestInit(t *testing.T) { t.Fatalf("it should not return an error: %v", err) } - if err := Set(1); err == nil { + nonPtrVal := 1 + + if err := Set(nonPtrVal); err == nil { + t.Fatalf("it should return an error when used for a non-pointer type") + } + if err := Set(&nonPtrVal); err == nil { t.Fatalf("it should return an error when used for a non-pointer type") }