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

Optional strings should return nil, not empty string #53

Closed
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
5 changes: 4 additions & 1 deletion lib/sweet_xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,10 @@ defmodule SweetXml do
spec = %SweetXpath{spec | is_list: true}
get_current_entities(parent, spec)
|> Enum.map(&(_value(&1) |> to_cast(string_type, is_opt?)))
|> Enum.join
|> case do
[] -> nil
items -> Enum.join(items)
end
|> spec.transform_fun.()
end

Expand Down
7 changes: 7 additions & 0 deletions test/sweet_xml_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ 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[/fantasy_content/league/short_invitation_url/text()]) == nil
assert xpath(doc, ~x[/fantasy_content/league/short_invitation_url/text()]o) == nil
assert xpath(doc, ~x[/fantasy_content/league/short_invitation_url/text()]os) == nil
assert xpath(doc, ~x[/fantasy_content/league/short_invitation_url/text()]oS) == nil
assert xpath(doc, ~x[/fantasy_content/idontexist/text()]o) == nil
assert xpath(doc, ~x[/fantasy_content/idontexist/text()]os) == nil
assert xpath(doc, ~x[/fantasy_content/idontexist/text()]oS) == nil
assert xpath(doc, ~x[//total/text()]f) == 204.68
end

Expand Down