-
Notifications
You must be signed in to change notification settings - Fork 321
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
PoC: omitNothingFields without INCOHERENT instances #839
Conversation
As long as it is possible to generate |
I'm not sure I understand this. |
On the object level we have only |
@tolysz I still don't understand what you are trying to say. |
Would this allow us to have something like
? |
data Nullable a = N | Nullable a
instance ToJSON a => ToJSON (Nullable a) where
toFieldJSON N = Just Null
toFieldJSON (Nullable a) = toFieldJSON a
data Undefinable a = U | Undefinable a
instance ToJSON a => ToJSON (Undefinable a) where
toFieldJSON U = Nothing
toFieldJSON (Undefinable a) = toFieldJSON a
dat NullOrUndefined a = N' | U' | NullOrUndefined a
data NullOrUndefined a => ToJSON (NullOrUndefined a) where
toFieldJSON U' = Nothing
toFieldJSON N' = Just Null
toFieldJSON (NullOrUndefined a) = toFieldJSON a There is also variations on how wrappers process (above they doesn't) the recursive call. E.g. Undefinable could convert |
This looks like it will solve a problem of mine, and I would argue the general problem of dealing with various Would it make any sense to expose the |
|
This looks very promising. What is the status of this PR? |
@cdornan, believe me! You do not want to see that code. No matter your power level, you certainly do not want to re-use it.
|
Clsoing in favour of #1039 |
The idea is dumb and simple:
(I speak only about
Encoding
,toJSON
/Value
is analogous).Add a
toFieldEncoding :: a -> Maybe Encoding
toToJSON
class (andliftToFieldEncoding
toToJSON1
etc.)Generic
framework to omit the field ifNothing
andomitNothingFields
or convert it toNull
otherwise.Then behavior per type can be specified per instance, and not inside the framework:
Can be easily made omittable.
This is kind of adding
Missing
clause toValue
, but withoutproblems that poses.
Additional bonus, which I haven't yet figured out,
is that we can have variants of
object
andpairs
such that nulls are omitted in hand written instances too.
Currently there is no good story for that.
The
FromJSON
will be analagous, we'll addWhich can succeed if value is
Nothing
, i.e. field doesn't exist.This is breaking change.
It fixes issue brought up many times on issue tracker, in a way I'm comfortable with.
(It removes use of
IncoherentInstances
).It's however not even very breaking, as tests didn't break (so far).
So there is a chance that users of
Generic
(andTH
) won't even notice.Should I proceed with fleshing this out properly?