Skip to content

Make FromJSON instance for Qualified backwards compatible#4403

Merged
thomashoneyman merged 2 commits intopurescript:masterfrom
pete-murphy:pm/qualified-compat-fromjson-instance
Oct 17, 2022
Merged

Make FromJSON instance for Qualified backwards compatible#4403
thomashoneyman merged 2 commits intopurescript:masterfrom
pete-murphy:pm/qualified-compat-fromjson-instance

Conversation

@pete-murphy
Copy link
Copy Markdown
Contributor

@pete-murphy pete-murphy commented Oct 16, 2022

Make FromJSON instance for Qualified backwards compatible

Prior to #4293, Qualified was encoded to JSON such that

>>> encode $ Qualified Nothing "foo"
[null,"foo"]
>>> encode $ Qualified (Just $ ModuleName "A") "bar"
["A","bar"]

The type of Qualified has changed so that null no longer appears in JSON output, but for sake of backwards-compatibility with JSON that was produced prior to those changes (pre-v0.15.2), we need to accept null, which will be interpreted as Qualified ByNullSourcePos.

Relates to: purescript/pursuit#470 (comment)


Checklist:

  • Added a file to CHANGELOG.d for this PR (see CHANGELOG.d/README.md)
  • Added myself to CONTRIBUTORS.md (if this is my first contribution)
  • Linked any existing issues or proposals that this pull request should close
  • Updated or added relevant documentation
  • Added a test for the contribution (if applicable)

@pete-murphy pete-murphy force-pushed the pm/qualified-compat-fromjson-instance branch from 06dc567 to 36ae105 Compare October 16, 2022 20:32
Comment on lines 294 to +305
instance FromJSON a => FromJSON (Qualified a) where
parseJSON v = byModule <|> bySourcePos
parseJSON v = byModule <|> bySourcePos <|> byMaybeModuleName'
where
byModule = do
(mn, a) <- parseJSON2 v
pure $ Qualified (ByModuleName mn) a
bySourcePos = do
(ss, a) <- parseJSON2 v
pure $ Qualified (BySourcePos ss) a
byMaybeModuleName' = do
(mn, a) <- parseJSON2 v
pure $ Qualified (byMaybeModuleName mn) a
Copy link
Copy Markdown
Contributor Author

@pete-murphy pete-murphy Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this

instance FromJSON a => FromJSON (Qualified a) where
  parseJSON v = byModule <|> bySourcePos <|> byNullSourcePos
    where
    -- ...
    byNullSourcePos = do
      (Nothing :: Maybe SourcePos, a) <- parseJSON2 v
      pure $ Qualified ByNullSourcePos a

would be preferable? The byMaybeModuleName is redundant I think, should only reach here if the array has null in first position.

Copy link
Copy Markdown
Contributor Author

@pete-murphy pete-murphy Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also wondering if the ToJSON instance should map ByNullSourcePos to null (currently maps to [0,0]), so that decoding then encoding is a round trip.

Copy link
Copy Markdown
Contributor Author

@pete-murphy pete-murphy Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just updating the SourcePos instances seems simpler 4d70482 Oops, it seemed more elegant, but now failing some tests Nevermind nevermind, I think my local branch was just in a bad state.

@pete-murphy pete-murphy force-pushed the pm/qualified-compat-fromjson-instance branch from 36ae105 to 1ae1490 Compare October 16, 2022 21:24
@pete-murphy pete-murphy marked this pull request as ready for review October 16, 2022 21:24
@pete-murphy pete-murphy changed the title Make FromJSON instance for (Qualified a) backwards compatible Make FromJSON instance for Qualified backwards compatible Oct 16, 2022
@pete-murphy pete-murphy force-pushed the pm/qualified-compat-fromjson-instance branch from 4d70482 to 1ae1490 Compare October 17, 2022 04:27
@pete-murphy pete-murphy marked this pull request as draft October 17, 2022 04:50
@pete-murphy pete-murphy marked this pull request as ready for review October 17, 2022 05:38
toJSON SourcePos{..} =
A.toJSON [sourcePosLine, sourcePosColumn]
toJSON = \case
NullSourcePos -> A.Null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change in that anything consuming this JSON now needs to handle the null case where it wouldn't have had to previously. If this data type occurs in CoreFn, that will be a problem. I think this is worse than losing the roundtrip property. We should have the roundtrip property for JSON produced by the current version, but losing it for JSON that can only be produced by previous versions is fine, I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I've reverted to previous commit.

@pete-murphy pete-murphy force-pushed the pm/qualified-compat-fromjson-instance branch from 4d70482 to 1ae1490 Compare October 17, 2022 12:58
@thomashoneyman thomashoneyman merged commit a2b0476 into purescript:master Oct 17, 2022
@pete-murphy pete-murphy deleted the pm/qualified-compat-fromjson-instance branch October 17, 2022 14:26
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