The FString format.
FString is a format created to extract data from complex data structures in order to create
well-formed files or strings.
The basic FString
- Any string is considered an FString.
- Any parts of the string starting with ‘{$’ and ending with ‘$}’ is a replacement directive.
- An FString support standard escape sequences like \n for newline.
The replacement directive
The replacement directive is any part of the string starting with ‘{$’ and ending with ‘$}’
The first part of this directive is the lookup key. This is a name referencing a value in the context
in which the FString is evaluated.
So if we were to evaluate the FString “Hello {$user_name$}” in the context
{
"user_name":"Bob"
}
We would get the string “Hello Bob”
There are some reserved lookup keys to enable different more complex replacements.
Key | Description |
---|---|
* or _ | Treat the whole input as the result |
0 1 2… | Look up the numbered element in a list |
name | Look for the key name in the context |
@ | The name of the container for this element. For instance if the input is from a fdl query it will be the column name |
Encoding hints
The lookup can be annotated with encoding hints.
Key | Description | _ Example | _ result |
---|---|---|---|
^ | Escape the content | {$^user_name$} | Bob |
^^ | Escape the content and put “ around it | {$^^user_name$} | “Bob” |
For each in collections
Some times the context contains lists of values.
{
"users":[
"Bob",
"Ben",
"Barbra"
}
The special character ‘|’ is used to repeat a string for each of the values in a list.
Key | Description |
---|---|
| | Repeat the folowing FString for each selected element |
¤ | Seperated by |
So the expression {$users|{$_$}¤, $} is interpreted as look up the key “users”, treat the value as a list and for each element in the list evaluate “{$_$}” meaning insert the value. Seperate these values by “, “.
The expression would yeald the string “Bob, Ben, Barbra”.
examples
Expression | What it does | result |
---|---|---|
{$*|”{$_$}“¤, $} | Treat the input as a list. For every element insert the value inside “” | “Bob”, “Ben”, “Barbra” |
Hi {$name$} | Replace with the value named “name” | Hi Bob |
Hi {$1/name$} | Treat the input as a list. Replace with the name of the second value. | Hi Ben |
{$*|{“{$@$}”:”{$_$}}“¤, $} | Treat the input as a list. For every element insert the column name then the value inside “” | {“name”:“Bob”}, {“name”:“Ben”}, {“name”:“Barbra”} |
Post your comment on this topic.