Skip to content

Commit 7a7b1a7

Browse files
committed
use an enum to replace a tuple to make the code easy to read
1 parent de7d152 commit 7a7b1a7

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/graphql.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,11 +1374,10 @@ fn function_args(schema: &Arc<__Schema>, func: &Arc<Function>) -> Vec<__InputVal
13741374
})
13751375
})
13761376
.map(|(arg_type, arg_name, arg_default)| {
1377-
let default_value = if let Some((default_value, is_null)) = arg_default {
1378-
if is_null {
1379-
None
1380-
} else {
1381-
Some(default_value)
1377+
let default_value = if let Some(default_value) = arg_default {
1378+
match default_value {
1379+
DefaultValue::Value(value) => Some(value),
1380+
DefaultValue::Null => None,
13821381
}
13831382
} else {
13841383
None

src/sql_types.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub struct Function {
8888
}
8989

9090
impl Function {
91-
pub fn args(&self) -> impl Iterator<Item = (u32, &str, Option<&str>, Option<(String, bool)>)> {
91+
pub fn args(&self) -> impl Iterator<Item = (u32, &str, Option<&str>, Option<DefaultValue>)> {
9292
ArgsIterator::new(
9393
&self.arg_types,
9494
&self.arg_type_names,
@@ -200,7 +200,13 @@ struct ArgsIterator<'a> {
200200
arg_types: &'a [u32],
201201
arg_type_names: &'a Vec<String>,
202202
arg_names: &'a Option<Vec<String>>,
203-
arg_defaults: Vec<Option<(String, bool)>>,
203+
arg_defaults: Vec<Option<DefaultValue>>,
204+
}
205+
206+
#[derive(Clone)]
207+
pub(crate) enum DefaultValue {
208+
Value(String),
209+
Null,
204210
}
205211

206212
impl<'a> ArgsIterator<'a> {
@@ -230,7 +236,7 @@ impl<'a> ArgsIterator<'a> {
230236
arg_defaults: &'a Option<String>,
231237
num_default_args: usize,
232238
num_total_args: usize,
233-
) -> Vec<Option<(String, bool)>> {
239+
) -> Vec<Option<DefaultValue>> {
234240
let mut defaults = vec![None; num_total_args];
235241
let Some(arg_defaults) = arg_defaults else {
236242
return defaults;
@@ -255,20 +261,26 @@ impl<'a> ArgsIterator<'a> {
255261
defaults
256262
}
257263

258-
fn sql_to_graphql_default(default_str: &str, type_oid: u32) -> Option<(String, bool)> {
264+
fn sql_to_graphql_default(default_str: &str, type_oid: u32) -> Option<DefaultValue> {
259265
let trimmed = default_str.trim();
260266
if trimmed.starts_with("NULL::") {
261-
return Some(("".to_string(), true));
267+
return Some(DefaultValue::Null);
262268
}
263269
match type_oid {
264-
21 | 23 => trimmed.parse::<i32>().ok().map(|i| (i.to_string(), false)),
265-
16 => trimmed.parse::<bool>().ok().map(|i| (i.to_string(), false)),
266-
700 | 701 => trimmed.parse::<f64>().ok().map(|i| (i.to_string(), false)),
270+
21 | 23 => trimmed
271+
.parse::<i32>()
272+
.ok()
273+
.map(|i| DefaultValue::Value(i.to_string())),
274+
16 => trimmed
275+
.parse::<bool>()
276+
.ok()
277+
.map(|i| DefaultValue::Value(i.to_string())),
278+
700 | 701 => trimmed
279+
.parse::<f64>()
280+
.ok()
281+
.map(|i| DefaultValue::Value(i.to_string())),
267282
25 => trimmed.strip_suffix("::text").map(|i| {
268-
(
269-
format!("\"{}\"", i.trim_matches(',').trim_matches('\'')),
270-
false,
271-
)
283+
DefaultValue::Value(format!("\"{}\"", i.trim_matches(',').trim_matches('\'')))
272284
}),
273285
_ => None,
274286
}
@@ -280,7 +292,7 @@ lazy_static! {
280292
}
281293

282294
impl<'a> Iterator for ArgsIterator<'a> {
283-
type Item = (u32, &'a str, Option<&'a str>, Option<(String, bool)>);
295+
type Item = (u32, &'a str, Option<&'a str>, Option<DefaultValue>);
284296

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

0 commit comments

Comments
 (0)