Skip to content

Add toJSONDiff and toJSONDiff' - #11

Open
marcosh wants to merge 1 commit into
clintonmead:masterfrom
marcosh:toJSONDiff
Open

Add toJSONDiff and toJSONDiff'#11
marcosh wants to merge 1 commit into
clintonmead:masterfrom
marcosh:toJSONDiff

Conversation

@marcosh

@marcosh marcosh commented Sep 5, 2024

Copy link
Copy Markdown

Generate patches for types with a ToJSON instance.

It makes sense to me to have such a function at the library level.

If this makes sense to you, let me know if you think that the new functions should be tested somehow

@clintonmead

clintonmead commented Aug 19, 2026

Copy link
Copy Markdown
Owner

@marcosh again apologies for the very late reply

I'm not sure I'm a great fan of this as it stands, because you're putting two strongly typed objects together and generating a weakly typed Patch object.

I think if one wants to do this it's better just to be explicit by saying:

diff (toJSON a1) (toJSON a2) directly

If you want an actual function that works on a instead of just a generic JSON Value, perhaps one could consider:

data TypedPatch a = TypedPatch Patch -- don't export the constructor

typedDiff :: ToJSON a => a -> a -> TypedPatch a
typedDiff a1 a2 = diff (toJSON a1) (toJSON a2)

typedPatch :: (ToJSON a, FromJSON a) => TypedPatch a -> a -> Result a
typedPatch (TypedPatch p) v = patch p (toJSON v) >>= fromJSON -- this may not be exactly correct, but it's the idea 

and maybe that makes sense, but keep in mind that now typedPatch has two failure modes.

  1. The patch fails to apply OR
  2. The patch applies successfully but creates something that isn't a valid a.

So I'm not sure this is wise either.

Happy for your thoughts on this.

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.

2 participants