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

Commit d75333d

Browse files
mromaszewiczclaude
andcommitted
refactor: use regex for types qualifier stripping in extractor
Replace the hand-maintained list of qualifierReplacements with a regex that matches any `types.Identifier` pattern. This avoids needing to update the list when new types are added to the runtime. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 79c2020 commit d75333d

File tree

1 file changed

+7
-21
lines changed
  • experimental/codegen/internal/runtimeextract

1 file changed

+7
-21
lines changed

experimental/codegen/internal/runtimeextract/extract.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go/token"
1111
"io/fs"
1212
"path"
13+
"regexp"
1314
"sort"
1415
"strings"
1516
)
@@ -25,21 +26,9 @@ type Import struct {
2526
// generator to rewrite them to the target base path.
2627
const RuntimeModulePrefix = "github.com/oapi-codegen/oapi-codegen-exp/experimental/codegen/internal/runtime/"
2728

28-
// qualifierReplacements maps qualified references in params code (which imports
29-
// the types sub-package) to their unqualified forms for inlining.
30-
var qualifierReplacements = []struct{ old, new string }{
31-
{"types.DateFormat", "DateFormat"},
32-
{"types.Date", "Date"},
33-
{"types.Email", "Email"},
34-
{"types.UUID", "UUID"},
35-
{"types.File", "File"},
36-
{"types.Nullable", "Nullable"},
37-
{"types.NewNullableWithValue", "NewNullableWithValue"},
38-
{"types.NewNullNullable", "NewNullNullable"},
39-
{"types.ErrNullableIsNull", "ErrNullableIsNull"},
40-
{"types.ErrNullableNotSpecified", "ErrNullableNotSpecified"},
41-
{"types.ErrValidationEmail", "ErrValidationEmail"},
42-
}
29+
// typesQualifierRe matches "types." followed by a Go identifier, used to
30+
// strip the package qualifier when inlining runtime code.
31+
var typesQualifierRe = regexp.MustCompile(`\btypes\.([A-Za-z_]\w*)`)
4332

4433
// ExtractPackage reads all .go files from a sub-directory of the given FS
4534
// that contain an //oapi-runtime:function annotation and returns the
@@ -279,11 +268,8 @@ func stripAnnotations(code string) string {
279268
return strings.Join(lines, "\n")
280269
}
281270

282-
// dequalifyTypes replaces qualified type references (types.Date, etc.)
283-
// with unqualified forms for inlining into a single package.
271+
// dequalifyTypes strips the "types." package qualifier from all references,
272+
// since when inlined everything lives in the same package.
284273
func dequalifyTypes(code string) string {
285-
for _, r := range qualifierReplacements {
286-
code = strings.ReplaceAll(code, r.old, r.new)
287-
}
288-
return code
274+
return typesQualifierRe.ReplaceAllString(code, "$1")
289275
}

0 commit comments

Comments
 (0)