Skip to content

Replace old hand written slice sorting with slices package, and other built-in helpers - #2305

Merged
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
gaiaz-iusipov:modernize
Apr 3, 2026
Merged

mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
gaiaz-iusipov:modernize

Conversation

@gaiaz-iusipov

@gaiaz-iusipov gaiaz-iusipov commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

In this PR I made small stylistic changes: using functions from standard packages (maps.Copy, slices.Contains, etc.)

Signed-off-by: Gaiaz Iusipov <g.iusipov@gmail.com>
@gaiaz-iusipov
gaiaz-iusipov requested a review from a team as a code owner March 26, 2026 13:14
@mromaszewicz

Copy link
Copy Markdown
Member

You have two unrelated changes here - using modern packages, and the error compile time check.

I would very happily merge the first part, as-is, however, since we are a code generator, we can ensure that the generated code always implements error, without having a compile time check for our own output. I don't think we need that second part.

You've also only added that to the stdhttp server, and we try to keep as much parity between generated code as possible.

@gaiaz-iusipov

Copy link
Copy Markdown
Contributor Author

Thanks. This kind of compile-time check is used in various Go projects as a guarantee that certain types implement an interface and that this behavior won’t accidentally break in the future. But I agree that it’s better if I remove those changes and keep only the set that introduces the use of the standard package.

@mromaszewicz

Copy link
Copy Markdown
Member

@greptileai

@greptile-apps

greptile-apps Bot commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR replaces hand-rolled helper wrappers (StringInArray, MergeImports, sliceContains, stringInSlice) and manual map/slice iteration patterns with idiomatic stdlib equivalents from maps, slices, cmp, and strings (all available since Go 1.22–1.24, consistent with the module's go 1.24.3 requirement). Additional clean-ups include using strings.SplitSeq iterators, for range N loops, pre-sized allocations, removing == false patterns, and embedding http.Handler in the generated ServeMux interface.

Confidence Score: 5/5

Safe to merge — all changes are pure style simplifications with no behavioral differences.

Every substitution is semantically equivalent: maps.Copy mirrors the removed MergeImports loop, slices.Contains mirrors StringInArray/sliceContains, slices.SortFunc+cmp.Compare mirrors sort.Slice, and the HasMaskedRequestContentTypes pointer-receiver change is consistent with every other method on OperationDefinition. No logic paths change.

No files require special attention.

Important Files Changed

Filename Overview
pkg/codegen/codegen.go Replaces manual map/slice iteration patterns with slices/maps stdlib functions and removes MergeImports wrapper calls in favour of maps.Copy.
pkg/codegen/gather.go Adopts slices.Concat, slices.SortFunc/cmp.Compare, pre-sized slice allocation, and adds compile-time fmt.Stringer interface assertions for SchemaPath and SchemaContext.
pkg/codegen/merge_schemas.go Replaces manual double-loop map merges with maps.Copy and pre-sized map allocations for Extensions and Properties.
pkg/codegen/merge_schemas_v1.go Replaces StringInArray with slices.Contains.
pkg/codegen/operations.go Modernises with slices/cmp/maps, converts HasMaskedRequestContentTypes to pointer receiver (consistent with all other OperationDefinition methods), uses strings.SplitSeq iterator, pre-sizes slices/maps, and removes == false anti-patterns.
pkg/codegen/prune.go Removes trivial stringInSlice wrapper; calls slices.Contains directly.
pkg/codegen/resolve_names.go Replaces counted for loops with idiomatic for range forms where the loop variable was unused.
pkg/codegen/schema.go Replaces StringInArray/sliceContains with slices.Contains, switches strings.Split to strings.SplitSeq iterator, removes == false anti-patterns.
pkg/codegen/template_helpers.go Replaces all StringInArray calls with slices.Contains.
pkg/codegen/templates/stdhttp/std-http-handler.tmpl Embeds http.Handler in ServeMux interface instead of the explicit ServeHTTP method declaration; updates doc comment to use Go doc link syntax.
pkg/codegen/typemapping.go Removes ineffective omitempty from JSON struct-field tags (structs are never omitted by encoding/json), replaces manual map-copy loops with maps.Copy.
pkg/codegen/utils.go Removes StringInArray, MergeImports, and sliceContains wrapper functions; simplifies SortedSecuritySchemeKeys with slices.Collect(maps.Keys(...)); replaces sort.Slice with slices.SortFunc+cmp.Or.

Reviews (1): Last reviewed commit: "Merge branch 'main' into modernize" | Re-trigger Greptile

@mromaszewicz

Copy link
Copy Markdown
Member

Thank you!

@mromaszewicz
mromaszewicz merged commit 8822002 into oapi-codegen:main Apr 3, 2026
19 checks passed
@gaiaz-iusipov
gaiaz-iusipov deleted the modernize branch April 3, 2026 06:30
@jamietanna jamietanna added the chore Any maintenance tasks that are regular, not as important to call out in the changelog label May 1, 2026
@mromaszewicz mromaszewicz changed the title style: simplify and modernize Replace old hand written slice sorting with slices package, and other built-in helpers May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Any maintenance tasks that are regular, not as important to call out in the changelog internal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants