Skip to content

Conversation

@andrewmcgivery
Copy link
Contributor

@andrewmcgivery andrewmcgivery commented Jul 14, 2025


Checklist

Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.

  • PR description explains the motivation for the change and relevant context for reviewing
  • PR description links appropriate GitHub/Jira tickets (creating when necessary)
  • Changeset is included for user-facing changes
  • Changes are compatible1
  • Documentation2 completed
  • Performance impact assessed and acceptable
  • Metrics and logs are added3 and documented
  • Tests added and passing4
    • Unit tests
    • Integration tests
    • Manual tests, as necessary

Exceptions

Note any exceptions here

Notes

Footnotes

  1. It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this.

  2. Configuration is an important part of many changes. Where applicable please try to document configuration examples.

  3. A lot of (if not most) features benefit from built-in observability and debug-level logs. Please read this guidance on metrics best-practices.

  4. Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions.

@apollo-librarian
Copy link

apollo-librarian bot commented Jul 14, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: 59397bcbd3f5548f2a67810e

@github-actions
Copy link
Contributor

@andrewmcgivery, please consider creating a changeset entry in /.changesets/. These instructions describe the process and tooling.

@andrewmcgivery andrewmcgivery marked this pull request as ready for review July 14, 2025 21:49
@andrewmcgivery andrewmcgivery requested a review from a team as a code owner July 14, 2025 21:49
/// $(42)->toString() results in "42"
/// $("hello")->toString() results in "hello"
/// $(true)->toString() results in "true"
/// $(null)->toString() results in "null"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be empty string, actually, to match how our string templates (URL & headers) work. Ideally "{thing}" in a string template would be identical to thing->toString() in the rest of code. Then if we add in the ability to do template strings in body/selection/whatever as a way of formatting, that would also be consistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good case for us to have a single function which converts a JSON value to string and use that in all the places that need to be consistent. So call the same function from here and

fn to_string(value: &JSON, method_name: &str) -> Result<Option<String>, String> {
and
pub(crate) fn write_value<Output: Write>(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// $(null)->toString() results in "null"
/// $(null)->toString() results in ""

So this is accurate now?

/// $("hello")->toString() results in "hello"
/// $(true)->toString() results in "true"
/// $(null)->toString() results in "null"
/// $([1,2,3])->toString() results in "[1,2,3]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an error, for the same reason above. ->jsonStringify will give the json representation, and ->joinNotNull will give CSV, so whichever format they wanted, they can explicitly ask for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, forgot to update this! 😩

Comment on lines 52 to 64
JSON::Array(_) | JSON::Object(_) => {
return (
None,
vec![ApplyToError::new(
format!(
"Method ->{} cannot convert arrays or objects to strings. Use ->jsonStringify instead",
method_name.as_ref()
),
input_path.to_vec(),
method_name.range(),
)],
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the docstring up top was just wrong about arrays, so we'll want to correct that.

fn to_string_shape(
method_name: &WithRange<String>,
method_args: Option<&MethodArgs>,
_input_shape: Shape,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_input_shape: Shape,
input_shape: Shape,

}

#[test]
fn to_string_shape_should_return_string_for_any_input() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn to_string_shape_should_return_string_for_any_input() {
fn to_string_shape_should_return_string_for_int_input() {

assert_eq!(
get_shape(vec![], Shape::tuple([], [])),
Shape::error_with_partial(
"Method ->toString cannot convert arrays or objects to strings. Use ->jsonStringify instead"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Method ->toString cannot convert arrays or objects to strings. Use ->jsonStringify instead"
"Method ->toString cannot convert arrays or objects to strings. Use ->jsonStringify or ->joinNotNull instead"

I think in some cases, joining to like a,b,c is what the user might be looking for.

Complete side note, we should probably start including docs links somehow for some of these errors 🤔. Something to investigate at some point.

Copy link
Member

@dylan-apollo dylan-apollo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few more suggestions, other than those this LGTM

Comment on lines 82 to 93
pub(crate) fn json_to_string(json: &JSON) -> Result<Option<String>, String> {
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.".to_string())
}
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) fn json_to_string(json: &JSON) -> Result<Option<String>, String> {
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.".to_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."),
}
}

The callers don't even need a String it looks like, so might as well skip the allocation here.

}
}

pub(crate) fn json_to_string(json: &JSON) -> Result<Option<String>, String> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a doc comment /// about what this function is for & why?

/// $(42)->toString() results in "42"
/// $("hello")->toString() results in "hello"
/// $(true)->toString() results in "true"
/// $(null)->toString() results in "null"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// $(null)->toString() results in "null"
/// $(null)->toString() results in ""

So this is accurate now?

return Err("Expression is not allowed to evaluate to arrays or objects.".into());
}
match json_to_string(value) {
Ok(result) => write!(output, "{}", result.unwrap_or_default()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This write! stuff is actually a bit silly now... the whole reason I made it this way was to avoid allocating the extra String for each value, but json_to_string is doing that anyway now....

Maybe not worth refactoring all the code, though, to deal with it.

The "right" way is probably to have our own Value type with impl Display on it (which would either write to buffer (efficient) or allocate a new string (🤢) as needed. I'm still assuming we want that some day, but not today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps could be refactored in a future PR as needed? :D

Comment on lines 78 to 79
"Method ->{} requires an array of scalar values as input",
method_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Method ->{} requires an array of scalar values as input",
method_name
"Method ->{method_name} requires an array of scalar values as input",

Since you're here 😁

@andrewmcgivery andrewmcgivery merged commit 607c6ec into dev Jul 18, 2025
15 checks passed
@andrewmcgivery andrewmcgivery deleted the am/tostring_method branch July 18, 2025 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants