From e6fec6be488b0f3aa12634ec9176eca9c4bf4aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 5 Feb 2025 16:26:25 +0000 Subject: [PATCH] internal/envflag: small follow-ups to int and string support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: I552f17d7eee77a1ca0b1d575b3b782acfea0948c Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208382 Reviewed-by: Roger Peppe TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine --- internal/envflag/flag.go | 13 +++---------- internal/envflag/flag_test.go | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/internal/envflag/flag.go b/internal/envflag/flag.go index f7fddcd76..b39abea0a 100644 --- a/internal/envflag/flag.go +++ b/internal/envflag/flag.go @@ -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) @@ -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 == "" { @@ -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 } @@ -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) diff --git a/internal/envflag/flag_test.go b/internal/envflag/flag_test.go index 5bd232177..bd064baea 100644 --- a/internal/envflag/flag_test.go +++ b/internal/envflag/flag_test.go @@ -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", -- 2.51.2