Skip to content

Commit

Permalink
refactor: common protofile, refactor proto gen
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-hagemann committed Nov 27, 2023
1 parent 9bdd1e5 commit 6440891
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.rock
*.nix

proto/*.rs
venv/
build/
*.charm
Expand Down
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define the path to the output directory within the `src` folder
let out_dir = Path::new("proto");
std::fs::create_dir_all(out_dir)?;

let descriptor_set_path = format!(
"{}/ratings_descriptor.bin",
std::env::var("OUT_DIR").unwrap()
Expand All @@ -14,6 +20,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
.build_server(true)
.file_descriptor_set_path(descriptor_set_path)
.out_dir(out_dir)
.compile(files, &["proto"])?;

Ok(())
Expand Down
11 changes: 3 additions & 8 deletions src/features/app/interface.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use crate::app::AppContext;

use self::protobuf::{App, GetRatingRequest, GetRatingResponse};
pub use protobuf::app_server;
use crate::features::pb::app::{GetRatingRequest, GetRatingResponse};
use tonic::{Request, Response, Status};

use super::{service::AppService, use_cases};
use crate::features::pb::app::app_server::App;

pub mod protobuf {
pub use self::app_server::App;
tonic::include_proto!("ratings.features.common");
tonic::include_proto!("ratings.features.app");
}
use super::{service::AppService, use_cases};

#[tonic::async_trait]
impl App for AppService {
Expand Down
2 changes: 1 addition & 1 deletion src/features/app/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::interface::app_server::AppServer;
use crate::features::pb::app::app_server::AppServer;

#[derive(Debug, Default)]
pub struct AppService;
Expand Down
8 changes: 3 additions & 5 deletions src/features/common/entities.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use sqlx::FromRow;

pub mod protobuf {
tonic::include_proto!("ratings.features.common");
}
use crate::features::pb::common as pb;

const INSUFFICIENT_VOTES_QUANTITY: usize = 25;

Expand Down Expand Up @@ -55,8 +53,8 @@ impl Rating {
}
}

pub(crate) fn into_dto(self) -> protobuf::Rating {
protobuf::Rating {
pub(crate) fn into_dto(self) -> pb::Rating {
pb::Rating {
snap_id: self.snap_id,
total_votes: self.total_votes,
ratings_band: self.ratings_band as i32,
Expand Down
1 change: 1 addition & 0 deletions src/features/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod app;
mod common;
mod pb;
pub mod user;
11 changes: 11 additions & 0 deletions src/features/pb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod app {
include!("../../proto/ratings.features.app.rs");
}

pub mod common {
include!("../../proto/ratings.features.common.rs");
}

pub mod user {
include!("../../proto/ratings.features.user.rs");
}
6 changes: 3 additions & 3 deletions src/features/user/entities.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sqlx::FromRow;
use time::OffsetDateTime;

use super::interface::protobuf;
use crate::features::pb::user;

pub type ClientHash = String;

Expand Down Expand Up @@ -35,13 +35,13 @@ pub struct Vote {
}

impl Vote {
pub(crate) fn into_dto(self) -> protobuf::Vote {
pub(crate) fn into_dto(self) -> user::Vote {
let timestamp = Some(prost_types::Timestamp {
seconds: self.timestamp.unix_timestamp(),
nanos: 0,
});

protobuf::Vote {
user::Vote {
snap_id: self.snap_id,
snap_revision: self.snap_revision as i32,
vote_up: self.vote_up,
Expand Down
12 changes: 3 additions & 9 deletions src/features/user/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ use time::OffsetDateTime;

use tonic::{Request, Response, Status};

pub use protobuf::user_server;

use crate::app::AppContext;
use crate::utils::jwt::Claims;

use super::entities::Vote;
use super::service::UserService;
use super::use_cases;

use self::protobuf::{
use crate::features::pb::user::{
AuthenticateRequest, AuthenticateResponse, GetSnapVotesRequest, GetSnapVotesResponse,
ListMyVotesRequest, ListMyVotesResponse, User, VoteRequest,
ListMyVotesRequest, ListMyVotesResponse, VoteRequest,
};

pub mod protobuf {
pub use self::user_server::User;

tonic::include_proto!("ratings.features.user");
}
use crate::features::pb::user::user_server::User;

#[tonic::async_trait]
impl User for UserService {
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::interface::user_server::UserServer;
use crate::features::pb::user::user_server::UserServer;

#[derive(Debug, Default)]
pub struct UserService;
Expand Down
9 changes: 4 additions & 5 deletions tests/app_tests/lifecycle_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use ratings::{
use super::super::helpers::with_lifecycle::with_lifecycle;
use crate::helpers::test_data::TestData;
use crate::helpers::vote_generator::generate_votes;
use crate::helpers::{self, client_app::pb::RatingsBand, client_app::AppClient};
use crate::helpers::{
client_user::{pb::AuthenticateResponse, UserClient},
data_faker,
};
use crate::helpers::{self, client_app::AppClient};
use crate::helpers::{client_user::UserClient, data_faker};
use crate::pb::common::RatingsBand;
use crate::pb::user::AuthenticateResponse;

#[tokio::test]
async fn app_lifecycle_test() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
11 changes: 4 additions & 7 deletions tests/helpers/client_app.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use tonic::{metadata::MetadataValue, transport::Endpoint, Request, Response, Status};

pub mod pb {
pub use self::app_client::AppClient;

tonic::include_proto!("ratings.features.app");
}
use crate::pb::app::app_client as pb;
use crate::pb::app::{GetRatingRequest, GetRatingResponse};

#[derive(Debug, Clone)]
pub struct AppClient {
Expand All @@ -22,7 +19,7 @@ impl AppClient {
&self,
id: &str,
token: &str,
) -> Result<Response<pb::GetRatingResponse>, Status> {
) -> Result<Response<GetRatingResponse>, Status> {
let channel = Endpoint::from_shared(self.url.clone())
.unwrap()
.connect()
Expand All @@ -34,7 +31,7 @@ impl AppClient {
Ok(req)
});
client
.get_rating(pb::GetRatingRequest {
.get_rating(GetRatingRequest {
snap_id: id.to_string(),
})
.await
Expand Down
22 changes: 9 additions & 13 deletions tests/helpers/client_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use tonic::metadata::MetadataValue;
use tonic::transport::Endpoint;
use tonic::{Request, Response, Status};

use self::pb::GetSnapVotesResponse;
pub mod pb {
pub use self::user_client::UserClient;

tonic::include_proto!("ratings.features.user");
}
use crate::pb::user::user_client as pb;
use crate::pb::user::{
AuthenticateRequest, AuthenticateResponse, GetSnapVotesRequest, GetSnapVotesResponse,
VoteRequest,
};

#[derive(Debug, Clone)]
pub struct UserClient {
Expand All @@ -22,18 +21,15 @@ impl UserClient {
}

#[allow(dead_code)]
pub async fn authenticate(
&self,
id: &str,
) -> Result<Response<pb::AuthenticateResponse>, Status> {
pub async fn authenticate(&self, id: &str) -> Result<Response<AuthenticateResponse>, Status> {
let mut client = pb::UserClient::connect(self.url.clone()).await.unwrap();
client
.authenticate(pb::AuthenticateRequest { id: id.to_string() })
.authenticate(AuthenticateRequest { id: id.to_string() })
.await
}

#[allow(dead_code)]
pub async fn vote(&self, token: &str, ballet: pb::VoteRequest) -> Result<Response<()>, Status> {
pub async fn vote(&self, token: &str, ballet: VoteRequest) -> Result<Response<()>, Status> {
let channel = Endpoint::from_shared(self.url.clone())
.unwrap()
.connect()
Expand All @@ -51,7 +47,7 @@ impl UserClient {
pub async fn get_snap_votes(
&self,
token: &str,
request: pb::GetSnapVotesRequest,
request: GetSnapVotesRequest,
) -> Result<Response<GetSnapVotesResponse>, Status> {
let channel = Endpoint::from_shared(self.url.clone())
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/vote_generator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest};
use super::test_data::TestData;
use crate::helpers;
use crate::pb::user::{AuthenticateResponse, VoteRequest};

pub async fn generate_votes(
snap_id: &str,
Expand Down
2 changes: 2 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ mod app_tests {
}

mod helpers;

mod pb;
11 changes: 11 additions & 0 deletions tests/pb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod app {
include!("../proto/ratings.features.app.rs");
}

pub mod common {
include!("../proto/ratings.features.common.rs");
}

pub mod user {
include!("../proto/ratings.features.user.rs");
}
2 changes: 1 addition & 1 deletion tests/user_tests/double_authenticate_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::helpers;
use crate::helpers::test_data::TestData;

use super::super::helpers::client_user::pb::AuthenticateResponse;
use super::super::helpers::client_user::UserClient;
use super::super::helpers::with_lifecycle::with_lifecycle;
use crate::pb::user::AuthenticateResponse;
use ratings::app::AppContext;
use ratings::utils::{self, Infrastructure};
use sqlx::Row;
Expand Down
4 changes: 2 additions & 2 deletions tests/user_tests/get_votes_lifecycle_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::helpers;
use crate::helpers::client_user::pb::GetSnapVotesRequest;
use crate::helpers::test_data::TestData;

use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest};
use crate::pb::user::{AuthenticateResponse, GetSnapVotesRequest, VoteRequest};

use super::super::helpers::client_user::UserClient;
use super::super::helpers::with_lifecycle::with_lifecycle;
use futures::FutureExt;
Expand Down
2 changes: 1 addition & 1 deletion tests/user_tests/simple_lifecycle_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::helpers;
use crate::helpers::test_data::TestData;

use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest};
use super::super::helpers::client_user::UserClient;
use super::super::helpers::with_lifecycle::with_lifecycle;
use crate::pb::user::{AuthenticateResponse, VoteRequest};
use futures::FutureExt;
use ratings::app::AppContext;
use ratings::utils::{self, Infrastructure};
Expand Down

0 comments on commit 6440891

Please sign in to comment.