Skip to content

Commit

Permalink
fix: allow arbitrary context keys (#388)
Browse files Browse the repository at this point in the history
* allow arbitrary names context keys that contain whitespace or symbols
  • Loading branch information
p3trur0 committed Jan 27, 2022
1 parent 4cd12fb commit d538afe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ object FeelParser {
// use only if the identifier is followed by a predefined character (e.g. `(` or `:`)
private def identifierWithWhitespaces[_: P]: P[String] =
P(
identifier ~~ (" " ~~ identifier).repX(1)
identifier ~~ (" ".repX(1) ~~ identifier).repX(1)
).!

private def name[_: P]: P[String] = P(
Expand Down Expand Up @@ -423,9 +423,19 @@ object FeelParser {
).map(entries => ConstContext(entries.toList))

private def contextEntry[_: P]: P[(String, Exp)] = P(
(name | stringWithQuotes) ~ ":" ~ expression
(contextKeyAnySymbol | identifierWithWhitespaces | name | stringWithQuotes) ~ ":" ~ expression
)

private def contextKeyAnySymbol[_: P]: P[String] =
P(
(!reservedSymbol ~ AnyChar).rep(1)
).!

private def reservedSymbol[_: P]: P[String] =
P(
CharIn("\"", "{", "}", ":", ",", "[", "]", "`")
).!

private def variableRef[_: P]: P[Exp] =
P(
qualifiedName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,29 @@ class InterpreterContextExpressionTest
)
}

it should "be accessed when defined with a name having white spaces" in {
eval("{foo bar:1}.`foo bar` = 1") should be(ValBoolean(true))
eval("{foo bar:1}.`foo bar` = 1") should be(ValBoolean(true))
eval("{foo bar:1, fizz buzz: 30}.`fizz buzz` = 1") should be(
ValBoolean(false))
}

it should "be accessed when defined with a name having special symbols" in {
eval("{foo+bar:1}.`foo+bar` = 1") should be(ValBoolean(true))
eval("{foo+bar:1, simple_special++char:4}.`simple_special++char` = 4") should be(
ValBoolean(true))
eval("""{\uD83D\uDC0E:"\uD83D\uDE00"}.`\uD83D\uDC0E`""") should be(
ValString("\uD83D\uDE00"))

eval(
"{ friend+of+mine:2, hello_there:{ how_are_you?:2, are_you_happy?:`friend+of+mine`+3 } }.hello_there.`are_you_happy?`") should be(
ValNumber(5))
}

it should "fail when special symbols violate context syntax" in {
eval("{foo{bar:1}.`foo{bar` = 1") shouldBe a[ValError]
eval("{foo,bar:1}.`foo,bar` = 1") shouldBe a[ValError]
eval("{foo:bar:1}.`foo:bar` = 1") shouldBe a[ValError]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ class InterpreterFunctionTest
eval("f(test name:2)", functions = functions) should be(ValNumber(3))
}

it should "be invoked with one named parameter containing more than one whitespace" in {
val functions =
Map("f" -> eval("""function(test name yada) `test name yada` + 1""").asInstanceOf[ValFunction])

eval("f(test name yada:1)", functions = functions) should be(ValNumber(2))
eval("f(test name yada:2)", functions = functions) should be(ValNumber(3))
}

"An external java function definition" should "be invoked with one double parameter" in {

val functions = Map(
Expand Down

0 comments on commit d538afe

Please sign in to comment.