Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliased basic type names render unexpectedly #8

Open
gonzojive opened this issue Feb 25, 2020 · 2 comments
Open

Aliased basic type names render unexpectedly #8

gonzojive opened this issue Feb 25, 2020 · 2 comments

Comments

@gonzojive
Copy link

int32 always seems to render as rune, and the same for uint8 and byte. This may result in compatible code (except for embedded alised types perhaps), but it is odd and makes the code written by gopoet less readable.

@jhump
Copy link
Owner

jhump commented Mar 3, 2020

It certainly won't result in incompatible code: in the Go type system, byte and uint8 are indistinguishable -- they are just aliases for one another. Same goes for int32 and rune.

You'll see they are literally the same value in the "go/types" API. And you'll also note that there is no reflect.Kind value for byte or rune: types defined that way have kinds of reflect.Uint8 and reflect.Int32 when examining them with the "reflect" package.

But I understand the desire to control which of the aliases is used in the resulting code. I wonder if it would suffice to have configuration options passed to WriteGoFile (and its ilk) that could be used to control this. What do you think, @gonzojive?

@gonzojive
Copy link
Author

I would prefer some way to specify the rendering preference on the TypeName, that way you can render a signature like func foo(a rune, b int32).

Maybe something like

type basicTypeName reflect.Kind

var _ TypeName = basicTypeName(0)

func (t basicTypeName) Kind() TypeKind          { return KindBasic }
func (t basicTypeName) Symbol() Symbol          { return Symbol{} }
func (t basicTypeName) Elem() TypeName          { return nil }
func (t basicTypeName) Key() TypeName           { return nil }
func (t basicTypeName) Len() int64              { return -1 }
func (t basicTypeName) Dir() reflect.ChanDir    { return 0 }
func (t basicTypeName) BasicKind() reflect.Kind { return reflect.Kind(t) }
func (t basicTypeName) Signature() *Signature   { return nil }
func (t basicTypeName) Fields() []FieldType     { return nil }
func (t basicTypeName) Methods() []MethodType   { return nil }
func (t basicTypeName) Embeds() []Symbol        { return nil }
func (t basicTypeName) String() string          { return typeNameToString(t) }
func (t basicTypeName) isTypeNameOrSpec()       {}

type basicTypeNameWithRenderingPref struct {
  kind reflect.Kind
  literal string
}

var _ TypeName = basicTypeName(0)

func (t basicTypeNameWithRenderingPref) Kind() TypeKind          { return KindBasic }
func (t basicTypeNameWithRenderingPref) Symbol() Symbol          { return Symbol{} }
func (t basicTypeNameWithRenderingPref) Elem() TypeName          { return nil }
func (t basicTypeNameWithRenderingPref) Key() TypeName           { return nil }
func (t basicTypeNameWithRenderingPref) Len() int64              { return -1 }
func (t basicTypeNameWithRenderingPref) Dir() reflect.ChanDir    { return 0 }
func (t basicTypeNameWithRenderingPref) BasicKind() reflect.Kind { return reflect.Kind(t) }
func (t basicTypeNameWithRenderingPref) Signature() *Signature   { return nil }
func (t basicTypeNameWithRenderingPref) Fields() []FieldType     { return nil }
func (t basicTypeNameWithRenderingPref) Methods() []MethodType   { return nil }
func (t basicTypeNameWithRenderingPref) Embeds() []Symbol        { return nil }
func (t basicTypeNameWithRenderingPref) String() string          { 
if t.literal == "" {
  return basicTypeName(t.kind).String()
}
  return t.literal
 }
func (t basicTypeNameWithRenderingPref) isTypeNameOrSpec()       {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants