Skip to content

Commit

Permalink
Make some more things public.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerard Klijs authored and gklijs committed Oct 14, 2023
1 parent 00b7f36 commit 13c9460
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ mod proto_common_types;
#[cfg(feature = "proto_raw")]
pub mod proto_raw_common;
#[cfg(any(feature = "proto_decoder", feature = "proto_raw"))]
mod proto_resolver;
pub mod proto_resolver;
pub mod schema_registry_common;
25 changes: 11 additions & 14 deletions src/proto_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub struct MessageResolver {
}

#[derive(Debug, Clone)]
pub(crate) struct IndexResolver {
pub struct IndexResolver {
map: DashMap<String, Arc<Vec<i32>>>,
}

impl MessageResolver {
pub(crate) fn new(s: &str) -> MessageResolver {
pub fn new(s: &str) -> MessageResolver {
let helper = ResolverHelper::new(s);
let map = DashMap::new();
for i in &helper.indexes {
Expand All @@ -30,16 +30,16 @@ impl MessageResolver {
}
}

pub(crate) fn find_name(&self, index: &[i32]) -> Option<Arc<String>> {
pub fn find_name(&self, index: &[i32]) -> Option<Arc<String>> {
self.map.get(index).map(|e| e.value().clone())
}
pub(crate) fn imports(&self) -> &Vec<String> {
pub fn imports(&self) -> &Vec<String> {
&self.imports
}
}

impl IndexResolver {
pub(crate) fn new(s: &str) -> IndexResolver {
pub fn new(s: &str) -> IndexResolver {
let helper = ResolverHelper::new(s);
let map = DashMap::new();
for i in &helper.indexes {
Expand All @@ -48,16 +48,16 @@ impl IndexResolver {
IndexResolver { map }
}

pub(crate) fn find_index(&self, name: &str) -> Option<Arc<Vec<i32>>> {
pub fn find_index(&self, name: &str) -> Option<Arc<Vec<i32>>> {
self.map.get(name).map(|e| e.value().clone())
}

pub(crate) fn is_single_message(&self) -> bool {
pub fn is_single_message(&self) -> bool {
self.map.len() == 1
}
}

struct ResolverHelper {
pub struct ResolverHelper {
package: Option<String>,
indexes: Vec<Vec<i32>>,
names: Vec<String>,
Expand Down Expand Up @@ -87,7 +87,7 @@ enum Token {
}

impl ResolverHelper {
fn new(s: &str) -> ResolverHelper {
pub fn new(s: &str) -> ResolverHelper {
let mut index: Vec<i32> = vec![0];
let mut package: Option<String> = None;
let mut indexes: Vec<Vec<i32>> = Vec::new();
Expand Down Expand Up @@ -175,7 +175,7 @@ fn same_vec(first: &[i32], second: &[i32]) -> bool {
true
}

pub(crate) fn to_index_and_data(bytes: &[u8]) -> (Vec<i32>, Vec<u8>) {
pub fn to_index_and_data(bytes: &[u8]) -> (Vec<i32>, Vec<u8>) {
if bytes[0] == 0 {
(vec![0], bytes[1..].to_vec())
} else {
Expand All @@ -189,10 +189,7 @@ pub(crate) fn to_index_and_data(bytes: &[u8]) -> (Vec<i32>, Vec<u8>) {
}
}

pub(crate) fn resolve_name(
resolver: &MessageResolver,
index: &[i32],
) -> Result<Arc<String>, SRCError> {
pub fn resolve_name(resolver: &MessageResolver, index: &[i32]) -> Result<Arc<String>, SRCError> {
match resolver.find_name(index) {
Some(n) => Ok(n),
None => Err(SRCError::non_retryable_without_cause(&format!(
Expand Down
2 changes: 2 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#[cfg(all(feature = "blocking", feature = "kafka_test"))]
pub mod blocking;
#[cfg(all(feature = "proto_raw", feature = "kafka_test"))]
pub mod proto_resolver;
62 changes: 62 additions & 0 deletions tests/proto_resolver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use schema_registry_converter::proto_resolver::{IndexResolver, MessageResolver};
use std::sync::Arc;

fn get_proto_complex() -> &'static str {
r#"syntax = "proto3"; import "result.proto"; message A {bytes id = 1;} message B {bytes id = 1;} message C {bytes id = 1; D d = 2; message D {int64 counter = 1;}} package org.schema_registry_test_app.proto; message ProtoTest {bytes id = 1; enum Language {Java = 0;Rust = 1;} Language by = 2;int64 counter = 3;string input = 4;repeated A results = 5;}"#
}

fn get_three_complex() -> &'static str {
r#"syntax = "proto3";
package org.schema_registry_test_app.proto;
import "google/type/color.proto";
import "google/type/datetime.proto";
import "google/type/money.proto";
message GoogleTest {
google.type.Color color = 1;
google.type.DateTime dateTime = 2;
google.type.Money money = 3;
Language by = 4;
int64 counter = 5;
enum Language {
Java = 0;
Rust = 1;
Js = 2;
Python = 3;
Go = 4;
C = 5;
}
}"#
}

#[test]
fn test_schema_with_three_imports() {
let resolver = MessageResolver::new(get_three_complex());
assert_eq!(resolver.imports.len(), 3);
assert_eq!(resolver.imports[0], String::from("google/type/color.proto"));
assert_eq!(
resolver.imports[1],
String::from("google/type/datetime.proto")
);
assert_eq!(resolver.imports[2], String::from("google/type/money.proto"));
}

#[test]
fn test_complex_schema_index_resolver() {
let resolver = IndexResolver::new(get_proto_complex());

assert_eq!(
resolver.find_index("org.schema_registry_test_app.proto.A"),
Some(Arc::new(vec![0]))
);
assert_eq!(
resolver.find_index("org.schema_registry_test_app.proto.C.D"),
Some(Arc::new(vec![2, 0]))
);
assert_eq!(
resolver.find_index("org.schema_registry_test_app.proto.ProtoTest"),
Some(Arc::new(vec![3]))
);
}

0 comments on commit 13c9460

Please sign in to comment.