Skip to content

Commit

Permalink
Merge pull request #41 from golobby/setup-method
Browse files Browse the repository at this point in the history
improve setup method
  • Loading branch information
miladrahimi authored Jun 19, 2022
2 parents c3e3855 + f4c2c5a commit d86829c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (c *Config) Feed() error {
return err
}
}

return nil
}

Expand All @@ -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
Expand All @@ -75,16 +78,17 @@ func (c *Config) SetupListener(fallback func(err error)) *Config {
}
}
}()

return c
}

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 {
Expand Down
4 changes: 2 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (fc *FullConfig) Setup() error {
} else if fc.SexRaw == 1 {
fc.Sex = Female
} else {
return errors.New("app: invalid sex")
return errors.New("invalid sex")
}

return nil
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d86829c

Please sign in to comment.