Skip to content

Commit

Permalink
add: support nested messages (#36)
Browse files Browse the repository at this point in the history
* add: support nested messages

* Update features/fastreflection/descriptors.go

Co-authored-by: Aaron Craelius <aaron@regen.network>

* chore: document better

Co-authored-by: Aaron Craelius <aaron@regen.network>
  • Loading branch information
fdymylja and aaronc authored Nov 23, 2021
1 parent f005f0a commit 528f500
Show file tree
Hide file tree
Showing 5 changed files with 1,226 additions and 1,198 deletions.
10 changes: 9 additions & 1 deletion features/fastreflection/descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fastreflection
import (
"fmt"
"github.com/cosmos/cosmos-proto/generator"
"google.golang.org/protobuf/reflect/protoreflect"

"github.com/cosmos/cosmos-proto/features/fastreflection/copied"
"google.golang.org/protobuf/compiler/protogen"
Expand All @@ -24,7 +25,14 @@ func (g *descGen) generate() {
g.P(")")
g.P("func init() {")
g.P(copied.InitFunctionName(g.file), "()")
g.P(messageDescriptorName(g.message), " = ", g.file.GoDescriptorIdent.GoName, ".Messages().ByName(\"", g.message.Desc.Name(), "\")")
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(), "\")")
}
for _, field := range g.message.Fields {
g.P(fieldDescriptorName(field), " = ", messageDescriptorName(g.message), ".Fields().ByName(\"", field.Desc.Name(), "\")")
}
Expand Down
12 changes: 12 additions & 0 deletions features/fastreflection/proto_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const (
)

func GenProtoMessage(f *protogen.File, g *generator.GeneratedFile, message *protogen.Message) {
genMessage(f, g, message)
// check for message declarations within a message declaration
for _, nested := range message.Messages {
// map entries are defines as messages, but we don't want to generate those.
if nested.Desc.IsMapEntry() {
continue
}
genMessage(f, g, nested)
}
}

func genMessage(f *protogen.File, g *generator.GeneratedFile, message *protogen.Message) {
gen := newGenerator(f, g, message)
gen.generateExtraTypes()
gen.generateReflectionType()
Expand Down
2 changes: 1 addition & 1 deletion internal/testprotos/test3/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMarshalUnmarshal(t *testing.T) {

// TestZeroValueOneofIsMarshalled tests that zero values in oneofs are marshalled
func TestZeroValueOneofIsMarshalled(t *testing.T) {
msg1 := &TestAllTypes{OneofField: &TestAllTypes_OneofEnum{OneofEnum: NestedEnum_FOO}}
msg1 := &TestAllTypes{OneofField: &TestAllTypes_OneofEnum{OneofEnum: TestAllTypes_FOO}}
b, err := proto.Marshal(msg1)
require.NoError(t, err)

Expand Down
27 changes: 14 additions & 13 deletions internal/testprotos/test3/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import "internal/testprotos/test3/test_import.proto";

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

// NestedMessage should eventually become nested when fast-reflection supports it
message NestedMessage {
int32 a = 1;
TestAllTypes corecursive = 2;
}
message TestAllTypes {

// NestedEnum should eventually become nested when fast-reflection supports it.
enum NestedEnum {
FOO = 0;
BAR = 1;
BAZ = 2;
NEG = -1; // Intentionally negative.
}
// NestedMessage should eventually become nested when fast-reflection supports it
message NestedMessage {
int32 a = 1;
TestAllTypes corecursive = 2;
}

// NestedEnum should eventually become nested when fast-reflection supports it.
enum NestedEnum {
FOO = 0;
BAR = 1;
BAZ = 2;
NEG = -1; // Intentionally negative.
}

message TestAllTypes {

int32 singular_int32 = 81;
int64 singular_int64 = 82;
Expand Down
Loading

0 comments on commit 528f500

Please sign in to comment.