Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

Commit 382d8d7

Browse files
mromaszewiczclaude
andcommitted
Centralize import/helper tracking via CodegenContext and add behavioral tests for helper templates
Replace the scattered ParamUsageTracker and per-phase import collection with a single CodegenContext that all generators share. This eliminates duplicate param tracker creation across client, server, webhook, and callback code paths, and moves import registration, helper tracking, param function tracking, and custom type tracking into one place. Key changes to code generation pipeline: - Add CodegenContext (codegen_context.go) as the centralized tracker for imports, helpers, param functions, and custom type templates - Refactor Generate() in codegen.go into three phases: (1) generate all code sections, (2) render helpers/params/custom types, (3) assemble output — eliminating repeated param/form-helper generation per phase - Remove ParamUsageTracker from paramgen.go; its logic is absorbed by CodegenContext.NeedParam(), GetRequiredParamTemplates(), etc. - Update TypeGenerator to delegate imports and template tracking to ctx - Update GatherOperations, GatherWebhookOperations, and GatherCallbackOperations to accept *CodegenContext instead of *ParamUsageTracker - Move loadCustomType into CodegenContext.loadAndRegisterCustomType so custom type imports are registered directly on the context Add behavioral tests for helper function templates (test/helpers/): - genhelpers/main.go: CLI generator tool that reads ParamHelpersTemplate, MarshalFormHelperTemplate, and ParamTemplates registries, writes .gen.go files with goimports to prune unused imports - 16 generated .gen.go files: helpers, marshal_form, plus style/bind for simple, simple_explode, form, form_explode, label, label_explode, and deepObject - 7 test files (55 tests) covering marshalForm, primitiveToString, BindStringToObject, Date, escape/unescape, and roundtrip style-then-bind for all parameter styles Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 32c2089 commit 382d8d7

34 files changed

Lines changed: 3417 additions & 474 deletions

experimental/internal/codegen/clientgen_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func TestClientGenerator(t *testing.T) {
3535
}
3636

3737
// Create param tracker
38-
paramTracker := NewParamUsageTracker()
38+
ctx := NewCodegenContext()
3939

4040
// Gather operations
41-
ops, err := GatherOperations(doc, paramTracker, NewContentTypeMatcher(DefaultContentTypes()))
41+
ops, err := GatherOperations(doc, ctx, NewContentTypeMatcher(DefaultContentTypes()))
4242
require.NoError(t, err, "Failed to gather operations")
4343
require.Len(t, ops, 4, "Expected 4 operations")
4444

@@ -102,8 +102,8 @@ func TestClientGenerator_FormEncoded(t *testing.T) {
102102
schemaIndex[s.Path.String()] = s
103103
}
104104

105-
paramTracker := NewParamUsageTracker()
106-
ops, err := GatherOperations(doc, paramTracker, contentTypeMatcher)
105+
ctx := NewCodegenContext()
106+
ops, err := GatherOperations(doc, ctx, contentTypeMatcher)
107107
require.NoError(t, err, "Failed to gather operations")
108108

109109
// Verify we have an operation with a form-encoded body
@@ -131,7 +131,8 @@ func TestClientGenerator_FormEncoded(t *testing.T) {
131131
require.Contains(t, clientCode, "marshalForm(body)")
132132

133133
// Verify we generate the form helper when needed
134-
formHelper, err := generateFormHelper(ops)
134+
ctx.NeedFormHelper(ops)
135+
formHelper, err := generateMarshalFormHelper()
135136
require.NoError(t, err, "Failed to generate form helper")
136137
require.NotEmpty(t, formHelper, "Form helper should be generated when form-encoded bodies exist")
137138
require.Contains(t, formHelper, "func marshalForm(")

0 commit comments

Comments
 (0)