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

Add a 'o' modifier to the ~x sigil allowing a path to not exist #10

Merged
merged 2 commits into from
Oct 11, 2015

Conversation

benwilson512
Copy link
Contributor

Motivation:

I'm the author if ExAws, a set of elixir clients for interacting with AWS, and have been using sweet_xml to parse the resulting xml. This has been an extremely helpful library for handling xml results returned by AWS.

However, as it turns out, AWS will often leave out portions of xml depending on permissions, request parameters, and so on. Presently this will return the following error:

     ** (Protocol.UndefinedError) protocol Enumerable not implemented for nil
     stacktrace:
       (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
       (elixir) lib/enum.ex:112: Enumerable.reduce/3
       (elixir) lib/stream.ex:695: Stream.do_enum_transform/8
       (elixir) lib/stream.ex:647: Stream.do_transform/7
       (sweet_xml) lib/sweet_xml.ex:513: anonymous fn/4 in SweetXml.continuation_opts/2
       xmerl_scan.erl:563: :xmerl_scan.scan_document/2
       xmerl_scan.erl:286: :xmerl_scan.string/2
       (sweet_xml) lib/sweet_xml.ex:189: SweetXml.parse/2

What this PR does is add an 'o' modifier ~x"//some/bad/path"o such that it will return nil if the path does not exist instead of exploding.


def xmap(nil, _, %{is_optional: true}), do: nil

def xmap(parent, [], atom) when is_atom(atom), do: xmap(parent, [], %{is_keyword: atom})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this clause here ensures that the changes to xmap are not breaking. All current uses will pass without trouble.

@benwilson512
Copy link
Contributor Author

Are there any outstanding questions or concerns?

@viniciussbs
Copy link
Contributor

Hi, @benwilson512. A modifier for optional paths is a nice feature. Could you updated the README, too, describing this feature? I'm not a maintainer, I'm just trying to help.

@benwilson512
Copy link
Contributor Author

Done.

@benwilson512
Copy link
Contributor Author

Hey, any possibility for movement on this?

@viniciussbs
Copy link
Contributor

I'm not a maintainer, but 👍 What do you thing, @awetzel ?

@awetzel
Copy link
Collaborator

awetzel commented Oct 11, 2015

Yes it is perfect sorry for the late answer, I had already reviewed your commit and thought that it was already merged... Thank you very much for the contribution, I will publish to hex next week maybe with other improvements.

awetzel added a commit that referenced this pull request Oct 11, 2015
Add a 'o' modifier to the ~x sigil allowing a path to not exist
@awetzel awetzel merged commit 5bb97d2 into kbrw:master Oct 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants