-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from supabase/fix/options-refactor
Options refactoring
- Loading branch information
Showing
15 changed files
with
267 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
use crate::prelude::*; | ||
use pgrx::pg_sys::panic::{ErrorReport, ErrorReportable}; | ||
use pgrx::pg_sys::panic::ErrorReport; | ||
use pgrx::prelude::*; | ||
|
||
use super::utils; | ||
|
||
// create a fdw instance | ||
pub(super) unsafe fn create_fdw_instance<E: Into<ErrorReport>, W: ForeignDataWrapper<E>>( | ||
ftable_id: pg_sys::Oid, | ||
) -> W { | ||
let ftable = pg_sys::GetForeignTable(ftable_id); | ||
let fserver = pg_sys::GetForeignServer((*ftable).serverid); | ||
let fserver_opts = utils::options_to_hashmap((*fserver).options); | ||
let fserver_opts = options_to_hashmap((*fserver).options).report_unwrap(); | ||
let wrapper = W::new(&fserver_opts); | ||
wrapper.map_err(|e| e.into()).report() | ||
wrapper.report_unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use pgrx::pg_sys::panic::ErrorReport; | ||
use pgrx::{pg_sys, PgList, PgSqlErrorCode}; | ||
use std::collections::HashMap; | ||
use std::ffi::CStr; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum OptionsError { | ||
#[error("required option `{0}` is not specified")] | ||
OptionNameNotFound(String), | ||
#[error("option name `{0}` is not a valid UTF-8 string")] | ||
OptionNameIsInvalidUtf8(String), | ||
#[error("option value `{0}` is not a valid UTF-8 string")] | ||
OptionValueIsInvalidUtf8(String), | ||
} | ||
|
||
impl From<OptionsError> for ErrorReport { | ||
fn from(value: OptionsError) -> Self { | ||
let error_message = format!("{value}"); | ||
match value { | ||
OptionsError::OptionNameNotFound(_) => ErrorReport::new( | ||
PgSqlErrorCode::ERRCODE_FDW_OPTION_NAME_NOT_FOUND, | ||
error_message, | ||
"", | ||
), | ||
OptionsError::OptionNameIsInvalidUtf8(_) | ||
| OptionsError::OptionValueIsInvalidUtf8(_) => ErrorReport::new( | ||
PgSqlErrorCode::ERRCODE_FDW_INVALID_STRING_FORMAT, | ||
error_message, | ||
"", | ||
), | ||
} | ||
} | ||
} | ||
|
||
/// Get required option value from the `options` map | ||
/// | ||
/// Get the required option's value from `options` map, return None and report | ||
/// error and stop current transaction if it does not exist. | ||
/// | ||
/// For example, | ||
/// | ||
/// ```rust,no_run | ||
/// # use supabase_wrappers::prelude::require_option; | ||
/// # use std::collections::HashMap; | ||
/// # use supabase_wrappers::options::OptionsError; | ||
/// # fn main() -> Result<(), OptionsError> { | ||
/// # let options = &HashMap::new(); | ||
/// require_option("my_option", options)?; | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn require_option<'map>( | ||
opt_name: &str, | ||
options: &'map HashMap<String, String>, | ||
) -> Result<&'map str, OptionsError> { | ||
options | ||
.get(opt_name) | ||
.map(|t| t.as_ref()) | ||
.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"); | ||
/// ``` | ||
pub fn require_option_or<'a>( | ||
opt_name: &str, | ||
options: &'a HashMap<String, String>, | ||
default: &'a str, | ||
) -> &'a str { | ||
options.get(opt_name).map(|t| t.as_ref()).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) -> Result<(), OptionsError> { | ||
let search_key = tgt.to_owned() + "="; | ||
if !opt_list.iter().any(|opt| { | ||
if let Some(s) = opt { | ||
s.starts_with(&search_key) | ||
} else { | ||
false | ||
} | ||
}) { | ||
Err(OptionsError::OptionNameNotFound(tgt.to_string())) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
// convert options definition to hashmap | ||
pub(super) unsafe fn options_to_hashmap( | ||
options: *mut pg_sys::List, | ||
) -> Result<HashMap<String, String>, OptionsError> { | ||
let mut ret = HashMap::new(); | ||
let options: PgList<pg_sys::DefElem> = PgList::from_pg(options); | ||
for option in options.iter_ptr() { | ||
let name = CStr::from_ptr((*option).defname); | ||
let value = CStr::from_ptr(pg_sys::defGetString(option)); | ||
let name = name.to_str().map_err(|_| { | ||
OptionsError::OptionNameIsInvalidUtf8( | ||
String::from_utf8_lossy(name.to_bytes()).to_string(), | ||
) | ||
})?; | ||
let value = value.to_str().map_err(|_| { | ||
OptionsError::OptionValueIsInvalidUtf8( | ||
String::from_utf8_lossy(value.to_bytes()).to_string(), | ||
) | ||
})?; | ||
ret.insert(name.to_string(), value.to_string()); | ||
} | ||
Ok(ret) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.