Skip to content

Add support for decoding missing record fields to Nothing #93

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

Merged

Conversation

jvliwanag
Copy link
Contributor

@jvliwanag jvliwanag commented Dec 21, 2020

Decodes missing fields in a record as Nothing.

Adds a helper type class DecodeJsonField

Fixes #92

@jvliwanag jvliwanag changed the title Add support for decoding optional record fields Add support for decoding missing record fields to Nothing Dec 21, 2020
@thomashoneyman thomashoneyman added the type: breaking change A change that requires a major version bump. label Dec 23, 2020
@thomashoneyman
Copy link
Contributor

This is an interesting suggestion. From my reading, we're essentially moving from using getField for each field in a record:

getField
:: forall a
. (Json -> Either JsonDecodeError a)
-> FO.Object Json
-> String
-> Either JsonDecodeError a
getField decoder obj str =
maybe
(Left $ AtKey str MissingValue)
(lmap (AtKey str) <<< decoder)
(FO.lookup str obj)

to attempting to use getFieldOptional for each field, falling back to getField:

getFieldOptional
:: forall a
. (Json -> Either JsonDecodeError a)
-> FO.Object Json
-> String
-> Either JsonDecodeError (Maybe a)
getFieldOptional decoder obj str =
maybe (pure Nothing) (map Just <<< decode) (FO.lookup str obj)
where
decode = lmap (AtKey str) <<< decoder

I'm intrigued, but this would be a significant change so I'd like to leave this open for a time to allow some discussion. Thank you for taking the time to write this up!

@jvliwanag
Copy link
Contributor Author

Anything we can do to see this go through? Would love to use argonaut records directly for simple web api’s.

@thomashoneyman thomashoneyman removed the type: breaking change A change that requires a major version bump. label Apr 1, 2021
@thomashoneyman
Copy link
Contributor

I'll leave this open for a little while to let other maintainers weigh in, but if I haven't merged it in a week please ping me and I will @jvliwanag -- thanks!

@garyb
Copy link
Member

garyb commented Apr 1, 2021

I'm fine with it - I probably wouldn't choose to have it work this way personally if I was designing a library from scratch, as I don't think you can distinguish between the field missing and having no value present now, right? But Maybe collapsing happens with the encoding used elsewhere so it's already established that this library doesn't care about that kind of distinction.

@jvliwanag
Copy link
Contributor Author

Are we good with this @thomashoneyman ? :)

On a side note, I do prefer Nothing be encoded as missing fields. I'll open a separate issue on that.

@thomashoneyman thomashoneyman merged commit b0d07f4 into purescript-contrib:main Apr 9, 2021
considerate added a commit to xc-jp/purescript-argonaut-codecs that referenced this pull request Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Support decoding of undefined record field to Nothing
3 participants