Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug resolving enum across multiple files. #31

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading