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 e92e157 commit 24525d7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 257 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
2 changes: 1 addition & 1 deletion fileutil/properties_file_loader_test.go
Expand Up @@ -14,7 +14,7 @@ func TestReadPropertiesFile(t *testing.T) {
t.FailNow()
}

if props["host"] != "localhost" || props["proxyHost"] != "test" || props["protocol"] != "https://" || props["chunk"] != "" || props["boolean"] != "true" {
if props["host"] != "localhost" || props["proxyHost"] != "test" || props["protocol"] != "https://" || props["chunk"] != "" || props["boolean"] != true {
fmt.Printf("props: %q", props)
t.Error("Error properties not loaded correctly")
t.Fail()
Expand Down

0 comments on commit 24525d7

Please sign in to comment.