Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
imor committed Sep 14, 2023
1 parent 6913cf6 commit cd1a47b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
37 changes: 8 additions & 29 deletions supabase-wrappers/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ impl From<OptionsError> for ErrorReport {
/// ```rust,no_run
/// # use supabase_wrappers::prelude::require_option;
/// # use std::collections::HashMap;
/// # fn main() -> Result<(), OptionsError> {
/// # let options = &HashMap::new();
/// require_option("my_option", options);
/// require_option("my_option", options)?;
/// # Ok(())
/// # }
/// ```
pub fn require_option<'map>(
opt_name: &str,
Expand All @@ -58,31 +61,8 @@ pub fn require_option<'map>(
.ok_or(OptionsError::OptionNameNotFound(opt_name.to_string()))
}

/// Get required option value from the `options` map or a provided default
///
/// Get the required option's value from `options` map, return default if it does not exist.
///
/// For example,
///
/// ```rust,no_run
/// # use supabase_wrappers::prelude::require_option_or;
/// # use std::collections::HashMap;
/// # let options = &HashMap::new();
/// require_option_or("my_option", options, "default value".to_string());
/// ```
pub fn require_option_or(
opt_name: &str,
options: &HashMap<String, String>,
default: String,
) -> String {
options
.get(opt_name)
.map(|t| t.to_owned())
.unwrap_or(default)
}

/// Check if the option list contains a specific option, used in [validator](crate::interface::ForeignDataWrapper::validator)
pub fn check_options_contain(opt_list: &[Option<String>], tgt: &str) {
pub fn check_options_contain(opt_list: &[Option<String>], tgt: &str) -> Result<bool, OptionsError> {
let search_key = tgt.to_owned() + "=";
if !opt_list.iter().any(|opt| {
if let Some(s) = opt {
Expand All @@ -91,11 +71,10 @@ pub fn check_options_contain(opt_list: &[Option<String>], tgt: &str) {
false
}
}) {
report_error(
PgSqlErrorCode::ERRCODE_FDW_OPTION_NAME_NOT_FOUND,
&format!("required option \"{}\" is not specified", tgt),
);
Err(OptionsError::OptionNameNotFound(tgt.to_string()))
}

Ok(true)
}

// convert options definition to hashmap
Expand Down
9 changes: 4 additions & 5 deletions wrappers/src/fdw/firebase_fdw/firebase_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,10 @@ impl FirebaseFdw {
// ref: https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/listDocuments
let re = Regex::new(r"^firestore/(?P<collection>[^/]+)").unwrap();
if let Some(caps) = re.captures(obj) {
let base_url = require_option_or(
"base_url",
options,
Self::DEFAULT_FIRESTORE_BASE_URL.to_owned(),
);
let base_url = options
.get("base_url")
.map(|v| v.as_ref())
.unwrap_or(Self::DEFAULT_FIRESTORE_BASE_URL);
let collection = caps.name("collection").unwrap().as_str();
let mut ret = format!(
"{}/{}/databases/(default)/documents/{}?pageSize={}",
Expand Down

0 comments on commit cd1a47b

Please sign in to comment.