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

skippable optional helper #7

Open
joprice opened this issue Jul 29, 2024 · 0 comments
Open

skippable optional helper #7

joprice opened this issue Jul 29, 2024 · 0 comments

Comments

@joprice
Copy link

joprice commented Jul 29, 2024

It would be convenient to have a helper that allows avoiding writing nulls into the output, as it can save large amounts of storage and data transfer. Also, some pre-existing data formats don't include the null fields, so it can be necessary to distinguish them. This approach seems to work but curious about your thoughts:

module Codec =
    let fieldOpt
      (fieldName: string)
      (picker: 'u -> 't option)
      (fieldCodec: Codec<'t>)
      : ObjectCodecFieldSet<'t option, 'u>
      =
      {
        Values =
          fun i ->
            match i with
            | Some value -> [ fieldName, fieldCodec.Encoder value ]
            | None -> []
        Decoder = Decode.optional fieldName fieldCodec.Decoder
        Picker = picker
      }
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

1 participant