Skip to content

Commit

Permalink
Merge pull request #41 from EdwinBetanc0urt/bugfix/find-index-empty-s…
Browse files Browse the repository at this point in the history
…tring

fix: Find index with empty string.
  • Loading branch information
yamelsenih authored Jul 25, 2024
2 parents 4c1663d + 9e781e8 commit fd84deb
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/models/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ pub struct DependendField {
}

pub async fn browser_from_id(_id: Option<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Browser, String> {
if _id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Browser Identifier is Mandatory").to_string());
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "Browser Identifier is Mandatory").to_string()
);
}
let mut _document = Browser::from_id(_id);

Expand Down
6 changes: 4 additions & 2 deletions src/models/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ impl IndexDocument for Form {
}

pub async fn form_from_id(_id: Option<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Form, String> {
if _id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Form Identifier is Mandatory").to_string());
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "Form Identifier is Mandatory").to_string()
);
}
let mut _document = Form::from_id(_id);

Expand Down
16 changes: 10 additions & 6 deletions src/models/menu_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,19 @@ impl IndexDocument for MenuTree {
}

pub async fn menu_tree_from_id(_id: Option<String>, _dictionary_code: Option<&String>) -> Result<MenuTree, std::io::Error> {
if _id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "MenuTree Identifier is Mandatory"))
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "MenuTree Identifier is Mandatory")
);
}
let mut _document = MenuTree::from_id(_id);

let mut _index_name = "menu_tree".to_string();
if _dictionary_code.is_some() {
_index_name.push_str("_");
_index_name.push_str(_dictionary_code.unwrap());
if let Some(code) = _dictionary_code {
if !code.trim().is_empty() {
_index_name.push_str("_");
_index_name.push_str(code);
}
}
log::info!("Index to search {:}", _index_name);

Expand All @@ -147,4 +151,4 @@ pub async fn menu_tree_from_id(_id: Option<String>, _dictionary_code: Option<&St
Err(Error::new(ErrorKind::InvalidData.into(), error))
},
}
}
}
43 changes: 27 additions & 16 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,41 @@ fn default_index(_index_name: String) -> String {
fn language_index(_index_name: String, _language: Option<&String>) -> String {
let mut _index_to_find: String = default_index(_index_name);
if let Some(language) = _language {
_index_to_find.push_str("_");
_index_to_find.push_str(language);
if !language.trim().is_empty() {
_index_to_find.push_str("_");
_index_to_find.push_str(language);
}
}
_index_to_find.to_lowercase()
}

fn client_index_only(_index_name: String, _client_id: Option<&String>) -> String {
let mut _index_to_find: String = default_index(_index_name);
if let Some(language) = _client_id {
_index_to_find.push_str("_");
_index_to_find.push_str(language);
if let Some(client) = _client_id {
if !client.trim().is_empty() {
_index_to_find.push_str("_");
_index_to_find.push_str(client);
}
}
_index_to_find.to_lowercase()
}

async fn get_index_name(_index_name: String, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<String, std::io::Error> {
// Validate
if _language.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Language is Mandatory"));
}

let mut _language_index = language_index(_index_name, _language);
if _dictionary_code.is_some() {
_language_index.push_str("_");
_language_index.push_str(_dictionary_code.unwrap());
// Validate
if _language.is_none() || _language.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "Language is Mandatory")
);
}

let mut _language_index = language_index(_index_name, _language);
if let Some(code) = _dictionary_code {
if !code.trim().is_empty() {
_language_index.push_str("_");
_language_index.push_str(code);
}
}

// Find index
match exists_index(_language_index.to_owned()).await {
Ok(_) => {
Expand All @@ -65,7 +74,9 @@ async fn get_index_name(_index_name: String, _language: Option<&String>, _dictio
Err(error) => {
log::warn!("No menu item index `{:}`", _language_index);
log::error!("No role index `{:}`", _language_index);
return Err(Error::new(ErrorKind::InvalidData.into(), error))
return Err(
Error::new(ErrorKind::InvalidData.into(), error)
)
}
}
}
}
6 changes: 4 additions & 2 deletions src/models/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ pub struct Workflow {
}

pub async fn process_from_id(_id: Option<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Process, String> {
if _id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Process/Report Identifier is Mandatory").to_string());
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "Process/Report Identifier is Mandatory").to_string()
);
}
let mut _document = Process::from_id(_id);

Expand Down
12 changes: 8 additions & 4 deletions src/models/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ impl IndexDocument for Role {
}

pub async fn role_from_id(_id: Option<&String>, _client_id: Option<&String>, _dictionary_code: Option<&String>) -> Result<Role, String> {
if _id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Role Identifier is Mandatory").to_string());
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "Role Identifier is Mandatory").to_string()
);
}
let mut _document = Role::from_id(_id);

Expand Down Expand Up @@ -166,8 +168,10 @@ pub async fn role_from_id(_id: Option<&String>, _client_id: Option<&String>, _di
}

async fn get_index_name(_client_id: Option<&String>) -> Result<String, std::io::Error> {
if _client_id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Client is Mandatory"));
if _client_id.is_none() || _client_id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "Client is Mandatory")
);
}

let _base_index: String = "role".to_string();
Expand Down
6 changes: 4 additions & 2 deletions src/models/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ pub struct Table {
}

pub async fn window_from_id(_id: Option<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Window, String> {
if _id.is_none() {
return Err(Error::new(ErrorKind::InvalidData.into(), "Window Identifier is Mandatory").to_string());
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Error::new(ErrorKind::InvalidData.into(), "Window Identifier is Mandatory").to_string()
);
}
let mut _document = Window::from_id(_id.to_owned());

Expand Down

0 comments on commit fd84deb

Please sign in to comment.