From 41636030d357103542cdda4c4219b52793b54a62 Mon Sep 17 00:00:00 2001 From: Andrea Rossi Date: Fri, 30 Sep 2016 20:39:13 +0100 Subject: [PATCH 1/2] add cast for boolean values --- lib/sweet_xml.ex | 3 +++ test/files/complex.xml | 1 + test/sweet_xml_test.exs | 20 ++++++++++++-------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/sweet_xml.ex b/lib/sweet_xml.ex index a189763..1d2385b 100644 --- a/lib/sweet_xml.ex +++ b/lib/sweet_xml.ex @@ -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 } @@ -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 diff --git a/test/files/complex.xml b/test/files/complex.xml index 14f1632..ada9456 100644 --- a/test/files/complex.xml +++ b/test/files/complex.xml @@ -7,6 +7,7 @@ http://football.fantasysports.yahoo.com/archive/nfl/2012/239541 postdraft 10 + true 17 1357339553 diff --git a/test/sweet_xml_test.exs b/test/sweet_xml_test.exs index 6867a99..96b36ff 100644 --- a/test/sweet_xml_test.exs +++ b/test/sweet_xml_test.exs @@ -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 @@ -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 From 781511f100178dcd792a7ad404b24b5e93b8b044 Mon Sep 17 00:00:00 2001 From: Andrea Rossi Date: Fri, 30 Sep 2016 21:18:21 +0100 Subject: [PATCH 2/2] update README with boolean cast --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 29b7636..fb52912 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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