Skip to content

Commit

Permalink
Add decode hooks to other parse functions
Browse files Browse the repository at this point in the history
Now that the original config parsing leaves the opaque maps unmodified
these secondary decodes must use the hooks to normalize fields.
  • Loading branch information
dnephin committed May 20, 2020
1 parent d951a58 commit 7f49759
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 0 additions & 2 deletions agent/structs/connect_proxy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,6 @@ type ExposePath struct {
ParsedFromCheck bool
}

// TODO: this is most likely not used. The API type is the one to be deserialized
// with JSON
func (t *ExposePath) UnmarshalJSON(data []byte) (err error) {
type Alias ExposePath
aux := &struct {
Expand Down
23 changes: 21 additions & 2 deletions agent/xds/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,22 @@ type ProxyConfig struct {
// allows caller to choose whether and how to report the error.
func ParseProxyConfig(m map[string]interface{}) (ProxyConfig, error) {
var cfg ProxyConfig
err := mapstructure.WeakDecode(m, &cfg)
decodeConf := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
decode.HookNormalizeHCLNestedBlocks,
decode.HookTranslateKeys,
),
Result: &cfg,
WeaklyTypedInput: true,
}
decoder, err := mapstructure.NewDecoder(decodeConf)
if err != nil {
return cfg, err
}
if err := decoder.Decode(m); err != nil {
return cfg, err
}

// Set defaults (even if error is returned)
if cfg.Protocol == "" {
cfg.Protocol = "tcp"
Expand Down Expand Up @@ -202,7 +217,11 @@ func (p PassiveHealthCheck) AsOutlierDetection() *envoycluster.OutlierDetection
func ParseUpstreamConfigNoDefaults(m map[string]interface{}) (UpstreamConfig, error) {
var cfg UpstreamConfig
config := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
DecodeHook: mapstructure.ComposeDecodeHookFunc(
decode.HookNormalizeHCLNestedBlocks,
decode.HookTranslateKeys,
mapstructure.StringToTimeDurationHookFunc(),
),
Result: &cfg,
WeaklyTypedInput: true,
}
Expand Down

0 comments on commit 7f49759

Please sign in to comment.