Skip to content

Commit

Permalink
fix: nested descriptors (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdymylja authored Jan 6, 2022
1 parent 8dfac11 commit 42f1f41
Show file tree
Hide file tree
Showing 3 changed files with 2,106 additions and 7 deletions.
22 changes: 15 additions & 7 deletions features/fastreflection/descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ func (g *descGen) generate() {
g.P(")")
g.P("func init() {")
g.P(copied.InitFunctionName(g.file), "()")
parentMd, ok := g.message.Desc.Parent().(protoreflect.MessageDescriptor)
switch ok {
// case nested message
case true:
g.P(messageDescriptorName(g.message), " = ", g.file.GoDescriptorIdent.GoName, ".Messages().ByName(\"", parentMd.Name(), "\").Messages().ByName(\"", g.message.Desc.Name(), "\")")
default:
g.P(messageDescriptorName(g.message), " = ", g.file.GoDescriptorIdent.GoName, ".Messages().ByName(\"", g.message.Desc.Name(), "\")")
// we need to consider nested messages. so we try to recreate the hierarchy
// ex messageA contains messageB contains messageC
args := []interface{}{messageDescriptorName(g.message), " = ", g.file.GoDescriptorIdent.GoName}
for _, md := range findParents(g.message.Desc) {
args = append(args, ".Messages().ByName(\"", md.Name(), "\")")
}
g.P(args...)
for _, field := range g.message.Fields {
g.P(fieldDescriptorName(field), " = ", messageDescriptorName(g.message), ".Fields().ByName(\"", field.Desc.Name(), "\")")
}
Expand All @@ -42,3 +41,12 @@ func (g *descGen) generate() {
func fieldDescriptorName(field *protogen.Field) string {
return fmt.Sprintf("fd_%s_%s", field.Parent.GoIdent.GoName, field.Desc.Name())
}

func findParents(message protoreflect.MessageDescriptor) []protoreflect.MessageDescriptor {
parent, ok := message.Parent().(protoreflect.MessageDescriptor)
if !ok {
return []protoreflect.MessageDescriptor{message}
}

return append(findParents(parent), message)
}
19 changes: 19 additions & 0 deletions internal/testprotos/test3/test_nesting.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";
package goproto.proto.test3;

option go_package = "github.com/cosmos/cosmos-proto/internal/testprotos/test3";

message MultiLayeredNesting {
message Nested1 {
message Nested2 {
message Nested3 {
oneof nested3_oneof {
string nested_3_string = 1;
int32 nested_3_int32 = 2;
}
}
Nested3 nested_3 = 1;
}
}
Nested1 nested1 = 1;
}
Loading

0 comments on commit 42f1f41

Please sign in to comment.