-
-
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.
Try underlying types for named type custom methods
- Loading branch information
Showing
11 changed files
with
294 additions
and
7 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
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" | ||
) | ||
|
||
// TryUnderlyingNamed handles FlagTryUnderlyingNamedType. | ||
type TryUnderlyingNamed struct{} | ||
|
||
// Matches returns true, if the builder can create handle the given types. | ||
func (*TryUnderlyingNamed) Matches(ctx *MethodContext, source, target *xtype.Type) bool { | ||
if !ctx.Flags.Has(FlagTryUnderlyingNamedType) { | ||
return false | ||
} | ||
|
||
sourceUnderlying, targetUnderlying := findUnderlyingExtendMapping(ctx, source, target) | ||
return sourceUnderlying || targetUnderlying | ||
} | ||
|
||
// Build creates conversion source code for the given source and target type. | ||
func (*TryUnderlyingNamed) 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.HasExtend(source.NamedType.Underlying(), target.NamedType) { | ||
return true, false | ||
} | ||
|
||
if target.Named && ctx.HasExtend(source.NamedType.Underlying(), target.NamedType.Underlying()) { | ||
return true, true | ||
} | ||
} | ||
|
||
if target.Named && ctx.HasExtend(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
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,34 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
type Converter interface { | ||
// goverter:tryUnderlyingNamedType | ||
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: | | ||
// 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,34 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
// goverter:tryUnderlyingNamedType | ||
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: | | ||
// 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,34 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
type Converter interface { | ||
// goverter:tryUnderlyingNamedType | ||
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: | | ||
// 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 | ||
} |
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,34 @@ | ||
input: | ||
input.go: | | ||
package structs | ||
// goverter:converter | ||
// goverter:extend ConvertUnderlying | ||
type Converter interface { | ||
// goverter:tryUnderlyingNamedType | ||
Convert(source Input) Output | ||
} | ||
func ConvertUnderlying(s InputID) string { | ||
return "" | ||
} | ||
type InputID int | ||
type OutputID string | ||
type Input struct { ID InputID } | ||
type Output struct { ID OutputID } | ||
success: | | ||
// 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(source.ID)) | ||
return structsOutput | ||
} |