Skip to content

Commit

Permalink
support alias type in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
dnyg committed Sep 11, 2024
1 parent fda0539 commit 5420b8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ func TypeIdentifier(t types.Type) string {
res := ""
for {
switch it := code.Unalias(t).(type) {
case *types.Alias:
return TypeIdentifier(it.Underlying())
case *types.Pointer:
t.Underlying()
res += "ᚖ"
Expand Down
23 changes: 23 additions & 0 deletions codegen/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,26 @@ func TestTypeName(t *testing.T) {
assert.Equal(t, test.expected, result)
}
}

func TestTypeIdentifierWithAlias(t *testing.T) {
// Create a new package
pkg := types.NewPackage("example.com/pkg", "pkg")

// Create a basic type (e.g., string)
basicType := types.Typ[types.String]

// Create an alias type
aliasType := types.NewAlias(types.NewTypeName(0, pkg, "MyAlias", nil), basicType)

// Create a named type that uses the alias
namedType := types.NewNamed(types.NewTypeName(0, pkg, "MyType", nil), aliasType, nil)

// Test the TypeIdentifier function
result := TypeIdentifier(namedType)

// Expected result
// The package path will be encoded, and we expect it to resolve to the underlying type
expected := "exampleᚗcomᚋpkgᚐMyType"

assert.Equal(t, expected, result, "TypeIdentifier should handle alias types correctly")
}

0 comments on commit 5420b8e

Please sign in to comment.