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

relax JSON string arg types #85

Merged
merged 4 commits into from
Dec 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
2 changes: 1 addition & 1 deletion rust/routee-compass/src/app/compass/compass_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl TryFrom<(&Config, &CompassAppBuilder)> for CompassApp {
let config_json = config
.clone()
.try_deserialize::<serde_json::Value>()?
.normalize_file_paths("", &root_config_path)?;
.normalize_file_paths(&"", &root_config_path)?;

let alg_params = config_json.get_config_section(CompassConfigurationField::Algorithm)?;
let search_algorithm = SearchAlgorithm::try_from(&alg_params)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ impl CompassAppBuilder {
config: &serde_json::Value,
) -> Result<Vec<Box<dyn InputPlugin>>, CompassConfigurationError> {
let input_plugins = config.get_config_array(
CompassConfigurationField::InputPlugins.to_string(),
CompassConfigurationField::Plugins.to_string(),
&CompassConfigurationField::InputPlugins,
&CompassConfigurationField::Plugins,
)?;

let mut plugins: Vec<Box<dyn InputPlugin>> = Vec::new();
Expand Down Expand Up @@ -286,8 +286,8 @@ impl CompassAppBuilder {
config: &serde_json::Value,
) -> Result<Vec<Box<dyn OutputPlugin>>, CompassConfigurationError> {
let output_plugins = config.get_config_array(
CompassConfigurationField::OutputPlugins.to_string(),
CompassConfigurationField::Plugins.to_string(),
&CompassConfigurationField::OutputPlugins,
&CompassConfigurationField::Plugins,
)?;

let mut plugins: Vec<Box<dyn OutputPlugin>> = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ impl From<CompassConfigurationField> for String {
}
}

impl AsRef<str> for CompassConfigurationField {
fn as_ref(&self) -> &str {
self.to_str()
}
}

impl Display for CompassConfigurationField {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_str())
Expand Down
142 changes: 71 additions & 71 deletions rust/routee-compass/src/app/compass/config/config_json_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,52 @@ pub trait ConfigJsonExtensions {
) -> Result<serde_json::Value, CompassConfigurationError>;
fn get_config_path(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<PathBuf, CompassConfigurationError>;
fn get_config_path_optional(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<Option<PathBuf>, CompassConfigurationError>;
fn get_config_string(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<String, CompassConfigurationError>;
fn get_config_array(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<Vec<serde_json::Value>, CompassConfigurationError>;
fn get_config_i64(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<i64, CompassConfigurationError>;
fn get_config_f64(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<f64, CompassConfigurationError>;
fn get_config_from_str<T: FromStr>(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<T, CompassConfigurationError>;
fn get_config_serde<T: de::DeserializeOwned>(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<T, CompassConfigurationError>;
fn get_config_serde_optional<T: de::DeserializeOwned>(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<Option<T>, CompassConfigurationError>;
fn normalize_file_paths(
&self,
parent_key: &str,
parent_key: &dyn AsRef<str>,
root_config_path: &Path,
) -> Result<serde_json::Value, CompassConfigurationError>;
}
Expand All @@ -82,10 +82,10 @@ impl ConfigJsonExtensions for serde_json::Value {
}
fn get_config_path_optional(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<Option<PathBuf>, CompassConfigurationError> {
match self.get(key.clone()) {
match self.get(key.as_ref()) {
None => Ok(None),
Some(_) => {
let config_path = self.get_config_path(key, parent_key)?;
Expand All @@ -95,10 +95,10 @@ impl ConfigJsonExtensions for serde_json::Value {
}
fn get_config_path(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<PathBuf, CompassConfigurationError> {
let path_string = self.get_config_string(key.clone(), parent_key.clone())?;
let path_string = self.get_config_string(key, parent_key)?;
let path = PathBuf::from(&path_string);

// if file can be found, just it
Expand All @@ -108,45 +108,45 @@ impl ConfigJsonExtensions for serde_json::Value {
// can't find the file
Err(CompassConfigurationError::FileNotFoundForComponent(
path_string,
key,
parent_key,
String::from(key.as_ref()),
String::from(parent_key.as_ref()),
))
}
}
fn get_config_string(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<String, CompassConfigurationError> {
let value = self
.get(&key)
.get(key.as_ref())
.ok_or(CompassConfigurationError::ExpectedFieldForComponent(
key.clone(),
parent_key.clone(),
String::from(key.as_ref()),
String::from(parent_key.as_ref()),
))?
.as_str()
.map(String::from)
.ok_or(CompassConfigurationError::ExpectedFieldWithType(
key.clone(),
String::from(key.as_ref()),
String::from("String"),
))?;
Ok(value)
}

fn get_config_array(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<Vec<serde_json::Value>, CompassConfigurationError> {
let array = self
.get(&key)
.get(key.as_ref())
.ok_or(CompassConfigurationError::ExpectedFieldForComponent(
key.clone(),
parent_key.clone(),
String::from(key.as_ref()),
String::from(parent_key.as_ref()),
))?
.as_array()
.ok_or(CompassConfigurationError::ExpectedFieldWithType(
key.clone(),
String::from(key.as_ref()),
String::from("Array"),
))?
.to_owned();
Expand All @@ -155,61 +155,61 @@ impl ConfigJsonExtensions for serde_json::Value {

fn get_config_i64(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<i64, CompassConfigurationError> {
let value = self
.get(&key)
.get(key.as_ref())
.ok_or(CompassConfigurationError::ExpectedFieldForComponent(
key.clone(),
parent_key.clone(),
String::from(key.as_ref()),
String::from(parent_key.as_ref()),
))?
.as_i64()
.ok_or(CompassConfigurationError::ExpectedFieldWithType(
key.clone(),
String::from(key.as_ref()),
String::from("64-bit signed integer"),
))?;
Ok(value)
}

fn get_config_f64(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<f64, CompassConfigurationError> {
let value = self
.get(&key)
.get(key.as_ref())
.ok_or(CompassConfigurationError::ExpectedFieldForComponent(
key.clone(),
parent_key.clone(),
String::from(key.as_ref()),
String::from(parent_key.as_ref()),
))?
.as_f64()
.ok_or(CompassConfigurationError::ExpectedFieldWithType(
key.clone(),
String::from(key.as_ref()),
String::from("64-bit floating point"),
))?;
Ok(value)
}

fn get_config_from_str<T: FromStr>(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<T, CompassConfigurationError> {
let value = self
.get(&key)
.get(key.as_ref())
.ok_or(CompassConfigurationError::ExpectedFieldForComponent(
key.clone(),
parent_key.clone(),
String::from(key.as_ref()),
String::from(parent_key.as_ref()),
))?
.as_str()
.ok_or(CompassConfigurationError::ExpectedFieldWithType(
key.clone(),
String::from(key.as_ref()),
String::from("string-parseable"),
))?;
let result = T::from_str(value).map_err(|_| {
CompassConfigurationError::ExpectedFieldWithType(
key.clone(),
String::from(key.as_ref()),
format!("failed to parse type from string {}", value),
)
})?;
Expand All @@ -218,14 +218,14 @@ impl ConfigJsonExtensions for serde_json::Value {

fn get_config_serde<T: de::DeserializeOwned>(
&self,
key: String,
parent_key: String,
key: &dyn AsRef<str>,
parent_key: &dyn AsRef<str>,
) -> Result<T, CompassConfigurationError> {
let value = self
.get(key.clone())
.get(key.as_ref())
.ok_or(CompassConfigurationError::ExpectedFieldForComponent(
key.clone(),
parent_key.clone(),
String::from(key.as_ref()),
String::from(parent_key.as_ref()),
))?
.to_owned();

Expand All @@ -235,13 +235,13 @@ impl ConfigJsonExtensions for serde_json::Value {
}
fn get_config_serde_optional<T: de::DeserializeOwned>(
&self,
key: String,
_parent_key: String,
key: &dyn AsRef<str>,
_parent_key: &dyn AsRef<str>,
) -> Result<Option<T>, CompassConfigurationError> {
match self.get(key.clone()) {
match self.get(key.as_ref()) {
None => Ok(None),
Some(value) => {
let result: T = serde_json::from_value(value.clone())
let result: T = serde_json::from_value(value.to_owned())
.map_err(CompassConfigurationError::SerdeDeserializationError)?;
Ok(Some(result))
}
Expand All @@ -268,7 +268,7 @@ impl ConfigJsonExtensions for serde_json::Value {
/// * `Result<serde_json::Value, CompassConfigurationError>` - The JSON object with normalized paths.
fn normalize_file_paths(
&self,
parent_key: &str,
parent_key: &dyn AsRef<str>,
root_config_path: &Path,
) -> Result<serde_json::Value, CompassConfigurationError> {
match self {
Expand Down Expand Up @@ -296,7 +296,7 @@ impl ConfigJsonExtensions for serde_json::Value {
} else {
// if we can't find the file in either location, we throw an error
Err(CompassConfigurationError::FileNormalizationNotFound(
parent_key.to_string(),
String::from(parent_key.as_ref()),
path_string.clone(),
new_path_string,
))
Expand All @@ -311,11 +311,11 @@ impl ConfigJsonExtensions for serde_json::Value {
|| value.is_array()
{
new_obj.insert(
key.clone(),
String::from(key),
value.normalize_file_paths(key, root_config_path)?,
);
} else {
new_obj.insert(key.clone(), value.clone());
new_obj.insert(String::from(key), value.clone());
}
}
Ok(serde_json::Value::Object(new_obj))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl FrontierModelBuilder for RoadClassBuilder {
let road_class_file_key = String::from("road_class_input_file");

let road_class_file = parameters
.get_config_path(road_class_file_key.clone(), frontier_key.clone())
.get_config_path(&road_class_file_key, &frontier_key)
.map_err(|e| {
FrontierModelError::BuildError(format!(
"configuration error due to {}: {}",
Expand All @@ -33,14 +33,15 @@ impl FrontierModelBuilder for RoadClassBuilder {
})?;

let road_class_lookup: Box<[String]> =
read_utils::read_raw_file(road_class_file.clone(), read_decoders::string, None)
.map_err(|e| {
read_utils::read_raw_file(&road_class_file, read_decoders::string, None).map_err(
|e| {
FrontierModelError::BuildError(format!(
"failed to load file at {:?}: {}",
road_class_file.clone().to_str(),
e
))
})?;
},
)?;

let m: Arc<dyn FrontierModelService> = Arc::new(RoadClassFrontierService {
road_class_lookup: Arc::new(road_class_lookup),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ impl FrontierModelService for RoadClassFrontierService {
query: &serde_json::Value,
) -> Result<Arc<dyn FrontierModel>, FrontierModelError> {
let service: Arc<RoadClassFrontierService> = Arc::new(self.clone());
let road_classes_key = String::from("road_classes");
let road_classes = query
.get_config_serde_optional::<HashSet<String>>(road_classes_key, String::from(""))
.get_config_serde_optional::<HashSet<String>>(&"road_classes", &"")
.map_err(|e| {
FrontierModelError::BuildError(format!("unable to deserialize as array: {}", e))
})?;
Expand Down
Loading