-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package main | ||
|
|
||
| //go:generate go run ../cmd/cfgx/main.go generate --in config/config.toml --out config/config.go --pkg config | ||
| //go:generate go run ../cmd/cfgx/main.go generate --in config/config.toml --out getter_config/config.go --pkg getter_config --mode getter | ||
| //go:generate go run ../cmd/cfgx generate --in config/config.toml --out config/config.go --pkg config | ||
| //go:generate go run ../cmd/cfgx generate --in config/config.toml --out getter_config/config.go --pkg getter_config --mode getter |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -412,25 +412,56 @@ func (g *Generator) generateStructsAndGetters(buf *bytes.Buffer, data map[string | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Generate var declarations | ||||||||||||||||||||||||||||
| // Generate top-level getter functions for simple variables | ||||||||||||||||||||||||||||
| for _, key := range keys { | ||||||||||||||||||||||||||||
| value := data[key] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Only generate getters for non-struct, non-array-of-structs values | ||||||||||||||||||||||||||||
| switch val := value.(type) { | ||||||||||||||||||||||||||||
| case map[string]any, []map[string]any: | ||||||||||||||||||||||||||||
| // Skip structs - they will be var declarations | ||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||
| // Generate getter for simple types | ||||||||||||||||||||||||||||
| if err := g.generateTopLevelGetter(buf, key, value); err != nil { | ||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Generate var declarations (only for structs and arrays of structs) | ||||||||||||||||||||||||||||
| buf.WriteString("var (\n") | ||||||||||||||||||||||||||||
| for _, key := range keys { | ||||||||||||||||||||||||||||
| varName := sx.PascalCase(key) | ||||||||||||||||||||||||||||
| value := data[key] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| switch value.(type) { | ||||||||||||||||||||||||||||
| switch val := value.(type) { | ||||||||||||||||||||||||||||
| case map[string]any: | ||||||||||||||||||||||||||||
| structName := sx.CamelCase(key) + "Config" | ||||||||||||||||||||||||||||
| fmt.Fprintf(buf, "\t%s %s\n", varName, structName) | ||||||||||||||||||||||||||||
| case []map[string]any: | ||||||||||||||||||||||||||||
| structName := sx.CamelCase(key) + "Item" | ||||||||||||||||||||||||||||
| fmt.Fprintf(buf, "\t%s []%s\n", varName, structName) | ||||||||||||||||||||||||||||
| case []any: | ||||||||||||||||||||||||||||
| goType := g.toGoType(value) | ||||||||||||||||||||||||||||
| fmt.Fprintf(buf, "\t%s %s\n", varName, goType) | ||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||
| goType := g.toGoType(value) | ||||||||||||||||||||||||||||
| fmt.Fprintf(buf, "\t%s %s\n", varName, goType) | ||||||||||||||||||||||||||||
| // Check if it's an array of maps (structs) | ||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+459
to
+463
|
||||||||||||||||||||||||||||
| 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) |
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. Whenlen(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.