Skip to content

Commit

Permalink
Merge pull request #31 from rkrishnan2012/rk/enum_bug_fix
Browse files Browse the repository at this point in the history
Fix bug resolving enum across multiple files.
  • Loading branch information
rholshausen authored Sep 18, 2023
2 parents d5fcc14 + 2731ee4 commit bd33f05
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
35 changes: 25 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,19 @@ pub fn find_enum_value_by_name(
let package_names = enum_name.split('.').filter(|v| !v.is_empty()).collect::<Vec<_>>();
if let Some((_name, package)) = package_names.split_last() {
let package = package.join(".");
descriptors.values()
.find(|fd| fd.package.clone().unwrap_or_default() == package)
.and_then(|fd| find_enum_value_by_name_in_message(&fd.enum_type, enum_name, enum_value))
let result = descriptors.values()
.find_map(|fd| {
if fd.package.clone().unwrap_or_default() == package {
find_enum_value_by_name_in_message(&fd.enum_type, enum_name, enum_value)
} else {
None
}
});
if result.is_some() {
result
} else {
None
}
} else {
None
}
Expand All @@ -235,15 +245,20 @@ pub fn find_enum_by_name(
trace!("package_names={:?}", package_names);
if let Some((_name, package)) = package_names.split_last() {
let package = package.join(".");
descriptors.file.iter()
.find(|fd| {
let result = descriptors.file.iter()
.find_map(|fd| {
if let Some(fd_package) = &fd.package {
package == fd_package.as_str()
} else {
false
if package == fd_package.as_str() {
return find_enum_by_name_in_message(&fd.enum_type, enum_name)
}
}
})
.and_then(|fd| find_enum_by_name_in_message(&fd.enum_type, enum_name))
return None
});
if result.is_some() {
result
} else {
None
}
} else {
None
}
Expand Down
5 changes: 5 additions & 0 deletions tests/enum.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
syntax = "proto3";

package example.enum.package;

import "enum_imported.proto";

enum Values {
NONE = 0;
A = 1;
Expand All @@ -9,6 +13,7 @@ enum Values {

message MessageIn {
repeated Values in = 1;
repeated Values2 in2 = 2;
}

message MessageOut {
Expand Down
10 changes: 10 additions & 0 deletions tests/enum_imported.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package example.enum.package;

enum Values2 {
VALUES_2_NONE = 0;
VALUES_2_A = 1;
VALUES_2_B = 2;
VALUES_2_C = 3;
}
9 changes: 3 additions & 6 deletions tests/enum_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ async fn repeated_enum_test() {

"in": [
"matching(equalTo, 'A')"
],
"in2": [
"matching(equalTo, 'VALUES_2_A')"
]
})).await;
i
Expand Down Expand Up @@ -68,12 +71,6 @@ async fn repeated_enum_test() {
wire_type: WireType::LengthDelimited,
data: ProtobufFieldData::Enum(1, enum_descriptor.clone())
},
// ProtobufField {
// field_num: 1,
// field_name: "in".to_string(),
// wire_type: WireType::LengthDelimited,
// data: ProtobufFieldData::Enum(1, enum_descriptor.clone())
// }
];

let result = compare_message(
Expand Down
2 changes: 2 additions & 0 deletions tests/simple.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
syntax = "proto3";

package com.pact.protobuf.example;

message MessageIn {
bool in = 1;
}
Expand Down

0 comments on commit bd33f05

Please sign in to comment.