Skip to content

Commit

Permalink
Add decode hook
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelreiswildlife committed Mar 25, 2024
1 parent c7bfd0a commit 965ede5
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package config

import (
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/topfreegames/pusher/util"
"reflect"
"strings"
)

Expand Down Expand Up @@ -36,7 +39,7 @@ func NewConfigAndViper(configFile string) (*Config, *viper.Viper, error) {
}

config := &Config{}
if err := v.Unmarshal(config); err != nil {
if err := v.Unmarshal(config, decodeHookFunc()); err != nil {
return nil, nil, fmt.Errorf("error unmarshalling config: %s", err)
}

Expand All @@ -52,3 +55,35 @@ func (c *Config) GetAppsArray() []string {

return res
}

func decodeHookFunc() viper.DecoderConfigOption {
hooks := mapstructure.ComposeDecodeHookFunc(
StringToMapStringHookFunc(),
)
return viper.DecodeHook(hooks)
}

func StringToMapStringHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String || t.Kind() != reflect.Map {
return data, nil
}

if t.Key().Kind() != reflect.String || t.Elem().Kind() != reflect.String {
return data, nil
}

raw := data.(string)
if raw == "" {
return map[string]string{}, nil
}

m := map[string]string{}
err := json.Unmarshal([]byte(raw), &m)
return m, err
}
}

0 comments on commit 965ede5

Please sign in to comment.