mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
go-to-protobuf: fix rewrite of embedded struct fields
The protobuf generator cannot handle embedded struct fields correctly. When using an embedded field by pointer it produces the error message `unable to get name for tag from struct "...", field ...` The reason is that the name determination evaluating the AST does not handle pointers if the AST does not already contain a field name. This seems to be the case for structs embedded by pointers. Example: ``` type MyStruct struct { *OtherStruct `protobuf:"bytes,1,opt,name=otherStruct"` } ```
This commit is contained in:
parent
7f7378eddf
commit
3c2de633d4
@ -375,6 +375,21 @@ func RewriteTypesWithProtobufStructTags(name string, structTags map[string]map[s
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getFieldName(expr ast.Expr, structname string) (name string, err error) {
|
||||||
|
for {
|
||||||
|
switch t := expr.(type) {
|
||||||
|
case *ast.Ident:
|
||||||
|
return t.Name, nil
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
return t.Sel.Name, nil
|
||||||
|
case *ast.StarExpr:
|
||||||
|
expr = t.X
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("unable to get name for tag from struct %q, field %#v", structname, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func updateStructTags(decl ast.Decl, structTags map[string]map[string]string, toCopy []string) []error {
|
func updateStructTags(decl ast.Decl, structTags map[string]map[string]string, toCopy []string) []error {
|
||||||
var errs []error
|
var errs []error
|
||||||
t, ok := decl.(*ast.GenDecl)
|
t, ok := decl.(*ast.GenDecl)
|
||||||
@ -403,14 +418,11 @@ func updateStructTags(decl ast.Decl, structTags map[string]map[string]string, to
|
|||||||
for i := range st.Fields.List {
|
for i := range st.Fields.List {
|
||||||
f := st.Fields.List[i]
|
f := st.Fields.List[i]
|
||||||
var name string
|
var name string
|
||||||
|
var err error
|
||||||
if len(f.Names) == 0 {
|
if len(f.Names) == 0 {
|
||||||
switch t := f.Type.(type) {
|
name, err = getFieldName(f.Type, spec.Name.Name)
|
||||||
case *ast.Ident:
|
if err != nil {
|
||||||
name = t.Name
|
errs = append(errs, err)
|
||||||
case *ast.SelectorExpr:
|
|
||||||
name = t.Sel.Name
|
|
||||||
default:
|
|
||||||
errs = append(errs, fmt.Errorf("unable to get name for tag from struct %q, field %#v", spec.Name.Name, t))
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user