-
-
Notifications
You must be signed in to change notification settings - Fork 11
fix: ambiguous map update syntax #87
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| defmodule Spitfire.Context do | ||
| @moduledoc false | ||
|
|
||
| @doc """ | ||
| Temporarily applies parser state updates and restores them after the block. | ||
|
|
||
| Use the `:capture` option to return a map of selected keys as they were | ||
| at the end of the block, before state is restored. | ||
| """ | ||
| def with_state(parser, updates, fun) when is_function(fun, 1) do | ||
| with_state(parser, updates, [], fun) | ||
| end | ||
|
|
||
| def with_state(parser, updates, opts, fun) when is_function(fun, 1) do | ||
| old = | ||
| Enum.reduce(updates, %{}, fn {key, _value}, acc -> | ||
| Map.put(acc, key, {Map.has_key?(parser, key), Map.get(parser, key)}) | ||
| end) | ||
|
|
||
| parser = Map.merge(parser, updates) | ||
| result = fun.(parser) | ||
| capture_keys = Keyword.get(opts, :capture, []) | ||
|
|
||
| restore = fn parser -> | ||
| Enum.reduce(old, parser, fn {key, {had_key, value}}, acc -> | ||
| if had_key do | ||
| Map.put(acc, key, value) | ||
| else | ||
| Map.delete(acc, key) | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| capture = fn parser -> | ||
| Enum.reduce(capture_keys, %{}, fn key, acc -> | ||
| Map.put(acc, key, Map.get(parser, key)) | ||
| end) | ||
| end | ||
|
|
||
| case result do | ||
| {value, parser} -> | ||
| captured = capture.(parser) | ||
| restored = restore.(parser) | ||
|
|
||
| if capture_keys == [] do | ||
| {value, restored} | ||
| else | ||
| {{value, captured}, restored} | ||
| end | ||
|
|
||
| parser when is_map(parser) -> | ||
| captured = capture.(parser) | ||
| restored = restore.(parser) | ||
|
|
||
| if capture_keys == [] do | ||
| restored | ||
| else | ||
| {{parser, captured}, restored} | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| defmodule Spitfire.ContextTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| import Spitfire.Context, only: [with_state: 3, with_state: 4] | ||
|
|
||
| test "restores updated keys after block" do | ||
| parser = %{mode: :original, keep: :ok} | ||
|
|
||
| restored = | ||
| with_state(parser, %{mode: :temp, new_key: :temp}, fn parser -> | ||
| assert parser[:mode] == :temp | ||
| assert parser[:new_key] == :temp | ||
| Map.merge(parser, %{mode: :inner, new_key: :inner}) | ||
| end) | ||
|
|
||
| assert restored == %{mode: :original, keep: :ok} | ||
| end | ||
|
|
||
| test "captures state before restore" do | ||
| parser = %{mode: :original} | ||
|
|
||
| {{value, captured}, restored} = | ||
| with_state(parser, %{mode: :temp, flag: false}, [capture: [:mode, :flag]], fn parser -> | ||
| {:ok, Map.merge(parser, %{mode: :final, flag: true})} | ||
| end) | ||
|
|
||
| assert value == :ok | ||
| assert captured == %{mode: :final, flag: true} | ||
| assert restored == %{mode: :original} | ||
| end | ||
|
|
||
| test "nested calls restore to outer then original" do | ||
| parser = %{mode: :base} | ||
|
|
||
| {value, restored} = | ||
| with_state(parser, %{mode: :outer, outer: true}, fn parser -> | ||
| assert parser[:mode] == :outer | ||
| assert parser[:outer] | ||
|
|
||
| {inner_value, parser} = | ||
| with_state(parser, %{mode: :inner, inner: true}, fn parser -> | ||
| assert parser[:mode] == :inner | ||
| assert parser[:outer] | ||
| assert parser[:inner] | ||
| {:inner, Map.put(parser, :mode, :inner_set)} | ||
| end) | ||
|
|
||
| assert inner_value == :inner | ||
| assert parser[:mode] == :outer | ||
| assert parser[:outer] | ||
| refute Map.has_key?(parser, :inner) | ||
|
|
||
| {:outer, parser} | ||
| end) | ||
|
|
||
| assert value == :outer | ||
| assert restored == %{mode: :base} | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment that has an example of code that matches each clause? I'm getting confused cuz this says pairs but it's a list of one element.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. It was called
pairsbecause it's the key/value pairs fora => b, c => d. It may also be a single element(the second clause) so I renamed this toentriesinstead.I added a comment with the shape each clause handles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I think I was getting confused at to what the ast for this is. The args are the pairs and the "call" is the pipe?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this pattern
[{{call, call_meta, [_, _ | _] = args}, value}]matches this interpretation:%{... | (b(c, d)) => e}Where:
callisb(c, d)[_, _ | _] = argsis checking forc, d, ...in that callvalueis e:"=>"ast node, this pair represents thatThe pipe parsing happens in
parse_expressionwhen the flagis_mapistrue, thenparse_pipe_op->parse_map_update_pairs->parse_comma_list. The last function there is what produces a list of entries for the map, and from that list is where this "pair" comes from if it finds anassoc_op(=>) token.Then, in
parse_pipe_op(which is only called for map updates, not list cons like[head | tail], we call this function that sees the ambiguity and resolves it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going over this again I realize if there's more entries like
%{a do :ok end | b c, d => e, f => g}then there's still a mismatch, so I'll have to fix that too