Skip to content

Commit

Permalink
Deprecate binary content in XML encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
qcam committed Oct 2, 2020
1 parent d59a920 commit 345ef5b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/saxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ defmodule Saxy do
iex> import Saxy.XML
iex> element = element("person", [gender: "female"], "Alice")
{"person", [{"gender", "female"}], ["Alice"]}
{"person", [{"gender", "female"}], [{:characters, "Alice"}]}
iex> Saxy.encode!(element, [version: "1.0"])
"<?xml version=\"1.0\"?><person gender=\"female\">Alice</person>"
Expand Down
4 changes: 2 additions & 2 deletions lib/saxy/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defprotocol Saxy.Builder do
iex> person = %Person{name: "Alice", gender: "female"}
iex> Saxy.Builder.build(person)
{"person", [{"gender", "female"}], ["Alice"]}
{"person", [{"gender", "female"}], [{:characters, "Alice"}]}
Custom implementation could be done by implementing protocol:
Expand All @@ -44,7 +44,7 @@ defprotocol Saxy.Builder do
iex> user = %User{name: "Alice", username: "alice99"}
iex> Saxy.Builder.build(user)
{"Person", [{"userName", "alice99"}], [{"Name", [], ["Alice"]}]}
{"Person", [{"userName", "alice99"}], [{"Name", [], [{:characters, "Alice"}]}]}
"""

@doc """
Expand Down
6 changes: 6 additions & 0 deletions lib/saxy/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ defmodule Saxy.Encoder do
end

defp content([characters | elements]) when is_binary(characters) do
IO.warn("""
Passing binary as :characters content is derepcated and will be removed in future versions.
Please wrap the binary with Saxy.XML.characters/1.
""")

[characters | content(elements)]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/saxy/xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ defmodule Saxy.XML do
defp children(children, acc \\ [])

defp children([binary | children], acc) when is_binary(binary) do
children(children, [binary | acc])
children(children, [characters(binary) | acc])
end

defp children([{type, _} = form | children], acc)
Expand Down
4 changes: 2 additions & 2 deletions test/saxy/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ defmodule Saxy.BuilderTest do

test "builds element from struct" do
struct = %Struct{foo: "foo", bar: "bar"}
assert build(struct) == {"test", [{"foo", "foo"}], ["bar"]}
assert build(struct) == {"test", [{"foo", "foo"}], [{:characters, "bar"}]}

nested_struct = %Struct{bar: struct}

assert build(nested_struct) == {"test", [{"foo", ""}], [{"test", [{"foo", "foo"}], ["bar"]}]}
assert build(nested_struct) == {"test", [{"foo", ""}], [{"test", [{"foo", "foo"}], [{:characters, "bar"}]}]}

underived_struct = %UnderivedStruct{}
assert_raise Protocol.UndefinedError, fn -> build(underived_struct) end
Expand Down

0 comments on commit 345ef5b

Please sign in to comment.