-
Notifications
You must be signed in to change notification settings - Fork 60
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
The optional modifier produces empty strings instead of nil #87
Comments
Perhaps the most obvious (initial) fix would be to document the exception in the README (and possibly a workaround). |
The main problem (from memory), is that Overall, I think a formal description of how the modifiers interacts, and what they expect as input and should return as output is necessary before analyzing how to update the library. I don't think I have enough time to do either in the coming weeks so any participation from anyone is welcomed. Regarding the README update, I'm totally in favor of it. [1] https://www.erlang.org/doc/man/xmerl_xpath.html#string-3 |
Thanks for the explanation. So if xmerl doesn't provide information on whether the path is reachable for strings, then SweetXML cannot distinguish between If that is the case, it sounds like To be honest, I'm not sure how valuable that distinction of reachable or not is. I think I'm more likely to want all empty strings to be converted to nil for my current situation, and others may prefer all strings (as is). Is there anything less cumbersome than using name: ~x"./NAME/text()"s |> transform_by(&Transform.to_string_or_nil/1), |
This part needs testing, as this is simply my inference from what the type of
It's not that it is invalid, it is that the behavior is incompletely defined, if you take a look at the So then what does the modifiers string and soft_string mean ? is it simply use That's why I wish to formalize the behavior of the modifiers.
A bit, you can define a function that navigate the supbspec given to def inspect_spec(xs) do
Enum.map(xs, fn
{k, [x | xs]} -> {k, [x | Enum.map(xs, fn {k2, x} -> {k2, x |> SweetXml.transform_by(&IO.inspect(&1, label: "#{k}.#{k2}"))} end)]}
{k, x} -> {k, x |> SweetXml.transform_by(&IO.inspect(&1, label: k))}
end)
end You could imagine a function You would then only need to apply it like so: subspec_map(subspec, fn _, x ->
if x.cast_to == :soft_string and x.is_optional do
SweetXml.transform_by(x, fn "" -> nil ; y -> y end)
else
x
end
end) Or other stuff of the sort. [1] https://github.com/kbrw/sweet_xml/blob/master/lib/sweet_xml.ex#L763-L778 |
@spec subspec_map(subspec, (%SweetXpath{} -> %SweetXpath{})) :: subspec
when subspec: (%SweetXpath{}| keyword(%SweetXpath{} | subspec))
def subspec_map(%SweetXpath{} = x, transform) do
transform.(x)
end
def subspec_map({k, x}, transform) do
{k, subspec_map(x, transform)}
end
def subspec_map(subspec, transform) when is_list(subspec) do
Enum.map(subspec, fn x -> subspec_map(x, transform) end)
end |
Hi, so it's confirmed that we won't do breaking changes, and we will go the path of documenting thoroughly the modifiers in order to leave no surprises to the users |
Strings don't behave as described by the README:
"If you use the optional modifier o together with a soft cast modifier (uppercase), then the value is set to nil"
~x"./NAME/text()"So
produces an empty string instead of nil.Someone attempted to fix this with #53, but it would've been better to open an issue to discuss possible solutions and backwards compatibility.
The text was updated successfully, but these errors were encountered: