Skip to content

Commit

Permalink
add parsing of bools
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed May 13, 2024
1 parent dbf6baf commit bbd7dcc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions crates/graphql_artifact_generation/src/generate_artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ pub(crate) fn get_serialized_field_arguments(
{indent_1}],\n"
)
}
NonConstantValue::Boolean(bool) => {
let bool_string = bool.to_string();
format!(
"\n\
{indent_1}[\n\
{indent_2}\"{argument_name}\",\n\
{indent_2}\"{{ kind: \"Literal\", value: {bool_string} }},\n\
{indent_1}],\n"
)
}
};

s.push_str(&arg_value);
Expand Down
1 change: 1 addition & 0 deletions crates/graphql_artifact_generation/src/query_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,6 @@ fn serialize_non_constant_value_for_graphql(value: &NonConstantValue) -> String
match value {
NonConstantValue::Variable(variable_name) => format!("${}", variable_name),
NonConstantValue::Integer(int_value) => int_value.to_string(),
NonConstantValue::Boolean(bool) => bool.to_string(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum IsographLiteralParseError {
suggested_const_export_name: ScalarFieldName,
},

#[error("Expected a valid value, like $foo or 42")]
#[error("Expected a valid value, like $foo, 42, true or false")]
ExpectedNonConstantValue,

#[error("Descriptions are currently disallowed")]
Expand All @@ -58,6 +58,9 @@ pub enum IsographLiteralParseError {
this client field declaration"
)]
DuplicateNameOrAlias { name_or_alias: FieldNameOrAlias },

#[error("Expected a boolean value (true or false).")]
ExpectedBoolean,
}

impl From<LowLevelParseError> for IsographLiteralParseError {
Expand Down
16 changes: 16 additions & 0 deletions crates/isograph_lang_parser/src/parse_iso_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,22 @@ fn parse_non_constant_value(
}))
})?;

to_control_flow::<_, WithSpan<IsographLiteralParseError>>(|| {
let bool = tokens
.parse_source_of_kind(IsographLangTokenKind::Identifier)
.map_err(|with_span| with_span.map(IsographLiteralParseError::from))?;

let span = bool.span;

bool.and_then(|bool| match bool.parse::<bool>() {
Ok(b) => Ok(NonConstantValue::Boolean(b)),
Err(_) => Err(WithSpan::new(
IsographLiteralParseError::ExpectedBoolean,
span,
)),
})
})?;

ControlFlow::Continue(WithSpan::new(
IsographLiteralParseError::ExpectedNonConstantValue,
Span::todo_generated(),
Expand Down
3 changes: 3 additions & 0 deletions crates/isograph_lang_types/src/client_field_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,15 @@ impl SelectionFieldArgument {
pub enum NonConstantValue {
Variable(VariableName),
Integer(u64),
Boolean(bool),
}

impl NonConstantValue {
pub fn reachable_variables(&self) -> Vec<VariableName> {
match self {
NonConstantValue::Variable(name) => vec![*name],
NonConstantValue::Integer(_) => vec![],
NonConstantValue::Boolean(_) => vec![],
}
}

Expand All @@ -265,6 +267,7 @@ impl NonConstantValue {
NonConstantValue::Variable(name) => format!("v_{}", name),
// l for literal, i.e. this is shared with others
NonConstantValue::Integer(int_value) => format!("l_{}", int_value),
NonConstantValue::Boolean(bool) => format!("l_{}", bool),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/isograph_lang_types/src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl<'de> Deserializer<'de> for NonConstantValueDeserializer<'de> {
match self.value {
NonConstantValue::Variable(_variable) => todo!("Variable?"),
NonConstantValue::Integer(u_64) => visitor.visit_u64(*u_64),
NonConstantValue::Boolean(bool) => visitor.visit_bool(*bool),
}
}

Expand Down

0 comments on commit bbd7dcc

Please sign in to comment.