From 37088ffb40c47278a57922395e60e68d6639d5d1 Mon Sep 17 00:00:00 2001 From: Milad Rahimi <realmiladrahimi@gmail.com> Date: Sun, 19 Jun 2022 15:40:44 +0430 Subject: [PATCH 1/2] improve setup method --- config.go | 10 +++++++--- config_test.go | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 5fff984..6fcc242 100644 --- a/config.go +++ b/config.go @@ -58,6 +58,7 @@ func (c *Config) Feed() error { return err } } + return nil } @@ -66,7 +67,9 @@ func (c *Config) Feed() error { // It would call the provided fallback if the refresh process failed. func (c *Config) SetupListener(fallback func(err error)) *Config { s := make(chan os.Signal, 1) + signal.Notify(s, syscall.SIGHUP) + go func() { for { <-s @@ -75,6 +78,7 @@ func (c *Config) SetupListener(fallback func(err error)) *Config { } } }() + return c } @@ -82,9 +86,9 @@ func (c *Config) setupStruct(s interface{}) error { sType := reflect.TypeOf(s) if sType != nil && sType.Kind() == reflect.Ptr { if elem := sType.Elem(); elem.Kind() == reflect.Struct { - if _, ok := reflect.TypeOf(s).MethodByName("Setup"); ok { - v := reflect.ValueOf(s).MethodByName("Setup").Call([]reflect.Value{}) - if len(v) > 0 && v[0].CanInterface() { + if m := reflect.ValueOf(s).MethodByName("setup"); m.IsValid() { + v := m.Call([]reflect.Value{}) + if len(v) == 1 && v[0].CanInterface() { if v[0].IsNil() { return nil } else { diff --git a/config_test.go b/config_test.go index 75bcdde..b0b1fa6 100644 --- a/config_test.go +++ b/config_test.go @@ -32,7 +32,7 @@ type FullConfig struct { Sex Sex } -func (fc *FullConfig) Setup() error { +func (fc *FullConfig) setup() error { if fc.SexRaw == 0 { fc.Sex = Male } else if fc.SexRaw == 1 { From f4c2c5a16871893c4d3a61adc534dcbbb98c101b Mon Sep 17 00:00:00 2001 From: Milad Rahimi <realmiladrahimi@gmail.com> Date: Sun, 19 Jun 2022 15:44:34 +0430 Subject: [PATCH 2/2] revert to uppercase --- config.go | 2 +- config_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 6fcc242..3601fc8 100644 --- a/config.go +++ b/config.go @@ -86,7 +86,7 @@ func (c *Config) setupStruct(s interface{}) error { sType := reflect.TypeOf(s) if sType != nil && sType.Kind() == reflect.Ptr { if elem := sType.Elem(); elem.Kind() == reflect.Struct { - if m := reflect.ValueOf(s).MethodByName("setup"); m.IsValid() { + if m := reflect.ValueOf(s).MethodByName("Setup"); m.IsValid() { v := m.Call([]reflect.Value{}) if len(v) == 1 && v[0].CanInterface() { if v[0].IsNil() { diff --git a/config_test.go b/config_test.go index b0b1fa6..6b0d4ac 100644 --- a/config_test.go +++ b/config_test.go @@ -32,13 +32,13 @@ type FullConfig struct { Sex Sex } -func (fc *FullConfig) setup() error { +func (fc *FullConfig) Setup() error { if fc.SexRaw == 0 { fc.Sex = Male } else if fc.SexRaw == 1 { fc.Sex = Female } else { - return errors.New("app: invalid sex") + return errors.New("invalid sex") } return nil @@ -90,7 +90,7 @@ func TestConfig_Feed_With_Setup_Returning_Error(t *testing.T) { f2 := feeder.Env{} err := config.New().AddFeeder(f1, f2).AddStruct(c).Feed() - assert.Error(t, err, "app: invalid sex") + assert.Error(t, err, "invalid sex") } func TestConfig_ReFeeding(t *testing.T) {