-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from jmattheis/alias
Alias
- Loading branch information
Showing
15 changed files
with
162 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Use-Cases | ||
|
||
Goverter generates boilerplate code for you to convert types to other types. | ||
This can be useful if you have types representing similar data. Use-Cases can | ||
be: | ||
|
||
[[toc]] | ||
|
||
## Converting between database and API types | ||
|
||
Sometimes you have different database and API types because they are: | ||
|
||
* Generated with e.g. via [sqlc-dev/sqlc](https://github.com/sqlc-dev/sqlc), | ||
[volatiletech/sqlboiler](https://github.com/volatiletech/sqlboiler) | ||
[99designs/gqlgen](https://github.com/99designs/gqlgen), | ||
[protobuf](https://protobuf.dev/getting-started/gotutorial/) or | ||
[deepmap/oapi-codegen](https://github.com/deepmap/oapi-codegen) | ||
* or because the database ORM requires different typing than the api types | ||
* or because the database models are just different from the API because the | ||
API must stay backwards compatible | ||
|
||
Given any of these reasons it may be useful to generate the conversion via | ||
goverter because it validates that all fields are mapped and reduces the need | ||
for manually converting these types. | ||
|
||
## Converting between API versions | ||
|
||
When you have to support older API version you could structure you app so it | ||
always uses the latest api types and then convert the latest api types to older | ||
api types via Goverter. The types may have a lot of similarities and therefore, | ||
goverter should be able to automatically convert most of the types. | ||
|
||
## Deep copy instances | ||
|
||
Goverter allows you to deep copy types by defining a conversion where the | ||
source and target type is the same. This is useful when you want to adjust a | ||
copy of a instance without affecting the source instance. | ||
|
||
This can be done at runtime with a library like | ||
[github.com/jinzhu/copier](ttps://github.com/jinzhu/copier) but using generated | ||
code will be much faster and you'll have a compile-time guarantee that the | ||
types can be coverted. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
input: | ||
input.go: | | ||
package alias | ||
// goverter:converter | ||
type Converter interface { | ||
Convert(source InputAlias) OutputAlias | ||
} | ||
type InputAlias = map[string]Input | ||
type OutputAlias = map[string]Output | ||
type Input struct { Name string } | ||
type Output struct { Name string } | ||
success: | ||
- generated/generated.go: | | ||
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT. | ||
package generated | ||
import execution "github.com/jmattheis/goverter/execution" | ||
type ConverterImpl struct{} | ||
func (c *ConverterImpl) Convert(source map[string]execution.Input) map[string]execution.Output { | ||
var mapStringAliasOutput map[string]execution.Output | ||
if source != nil { | ||
mapStringAliasOutput = make(map[string]execution.Output, len(source)) | ||
for key, value := range source { | ||
mapStringAliasOutput[key] = c.aliasInputToAliasOutput(value) | ||
} | ||
} | ||
return mapStringAliasOutput | ||
} | ||
func (c *ConverterImpl) aliasInputToAliasOutput(source execution.Input) execution.Output { | ||
var aliasOutput execution.Output | ||
aliasOutput.Name = source.Name | ||
return aliasOutput | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
input: | ||
input.go: | | ||
package alias | ||
// goverter:converter | ||
type Converter interface { | ||
Convert(source InputAlias) OutputAlias | ||
} | ||
type InputAlias = Input | ||
type OutputAlias = Output | ||
type Input struct { Name string } | ||
type Output struct { Name string } | ||
success: | ||
- generated/generated.go: | | ||
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT. | ||
package generated | ||
import execution "github.com/jmattheis/goverter/execution" | ||
type ConverterImpl struct{} | ||
func (c *ConverterImpl) Convert(source execution.Input) execution.Output { | ||
var aliasOutput execution.Output | ||
aliasOutput.Name = source.Name | ||
return aliasOutput | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build go1.22 | ||
// +build go1.22 | ||
|
||
package xtype | ||
|
||
import "go/types" | ||
|
||
func Unalias(t types.Type) types.Type { | ||
return types.Unalias(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build !go1.22 | ||
// +build !go1.22 | ||
|
||
package xtype | ||
|
||
import "go/types" | ||
|
||
func Unalias(t types.Type) (types.Type) { | ||
return t | ||
} |