Skip to content

Commit b5a6f18

Browse files
committed
fix: crash with panic: unsupported type any
With never x/tools versions, nested aliases aren't resolved automatically. They have to be handled when rendering them in jennifier.
1 parent 732c090 commit b5a6f18

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ jobs:
3030
files: ./filtered-coverage.txt
3131
env:
3232
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
33+
test_latest_xtools:
34+
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.repository != github.event.pull_request.head.repo.full_name)
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/setup-go@v5
38+
with:
39+
go-version: 1.24.x
40+
- uses: actions/checkout@v4
41+
- run: |
42+
go get -u golang.org/x/tools
43+
go mod tidy
44+
- run: go test ./...
45+
env:
46+
SKIP_VERSION_DEPENDENT: 'true'
3347
test_go122:
3448
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.repository != github.event.pull_request.head.repo.full_name)
3549
runs-on: ubuntu-latest

scenario/nested_alias.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
version_dependent: true
2+
input:
3+
input.go: |
4+
package alias
5+
6+
// goverter:converter
7+
// goverter:extend AnyAny
8+
type Converter interface {
9+
A(source map[string]InputAlias) map[string]OutputAlias
10+
B(source map[string]any) map[string]any
11+
C(source struct{A InputAlias}) struct{A OutputAlias}
12+
D(source []GenAlias[string]) []GenAlias[string]
13+
}
14+
15+
func AnyAny(a any) any {
16+
return a
17+
}
18+
19+
type GenAlias[T any] []T
20+
type InputAlias = string
21+
type OutputAlias = string
22+
success:
23+
- generated/generated.go: |
24+
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
25+
26+
package generated
27+
28+
import execution "github.com/jmattheis/goverter/execution"
29+
30+
type ConverterImpl struct{}
31+
32+
func (c *ConverterImpl) A(source map[string]string) map[string]string {
33+
var mapStringString map[string]string
34+
if source != nil {
35+
mapStringString = make(map[string]string, len(source))
36+
for key, value := range source {
37+
mapStringString[key] = value
38+
}
39+
}
40+
return mapStringString
41+
}
42+
func (c *ConverterImpl) B(source map[string]interface{}) map[string]interface{} {
43+
var mapStringUnknown map[string]interface{}
44+
if source != nil {
45+
mapStringUnknown = make(map[string]interface{}, len(source))
46+
for key, value := range source {
47+
mapStringUnknown[key] = execution.AnyAny(value)
48+
}
49+
}
50+
return mapStringUnknown
51+
}
52+
func (c *ConverterImpl) C(source struct {
53+
A string
54+
}) struct {
55+
A string
56+
} {
57+
var unnamed struct {
58+
A string
59+
}
60+
unnamed.A = source.A
61+
return unnamed
62+
}
63+
func (c *ConverterImpl) D(source []execution.GenAlias[string]) []execution.GenAlias[string] {
64+
var aliasGenAliasList []execution.GenAlias[string]
65+
if source != nil {
66+
aliasGenAliasList = make([]execution.GenAlias[string], len(source))
67+
for i := 0; i < len(source); i++ {
68+
aliasGenAliasList[i] = c.aliasGenAliasToAliasGenAlias(source[i])
69+
}
70+
}
71+
return aliasGenAliasList
72+
}
73+
func (c *ConverterImpl) aliasGenAliasToAliasGenAlias(source execution.GenAlias[string]) execution.GenAlias[string] {
74+
var aliasGenAlias execution.GenAlias[string]
75+
if source != nil {
76+
aliasGenAlias = make(execution.GenAlias[string], len(source))
77+
for i := 0; i < len(source); i++ {
78+
aliasGenAlias[i] = source[i]
79+
}
80+
}
81+
return aliasGenAlias
82+
}

xtype/tocode.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func toCode(t types.Type) *jen.Statement {
1111
switch cast := t.(type) {
1212
case *types.Named:
13-
return toCodeNamed(cast)
13+
return toCodeNamedLike(cast)
1414
case *types.Map:
1515
return jen.Map(toCode(cast.Key())).Add(toCode(cast.Elem()))
1616
case *types.Slice:
@@ -29,6 +29,8 @@ func toCode(t types.Type) *jen.Statement {
2929
return jen.Func().Add(toCodeSignature(cast))
3030
case *types.Chan:
3131
return toChan(cast)
32+
case *types.Alias:
33+
return toCodeNamedLike(cast)
3234
}
3335
panic("unsupported type " + t.String())
3436
}
@@ -84,7 +86,12 @@ func toCodeSignature(t *types.Signature) *jen.Statement {
8486
return jen.Params(jenParams...).Params(jenResults...)
8587
}
8688

87-
func toCodeNamed(t *types.Named) *jen.Statement {
89+
type namedLike interface {
90+
Obj() *types.TypeName
91+
TypeArgs() *types.TypeList
92+
}
93+
94+
func toCodeNamedLike(t namedLike) *jen.Statement {
8895
name := toCodeObj(t.Obj())
8996

9097
args := t.TypeArgs()

0 commit comments

Comments
 (0)