Skip to content

Commit

Permalink
use an enum to replace a tuple to make the code easy to read
Browse files Browse the repository at this point in the history
  • Loading branch information
imor committed Apr 9, 2024
1 parent de7d152 commit 7a7b1a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
9 changes: 4 additions & 5 deletions src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,11 +1374,10 @@ fn function_args(schema: &Arc<__Schema>, func: &Arc<Function>) -> Vec<__InputVal
})
})
.map(|(arg_type, arg_name, arg_default)| {
let default_value = if let Some((default_value, is_null)) = arg_default {
if is_null {
None
} else {
Some(default_value)
let default_value = if let Some(default_value) = arg_default {
match default_value {
DefaultValue::Value(value) => Some(value),
DefaultValue::Null => None,
}
} else {
None
Expand Down
38 changes: 25 additions & 13 deletions src/sql_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct Function {
}

impl Function {
pub fn args(&self) -> impl Iterator<Item = (u32, &str, Option<&str>, Option<(String, bool)>)> {
pub fn args(&self) -> impl Iterator<Item = (u32, &str, Option<&str>, Option<DefaultValue>)> {
ArgsIterator::new(
&self.arg_types,
&self.arg_type_names,
Expand Down Expand Up @@ -200,7 +200,13 @@ struct ArgsIterator<'a> {
arg_types: &'a [u32],
arg_type_names: &'a Vec<String>,
arg_names: &'a Option<Vec<String>>,
arg_defaults: Vec<Option<(String, bool)>>,
arg_defaults: Vec<Option<DefaultValue>>,
}

#[derive(Clone)]
pub(crate) enum DefaultValue {
Value(String),
Null,
}

impl<'a> ArgsIterator<'a> {
Expand Down Expand Up @@ -230,7 +236,7 @@ impl<'a> ArgsIterator<'a> {
arg_defaults: &'a Option<String>,
num_default_args: usize,
num_total_args: usize,
) -> Vec<Option<(String, bool)>> {
) -> Vec<Option<DefaultValue>> {
let mut defaults = vec![None; num_total_args];
let Some(arg_defaults) = arg_defaults else {
return defaults;
Expand All @@ -255,20 +261,26 @@ impl<'a> ArgsIterator<'a> {
defaults
}

fn sql_to_graphql_default(default_str: &str, type_oid: u32) -> Option<(String, bool)> {
fn sql_to_graphql_default(default_str: &str, type_oid: u32) -> Option<DefaultValue> {
let trimmed = default_str.trim();
if trimmed.starts_with("NULL::") {
return Some(("".to_string(), true));
return Some(DefaultValue::Null);
}
match type_oid {
21 | 23 => trimmed.parse::<i32>().ok().map(|i| (i.to_string(), false)),
16 => trimmed.parse::<bool>().ok().map(|i| (i.to_string(), false)),
700 | 701 => trimmed.parse::<f64>().ok().map(|i| (i.to_string(), false)),
21 | 23 => trimmed
.parse::<i32>()
.ok()
.map(|i| DefaultValue::Value(i.to_string())),
16 => trimmed
.parse::<bool>()
.ok()
.map(|i| DefaultValue::Value(i.to_string())),
700 | 701 => trimmed
.parse::<f64>()
.ok()
.map(|i| DefaultValue::Value(i.to_string())),
25 => trimmed.strip_suffix("::text").map(|i| {
(
format!("\"{}\"", i.trim_matches(',').trim_matches('\'')),
false,
)
DefaultValue::Value(format!("\"{}\"", i.trim_matches(',').trim_matches('\'')))
}),
_ => None,
}
Expand All @@ -280,7 +292,7 @@ lazy_static! {
}

impl<'a> Iterator for ArgsIterator<'a> {
type Item = (u32, &'a str, Option<&'a str>, Option<(String, bool)>);
type Item = (u32, &'a str, Option<&'a str>, Option<DefaultValue>);

fn next(&mut self) -> Option<Self::Item> {
if self.index < self.arg_types.len() {
Expand Down

0 comments on commit 7a7b1a7

Please sign in to comment.