Skip to content

Commit 81e96bf

Browse files
authored
Merge pull request kubernetes#90296 from mandelsoft/master
go-to-protobuf: fix rewrite of embedded struct fields
2 parents 45a230a + 52063ca commit 81e96bf

File tree

4 files changed

+141
-7
lines changed

4 files changed

+141
-7
lines changed
8.82 MB
Binary file not shown.

staging/src/k8s.io/code-generator/cmd/go-to-protobuf/protobuf/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ go_test(
3737
srcs = [
3838
"cmd_test.go",
3939
"namer_test.go",
40+
"parser_test.go",
4041
],
4142
embed = [":go_default_library"],
4243
)

staging/src/k8s.io/code-generator/cmd/go-to-protobuf/protobuf/parser.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,21 @@ func RewriteTypesWithProtobufStructTags(name string, structTags map[string]map[s
375375
})
376376
}
377377

378+
func getFieldName(expr ast.Expr, structname string) (name string, err error) {
379+
for {
380+
switch t := expr.(type) {
381+
case *ast.Ident:
382+
return t.Name, nil
383+
case *ast.SelectorExpr:
384+
return t.Sel.Name, nil
385+
case *ast.StarExpr:
386+
expr = t.X
387+
default:
388+
return "", fmt.Errorf("unable to get name for tag from struct %q, field %#v", structname, t)
389+
}
390+
}
391+
}
392+
378393
func updateStructTags(decl ast.Decl, structTags map[string]map[string]string, toCopy []string) []error {
379394
var errs []error
380395
t, ok := decl.(*ast.GenDecl)
@@ -403,14 +418,11 @@ func updateStructTags(decl ast.Decl, structTags map[string]map[string]string, to
403418
for i := range st.Fields.List {
404419
f := st.Fields.List[i]
405420
var name string
421+
var err error
406422
if len(f.Names) == 0 {
407-
switch t := f.Type.(type) {
408-
case *ast.Ident:
409-
name = t.Name
410-
case *ast.SelectorExpr:
411-
name = t.Sel.Name
412-
default:
413-
errs = append(errs, fmt.Errorf("unable to get name for tag from struct %q, field %#v", spec.Name.Name, t))
423+
name, err = getFieldName(f.Type, spec.Name.Name)
424+
if err != nil {
425+
errs = append(errs, err)
414426
continue
415427
}
416428
} else {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package protobuf
18+
19+
import (
20+
"go/ast"
21+
"testing"
22+
)
23+
24+
/*
25+
struct fields in go AST:
26+
27+
type Struct struct {
28+
// fields with a direct field Name as <Ident>
29+
A X // regular fields
30+
B *X // pointer fields
31+
C // embedded type field
32+
33+
// qualified embedded type fields use an <SelExpr> in the AST
34+
v1.TypeMeta // X=v1, Sel=TypeMeta
35+
36+
// fields without a direct name, but
37+
// a <StarExpr> in the go-AST
38+
*D // type field embedded as pointer
39+
*v1.ListMeta // qualified type field embedded as pointer
40+
// with <StarExpr> pointing to <SelExpr>
41+
}
42+
*/
43+
44+
func TestProtoParser(t *testing.T) {
45+
ident := ast.NewIdent("FieldName")
46+
tests := []struct {
47+
expr ast.Expr
48+
err bool
49+
}{
50+
// valid struct field expressions
51+
{
52+
expr: ident,
53+
err: false,
54+
},
55+
{
56+
expr: &ast.SelectorExpr{
57+
Sel: ident,
58+
},
59+
err: false,
60+
},
61+
{
62+
expr: &ast.StarExpr{
63+
X: ident,
64+
},
65+
err: false,
66+
},
67+
{
68+
expr: &ast.StarExpr{
69+
X: &ast.StarExpr{
70+
X: ident,
71+
},
72+
},
73+
err: false,
74+
},
75+
{
76+
expr: &ast.StarExpr{
77+
X: &ast.SelectorExpr{
78+
Sel: ident,
79+
},
80+
},
81+
err: false,
82+
},
83+
84+
// something else should provide an error
85+
{
86+
expr: &ast.KeyValueExpr{
87+
Key: ident,
88+
Colon: 0,
89+
Value: ident,
90+
},
91+
err: true,
92+
},
93+
{
94+
expr: &ast.StarExpr{
95+
X: &ast.KeyValueExpr{
96+
Key: ident,
97+
Colon: 0,
98+
Value: ident,
99+
},
100+
},
101+
err: true,
102+
},
103+
}
104+
105+
for _, test := range tests {
106+
actual, err := getFieldName(test.expr, "Struct")
107+
if !test.err {
108+
if err != nil {
109+
t.Errorf("%s: unexpected error %s", test.expr, err)
110+
} else {
111+
if actual != ident.Name {
112+
t.Errorf("%s: expected %s, got %s", test.expr, ident.Name, actual)
113+
}
114+
}
115+
} else {
116+
if err == nil {
117+
t.Errorf("%s: expected error did not occur, got %s instead", test.expr, actual)
118+
}
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)