-
-
Notifications
You must be signed in to change notification settings - Fork 214
Fix: Support for nested pointer struct fields in CLI options #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Support for nested pointer struct fields in CLI options #824
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a panic issue when using nested pointer-to-struct CLI options by initializing nil pointers before setting nested field values and enhancing error messages. It also adds tests for both pointer and direct nested struct configurations.
- Supports nested pointer initialization in CLI options.
- Improves error handling for parsing failures.
- Adds dedicated tests for nested options via CLI flags and environment variables.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
humacli/humacli_test.go | Adds tests verifying that nested pointer-to-struct and direct struct options handle inputs correctly. |
humacli/humacli.go | Introduces logic to properly initialize parent pointers and enhances error handling in setupOptions. |
|
… value precedence
e01eead
to
92bf536
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes nested pointer-to-struct field support in CLI options by properly initializing nil pointers and handling field value extraction from flags and environment variables.
- Initializes nil pointer fields before accessing nested fields.
- Introduces helper functions to manage value precedence between CLI flags, environment variables, and defaults.
- Adds unit tests to verify both nested pointer and direct struct option handling and priority between flags and environment variables.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
humacli/humacli_test.go | Added tests to validate nested options and flag/env priority handling |
humacli/humacli.go | Updated option parsing, nil pointer initialization, and flag registration for nesting |
Comments suppressed due to low confidence (1)
humacli/humacli.go:234
- Undefined variable 'i' used when accessing a field; it appears the loop that iterates over 'opt.path' was removed. Reintroduce the loop to iterate over each index in 'opt.path' before accessing a field.
f = f.Field(i)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Description
This PR adds support for nested options in CLI configurations, particularly
when using pointer-to-struct fields. It addresses an issue where the CLI would
panic with
panic: Unsupported option type: ptr
orpanic: Unsupported option type: struct
when attempting to set values on fields within a nil pointer struct.
Example Use Case (my use case).
I was trying to implement nested configuration options with pointers for a clean optional field pattern:
When attempting to use this pattern with CLI flags like
--db.host localhost --db.port 5432
,I encountered a panic error:
panic: Unsupported option type: struct
.The fix in this PR allows for properly handling these nested pointer-to-struct
configurations, so users can effectively organize related options.
Changes
before setting values
wrapping
I tried to split every major change in its own commit, btw
Testing
Added a new test case TestCLINestedOptions that verifies both direct struct
fields and pointer-to-struct fields work correctly with CLI arguments and
environment variables.
Oh ! And thanks for your awesome project 💯