Tweak generators and tests for latest gengo

Required a couple tweaks to generators
This commit is contained in:
Tim Hockin 2024-08-13 09:42:52 -07:00 committed by Jefftree
parent 40f3453ace
commit 5fd8ee21a3
4 changed files with 101 additions and 71 deletions

View File

@ -158,16 +158,16 @@ func getManualConversionFunctions(context *generator.Context, pkg *types.Package
klog.V(6).Infof("%s has a receiver", f.Name)
continue
}
if len(signature.Parameters) != 3 || signature.Parameters[2].Name != scopeName {
if len(signature.Parameters) != 3 || signature.Parameters[2].Type.Name != scopeName {
klog.V(6).Infof("%s has wrong parameters", f.Name)
continue
}
if len(signature.Results) != 1 || signature.Results[0].Name != errorName {
if len(signature.Results) != 1 || signature.Results[0].Type.Name != errorName {
klog.V(6).Infof("%s has wrong results", f.Name)
continue
}
inType := signature.Parameters[0]
outType := signature.Parameters[1]
inType := signature.Parameters[0].Type
outType := signature.Parameters[1].Type
if inType.Kind != types.Pointer || outType.Kind != types.Pointer {
klog.V(6).Infof("%s has wrong parameter types", f.Name)
continue

View File

@ -277,8 +277,8 @@ func deepCopyMethod(t *types.Type) (*types.Signature, error) {
return nil, fmt.Errorf("type %v: invalid DeepCopy signature, expected exactly one result", t)
}
ptrResult := f.Signature.Results[0].Kind == types.Pointer && f.Signature.Results[0].Elem.Name == t.Name
nonPtrResult := f.Signature.Results[0].Name == t.Name
ptrResult := f.Signature.Results[0].Type.Kind == types.Pointer && f.Signature.Results[0].Type.Elem.Name == t.Name
nonPtrResult := f.Signature.Results[0].Type.Name == t.Name
if !ptrResult && !nonPtrResult {
return nil, fmt.Errorf("type %v: invalid DeepCopy signature, expected to return %s or *%s", t, t.Name.Name, t.Name.Name)
@ -329,7 +329,7 @@ func deepCopyIntoMethod(t *types.Type) (*types.Signature, error) {
return nil, fmt.Errorf("type %v: invalid DeepCopy signature, expected no result type", t)
}
ptrParam := f.Signature.Parameters[0].Kind == types.Pointer && f.Signature.Parameters[0].Elem.Name == t.Name
ptrParam := f.Signature.Parameters[0].Type.Kind == types.Pointer && f.Signature.Parameters[0].Type.Elem.Name == t.Name
if !ptrParam {
return nil, fmt.Errorf("type %v: invalid DeepCopy signature, expected parameter of type *%s", t, t.Name.Name)
@ -696,7 +696,7 @@ func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
leftPointer := ut.Elem.Kind == types.Pointer
rightPointer := !isReference(ut.Elem)
if dc != nil {
rightPointer = dc.Results[0].Kind == types.Pointer
rightPointer = dc.Results[0].Type.Kind == types.Pointer
}
if leftPointer == rightPointer {
sw.Do("(*out)[key] = val.DeepCopy()\n", nil)
@ -812,7 +812,7 @@ func (g *genDeepCopy) doStruct(t *types.Type, sw *generator.SnippetWriter) {
leftPointer := ft.Kind == types.Pointer
rightPointer := !isReference(ft)
if dc != nil {
rightPointer = dc.Results[0].Kind == types.Pointer
rightPointer = dc.Results[0].Type.Kind == types.Pointer
}
if leftPointer == rightPointer {
sw.Do("out.$.name$ = in.$.name$.DeepCopy()\n", args)
@ -866,7 +866,7 @@ func (g *genDeepCopy) doPointer(t *types.Type, sw *generator.SnippetWriter) {
case dc != nil || dci != nil:
rightPointer := !isReference(ut.Elem)
if dc != nil {
rightPointer = dc.Results[0].Kind == types.Pointer
rightPointer = dc.Results[0].Type.Kind == types.Pointer
}
if rightPointer {
sw.Do("*out = (*in).DeepCopy()\n", nil)

View File

@ -52,8 +52,8 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{},
},
},
},
@ -74,8 +74,8 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{},
},
},
},
@ -97,11 +97,10 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
Type: &types.Type{Name: types.Name{Name: "int"}, Kind: types.Builtin},
},
},
},
@ -125,11 +124,13 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Type: &types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
},
},
},
@ -150,11 +151,13 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{},
Results: []*types.Type{
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Type: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
},
},
@ -175,11 +178,13 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{},
Results: []*types.Type{
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Type: &types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
},
},
},
@ -202,11 +207,13 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Type: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
},
},
@ -229,16 +236,20 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
Parameters: []*types.ParamResult{
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
Type: &types.Type{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
},
},
},
Results: []*types.Type{
Results: []*types.ParamResult{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Type: &types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
},
},
},
@ -262,15 +273,19 @@ func Test_deepCopyMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Type: &types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
},
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
Type: &types.Type{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
},
},
},
},
@ -323,8 +338,8 @@ func Test_deepCopyIntoMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{},
},
},
},
@ -345,8 +360,8 @@ func Test_deepCopyIntoMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
Parameters: []*types.ParamResult{},
Results: []*types.ParamResult{},
},
},
},
@ -368,16 +383,20 @@ func Test_deepCopyIntoMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
Parameters: []*types.ParamResult{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Type: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
},
Results: []*types.Type{
Results: []*types.ParamResult{
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
Type: &types.Type{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
},
},
},
},
@ -401,10 +420,13 @@ func Test_deepCopyIntoMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.ParamResult{
{
Type: &types.Type{
Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
Results: []*types.Type{},
Results: []*types.ParamResult{},
},
},
},
@ -423,10 +445,14 @@ func Test_deepCopyIntoMethod(t *testing.T) {
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{
{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.ParamResult{
{
Type: &types.Type{
Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
Results: []*types.Type{},
Results: []*types.ParamResult{},
},
},
},
@ -445,13 +471,15 @@ func Test_deepCopyIntoMethod(t *testing.T) {
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{
Parameters: []*types.ParamResult{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Type: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
},
Results: []*types.Type{},
Results: []*types.ParamResult{},
},
},
},
@ -472,13 +500,15 @@ func Test_deepCopyIntoMethod(t *testing.T) {
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
Parameters: []*types.ParamResult{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Type: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
},
Results: []*types.Type{},
Results: []*types.ParamResult{},
},
},
},

View File

@ -165,7 +165,7 @@ func getManualDefaultingFunctions(context *generator.Context, pkg *types.Package
if len(signature.Results) != 0 {
continue
}
inType := signature.Parameters[0]
inType := signature.Parameters[0].Type
if inType.Kind != types.Pointer {
continue
}