Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## v3.0.0-rc.2

### Fixed

- Identifiers may now start with underscores, so attribute access on
WhatsApp webhook vendor keys such as `@event.message._vnd.v1.chat`
parses fully instead of stopping at the underscore. A bare `_` is
still not an identifier: `@(_)` remains literal text, and unresolved
variables like `@_missing` render back as-is.

## v3.0.0-rc.0

This is the first release candidate for v3.0.0. It contains the breaking
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ by adding `expression` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:expression, "~> 3.0.0-rc.1"}
{:expression, "~> 3.0.0-rc.2"}
]
end
```
Expand Down
7 changes: 6 additions & 1 deletion lib/expression/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ defmodule Expression.Parser do
)

# atom = atom
#
# Leading underscores are valid (WhatsApp webhook payloads have keys such as
# `_vnd`) but an atom needs at least one letter or digit, so that a bare `_`
# keeps failing to parse and `@(_)` remains literal text.
atom =
ascii_string([?a..?z, ?A..?Z, ?0..?9], min: 1)
ascii_string([?_], min: 0)
|> ascii_string([?a..?z, ?A..?Z, ?0..?9], min: 1)
|> ascii_string([?a..?z, ?A..?Z, ?0..?9, ?_, ?-], min: 0)
|> map({String, :downcase, []})
|> reduce({Enum, :join, []})
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Expression.MixProject do
use Mix.Project

@version "3.0.0-rc.1"
@version "3.0.0-rc.2"

def project do
[
Expand Down
30 changes: 30 additions & 0 deletions test/expression/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ defmodule Expression.ParserTest do
assert_ast([expression: [atom: "foo"]], "@foo")
end

test "expression with a leading underscore" do
assert_ast([expression: [atom: "_foo"]], "@_foo")
assert_ast([expression: [atom: "__foo"]], "@__foo")
end

test "escaped at" do
assert_ast([text: "user", text: "@", text: "example.org"], "user@@example.org")
end
Expand Down Expand Up @@ -419,6 +424,31 @@ defmodule Expression.ParserTest do
)
end

test "on keys with leading underscores" do
assert_ast(
[
expression: [
attribute: [
attribute: [
attribute: [
attribute: [atom: "event", atom: "message"],
atom: "_vnd"
],
atom: "v1"
],
atom: "chat"
]
]
],
"@event.message._vnd.v1.chat"
)

assert_ast(
[expression: [attribute: [atom: "foo", atom: "_bar"]]],
"@(foo._bar)"
)
end

test "on functions" do
assert_ast(
[
Expand Down
16 changes: 16 additions & 0 deletions test/expression_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ defmodule ExpressionTest do
assert "bar" == Expression.evaluate_as_string!("@foo[1]", %{"foo" => ["baz", "bar"]})
end

test "attributes with leading underscores" do
context = %{
"event" => %{
"message" => %{"_vnd" => %{"v1" => %{"chat" => %{"state" => "OPEN"}}}}
}
}

assert "OPEN" ==
Expression.evaluate_as_string!("@event.message._vnd.v1.chat.state", context)

assert %{"state" => "OPEN"} ==
Expression.evaluate_block!("event.message._vnd.v1.chat", context)

assert "@_missing" == Expression.evaluate_as_string!("@_missing", %{})
end

test "list with variable" do
assert "bar" =
Expression.evaluate_as_string!("@foo[cursor]", %{
Expand Down