Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apollo-federation/src/connectors/json_selection/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ pub(crate) const fn json_type_name(v: &JSON) -> &str {
}
}

/// Provides a standard method to convert JSON to string.
/// Errors on arrays or objects because "stringigying" is not semantically the same as converting to a string.
/// null is returned as None but commonly, it gets converted to a blank string ("")
pub(crate) fn json_to_string(json: &JSON) -> Result<Option<String>, &'static str> {
match json {
JSON::Null => Ok(None),
JSON::Bool(b) => Ok(Some(b.to_string())),
JSON::Number(n) => Ok(Some(n.to_string())),
JSON::String(s) => Ok(Some(s.as_str().to_string())),
JSON::Array(_) | JSON::Object(_) => Err("cannot convert arrays or objects to strings."),
}
}

pub(crate) fn vec_push<T>(mut vec: Vec<T>, item: T) -> Vec<T> {
vec.push(item);
vec
Expand Down
4 changes: 4 additions & 0 deletions apollo-federation/src/connectors/json_selection/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub(super) enum ArrowMethod {
Gt,
Lt,
Not,
ToString,

// Future methods:
TypeOf,
Expand Down Expand Up @@ -167,6 +168,7 @@ impl std::ops::Deref for ArrowMethod {
Self::Gt => &public::GtMethod,
Self::Lt => &public::LtMethod,
Self::Not => &public::NotMethod,
Self::ToString => &public::ToStringMethod,

// Future methods:
Self::TypeOf => &future::TypeOfMethod,
Expand Down Expand Up @@ -224,6 +226,7 @@ impl ArrowMethod {
"ne" => Some(Self::Ne),
"gt" => Some(Self::Gt),
"lt" => Some(Self::Lt),
"toString" => Some(Self::ToString),
_ => None,
};

Expand Down Expand Up @@ -259,6 +262,7 @@ impl ArrowMethod {
| Self::Gt
| Self::Lt
| Self::Not
| Self::ToString
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::connectors::json_selection::ApplyToError;
use crate::connectors::json_selection::ApplyToInternal;
use crate::connectors::json_selection::MethodArgs;
use crate::connectors::json_selection::VarsWithPathsMap;
use crate::connectors::json_selection::helpers::json_to_string;
use crate::connectors::json_selection::immutable::InputPath;
use crate::connectors::json_selection::location::Ranged;
use crate::connectors::json_selection::location::WithRange;
Expand Down Expand Up @@ -72,16 +73,9 @@ fn join_not_null_method(
};

fn to_string(value: &JSON, method_name: &str) -> Result<Option<String>, String> {
match value {
JSON::Bool(b) => Ok(Some(b.then_some("true").unwrap_or("false").to_string())),
JSON::Number(number) => Ok(Some(number.to_string())),
JSON::String(byte_string) => Ok(Some(byte_string.as_str().to_string())),
JSON::Null => Ok(None),
JSON::Array(_) | JSON::Object(_) => Err(format!(
"Method ->{} requires an array of scalar values as input",
method_name
)),
}
json_to_string(value).map_err(|_| {
format!("Method ->{method_name} requires an array of scalar values as input",)
})
}

let joined = match data {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ mod lt;
pub(crate) use lt::LtMethod;
mod not;
pub(crate) use not::NotMethod;
mod to_string;
pub(crate) use to_string::ToStringMethod;
Loading