Skip to content

Add support for multiline string literals #367

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

Closed
wants to merge 1 commit into from

Conversation

spaceweasel
Copy link

Currently, an env variable can hold a multiline string, for example:

func main() {
	env := map[string]interface{}{
		"lines": `Here are
two lines`,
	}

	out, _ := expr.Eval("lines", env)
	fmt.Print(out)
}

Which prints out:

Here are
two lines

Whereas if I want to use a multiline string literal, then an error is returned.

	out, err := expr.Eval("'Here are\ntwo lines'", env)
	if err != nil {
		panic(err)
	}
panic: literal not terminated (2:1)
 | two lines'
 | ^

A workaround is to use double backslashes:

	out, err := expr.Eval("'Here are\\ntwo lines'", env)

Whilst this gives the desired output in this situation, it is impractical for our case.
Apart from the excessive line length, there are other issues like readability.

We have a requirement where the input strings do not exist in Go code, but are read from other sources (e.g. yaml files) and use the map function on a collection. An example would be something like this, but with many more lines and arguments:

map(things, sprintf(`
Thing Name: %s
Thing Size: %d`, 
.name, 
.size))

This PR adds support for multiline string literals through the backtick character, so it will handle our situation as well as more simple ones:

	out, err := expr.Eval("`Here are\ntwo lines`", env)

@antonmedv
Copy link
Member

I'd like to use ` char for raw strings. Now only multiline strings. Let's implement those instead.

@@ -23,6 +23,10 @@ func TestParse(t *testing.T) {
`"str"`,
&StringNode{Value: "str"},
},
{
"`multi\nline\nstring`",
Copy link
Member

Choose a reason for hiding this comment

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

Tests for raw strings.

@antonmedv
Copy link
Member

Implemented in #485

@antonmedv antonmedv closed this Dec 25, 2023
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.

2 participants