Skip to content

Commit

Permalink
fix: default argument
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Jan 24, 2024
1 parent 4a9c688 commit d7ca3bc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 17 deletions.
41 changes: 31 additions & 10 deletions graphqxl_parser/src/ast_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ use crate::utils::{unknown_rule_error, OwnedSpan};
use crate::{parse_directive, parse_value_type, Directive, ValueType};
use pest::iterators::Pair;

#[derive(Debug, Clone, PartialEq)]
pub enum ArgumentDefaultValue {
None,
ValueData(ValueData),
Identifier(Identifier),
}

#[derive(Debug, Clone, PartialEq)]
pub struct Argument {
pub span: OwnedSpan,
pub name: Identifier,
pub description: String,
pub value_type: ValueType,
pub default: Option<ValueData>,
pub default: ArgumentDefaultValue,
pub directives: Vec<Directive>,
}

Expand All @@ -23,7 +30,7 @@ impl Argument {
name: Identifier::from(name),
description: "".to_string(),
value_type: t,
default: None,
default: ArgumentDefaultValue::None,
directives: Vec::new(),
}
}
Expand Down Expand Up @@ -53,8 +60,8 @@ impl Argument {
self.clone()
}

pub fn default(&mut self, value_data: ValueData) -> Self {
self.default = Some(value_data);
pub fn default(&mut self, default: ArgumentDefaultValue) -> Self {
self.default = default;
self.clone()
}

Expand All @@ -75,11 +82,13 @@ fn parse_argument(pair: Pair<Rule>, file: &str) -> Result<Argument, Box<RuleErro
// at this moment we are on [identifier, value]
let name = parse_identifier(next.unwrap(), file)?;
let value = parse_value_type(childs.next().unwrap(), file)?;
let mut default = None;
let mut default = ArgumentDefaultValue::None;
let mut directives = Vec::new();
if let Some(pair) = childs.next() {
if let Rule::value_data = pair.as_rule() {
default = Some(parse_value_data(pair, file)?)
default = ArgumentDefaultValue::ValueData(parse_value_data(pair, file)?)
} else if let Rule::identifier = pair.as_rule() {
default = ArgumentDefaultValue::Identifier(parse_identifier(pair, file)?)
}
for directive in childs {
directives.push(parse_directive(directive, file)?);
Expand Down Expand Up @@ -154,9 +163,19 @@ mod tests {
fn test_default_value_for_argument_works() {
assert_eq!(
parse_input("(arg: String = \"default\")"),
Ok(vec![
Argument::string("arg").default(ValueData::string("default"))
])
Ok(vec![Argument::string("arg").default(
ArgumentDefaultValue::ValueData(ValueData::string("default"))
)])
);
}

#[test]
fn test_default_value_as_identifier_for_argument_works() {
assert_eq!(
parse_input("(arg: String = Foo)"),
Ok(vec![Argument::string("arg").default(
ArgumentDefaultValue::Identifier(Identifier::from("Foo"))
)])
);
}

Expand All @@ -165,7 +184,9 @@ mod tests {
assert_eq!(
parse_input("(arg: String = \"default\" @dir1 @dir2)"),
Ok(vec![Argument::string("arg")
.default(ValueData::string("default"))
.default(ArgumentDefaultValue::ValueData(ValueData::string(
"default"
)))
.directive(Directive::build("dir1"))
.directive(Directive::build("dir2"))])
);
Expand Down
6 changes: 4 additions & 2 deletions graphqxl_parser/src/ast_block_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ mod tests {
use super::*;
use crate::ast_value_data::ValueData;
use crate::utils::parse_full_input;
use crate::ArgumentDefaultValue;

fn parse_with_args_input(input: &str) -> Result<BlockField, Box<RuleError>> {
parse_full_input(input, Rule::field_with_args, parse_block_field)
Expand Down Expand Up @@ -198,8 +199,9 @@ mod tests {
Ok(BlockField::build("field")
.string()
.arg(
Argument::build("arg1", ValueType::string().array().non_nullable())
.default(ValueData::string("default").list())
Argument::build("arg1", ValueType::string().array().non_nullable()).default(
ArgumentDefaultValue::ValueData(ValueData::string("default").list())
)
)
.arg(Argument::build("arg2", ValueType::float().non_nullable())))
);
Expand Down
2 changes: 1 addition & 1 deletion graphqxl_parser/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ field_without_args = { description? ~ identifier ~ ":" ~ value_type ~ directive*
field_without_args_without_value = { description? ~ identifier ~ directive* }

arguments = { "(" ~ argument* ~ ")" }
argument = { description? ~ identifier ~ ":" ~ value_type ~ ("=" ~ value_data)? ~ directive* }
argument = { description? ~ identifier ~ ":" ~ value_type ~ ("=" ~ (value_data | identifier))? ~ directive* }

function_call = { "(" ~ function_input+ ~ ")" }
function_input = { identifier ~ ":" ~ value_data }
Expand Down
15 changes: 11 additions & 4 deletions graphqxl_synthesizer/src/synth_arguments.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::synth_directive::DirectiveSynth;
use crate::synth_identifier::IdentifierSynth;
use crate::synth_value_data::ValueDataSynth;
use crate::synth_value_type::ValueTypeSynth;
use crate::synths::{
ChainSynth, MultilineListSynth, OneLineListSynth, StringSynth, Synth, SynthContext,
};
use graphqxl_parser::Argument;
use graphqxl_parser::{Argument, ArgumentDefaultValue};

pub(crate) struct ArgumentsSynth(pub(crate) Vec<Argument>);

Expand All @@ -18,10 +19,14 @@ impl Synth for ArgumentsSynth {
Box::new(StringSynth(argument.name.id.clone() + ": ")),
Box::new(ValueTypeSynth(argument.value_type.clone())),
];
if let Some(default) = &argument.default {
if let ArgumentDefaultValue::ValueData(default) = &argument.default {
v.push(Box::new(StringSynth::from(" = ")));
v.push(Box::new(ValueDataSynth(default.clone())));
} else if let ArgumentDefaultValue::Identifier(default) = &argument.default {
v.push(Box::new(StringSynth::from(" = ")));
v.push(Box::new(IdentifierSynth(default.clone())));
}

for directive in argument.directives.iter() {
v.push(Box::new(StringSynth::from(" ")));
v.push(Box::new(DirectiveSynth(directive.clone())));
Expand Down Expand Up @@ -68,7 +73,9 @@ mod tests {

#[test]
fn test_with_default_value() {
let synth = ArgumentsSynth(vec![Argument::int("arg").default(ValueData::int(1).list())]);
let synth =
ArgumentsSynth(vec![Argument::int("arg")
.default(ArgumentDefaultValue::ValueData(ValueData::int(1).list()))]);
assert_eq!(synth.synth_zero(), "(arg: Int = [ 1 ])")
}

Expand All @@ -81,7 +88,7 @@ mod tests {
#[test]
fn test_with_default_value_with_directives() {
let synth = ArgumentsSynth(vec![Argument::int("arg")
.default(ValueData::int(1).list())
.default(ArgumentDefaultValue::ValueData(ValueData::int(1).list()))
.directive(Directive::build("dir"))]);
assert_eq!(synth.synth_zero(), "(arg: Int = [ 1 ] @dir)")
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/default-arg.graphqxl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum Enum {
One
Other
}

type Query {
foo(bar: Enum! = One): Boolean
}
9 changes: 9 additions & 0 deletions src/test/default-arg.graphqxl.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum Enum {
One
Other
}

type Query {
foo(bar: Enum! = One): Boolean
}

0 comments on commit d7ca3bc

Please sign in to comment.