Skip to content

Commit 607c6ec

Browse files
Implement toString connectors method (#7888)
1 parent c7c334d commit 607c6ec

File tree

7 files changed

+430
-19
lines changed

7 files changed

+430
-19
lines changed

apollo-federation/src/connectors/json_selection/helpers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ pub(crate) const fn json_type_name(v: &JSON) -> &str {
7979
}
8080
}
8181

82+
/// Provides a standard method to convert JSON to string.
83+
/// Errors on arrays or objects because "stringigying" is not semantically the same as converting to a string.
84+
/// null is returned as None but commonly, it gets converted to a blank string ("")
85+
pub(crate) fn json_to_string(json: &JSON) -> Result<Option<String>, &'static str> {
86+
match json {
87+
JSON::Null => Ok(None),
88+
JSON::Bool(b) => Ok(Some(b.to_string())),
89+
JSON::Number(n) => Ok(Some(n.to_string())),
90+
JSON::String(s) => Ok(Some(s.as_str().to_string())),
91+
JSON::Array(_) | JSON::Object(_) => Err("cannot convert arrays or objects to strings."),
92+
}
93+
}
94+
8295
pub(crate) fn vec_push<T>(mut vec: Vec<T>, item: T) -> Vec<T> {
8396
vec.push(item);
8497
vec

apollo-federation/src/connectors/json_selection/methods.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub(super) enum ArrowMethod {
4949
Gt,
5050
Lt,
5151
Not,
52+
ToString,
5253

5354
// Future methods:
5455
TypeOf,
@@ -169,6 +170,7 @@ impl std::ops::Deref for ArrowMethod {
169170
Self::Gt => &public::GtMethod,
170171
Self::Lt => &public::LtMethod,
171172
Self::Not => &public::NotMethod,
173+
Self::ToString => &public::ToStringMethod,
172174

173175
// Future methods:
174176
Self::TypeOf => &future::TypeOfMethod,
@@ -227,6 +229,7 @@ impl ArrowMethod {
227229
"ne" => Some(Self::Ne),
228230
"gt" => Some(Self::Gt),
229231
"lt" => Some(Self::Lt),
232+
"toString" => Some(Self::ToString),
230233
_ => None,
231234
};
232235

@@ -263,6 +266,7 @@ impl ArrowMethod {
263266
| Self::Gt
264267
| Self::Lt
265268
| Self::Not
269+
| Self::ToString
266270
)
267271
}
268272
}

apollo-federation/src/connectors/json_selection/methods/public/join_not_null.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::connectors::json_selection::ApplyToError;
88
use crate::connectors::json_selection::ApplyToInternal;
99
use crate::connectors::json_selection::MethodArgs;
1010
use crate::connectors::json_selection::VarsWithPathsMap;
11+
use crate::connectors::json_selection::helpers::json_to_string;
1112
use crate::connectors::json_selection::immutable::InputPath;
1213
use crate::connectors::json_selection::location::Ranged;
1314
use crate::connectors::json_selection::location::WithRange;
@@ -72,16 +73,9 @@ fn join_not_null_method(
7273
};
7374

7475
fn to_string(value: &JSON, method_name: &str) -> Result<Option<String>, String> {
75-
match value {
76-
JSON::Bool(b) => Ok(Some(b.then_some("true").unwrap_or("false").to_string())),
77-
JSON::Number(number) => Ok(Some(number.to_string())),
78-
JSON::String(byte_string) => Ok(Some(byte_string.as_str().to_string())),
79-
JSON::Null => Ok(None),
80-
JSON::Array(_) | JSON::Object(_) => Err(format!(
81-
"Method ->{} requires an array of scalar values as input",
82-
method_name
83-
)),
84-
}
76+
json_to_string(value).map_err(|_| {
77+
format!("Method ->{method_name} requires an array of scalar values as input",)
78+
})
8579
}
8680

8781
let joined = match data {

apollo-federation/src/connectors/json_selection/methods/public/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ mod lt;
4141
pub(crate) use lt::LtMethod;
4242
mod not;
4343
pub(crate) use not::NotMethod;
44+
mod to_string;
45+
pub(crate) use to_string::ToStringMethod;

0 commit comments

Comments
 (0)