gopoet icon indicating copy to clipboard operation
gopoet copied to clipboard

Aliased basic type names render unexpectedly

Open gonzojive opened this issue 5 years ago • 2 comments

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.

gonzojive avatar Feb 25 '20 08:02 gonzojive

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?

jhump avatar Mar 03 '20 16:03 jhump

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()       {}

gonzojive avatar Mar 05 '20 05:03 gonzojive