feat: struct field tags customization#2448
Conversation
Greptile SummaryThis PR replaces the hardcoded struct-tag assembly in
Confidence Score: 5/5Safe to merge; the change is fully opt-in, default output is unchanged, and template errors are caught at configuration time. The new struct-tag generation path is thoroughly tested, backward-compatible by design, and validated eagerly so invalid user config surfaces before any code is written. No changes to generated function/method signatures or wire-format JSON tags are introduced unconditionally. No files require special attention; the JSON schema, configuration struct, and implementation are kept in sync.
|
| Filename | Overview |
|---|---|
| pkg/codegen/structtags.go | New file implementing the configurable struct-tag system. Template validation probes exhaustively cover all 16 boolean-flag combinations, silently-skipping was replaced with a stderr warning, and the merge logic correctly supersedes the legacy yaml-tags flag. Minor misleading comments about where alphabetical ordering is enforced. |
| pkg/codegen/configuration.go | Adds StructTagsConfig field and updates Validate() to eagerly parse user templates; JSON schema is updated in lock-step. |
| pkg/codegen/schema.go | Replaces hardcoded json/yaml/form tag assembly with schemaFieldTagGenerator().generateTagsMap(); default templates reproduce the historical output byte-for-byte. |
| pkg/codegen/operations.go | JsonTag() now delegates to paramFieldTagGenerator(), extending struct-tag customization to strict RequestObject parameter fields while preserving previous behavior. |
| pkg/codegen/codegen.go | Eagerly builds both structTagGenerator instances in Generate() so invalid user templates surface as errors rather than being silently skipped. |
| pkg/codegen/structtags_test.go | Comprehensive unit tests covering default config, merge semantics, parse/render errors including conditional branches, generateTagsMap output, and Validate() integration. |
| internal/test/options/struct_tags/struct_tags.gen.go | Generated file correctly shows db/json/validate/yaml tags alphabetically; form tag gated on NeedsFormTag; validate suppressed on optional fields. |
| configuration-schema.json | Schema updated in sync with configuration.go changes; struct-tags object and yaml-tags supersession documented correctly. |
| internal/test/options/struct_tags/struct_tags_test.go | Integration tests verify json default retention, yaml-tags flag supersession, custom tag addition, empty-render suppression, and form-tag gating. |
| internal/test/options/struct_tags/config.yaml | New test fixture under the correct options/ category (not issue-numbered); exercises yaml override and validate/db additions with yaml-tags: true coexisting. |
Reviews (2): Last reviewed commit: "feat: struct field tags customization" | Re-trigger Greptile
Adds an output-options.struct-tags configuration that makes struct tag generation user-configurable instead of hardcoded. Each entry is a tag name plus a Go text/template rendered against per-field data (.FieldName, .IsOptional, .OmitEmpty, .OmitZero, .NeedsFormTag); entries merge by name on top of the defaults, so users can override the built-in json/form templates or add new tags (yaml, db, validate, ...). A template that renders empty suppresses the tag. The default templates reproduce the existing hardcoded output byte-for-byte, so generation is unchanged unless struct-tags is set. The legacy yaml-tags flag is superseded: an explicit yaml entry in struct-tags takes precedence over it. Extension-driven behavior (x-go-json-ignore, x-oapi-codegen-extra-tags) is applied on top of the rendered tags as before. Invalid templates are reported as configuration errors at validation/generation time. Partially addresses oapi-codegen#2368. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
b6ad025 to
0c9a489
Compare
Adds an output-options.struct-tags configuration that makes struct tag generation user-configurable instead of hardcoded. Each entry is a tag name plus a Go text/template rendered against per-field data (.FieldName, .IsOptional, .OmitEmpty, .OmitZero, .NeedsFormTag); entries merge by name on top of the defaults, so users can override the built-in json/form templates or add new tags (yaml, db, validate, ...). A template that renders empty suppresses the tag.
The default templates reproduce the existing hardcoded output byte-for-byte, so generation is unchanged unless struct-tags is set. The legacy yaml-tags flag is superseded: an explicit yaml entry in struct-tags takes precedence over it. Extension-driven behavior (x-go-json-ignore, x-oapi-codegen-extra-tags) is applied on top of the rendered tags as before. Invalid templates are reported as configuration errors at validation/generation time.
Partially addresses #2368.