-
-
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 #95 from jmattheis/try-underlying-named-types
feat: `useUnderlyingTypeMethods`
- Loading branch information
Showing
13 changed files
with
349 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package builder | ||
|
||
import ( | ||
"github.com/dave/jennifer/jen" | ||
"github.com/jmattheis/goverter/xtype" | ||
) | ||
|
||
// UseUnderlyingTypeMethods handles UseUnderlyingTypeMethods. | ||
type UseUnderlyingTypeMethods struct{} | ||
|
||
// Matches returns true, if the builder can create handle the given types. | ||
func (*UseUnderlyingTypeMethods) Matches(ctx *MethodContext, source, target *xtype.Type) bool { | ||
if !ctx.Conf.UseUnderlyingTypeMethods { | ||
return false | ||
} | ||
|
||
sourceUnderlying, targetUnderlying := findUnderlyingExtendMapping(ctx, source, target) | ||
return sourceUnderlying || targetUnderlying | ||
} | ||
|
||
// Build creates conversion source code for the given source and target type. | ||
func (*UseUnderlyingTypeMethods) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type) ([]jen.Code, *xtype.JenID, *Error) { | ||
sourceUnderlying, targetUnderlying := findUnderlyingExtendMapping(ctx, source, target) | ||
|
||
innerSource := source | ||
innerTarget := target | ||
|
||
if sourceUnderlying { | ||
innerSource = xtype.TypeOf(source.NamedType.Underlying()) | ||
sourceID = xtype.OtherID(innerSource.TypeAsJen().Call(sourceID.Code)) | ||
} | ||
|
||
if targetUnderlying { | ||
innerTarget = xtype.TypeOf(target.NamedType.Underlying()) | ||
} | ||
|
||
stmt, id, err := gen.Build(ctx, sourceID, innerSource, innerTarget, NoWrap) | ||
if err != nil { | ||
return nil, nil, err.Lift(&Path{ | ||
SourceID: "*", | ||
SourceType: innerSource.T.String(), | ||
TargetID: "*", | ||
TargetType: innerTarget.T.String(), | ||
}) | ||
} | ||
|
||
if targetUnderlying { | ||
id = xtype.OtherID(target.TypeAsJen().Call(id.Code)) | ||
} | ||
|
||
return stmt, id, err | ||
} | ||
|
||
func findUnderlyingExtendMapping(ctx *MethodContext, source, target *xtype.Type) (underlyingSource, underlyingTarget bool) { | ||
if source.Named { | ||
if ctx.HasMethod(source.NamedType.Underlying(), target.NamedType) { | ||
return true, false | ||
} | ||
|
||
if target.Named && ctx.HasMethod(source.NamedType.Underlying(), target.NamedType.Underlying()) { | ||
return true, true | ||
} | ||
} | ||
|
||
if target.Named && ctx.HasMethod(source.NamedType, target.NamedType.Underlying()) { | ||
return false, true | ||
} | ||
|
||
return false, false | ||
} |
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,70 @@ | ||
`useUnderlyingTypeMethods [yes,no]` is a | ||
[boolean setting](config/define.md#boolean) and can be defined as | ||
[CLI argument](config/define.md#cli), | ||
[converter comment](config/define.md#converter) or | ||
[method comment](config/define.md#method). This setting is | ||
[inheritable](config/define.md#inheritance). | ||
|
||
For each type conversion, goverter will check if there is an existing method | ||
that can be used. For named types only the type itself will be checked and not | ||
the underlying type. For this type: | ||
|
||
```go | ||
type InputID int | ||
``` | ||
|
||
`InputID` would be the _named_ type and `int` the _underlying_ type. | ||
|
||
With `useUnderlyingTypeMethods`, goverter will check all named/underlying | ||
combinations. | ||
|
||
- named -> underlying | ||
- underlying -> named | ||
- underlying -> underlying | ||
|
||
<!-- tabs:start --> | ||
|
||
#### **input.go** | ||
|
||
```go | ||
package example | ||
|
||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
type Converter interface { | ||
// goverter:useUnderlyingTypeMethods | ||
Convert(source Input) Output | ||
} | ||
func ConvertUnderlying(s int) string { | ||
return "" | ||
} | ||
// these would be used too | ||
// func ConvertUnderlying(s int) OutputID | ||
// func ConvertUnderlying(s InputID) string | ||
|
||
type InputID int | ||
type OutputID string | ||
type Input struct { ID InputID } | ||
type Output struct { ID OutputID } | ||
``` | ||
|
||
#### **generated/generated.go** | ||
|
||
```go | ||
package generated | ||
|
||
import example "goverter/example" | ||
|
||
type ConverterImpl struct{} | ||
|
||
func (c *ConverterImpl) Convert(source example.Input) example.Output { | ||
var output example.Output | ||
output.ID = example.OutputID(example.ConvertUnderlying(int(source.ID))) | ||
return output | ||
} | ||
``` | ||
|
||
<!-- tabs:end --> | ||
|
||
Without the setting only the custom method with this signature could be used | ||
for the conversion. `func ConvertUnderlying(s InputID) OutputID` |
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,35 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
type Converter interface { | ||
// goverter:useUnderlyingTypeMethods | ||
Convert(source Input) Output | ||
} | ||
func ConvertUnderlying(s int) string { | ||
return "" | ||
} | ||
type InputID int | ||
type OutputID string | ||
type Input struct { ID InputID } | ||
type Output struct { ID OutputID } | ||
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 structsOutput execution.Output | ||
structsOutput.ID = execution.OutputID(execution.ConvertUnderlying(int(source.ID))) | ||
return structsOutput | ||
} |
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,48 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
type Converter interface { | ||
// goverter:useUnderlyingTypeMethods | ||
Convert(source Input) Output | ||
} | ||
func ConvertUnderlying(s int) (string, error) { | ||
return "", nil | ||
} | ||
type InputID int | ||
type OutputID string | ||
type Input struct { ID InputID } | ||
type Output struct { ID OutputID } | ||
error: |- | ||
Error while creating converter method: | ||
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output | ||
| github.com/jmattheis/goverter/execution.Input | ||
| | ||
| | github.com/jmattheis/goverter/execution.InputID | ||
| | | ||
| | | int | ||
| | | | ||
source.ID* | ||
target.ID* | ||
| | | | ||
| | | string | ||
| | | ||
| | github.com/jmattheis/goverter/execution.OutputID | ||
| | ||
| github.com/jmattheis/goverter/execution.Output | ||
ReturnTypeMismatch: Cannot use | ||
func github.com/jmattheis/goverter/execution.ConvertUnderlying(s int) (string, error) | ||
in | ||
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output | ||
because no error is returned as second return parameter |
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,35 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
// goverter:useUnderlyingTypeMethods | ||
type Converter interface { | ||
Convert(source Input) Output | ||
} | ||
func ConvertUnderlying(s int) string { | ||
return "" | ||
} | ||
type InputID int | ||
type OutputID string | ||
type Input struct { ID InputID } | ||
type Output struct { ID OutputID } | ||
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 structsOutput execution.Output | ||
structsOutput.ID = execution.OutputID(execution.ConvertUnderlying(int(source.ID))) | ||
return structsOutput | ||
} |
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,35 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
type Converter interface { | ||
// goverter:useUnderlyingTypeMethods | ||
Convert(source Input) Output | ||
} | ||
func ConvertUnderlying(s int) OutputID { | ||
return "" | ||
} | ||
type InputID int | ||
type OutputID string | ||
type Input struct { ID InputID } | ||
type Output struct { ID OutputID } | ||
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 structsOutput execution.Output | ||
structsOutput.ID = execution.ConvertUnderlying(int(source.ID)) | ||
return structsOutput | ||
} |
Oops, something went wrong.