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

Hide record fields with default values in to_yaml output #37

Merged
merged 1 commit into from
Jun 11, 2022

Conversation

maurobringolf
Copy link
Contributor

@maurobringolf maurobringolf commented Jun 8, 2022

This modifies the to_yaml function to take into account the [@default] attribute of record fields. If the field value is equal (=) to the default value, then it is omitted from the YAML object as proposed in #35.

I can see how this is useful for 'a option fields with default None. But I can imagine that for other types/scenarios this behavior is less useful than the current one. Maybe the library could let the user decide by including an optional argument in to_yaml, e.g.

val person_to_yaml : ?include_defaults:bool -> person -> Yaml.value 

Just a thought. If you have any feedback/suggestions on the implementation I am happy to iterate on it.

@sim642
Copy link

sim642 commented Jun 9, 2022

I can see how this is useful for 'a option fields with default None. But I can imagine that for other types/scenarios this behavior is less useful than the current one.

Indeed, this also went through my mind when I opened the issue, but ppx_deriving_yojson, which is much more widely used, has always done it this way without much complaining.

There does appear to be an old issue about it there though: ocaml-ppx/ppx_deriving_yojson#19. The more flexible approach suggested there is to make it controllable per-field, instead of globally.

@maurobringolf maurobringolf marked this pull request as ready for review June 9, 2022 14:37
@patricoferris
Copy link
Owner

Thanks @maurobringolf for the PR (and @sim642 for the research into how others do it!). Given this is how yojson does it, which is used by many, I'm inclined to merge this.

There does appear to be an old issue about it there though: ocaml-ppx/ppx_deriving_yojson#19. The more flexible approach suggested there is to make it controllable per-field, instead of globally.

If people complain I think going the per-field approach would be better, but it doesn't need to be a part of this PR :))

@patricoferris patricoferris merged commit 717d81b into patricoferris:main Jun 11, 2022
kit-ty-kate pushed a commit to ocaml/opam-repository that referenced this pull request Oct 14, 2022
CHANGES:

- Add custom `to_yaml` and `of_yaml` attributes (patricoferris/ppx_deriving_yaml#38, @patricoferris)
- Add `skip_unknown` flag to allow partially decoding yaml (patricoferris/ppx_deriving_yaml#40, @code-ghalib)
- Hide record fields with default values in to_yaml output (patricoferris/ppx_deriving_yaml#37, @maurobringolf, reviewed by @sim642 and @patricoferris)
- Expose `to_yaml` and `of_yaml` derivers with `yaml` being an alias (patricoferris/ppx_deriving_yaml#36, @patricoferris)
- Improved error messages (patricoferris/ppx_deriving_yaml#32, @prosper74, reviewed by @patricoferris)
- Add a default attribute for providing placeholder values (patricoferris/ppx_deriving_yaml#31, @prosper74, reviewed by @ayc9, @pitag-ha and @patricoferris)
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

Successfully merging this pull request may close these issues.

3 participants