Skip to content

Commit

Permalink
Fix parsing of boolean config parameters.
Browse files Browse the repository at this point in the history
Other file formats like JSON or YAML would provide more type information, and unmarshalling would create correct types. Properties files aren't that useful, so we have to enforce some types by ourselves. We have to keep an eye on other types beside boolean, but this one seems sufficient for now.

Relates to urfave/cli#1376, which made our previous workaround (see #81 and #83) fail due to a private function in the InputSourceContext interface.
  • Loading branch information
gesellix committed May 26, 2022
1 parent 95949db commit b61fef4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 256 deletions.
255 changes: 0 additions & 255 deletions fileutil/map_input_source.go

This file was deleted.

21 changes: 20 additions & 1 deletion fileutil/properties_file_loader.go
Expand Up @@ -12,6 +12,7 @@ import (
"net/url"
"os"
"runtime"
"strconv"
"strings"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -50,9 +51,27 @@ func readPropertiesFile(filename string) (map[interface{}]interface{}, error) {
return nil, err
}

// Other file formats would provide more type information, and unmarshalling would
// create correct types. Properties files aren't that useful, so we have to enforce
// some types by ourselves. We have to keep an eye on other types beside boolean,
// but this one seems good enough for now.
coerceBooleanValues(config)

return config, nil
}

func coerceBooleanValues(m map[interface{}]interface{}) {
for k, v := range m {
switch v := v.(type) {
case string:
parsed, err := strconv.ParseBool(v)
if err == nil {
m[k] = parsed
}
}
}
}

type propertiesSourceContext struct {
FilePath string
}
Expand All @@ -66,7 +85,7 @@ func NewPropertiesSourceFromFile(file string) (altsrc.InputSourceContext, error)
return nil, fmt.Errorf("Unable to load Properties file '%s': inner error: \n'%v'", ysc.FilePath, err.Error())
}

return &MapInputSource{valueMap: results}, nil
return altsrc.NewMapInputSource(file, results), nil
}

// NewPropertiesSourceFromFlagFunc creates a new Properties InputSourceContext from a provided flag name and source context.
Expand Down

0 comments on commit b61fef4

Please sign in to comment.