@@ -19,11 +19,11 @@ import (
1919
2020type addStructField struct {
2121 Name string
22- TypeName * typed.NamedType
22+ TypeExpr typed.Type
2323}
2424
2525// AddStructField adds a new synthetic field at the tail end of a struct declaration.
26- func AddStructField (fieldName string , fieldType * typed.NamedType ) * addStructField {
26+ func AddStructField (fieldName string , fieldType typed.Type ) * addStructField {
2727 return & addStructField {fieldName , fieldType }
2828}
2929
@@ -44,24 +44,28 @@ func (a *addStructField) Apply(ctx context.AdviceContext) (bool, error) {
4444
4545 typeDef .Fields .List = append (typeDef .Fields .List , & dst.Field {
4646 Names : []* dst.Ident {dst .NewIdent (a .Name )},
47- Type : a .TypeName .AsNode (),
47+ Type : a .TypeExpr .AsNode (),
4848 })
4949
50- if importPath := a .TypeName .ImportPath ; importPath != "" {
51- // If the type name is qualified, we may need to import the package, too.
52- _ = ctx .AddImport (importPath , inferPkgName (importPath ))
50+ if namedType , err := typed .ExtractNamedType (a .TypeExpr ); err == nil {
51+ if importPath := namedType .ImportPath ; importPath != "" {
52+ // If the type name is qualified, we may need to import the package, too.
53+ _ = ctx .AddImport (importPath , inferPkgName (importPath ))
54+ }
5355 }
5456
5557 return true , nil
5658}
5759
5860func (a * addStructField ) Hash (h * fingerprint.Hasher ) error {
59- return h .Named ("add-struct-field" , fingerprint .String (a .Name ), a .TypeName )
61+ return h .Named ("add-struct-field" , fingerprint .String (a .Name ), a .TypeExpr )
6062}
6163
6264func (a * addStructField ) AddedImports () []string {
63- if path := a .TypeName .ImportPath ; path != "" {
64- return []string {path }
65+ if namedType , err := typed .ExtractNamedType (a .TypeExpr ); err == nil {
66+ if path := namedType .ImportPath ; path != "" {
67+ return []string {path }
68+ }
6569 }
6670 return nil
6771}
@@ -76,11 +80,12 @@ func init() {
7680 if err := yaml .NodeToValueContext (ctx , node , & spec ); err != nil {
7781 return nil , err
7882 }
79- namedType , err := typed .NewNamedType (spec .Type )
83+ // Use NewType instead of NewNamedType to preserve pointer information
84+ typeExpr , err := typed .NewType (spec .Type )
8085 if err != nil {
8186 return nil , fmt .Errorf ("invalid type %q: %w" , spec .Type , err )
8287 }
8388
84- return AddStructField (spec .Name , namedType ), nil
89+ return AddStructField (spec .Name , typeExpr ), nil
8590 }
8691}
0 commit comments