Skip to content

Commit

Permalink
stability: remove unwrap call in volo
Browse files Browse the repository at this point in the history
  • Loading branch information
Ggiggle committed Jan 22, 2024
1 parent 90aca21 commit 34f95b9
Show file tree
Hide file tree
Showing 23 changed files with 293 additions and 3,585 deletions.
3,385 changes: 0 additions & 3,385 deletions Cargo.lock

This file was deleted.

6 changes: 3 additions & 3 deletions examples/src/http/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn json_get() -> Json<Person> {
async fn json_post(Json(request): Json<Person>) -> String {
let first_phone = request
.phones
.get(0)
.first()
.map(|p| p.as_str())
.unwrap_or("no number");
format!(
Expand Down Expand Up @@ -223,7 +223,7 @@ fn test_router() -> Router {
// ```
async fn tracing_from_fn(
uri: Uri,
peer: Address,
peer: Option<Address>,
cookie_jar: CookieJar,
cx: &mut ServerContext,
req: BodyIncoming,
Expand All @@ -237,7 +237,7 @@ async fn tracing_from_fn(
let resp = next.run(cx, req).await;
let elapsed = start.elapsed();

tracing::info!("seq: {count}: {peer} request {uri}, cost {elapsed:?}");
tracing::info!("seq: {count}: {peer:?} request {uri}, cost {elapsed:?}");

(
(
Expand Down
41 changes: 29 additions & 12 deletions volo-build/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
use anyhow::{bail, Context};
use itertools::Itertools;
use lazy_static::lazy_static;
use serde::de::Error;

use crate::model::{Config, GitSource, Idl, Source};

Expand All @@ -30,10 +31,24 @@ pub fn ensure_cache_path() -> std::io::Result<()> {
}

pub fn read_config_from_file(f: &File) -> Result<Config, serde_yaml::Error> {
if f.metadata().unwrap().len() != 0 {
serde_yaml::from_reader(f)
} else {
Ok(Config::new())
match f.metadata() {
Ok(metadata) => {
if metadata.len() == 0 {
Ok(Config::new())
} else {
serde_yaml::from_reader(f)
}
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Ok(Config::new())
} else {
Err(serde_yaml::Error::custom(format!(
"failed to read config file, err: {}",
e
)))
}
}
}
}

Expand Down Expand Up @@ -208,13 +223,17 @@ impl Task {
pub fn get_repo_latest_commit_id(repo: &str, r#ref: &str) -> anyhow::Result<String> {
let commit_list = unsafe {
String::from_utf8_unchecked(
Command::new("git")
match Command::new("git")
.arg("ls-remote")
.arg(repo)
.arg(r#ref)
.output()
.unwrap()
.stdout,
{
Ok(output) => output.stdout,
Err(e) => {
bail!("git ls-remote {} {} err:{}", repo, r#ref, e);
}
},
)
};
let commit_list: Vec<_> = commit_list
Expand Down Expand Up @@ -300,11 +319,9 @@ pub fn git_repo_init(path: &Path) -> anyhow::Result<()> {
}

pub fn strip_slash_prefix(p: &Path) -> PathBuf {
if p.starts_with("/") {
// remove the "/" prefix to the start of idl path
p.strip_prefix("/").unwrap().to_path_buf()
} else {
p.to_path_buf()
match p.strip_prefix("/") {
Ok(p) => p.to_path_buf(),
Err(_) => p.to_path_buf(),
}
}

Expand Down
38 changes: 27 additions & 11 deletions volo-build/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ where
{
pub fn gen(self) {
let work_dir = std::env::current_dir().unwrap();
let config = std::fs::read(work_dir.join("volo.workspace.yml")).unwrap();
let config = serde_yaml::from_slice::<WorkspaceConfig>(&config).unwrap();

let config = match std::fs::read(work_dir.join("volo.workspace.yml")) {
Ok(config) => config,
Err(e) => {
eprintln!("failed to read volo.workspace.yml file, err: {}", e);
std::process::exit(1);
}
};
let config = match serde_yaml::from_slice::<WorkspaceConfig>(&config) {
Ok(config) => config,
Err(e) => {
eprintln!("failed to make serde yaml file, err: {}", e);
std::process::exit(1);
}
};
let services = config
.services
.into_iter()
Expand All @@ -60,14 +71,19 @@ where
config: s.config,
})
})
.collect::<Result<Vec<_>, _>>()
.unwrap();

self.ignore_unused(!config.touch_all)
.dedup(config.dedup_list)
.nonstandard_snake_case(config.nonstandard_snake_case)
.pilota_builder
.compile_with_config(services, pilota_build::Output::Workspace(work_dir));
.collect::<Result<Vec<_>, _>>();
match services {
Ok(services) => {
self.ignore_unused(!config.touch_all)
.dedup(config.dedup_list)
.pilota_builder
.compile_with_config(services, pilota_build::Output::Workspace(work_dir));
}
Err(e) => {
eprintln!("failed to get or download idl, err: {}", e);
std::process::exit(1);
}
}
}

pub fn plugin(mut self, plugin: impl Plugin + 'static) -> Self {
Expand Down
35 changes: 24 additions & 11 deletions volo-cli/src/idl/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,36 @@ impl CliCommand for Add {
if idl.path != self.idl {
continue;
}
match idl.source {
Source::Git(ref mut source) if self.git.as_ref() == Some(&source.repo) => {
if let Some(r#ref) = self.r#ref.as_ref() {
let _ = source.r#ref.insert(r#ref.clone());
if let Source::Git(ref git) = new_idl.source {
let _ = source.lock.insert(git.lock.clone().unwrap());
}
}

let git_source = match idl.source {
Source::Git(ref mut git_source)
if self.git.as_ref() == Some(&git_source.repo) =>
{
has_found_idl = true;
break;
git_source
}
Source::Local if self.git.is_none() => {
Source::Git(_) => continue,
Source::Local => {
has_found_idl = true;
break;
}
_ => {}
};

if let Some(r#ref) = self.r#ref.as_ref() {
let _ = git_source.r#ref.insert(r#ref.clone());
} else {
unreachable!("git ref should be Some if git source is git")
}
let git = match new_idl.source {
Source::Git(ref git) => git,
_ => unreachable!("git source should be git if idl source is git"),
};
if let Some(lock) = git.lock.clone() {
let _ = git_source.lock.insert(lock);
} else {
unreachable!("git lock should be Some if idl source is git")
}
break;
}

if !has_found_idl {
Expand Down
26 changes: 19 additions & 7 deletions volo-cli/src/idl/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ impl CliCommand for Update {
std::process::exit(1);
}

let entry = config.entries.get_mut(&cx.entry_name).unwrap();
let entry = match config.entries.get_mut(&cx.entry_name) {
Some(entry) => entry,
None => {
eprintln!("entry {} not found", cx.entry_name);
std::process::exit(1);
}
};
let mut exists = HashSet::new();

entry.idls.iter_mut().for_each(|idl| {
idl.path = strip_slash_prefix(idl.path.as_path());
if let Source::Git(ref git) = idl.source {
Expand All @@ -46,12 +51,12 @@ impl CliCommand for Update {
.iter()
.filter_map(|repo| {
entry.idls.iter_mut().find_map(|config_idl| {
if let Source::Git(git_source) = &mut config_idl.source {
if *repo == git_source.repo {
return Some(git_source as *mut _);
match &mut config_idl.source {
Source::Git(git_source) if *repo == git_source.repo => {
Some(git_source as *mut _)
}
_ => None,
}
None
})
})
.collect()
Expand All @@ -72,7 +77,14 @@ impl CliCommand for Update {

should_update_gits
.into_iter()
.try_for_each(|git_source| unsafe { git_source.as_mut().unwrap().update() })?;
.try_for_each(|git_source| unsafe {
if let Some(git_source) = git_source.as_mut() {
git_source.update()
} else {
eprintln!("git source is null");
std::process::exit(1);
}
})?;

Ok(())
})
Expand Down
45 changes: 26 additions & 19 deletions volo-cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl CliCommand for Init {
lock,
});
}

if self.git.is_some() {
idl.path = strip_slash_prefix(&self.idl);
} else {
Expand All @@ -202,6 +203,7 @@ impl CliCommand for Init {
touch_all: false,
nonstandard_snake_case: false,
};

if self.is_grpc_project() {
self.copy_grpc_template(entry.clone())?;
} else {
Expand All @@ -210,17 +212,18 @@ impl CliCommand for Init {

if self.git.as_ref().is_none() {
// we will move volo.yml to volo-gen, so we need to add .. to includes and idl path
let idl = entry.idls.get_mut(0).unwrap();
if let Some(includes) = &mut idl.includes {
for i in includes {
if i.is_absolute() {
continue;
if let Some(idl) = entry.idls.get_mut(0) {
if let Some(includes) = &mut idl.includes {
for i in includes {
if i.is_absolute() {
continue;
}
*i = PathBuf::new().join("../").join(i.clone());
}
*i = PathBuf::new().join("../").join(i.clone());
}
}
if !idl.path.is_absolute() {
idl.path = PathBuf::new().join("../").join(self.idl.clone());
if !idl.path.is_absolute() {
idl.path = PathBuf::new().join("../").join(self.idl.clone());
}
}
}

Expand All @@ -233,24 +236,28 @@ impl CliCommand for Init {
if self.idl != idl.path {
continue;
}
match idl.source {
let (repo, r#ref) = match idl.source {
Source::Git(GitSource {
ref mut repo,
ref mut r#ref,
..
}) if self.git.is_some() => {
}) => {
// found the desired idl, update it
found = true;
if self.git.is_some() {
*repo = self.git.as_ref().unwrap().clone();
if self.r#ref.is_some() {
*r#ref = self.r#ref.clone();
}
}
break;
(repo, r#ref)
}
_ => continue,
};

if let Some(git) = self.git.as_ref() {
*repo = git.clone();
if self.r#ref.is_some() {
*r#ref = self.r#ref.clone();
}
_ => {}
} else {
unreachable!()
}
break;
}

if !found {
Expand Down
4 changes: 2 additions & 2 deletions volo-grpc/src/codec/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl<T: Message + Default> RecvStream<T> {
Err(_frame) => {
// **unreachable** because the `frame` cannot be `Frame::Data` here
debug!("[VOLO] unexpected data from stream");
return Err(Status::new(
Err(Status::new(
Code::Internal,
"Unexpected data from stream.".to_string(),
));
))
}
},
Some(Err(err)) => Err(Status::from_error(Box::new(err))),
Expand Down
2 changes: 1 addition & 1 deletion volo-grpc/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl Status {
.expect("Invalid status header, expected base64 encoded value")
})
.map(Bytes::from)
.unwrap_or_else(Bytes::new);
.unwrap_or_default();

// must remove these redundant message from the header map
let mut other_headers = header_map.clone();
Expand Down
4 changes: 3 additions & 1 deletion volo-http/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
}

impl<S: Sync> FromContext<S> for Address {
type Rejection = Infallible;
type Rejection = RejectionError;

async fn from_context(cx: &mut ServerContext, _state: &S) -> Result<Address, Self::Rejection> {
Ok(cx.peer.clone())
Expand Down Expand Up @@ -309,6 +309,7 @@ pub enum RejectionError {
JsonRejection(crate::json::Error),
QueryRejection(serde_urlencoded::de::Error),
FormRejection(serde_html_form::de::Error),
EmptyAddressError,
}

unsafe impl Send for RejectionError {}
Expand All @@ -322,6 +323,7 @@ impl IntoResponse for RejectionError {
Self::JsonRejection(_) => StatusCode::BAD_REQUEST,
Self::QueryRejection(_) => StatusCode::BAD_REQUEST,
Self::FormRejection(_) => StatusCode::BAD_REQUEST,
Self::EmptyAddressError => StatusCode::BAD_REQUEST,
};

status.into_response()
Expand Down
Loading

0 comments on commit 34f95b9

Please sign in to comment.