Skip to content

fix(requestflag): inspect underlying type in PostParse for empty string and bool env var handling - #35

Open
Adityakk9031 wants to merge 1 commit into
context-dot-dev:mainfrom
Adityakk9031:fix/requestflag-postparse-env-reflection
Open

Adityakk9031 wants to merge 1 commit into
context-dot-dev:mainfrom
Adityakk9031:fix/requestflag-postparse-env-reflection

Conversation

@Adityakk9031

Copy link
Copy Markdown

Summary

In internal/requestflag/requestflag.go (Flag[T].PostParse), when handling environment variable sources, the code previously checked:

  • reflect.TypeOf(f.value).Kind() == reflect.String
  • reflect.TypeOf(f.value).Kind() == reflect.Bool

Because f.value is of concrete type *cliValue[T], reflect.TypeOf(f.value).Kind() always returns reflect.Pointer, never reflect.String or reflect.Bool.

This caused two bugs:

  1. When an environment variable for a string flag was set to an empty string (""), val != "" evaluated to false, and the string check failed, so the flag was never populated from the environment variable.
  2. When an environment variable for a boolean flag was set to "", the bool check failed, and f.Set(f.Name, "false") was never executed.

Solution

Inspect the underlying type parameter T and flag metadata using reflect.TypeFor[T]() and f.IsBoolFlag().

Tests

  • Added TestFlagPostParse_EnvVars in internal/requestflag/requestflag_test.go verifying that both string flags and boolean flags correctly parse empty string environment variables during PostParse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant