Skip to content

Commit

Permalink
improve setup method
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Jun 19, 2022
1 parent c27db0b commit 37088ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 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
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 37088ff

Please sign in to comment.