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

Surplus newlines when children contain [] #59

Open
mneumann opened this issue Jan 31, 2025 · 1 comment
Open

Surplus newlines when children contain [] #59

mneumann opened this issue Jan 31, 2025 · 1 comment

Comments

@mneumann
Copy link

When encoding something like:

{:A, nil, [
   [],
   {:B, nil, []}
   [],
]} |> XmlBuilder.generate()

the resulting XML contains unnecessary newlines:

<A>
  
  <B>

</A>

This is due to map_intersperse adding newlines unconditionally.

Note that [] comes in handy in case you want to encode some values conditionally, like:

{:A, nil, [
   (if a, do: encode(a), else: []),
]}

A solution is to Enum.filter myself on the children list (removing empty arrays... possibly recursively), or to use formatter: :none.

I just wanted to bring this issue up as maybe there is a cleaner solution to that.

Also, handling nil in children lists would come in handy and allow code like that:

{:A, nil, [
   a && encode(a),
]}
@joshnuss
Copy link
Owner

joshnuss commented Feb 4, 2025

Thanks @mneumann,

Besides solving it outside the library, another 2 ways I can think of

  • Pattern matching empty lists and outputting nothing in the case
  • Filtering out empty lists

Open to PRs

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

No branches or pull requests

2 participants