Skip to content

Commit 528f500

Browse files
fdymyljaaaronc
andauthored
add: support nested messages (#36)
* 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>
1 parent f005f0a commit 528f500

File tree

5 files changed

+1226
-1198
lines changed

5 files changed

+1226
-1198
lines changed

features/fastreflection/descriptors.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fastreflection
33
import (
44
"fmt"
55
"github.com/cosmos/cosmos-proto/generator"
6+
"google.golang.org/protobuf/reflect/protoreflect"
67

78
"github.com/cosmos/cosmos-proto/features/fastreflection/copied"
89
"google.golang.org/protobuf/compiler/protogen"
@@ -24,7 +25,14 @@ func (g *descGen) generate() {
2425
g.P(")")
2526
g.P("func init() {")
2627
g.P(copied.InitFunctionName(g.file), "()")
27-
g.P(messageDescriptorName(g.message), " = ", g.file.GoDescriptorIdent.GoName, ".Messages().ByName(\"", g.message.Desc.Name(), "\")")
28+
parentMd, ok := g.message.Desc.Parent().(protoreflect.MessageDescriptor)
29+
switch ok {
30+
// case nested message
31+
case true:
32+
g.P(messageDescriptorName(g.message), " = ", g.file.GoDescriptorIdent.GoName, ".Messages().ByName(\"", parentMd.Name(), "\").Messages().ByName(\"", g.message.Desc.Name(), "\")")
33+
default:
34+
g.P(messageDescriptorName(g.message), " = ", g.file.GoDescriptorIdent.GoName, ".Messages().ByName(\"", g.message.Desc.Name(), "\")")
35+
}
2836
for _, field := range g.message.Fields {
2937
g.P(fieldDescriptorName(field), " = ", messageDescriptorName(g.message), ".Fields().ByName(\"", field.Desc.Name(), "\")")
3038
}

features/fastreflection/proto_message.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ const (
2121
)
2222

2323
func GenProtoMessage(f *protogen.File, g *generator.GeneratedFile, message *protogen.Message) {
24+
genMessage(f, g, message)
25+
// check for message declarations within a message declaration
26+
for _, nested := range message.Messages {
27+
// map entries are defines as messages, but we don't want to generate those.
28+
if nested.Desc.IsMapEntry() {
29+
continue
30+
}
31+
genMessage(f, g, nested)
32+
}
33+
}
34+
35+
func genMessage(f *protogen.File, g *generator.GeneratedFile, message *protogen.Message) {
2436
gen := newGenerator(f, g, message)
2537
gen.generateExtraTypes()
2638
gen.generateReflectionType()

internal/testprotos/test3/fuzz_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestMarshalUnmarshal(t *testing.T) {
2929

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

internal/testprotos/test3/test.proto

+14-13
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import "internal/testprotos/test3/test_import.proto";
1010

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

13-
// NestedMessage should eventually become nested when fast-reflection supports it
14-
message NestedMessage {
15-
int32 a = 1;
16-
TestAllTypes corecursive = 2;
17-
}
13+
message TestAllTypes {
1814

19-
// NestedEnum should eventually become nested when fast-reflection supports it.
20-
enum NestedEnum {
21-
FOO = 0;
22-
BAR = 1;
23-
BAZ = 2;
24-
NEG = -1; // Intentionally negative.
25-
}
15+
// NestedMessage should eventually become nested when fast-reflection supports it
16+
message NestedMessage {
17+
int32 a = 1;
18+
TestAllTypes corecursive = 2;
19+
}
20+
21+
// NestedEnum should eventually become nested when fast-reflection supports it.
22+
enum NestedEnum {
23+
FOO = 0;
24+
BAR = 1;
25+
BAZ = 2;
26+
NEG = -1; // Intentionally negative.
27+
}
2628

27-
message TestAllTypes {
2829

2930
int32 singular_int32 = 81;
3031
int64 singular_int64 = 82;

0 commit comments

Comments
 (0)