Skip to content
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

fix inline table and array transforms #41

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 2 additions & 7 deletions bench/bench.decoder.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ Benchee.run(decode_jobs,
memory_time: 1,
inputs: inputs,
formatters: [
&Benchee.Formatters.HTML.output/1,
&Benchee.Formatters.Console.output/1,
],
formatter_options: [
html: [
file: Path.expand("output/decode.html", __DIR__)
]
{Benchee.Formatters.HTML, file: Path.expand("output/decode.html", __DIR__)},
Benchee.Formatters.Console
]
)
9 changes: 2 additions & 7 deletions bench/bench.lexer.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ Benchee.run(jobs,
memory_time: 1,
inputs: inputs,
formatters: [
&Benchee.Formatters.HTML.output/1,
&Benchee.Formatters.Console.output/1,
],
formatter_options: [
html: [
file: Path.expand("output/decode.html", __DIR__)
]
{Benchee.Formatters.HTML, file: Path.expand("output/lexer.html", __DIR__)},
Benchee.Formatters.Console,
]
)
4 changes: 2 additions & 2 deletions lib/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ defmodule Toml.Decoder do
{:ok, elements} <- accumulate_array_elements(lexer),
{_, _, {:ok, {?\], _, _, _}}} <-
{:close, {skip, lines}, pop_skip(lexer, [:whitespace, :newline, :comment])} do
{:ok, elements}
{:ok, {:array, elements}}
else
{:error, _, _, _} = err ->
err
Expand All @@ -829,7 +829,7 @@ defmodule Toml.Decoder do
with {:ok, {?\{, skip, _, lines}} <- pop_skip(lexer, [:whitespace]),
{:ok, elements} <- accumulate_table_elements(lexer),
{_, _, {:ok, {?\}, _, _, _}}} <- {:close, {skip, lines}, pop_skip(lexer, [:whitespace])} do
{:ok, elements}
{:ok, {:inline_table, elements}}
else
{:error, _, _, _} = err ->
err
Expand Down
20 changes: 20 additions & 0 deletions lib/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ defmodule Toml.Document do
|> (&transform.(key, &1)).()
end

defp to_map3(key, {:array, array}, keyfun, nil) do
to_map3(key, array, keyfun, nil)
end

defp to_map3(key, {:array, array}, keyfun, transform) do
for v <- array do
to_map3(key, v, keyfun, transform)
end
|> (&transform.(key, &1)).()
end

defp to_map3(_key, {:inline_table, m}, keyfun, nil) do
to_map2(m, keyfun, nil)
end

defp to_map3(key, {:inline_table, m}, keyfun, transform) do
to_map2(m, keyfun, transform)
|> (&transform.(key, &1)).()
end

defp to_map3(_key, v, keyfun, nil) when is_map(v) do
to_map2(v, keyfun, nil)
end
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"},
"benchee": {:hex, :benchee, "1.3.1", "c786e6a76321121a44229dde3988fc772bca73ea75170a73fd5f4ddf1af95ccf", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "76224c58ea1d0391c8309a8ecbfe27d71062878f59bd41a390266bf4ac1cc56d"},
"benchee_html": {:hex, :benchee_html, "1.0.0", "5b4d24effebd060f466fb460ec06576e7b34a00fc26b234fe4f12c4f05c95947", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:benchee_json, "~> 1.0", [hex: :benchee_json, repo: "hexpm", optional: false]}], "hexpm", "5280af9aac432ff5ca4216d03e8a93f32209510e925b60e7f27c33796f69e699"},
"benchee_json": {:hex, :benchee_json, "1.0.0", "cc661f4454d5995c08fe10dd1f2f72f229c8f0fb1c96f6b327a8c8fc96a91fe5", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "da05d813f9123505f870344d68fb7c86a4f0f9074df7d7b7e2bb011a63ec231c"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
Expand All @@ -9,10 +9,10 @@
"earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.29.0", "4a1cb903ce746aceef9c1f9ae8a6c12b742a5461e6959b9d3b24d813ffbea146", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "f096adb8bbca677d35d278223361c7792d496b3fc0d0224c9d4bc2f651af5db1"},
"excoveralls": {:hex, :excoveralls, "0.15.0", "ac941bf85f9f201a9626cc42b2232b251ad8738da993cf406a4290cacf562ea4", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "9631912006b27eca30a2f3c93562bc7ae15980afb014ceb8147dc5cdd8f376f1"},
"excoveralls": {:hex, :excoveralls, "0.18.2", "86efd87a0676a3198ff50b8c77620ea2f445e7d414afa9ec6c4ba84c9f8bdcc2", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "230262c418f0de64077626a498bd4fdf1126d5c2559bb0e6b43deac3005225a4"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
Expand Down
12 changes: 10 additions & 2 deletions test/transform_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Toml.Test.TransformTest do

defmodule ServerTransform do
def transform(:servers, v) when is_map(v) do
for {name, s} <- v do
for {name, s} <- Enum.sort(v) do
case s do
%{ip: {_, _, _, _}} ->
struct(Toml.Test.TransformTest.Server, Map.put(s, :name, name))
Expand Down Expand Up @@ -80,6 +80,13 @@ defmodule Toml.Test.TransformTest do
[[servers.beta.ports]]
type = "UDP"
number = 8083

[servers.gamma]
ip = "192.168.1.3"
ports = [
{type = "TCP", number = 8888},
{type = "TCP", number = 9000}
]
"""

transforms = [
Expand All @@ -92,7 +99,8 @@ defmodule Toml.Test.TransformTest do
assert {:ok, result} = Toml.decode(input, keys: :atoms, transforms: transforms)
assert is_list(result[:servers])
assert [%Server{name: :alpha, ip: {192, 168, 1, 1}} | _] = result[:servers]
alpha = result[:servers] |> List.first()
[alpha, _beta, gamma] = result[:servers]
assert [8080, 8081] = alpha.ports
assert [8888, 9000] = gamma.ports
end
end