-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: Generate top-level getters in getter mode #7
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
Conversation
📊 Code Coverage ReportCoverage by file |
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 refactors the getter mode code generation to create top-level getter functions for simple variables instead of variable declarations, while maintaining variable declarations only for structs and arrays of structs. This provides a consistent interface where all values (simple types and structs) are accessed through function calls that support environment variable overrides.
- Introduced top-level getter functions for primitive types and arrays of primitives
- Refactored common getter body logic into a shared
writeGetterBodymethod - Updated variable declarations to only include structs and arrays of structs
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/generator/struct_gen.go | Implements top-level getter function generation and refactors getter body logic to avoid code duplication |
| internal/generator/generator_test.go | Adds comprehensive test coverage for the new top-level getter functionality |
| example/getter_config/config.go | Demonstrates the change where Name is now a getter function instead of a variable |
| example/gen.go | Simplifies go:generate commands by removing redundant path references |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| case []any: | ||
| // Check if it's an array of maps (structs) | ||
| if len(val) > 0 { | ||
| if _, ok := val[0].(map[string]any); ok { | ||
| // Skip array of structs | ||
| continue | ||
| } | ||
| } | ||
| // Generate getter for array of primitives | ||
| if err := g.generateTopLevelGetter(buf, key, value); err != nil { | ||
| return err | ||
| } |
Copilot
AI
Oct 28, 2025
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.
Empty arrays of primitives ([]any) are not handled. When len(val) == 0, the code neither generates a getter nor adds a var declaration, causing these arrays to be silently omitted from the generated code. Consider generating a getter for empty arrays or handling them explicitly.
| if len(val) > 0 { | ||
| if _, ok := val[0].(map[string]any); ok { | ||
| structName := sx.CamelCase(key) + "Item" | ||
| fmt.Fprintf(buf, "\t%s []%s\n", varName, structName) | ||
| } |
Copilot
AI
Oct 28, 2025
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.
Empty arrays of structs ([]any with length 0) are not added to the var block. When an empty array is encountered, no variable declaration is generated, which could cause compilation errors if the code expects this variable to exist. Consider adding a var declaration for empty arrays with a type comment or default struct type.
| if len(val) > 0 { | |
| if _, ok := val[0].(map[string]any); ok { | |
| structName := sx.CamelCase(key) + "Item" | |
| fmt.Fprintf(buf, "\t%s []%s\n", varName, structName) | |
| } | |
| structName := sx.CamelCase(key) + "Item" | |
| if len(val) > 0 { | |
| if _, ok := val[0].(map[string]any); ok { | |
| fmt.Fprintf(buf, "\t%s []%s\n", varName, structName) | |
| } | |
| } else { | |
| // Emit declaration for empty array of structs | |
| fmt.Fprintf(buf, "\t%s []%s\n", varName, structName) |
Fix: Generate top-level getters in getter mode
This PR fixes a bug where top-level variables in getter mode were generated as simple
vardeclarations instead of getter functions, preventing environment variable overrides for top-level configuration values.Changes
writeGetterBody()to share getter logic between struct methods and top-level functionsTestGenerator_GetterMode_TopLevelVariables)getter_config/config.goto demonstrate the fix -Nameis now a function with env override supportExample
Before (incorrect):
After (correct):
Structs and arrays of structs remain as var declarations as intended.