Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: support string interpolation in River #15

Closed
rfratto opened this issue Feb 13, 2023 · 4 comments
Closed

Proposal: support string interpolation in River #15

rfratto opened this issue Feb 13, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@rfratto
Copy link
Member

rfratto commented Feb 13, 2023

I'd like to see support for string interpolation added to River.

String interpolation is a quality-of-life feature which will make it easier to write expressions which return a string.

For example, this expression:

env("POD_NAMESPACE") + "/" + env("POD_NAME")

could be replaced with the following interpolated string:

"${env("POD_NAMESPACE")}/${env("POD_NAME")}" 

Syntax

String interpolation is initiated by placing ${EXPR} into a string. String interpolation can be escaped by escaping the dollar sign: \${EXPR} returns the literal string ${EXPR}.

This syntax matches the string interpolation syntax used by HCL.

The parser must keep track of pairs of curly braces to ensure that parsing the inner string does not end early; ${{a=5}} should be parsed as the expression {a=5} and not {a=5.

Evaluation

Each EXPR instead of ${EXPR} will be evaluated, converted to a string, and injected into the resulting output.

For example:

"3 + 5 is ${3 + 5}" 

evaluates to "3 + 5 is 8".

Each expression in an interpolated string should be converted into a string form which best represents that value. The representation for types should match the form of that value written in River. Functions and capsules should be converted to some description of the type of the function or capsule, similar to the description which displays in the UI.

Considerations

This would be a breaking change for users who are using some form of ${EXPR} in their configuration files already. I'm not sure if any of our existing Flow components would lead to someone using ${EXPR} inside of a string.

If this is going to be an issue, we can change the syntax used to denote a string that supports interpolation, such as any of the following:

  • C#-like: $"3 + 5 is {3 + 5}"
  • Javascript-like: `3 + 5 is ${3 + 5}`
@rfratto rfratto added the enhancement New feature or request label Feb 13, 2023
@tpaschalis tpaschalis changed the title River: support string interpolation Proposal: support string interpolation in River Feb 28, 2023
@rfratto
Copy link
Member Author

rfratto commented Apr 26, 2023

String interpolation as proposed here is mentioned in grafana/agent#2690.

@ptodev
Copy link

ptodev commented Sep 22, 2023

I think it might be better to have a special format() function if string interpolation is required:

format("string with {}, and {var2}", var1)

This has a few advantages over ${}:

  • Variables/expressions can be specified at the end of the function, leaving the initial string looking cleaner.
  • No breaking changes to River.
  • A format function could also be enhanced to use printf decorators like %.2f.

Disclaimer: my suggestion is inspired by Rust's format macro :)

Btw PowerShell also uses the ${} syntax for variables. But it also has $(), which is used for expressions. This post on StackOverflow explains it in more detail.

@ptodev
Copy link

ptodev commented Oct 3, 2023

@erikbaranowski pointed out to me that we already have a format function. I'd personally be in favour of adding a string interpolation feature to it, similar to what was described in the comment above.

@rfratto
Copy link
Member Author

rfratto commented Oct 20, 2023

I'm going to close this for now; it's not clear how necessary this is, especially given the format function exists in the stdlib. We can return to this in the future with a more fleshed out proposal if string interpolation is required.

@rfratto rfratto closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in Grafana Agent (Public) Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

2 participants