Skip to content

Commit

Permalink
Merge pull request #31 from pedro-gutierrez/generic-attributes
Browse files Browse the repository at this point in the history
Generic attributes
  • Loading branch information
pedro-gutierrez authored Dec 26, 2024
2 parents 5c9786a + 89aad12 commit 3431ca6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion guides/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The package can be installed by adding `diesel` to your list of dependencies in
```elixir
def deps do
[
{:diesel, "~> 0.7"}
{:diesel, "~> 0.8"}
]
end
```
Expand Down
12 changes: 12 additions & 0 deletions guides/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ state name: :pending do
end
```

**Note**: since 0.8.0, Diesel supports generic attributes, where the name or the kind are not predefined. This can be set by using the `:*` notation:

```elixir
defmodule MyApp.MyDsl.MyTag do
use Diesel.Tag

tag do
attribute name: :*, kind: :*, min: 1
end
end
```


#### The action tag

Expand Down
9 changes: 7 additions & 2 deletions lib/diesel/tag/validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ defmodule Diesel.Tag.Validator do
end

defp validate_expected_attributes(specs, attrs) do
specs = Enum.reject(specs, fn spec -> spec[:name] == :* end)

Enum.reduce_while(specs, :ok, fn spec, _ ->
attr_name = spec[:name]
default = Keyword.get(spec, :default, nil)
Expand Down Expand Up @@ -163,10 +165,13 @@ defmodule Diesel.Tag.Validator do
end

defp expected_name?(name, specs),
do: Enum.find(specs, fn spec -> is_nil(spec[:name]) || spec[:name] == name end)
do:
Enum.find(specs, fn spec ->
is_nil(spec[:name]) || spec[:name] == :* || spec[:name] == name
end)

defp expected_child_kind?(kind, specs) do
Enum.find(specs, fn spec -> spec[:kind] == :any || spec[:kind] == kind end)
Enum.find(specs, fn spec -> spec[:kind] in [:any, :*] || spec[:kind] == kind end)
end

defp tag_kind({_, _, _}), do: :tag
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 Diesel.MixProject do
use Mix.Project

@version "0.7.2"
@version "0.8.0"

def project do
[
Expand Down
7 changes: 7 additions & 0 deletions test/diesel/validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@ defmodule Diesel.ValidatorTest do
schema = {:tag, [], [{:child, [kind: :any, min: 2, max: 2], []}]}
assert :ok == Validator.validate(node, schema)
end

test "allows generic attributes with unspecified names and kinds" do
node = {:conditions, [{:a, 1}, {:b, 2}], []}
schema = {:tag, [], [{:attribute, [name: :*, kind: :*, min: 1], []}]}

assert :ok == Validator.validate(node, schema)
end
end
end

0 comments on commit 3431ca6

Please sign in to comment.