go-sprout / sprout

From sprig to sprout - Useful template functions for Go templates with steroids
https://sprout.atom.codes
MIT License
98 stars 5 forks source link

bug: toYaml should omit empty fields similar to toJson #80

Open mrueg opened 5 days ago

mrueg commented 5 days ago

You have a proposal, explain it!

Right now toYaml represents the whole object. It would be great if there were an option to allow it to ignore empty fields.

Describe the solution you'd like

Have new toYamlWithParams function maybe with https://github.com/go-sprout/sprout/issues/79 that allows setting omitting empty fields as well as setting indentation.

Additional context

No response

Code of Conduct

mrueg commented 5 days ago

What makes the empty fields go away is the following pipeline: {{ .Object |toJson | fromJson | toYaml }} as json seems to omit the empty fields by default.

42atomys commented 5 days ago

Hello, good catch, toYaml needs to follow the same logic as toJson, I will tag this one as bug and works on it soon :)

Thanks for your report

42atomys commented 5 hours ago

@mrueg Hi thanks for your patience, I have add a test on https://github.com/go-sprout/sprout/pull/96, but the omitempty and - seams to be resolve normally, can you have an example to provide ?

mrueg commented 29 minutes ago

Thanks for creating a test cases. Looking again at it, I can't reproduce it in a minimal example (tried a few and it works as expected). I'm using it in a library that does a lot of things with the struct in between (e.g. json marshalling and unmarshalling), so I'll need to take a look deeper and hopefully can reproduce it then.