Skip to content
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

Type aliasing #232

Merged
merged 3 commits into from Jan 29, 2021
Merged

Type aliasing #232

merged 3 commits into from Jan 29, 2021

Conversation

natsukagami
Copy link
Contributor

@natsukagami natsukagami commented Sep 21, 2020

Some types are unnecessary wrapped, for example

type AddPetJSONRequestBody Pet

which lengthens the code needed to create and handle such types.

I added an option, called -alias-types, that changes all types satisfying:

type A struct { /* ... */ } // unchanged
type B string // native type wrappers are unchanged, they are useful for e.g. enums
type C = A  // Direct wrappers of another defined type is aliased
type D = []A // Array of referenced types are aliased
type E []int // Array of non-referenced types are not aliased

This change is Reviewable

@@ -1,6 +1,6 @@
{{range .}}{{$opid := .OperationId}}
{{range .TypeDefinitions}}
// {{.TypeName}} defines parameters for {{$opid}}.
type {{.TypeName}} {{.Schema.TypeDecl}}
type {{.TypeName}} {{if and (opts.AliasTypes) (.CanAlias)}}={{end}} {{.Schema.TypeDecl}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if opts.AliasTypes=faalse or .CanAlias=false, you will generate an empty type alias, no?

type TypeName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one of them is false, the type will be
type TypeName .Schema.TypeDecl
else it will be
type TypeName = .Schema.TypeDecl

@deepmap-marcinr deepmap-marcinr merged commit e51f8ea into deepmap:master Jan 29, 2021
adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
* Add aliasable types logic and option

* Update templates

* Re-generate examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants