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

Documenting how to filter out newlines from the parsed model #103

Open
thbar opened this issue May 16, 2022 · 2 comments
Open

Documenting how to filter out newlines from the parsed model #103

thbar opened this issue May 16, 2022 · 2 comments

Comments

@thbar
Copy link
Contributor

thbar commented May 16, 2022

Just sharing a bit of code I'm using. For some context, I'm starting to use Saxy to build XML queries during testing. I must compare them to a baseline template, to ensure I used the Saxy.XML API correctly. These templates include newlines for human readability (indented/pretty-printed).

To make the comparison automated, I'm removing the non-significant newlines with the following code so far:

defmodule Stripper do
  def filter_newlines_from_model({element, tags, children}) do
    {
      element,
      tags,
      children
      |> Enum.map(&filter_newlines_from_model/1)
      |> Enum.reject(&is_nil/1)
    }
  end

  def filter_newlines_from_model(content) when is_binary(content) do
    if content |> String.trim() == "", do: nil, else: content
  end
end

I can use this like this afterwards:

    {:ok, parsed_request} = Saxy.SimpleForm.parse_string(xml, cdata_as_characters: false)
    IO.inspect(Stripper. filter_newlines_from_model(parsed_request))

This makes it easier to compare this using ExUnit assert x == y, for instance.

I wonder if:

  • this is worth adding to the documentation as an example
  • or if it could be an interesting option for Saxy.SimpleForm.parse_string(less sure at this point, we can mull over this!)

Let me know what you think!

@qcam
Copy link
Owner

qcam commented May 17, 2022

I think we can support an option like ignore_whitespaces: true in SimpleForm.parse_string. That would be helpful in this case. The option should default to false though to keep original behavior though.

@thbar
Copy link
Contributor Author

thbar commented Jun 16, 2022

I will mull over this ; I am not 100% sure it is useful outside of testing. Will comment here later, thanks for the input!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants