Skip to content

Commit

Permalink
Merge pull request #139 from jmattheis/panic-typeparams
Browse files Browse the repository at this point in the history
fix: panic with type params
  • Loading branch information
jmattheis authored Feb 22, 2024
2 parents 3b5f52f + 2ffa961 commit 7deef0f
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func parseMethodLine(ctx *context, c *Converter, m *Method, value string) (err e
OutputPackagePath: c.OutputPackagePath,
Converter: c.Type,
Params: method.ParamsOptional,
AllowTypeParams: true,
}
f.Function, err = ctx.Loader.GetOne(c.Package, custom, opts)
}
Expand Down Expand Up @@ -129,6 +130,7 @@ func parseMethodLine(ctx *context, c *Converter, m *Method, value string) (err e
OutputPackagePath: c.OutputPackagePath,
Converter: c.Type,
Params: method.ParamsOptional,
AllowTypeParams: true,
}
m.Constructor, err = ctx.Loader.GetOne(c.Package, rest, opts)
default:
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import GH from './GH.vue';
- Add [`wrapErrorsUsing`](./reference/wrapErrorsUsing.md)
- Add [`enum`](./reference/enum.md), See [Guide: Enums](guide/enum.md)
- Fix error messages when there is an return error mismatch
- Fix panic when using type params in [`extend`](./reference/extend),
[`map`](./reference/map) or [`default`](./reference/default).

_internals_:

Expand Down
4 changes: 2 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ func (g *generator) CallMethod(
if definition.Source != nil {
params = append(params, sourceID.Code.Clone())

if !source.AssignableTo(definition.Source) {
if !source.AssignableTo(definition.Source) && !definition.TypeParams {
cause := fmt.Sprintf("Method source type mismatches with conversion source: %s != %s", definition.Source.String, source.String)
return nil, nil, formatErr(cause)
}
}

if !definition.Target.AssignableTo(target) {
if !definition.Target.AssignableTo(target) && !definition.TypeParams {
cause := fmt.Sprintf("Method return type mismatches with target: %s != %s", definition.Target.String, target.String)
return nil, nil, formatErr(cause)
}
Expand Down
1 change: 1 addition & 0 deletions method/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (m *Definition) Signature() xtype.Signature {
type Parameters struct {
ReturnError bool
SelfAsFirstParameter bool
TypeParams bool
Source *xtype.Type
Target *xtype.Type
}
12 changes: 9 additions & 3 deletions method/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ type ParseOpts struct {
Converter types.Type
OutputPackagePath string

ErrorPrefix string
ConvFunction bool
Params ParamType
ErrorPrefix string
Params ParamType
ConvFunction bool
AllowTypeParams bool
}

// Parse parses an function into a Definition.
Expand Down Expand Up @@ -62,10 +63,15 @@ func Parse(obj types.Object, opts *ParseOpts) (*Definition, error) {
Parameters: Parameters{
ReturnError: returnError,
Target: xtype.TypeOf(sig.Results().At(0).Type()),
TypeParams: sig.TypeParams().Len() > 0,
},
Name: fn.Name(),
}

if methodDef.TypeParams && !opts.AllowTypeParams {
return nil, formatErr("must not be generic")
}

if opts.ConvFunction {
methodDef.Call = jen.Id(xtype.ThisVar).Dot(fn.Name())
} else {
Expand Down
24 changes: 24 additions & 0 deletions scenario/extend_generic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
input:
input.go: |
package structs
// goverter:converter
// goverter:extend Extend
type Converter interface {
Convert(source Input) (Output, error)
}
func Extend[T any](T) int {
return 0
}
type Input struct { Age string }
type Output struct { Age int }
error: |-
error parsing 'goverter:extend' at
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
func github.com/jmattheis/goverter/execution.Extend[T any](T) int
must not be generic
31 changes: 31 additions & 0 deletions scenario/map_custom_generic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
input:
input.go: |
package structs
// goverter:converter
type Converter interface {
// goverter:map ID | ZeroIfNil
Convert(source Input) Output
}
type Input struct { ID *int }
type Output struct { ID int }
func ZeroIfNil[T any](*T) T {
var t T
return t
}
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.ZeroIfNil(source.ID)
return structsOutput
}
2 changes: 2 additions & 0 deletions xtype/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ func TypeOf(t types.Type) *Type {
case *types.Signature:
rt.Signature = true
rt.SignatureType = value
case *types.TypeParam:
// ignore
default:
panic("unknown types.Type " + t.String())
}
Expand Down

0 comments on commit 7deef0f

Please sign in to comment.