Skip to content

Commit

Permalink
internal/envflag: small follow-ups to int and string support
Browse files Browse the repository at this point in the history
Update the godoc so that it is no longer boolean-centric.

Remove the shortcut for an empty env var value;
the rest of the code is a loop over its comma-separated list,
which results in no work at all anyway, so this seems unnecessary.

We don't treat any input string errors as fatal anymore,
meaning that we always continue processing the rest of the input list,
so remove a comment implying that it's a special case to continue.

Finally, one test had a trailing bit of input which was entirely
unnecessary for the test case, and also wrong, as keys are lowercase.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I552f17d7eee77a1ca0b1d575b3b782acfea0948c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208382
Reviewed-by: Roger Peppe <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Feb 7, 2025
1 parent 433e3c3 commit e6fec6b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
13 changes: 3 additions & 10 deletions internal/envflag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ func Init[T any](flags *T, envVar string) error {
// representing the boolean fields in the struct type T. If the value is omitted
// entirely, the value is assumed to be name=true.
//
// Names are treated case insensitively. Value strings are parsed as Go booleans
// via [strconv.ParseBool], meaning that they accept "true" and "false" but also
// the shorter "1" and "0".
// Names are treated case insensitively. Boolean values are parsed via [strconv.ParseBool],
// integers via [strconv.Atoi], and strings are accepted as-is.
func Parse[T any](flags *T, env string) error {
// Collect the field indices and set the default values.
indexByName := make(map[string]int)
Expand Down Expand Up @@ -67,9 +66,6 @@ func Parse[T any](flags *T, env string) error {
indexByName[name] = i
}

if env == "" {
return nil
}
var errs []error
for _, elem := range strings.Split(env, ",") {
if elem == "" {
Expand All @@ -85,7 +81,6 @@ func Parse[T any](flags *T, env string) error {

index, knownFlag := indexByName[name]
if !knownFlag {
// Unknown option, proceed processing options as long as the format is valid.
errs = append(errs, fmt.Errorf("unknown flag %q", elem))
continue
}
Expand Down Expand Up @@ -123,9 +118,7 @@ func Parse[T any](flags *T, env string) error {
return errors.Join(errs...)
}

func parseValue(name string, kind reflect.Kind, str string) (any, error) {
var val any
var err error
func parseValue(name string, kind reflect.Kind, str string) (val any, err error) {
switch kind {
case reflect.Bool:
val, err = strconv.ParseBool(str)
Expand Down
2 changes: 1 addition & 1 deletion internal/envflag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var tests = []struct {
}, "cannot parse TEST_VAR: unknown flag \"other1\"\nunknown flag \"other2\""),
}, {
testName: "InvalidIntForBool",
envVal: "foo=2,BarBaz=true",
envVal: "foo=2",
test: invalid(testFlags{DefaultTrue: true}),
}, {
testName: "StringValue",
Expand Down

0 comments on commit e6fec6b

Please sign in to comment.