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 cast modifier for boolean values #36

Open
wants to merge 2 commits into
base: master
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
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

`SweetXml` is a thin wrapper around `:xmerl`. It allows you to convert a
`char_list` or `xmlElement` record as defined in `:xmerl` to an elixir value such
as `map`, `list`, `string`, `integer`, `float` or any combination of these.
as `map`, `list`, `string`, `integer`, `float`, `boolean` or any combination of these.


## Examples
Expand Down Expand Up @@ -181,13 +181,18 @@ is being returned.

'i' stands for (i)nteger. This forces `xpath/2` to return the value as
integer instead of a char list.


* `~x"//some/path"il` - integer list.

* `~x"//some/path"f`

'f' stands for (f)loat. This forces `xpath/2` to return the value as
float instead of a char list.

* `~x"//some/path"il` - integer list.
* `~x"//some/path"b`

'b' stands for (b)oolean. This forces `xpath/2` to return the value as
boolean instead of the char lists `'true'` and `'false'`.

Also in the examples section, we always import SweetXml first. This
makes `x_sigil` available in the current scope. Without it, instead of using
Expand Down Expand Up @@ -249,7 +254,7 @@ result = doc
assert result == 'Match One'
```

We can specify multiple namespace prefixes:
We can specify multiple namespace prefixes:

```elixir
result = doc
Expand Down
3 changes: 3 additions & 0 deletions lib/sweet_xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ defmodule SweetXml do
?s in modifiers -> :string
?i in modifiers -> :integer
?f in modifiers -> :float
?b in modifiers -> :boolean
:otherwise -> false
end
}
Expand Down Expand Up @@ -658,5 +659,7 @@ defmodule SweetXml do
defp to_cast(value, :string), do: to_string(value)
defp to_cast(value, :integer), do: String.to_integer(to_string(value))
defp to_cast(value, :float), do: String.to_float(to_string(value))
defp to_cast('true', :boolean), do: true
defp to_cast('false', :boolean), do: false

end
1 change: 1 addition & 0 deletions test/files/complex.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<url>http://football.fantasysports.yahoo.com/archive/nfl/2012/239541</url>
<draft_status>postdraft</draft_status>
<num_teams>10</num_teams>
<active>true</active>
<edit_key>17</edit_key>
<weekly_deadline/>
<league_update_timestamp>1357339553</league_update_timestamp>
Expand Down
20 changes: 12 additions & 8 deletions test/sweet_xml_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ defmodule SweetXmlTest do
end

test "xpath sigil" do
assert ~x"//header/text()" == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: false}
assert ~x"//header/text()"e == %SweetXpath{path: '//header/text()', is_value: false, is_list: false, is_keyword: false, cast_to: false}
assert ~x"//header/text()"l == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: false}
assert ~x"//header/text()"k == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: true, cast_to: false}
assert ~x"//header/text()"s == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: :string}
assert ~x"//header/text()"i == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: :integer}
assert ~x"//header/text()"f == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: :float}
assert ~x"//header/text()" == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: false}
assert ~x"//header/text()"e == %SweetXpath{path: '//header/text()', is_value: false, is_list: false, is_keyword: false, cast_to: false}
assert ~x"//header/text()"l == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: false}
assert ~x"//header/text()"k == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: true, cast_to: false}
assert ~x"//header/text()"s == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: :string}
assert ~x"//header/text()"i == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: :integer}
assert ~x"//header/text()"f == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: :float}
assert ~x"//header/text()"el == %SweetXpath{path: '//header/text()', is_value: false, is_list: true, is_keyword: false, cast_to: false}
assert ~x"//header/text()"le == %SweetXpath{path: '//header/text()', is_value: false, is_list: true, is_keyword: false, cast_to: false}
assert ~x"//header/text()"sl == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: :string}
assert ~x"//header/text()"ls == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: :string}
assert ~x"//header/text()"il == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: :integer}
assert ~x"//header/text()"li == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: :integer}
assert ~x"//header/text()"b == %SweetXpath{path: '//header/text()', is_value: true, is_list: false, is_keyword: false, cast_to: :boolean}
assert ~x"//header/text()"bl == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: :boolean}
assert ~x"//header/text()"lb == %SweetXpath{path: '//header/text()', is_value: true, is_list: true, is_keyword: false, cast_to: :boolean}
end

test "xpath with sweet_xpath as only argment", %{simple: doc} do
Expand Down Expand Up @@ -418,7 +421,8 @@ defmodule SweetXmlTest do
assert xpath(doc, ~x[/fantasy_content/league/league_id/text()]) == '239541'
assert xpath(doc, ~x[/fantasy_content/league/league_id/text()]s) == "239541"
assert xpath(doc, ~x[/fantasy_content/league/league_id/text()]i) == 239541
assert xpath(doc, ~x[//total/text()]f) == 204.68
assert xpath(doc, ~x[//total/text()]f) == 204.68
assert xpath(doc, ~x[//active/text()]b) == true
end

test "xml entities do not split strings" do
Expand Down