Skip to content

Commit

Permalink
Fix: Add support for enums defined in message
Browse files Browse the repository at this point in the history
Proto allows for an enum to be defined within a Message. Register nested
enums in the type repo so they can be referenced but not created as
types.
  • Loading branch information
ikstewa authored and dorner committed May 1, 2024
1 parent f9e45cf commit 08c1411
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 8 deletions.
19 changes: 11 additions & 8 deletions avro/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

type Record struct {
Name string
Name string
Namespace string
Fields []Field
Fields []Field
}

func (t Record) GetName() string {
Expand Down Expand Up @@ -61,13 +61,13 @@ func RecordFromProto(proto *descriptorpb.DescriptorProto, namespace string, type
var fields []Field
oneofs := make([]Field, len(proto.OneofDecl))
nested := make([]NamedType, len(proto.NestedType))
// enums := make([]Enum, len(proto.EnumType))
enums := make([]Enum, len(proto.EnumType))
for i, field := range proto.NestedType {
nested[i] = RecordFromProto(field, fmt.Sprintf("%s.%s", namespace, proto.GetName()), typeRepo)[0]
}
//for i, field := range proto.EnumType {
// enums[i] = EnumFromProto(field)
//}
for i, field := range proto.EnumType {
enums[i] = EnumFromProto(field, fmt.Sprintf("%s.%s", namespace, proto.GetName()))
}
for i, oneof := range proto.OneofDecl {
oneofs[i] = Field{
Name: oneof.GetName(),
Expand Down Expand Up @@ -102,10 +102,13 @@ func RecordFromProto(proto *descriptorpb.DescriptorProto, namespace string, type
for _, subRecord := range nested {
types = append(types, subRecord)
}
for _, enum := range enums {
types = append(types, enum)
}
types = append(types, Record{
Name: proto.GetName(),
Name: proto.GetName(),
Namespace: namespace,
Fields: fields,
Fields: fields,
})
return types
}
13 changes: 13 additions & 0 deletions testdata/base/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@
"items": "testdata.StringList"
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
13 changes: 13 additions & 0 deletions testdata/base/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@
"items": "testdata.StringList"
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
Expand Down
13 changes: 13 additions & 0 deletions testdata/collapse_fields/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@
}
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
13 changes: 13 additions & 0 deletions testdata/collapse_fields/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@
}
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
Expand Down
13 changes: 13 additions & 0 deletions testdata/emit_only/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@
"items": "testdata.StringList"
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
Expand Down
5 changes: 5 additions & 0 deletions testdata/foobar.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ message Yowza {
}

message Foobar {
enum NestedEnum {
A = 0;
B = 1;
}
string name = 1;
Blarp blarp = 2;
Yowza yowza = 3;
Expand All @@ -32,4 +36,5 @@ message Foobar {
map<bool, Yowza> a_yowza_map = 13;
map<string, StringList> string_list_map = 14;
repeated StringList string_lists = 15;
NestedEnum nested_enum = 16;
}
13 changes: 13 additions & 0 deletions testdata/namespace_map/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@
"items": "mynamespace.StringList"
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
13 changes: 13 additions & 0 deletions testdata/namespace_map/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@
"items": "mynamespace.StringList"
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
Expand Down
13 changes: 13 additions & 0 deletions testdata/preserve_non_string_maps/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@
"items": "testdata.StringList"
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
13 changes: 13 additions & 0 deletions testdata/preserve_non_string_maps/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@
"items": "testdata.StringList"
},
"default": []
},
{
"name": "nested_enum",
"type": {
"type": "enum",
"name": "NestedEnum",
"symbols": [
"A",
"B"
],
"default": "A"
},
"default": "A"
}
]
}
Expand Down

0 comments on commit 08c1411

Please sign in to comment.