Skip to content

Commit

Permalink
Fix some misplaced parse warnings #4789
Browse files Browse the repository at this point in the history
Instead of FromJSON instances for Repo and PackageMetadata, use the
Data.Aeson.Extended mechanisms to properly track which fields are used.
  • Loading branch information
snoyberg committed May 1, 2019
1 parent 595e9ea commit 3f05443
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
63 changes: 28 additions & 35 deletions subs/pantry/src/Pantry/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,6 @@ instance Display Repo where
(if T.null subdir
then mempty
else " in subdirectory " <> display subdir)
instance FromJSON Repo where
parseJSON =
withObject "Repo" $ \o -> do
repoSubdir <- o .: "subdir"
repoCommit <- o .: "commit"
(repoType, repoUrl) <-
(o .: "git" >>= \url -> pure (RepoGit, url)) <|>
(o .: "hg" >>= \url -> pure (RepoHg, url))
pure Repo {..}


-- An unexported newtype wrapper to hang a 'FromJSON' instance off of. Contains
Expand Down Expand Up @@ -1411,16 +1402,15 @@ instance Display PackageMetadata where
, "cabal file == " <> display (pmCabal pm)
]

instance FromJSON PackageMetadata where
parseJSON =
withObject "PackageMetadata" $ \o -> do
pmCabal :: BlobKey <- o .: "cabal-file"
pantryTree :: BlobKey <- o .: "pantry-tree"
CabalString pkgName <- o .: "name"
CabalString pkgVersion <- o .: "version"
let pmTreeKey = TreeKey pantryTree
pmIdent = PackageIdentifier {..}
pure PackageMetadata {..}
parsePackageMetadata :: Object -> WarningParser PackageMetadata
parsePackageMetadata o = do
pmCabal :: BlobKey <- o ..: "cabal-file"
pantryTree :: BlobKey <- o ..: "pantry-tree"
CabalString pkgName <- o ..: "name"
CabalString pkgVersion <- o ..: "version"
let pmTreeKey = TreeKey pantryTree
pmIdent = PackageIdentifier {..}
pure PackageMetadata {..}


-- | Conver package metadata to its "raw" equivalent.
Expand Down Expand Up @@ -1540,35 +1530,38 @@ instance FromJSON (WithJSONWarnings (Unresolved PackageLocationImmutable)) where
<|> fail ("Could not parse a UnresolvedPackageLocationImmutable from: " ++ show v)
where
repoObject :: Value -> Parser (WithJSONWarnings (Unresolved PackageLocationImmutable))
repoObject value = do
pm <- parseJSON value
repo <- parseJSON value
pure $ noJSONWarnings $ pure $ PLIRepo repo pm

archiveObject value = do
pm <- parseJSON value
withObjectWarnings "UnresolvedPackageLocationImmutable.PLIArchive" (\o -> do
repoObject = withObjectWarnings "UnresolvedPackageLocationImmutable.PLIRepo" $ \o -> do
pm <- parsePackageMetadata o
repoSubdir <- o ..: "subdir"
repoCommit <- o ..: "commit"
(repoType, repoUrl) <-
(o ..: "git" >>= \url -> pure (RepoGit, url)) <|>
(o ..: "hg" >>= \url -> pure (RepoHg, url))
pure $ pure $ PLIRepo Repo {..} pm

archiveObject =
withObjectWarnings "UnresolvedPackageLocationImmutable.PLIArchive" $ \o -> do
pm <- parsePackageMetadata o
Unresolved mkArchiveLocation <- parseArchiveLocationObject o
archiveHash <- o ..: "sha256"
archiveSize <- o ..: "size"
archiveSubdir <- o ..:? "subdir" ..!= ""
pure $ Unresolved $ \mdir -> do
archiveLocation <- mkArchiveLocation mdir
pure $ PLIArchive Archive {..} pm
) value

hackageObject value =
withObjectWarnings "UnresolvedPackagelocationimmutable.PLIHackage (Object)" (\o -> do
hackageObject =
withObjectWarnings "UnresolvedPackagelocationimmutable.PLIHackage (Object)" $ \o -> do
treeKey <- o ..: "pantry-tree"
htxt <- o ..: "hackage"
case parseHackageText htxt of
Left e -> fail $ show e
Right (pkgIdentifier, blobKey) ->
pure $ pure $ PLIHackage pkgIdentifier blobKey (TreeKey treeKey)) value
pure $ pure $ PLIHackage pkgIdentifier blobKey (TreeKey treeKey)

github value = do
pm <- parseJSON value
github value =
withObjectWarnings "UnresolvedPackagelocationimmutable.PLIArchive:github" (\o -> do
pm <- parsePackageMetadata o
GitHubRepo ghRepo <- o ..: "github"
commit <- o ..: "commit"
let archiveLocation = ALUrl $ T.concat
Expand All @@ -1594,7 +1587,7 @@ instance FromJSON (WithJSONWarnings (Unresolved (NonEmpty RawPackageLocationImmu
<|> fail ("Could not parse a UnresolvedRawPackageLocationImmutable from: " ++ show v)
where
http :: Value -> Parser (WithJSONWarnings (Unresolved (NonEmpty RawPackageLocationImmutable)))
http = withText "UnresolvedPackageLocationImmutable.UPLIArchive (Text)" $ \t ->
http = withText "UnresolvedPackageLocationImmutable.RPLIArchive (Text)" $ \t ->
case parseArchiveLocationText t of
Nothing -> fail $ "Invalid archive location: " ++ T.unpack t
Just (Unresolved mkArchiveLocation) ->
Expand Down Expand Up @@ -1640,7 +1633,7 @@ instance FromJSON (WithJSONWarnings (Unresolved (NonEmpty RawPackageLocationImmu
os <- optionalSubdirs o
pure $ pure $ NE.map (\(repoSubdir, pm) -> RPLIRepo Repo {..} pm) (osToRpms os)

archiveObject = withObjectWarnings "UnresolvedPackageLocationImmutable.UPLIArchive" $ \o -> do
archiveObject = withObjectWarnings "UnresolvedPackageLocationImmutable.RPLIArchive" $ \o -> do
Unresolved mkArchiveLocation <- parseArchiveLocationObject o
raHash <- o ..:? "sha256"
raSize <- o ..:? "size"
Expand Down
19 changes: 6 additions & 13 deletions subs/pantry/test/Pantry/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ spec = do
liftIO $
Yaml.toJSON (nightlySnapshotLocation day) `shouldBe`
Yaml.String (T.pack $ "nightly-" ++ show day)
it "FromJSON instance for Repo" $ do
repValue <-
case Yaml.decodeThrow samplePLIRepo of
Just x -> pure x
Nothing -> fail "Can't parse Repo"
it "FromJSON instance for PLIRepo" $ do
WithJSONWarnings unresolvedPli warnings <- Yaml.decodeThrow samplePLIRepo
warnings `shouldBe` []
pli <- resolvePaths Nothing unresolvedPli
let repoValue =
Repo
{ repoSubdir = "wai"
Expand All @@ -153,13 +152,7 @@ spec = do
"d11d63f1a6a92db8c637a8d33e7953ce6194a3e0"
, repoUrl = "https://github.com/yesodweb/wai.git"
}
repValue `shouldBe` repoValue
it "FromJSON instance for PackageMetadata" $ do
pkgMeta <-
case Yaml.decodeThrow samplePLIRepo of
Just x -> pure x
Nothing -> fail "Can't parse Repo"
let cabalSha =
cabalSha =
SHA256.fromHexBytes
"eea52c4967d8609c2f79213d6dffe6d6601034f1471776208404781de7051410"
pantrySha =
Expand All @@ -177,7 +170,7 @@ spec = do
, pmTreeKey = TreeKey (BlobKey psha (FileSize 714))
, pmCabal = BlobKey csha (FileSize 1765)
}
pkgMeta `shouldBe` pkgValue
pli `shouldBe` PLIRepo repoValue pkgValue
it "parseHackageText parses" $ do
let txt =
"persistent-2.8.2@sha256:df118e99f0c46715e932fe82d787fc09689d87898f3a8b13f5954d25af6b46a1,5058"
Expand Down

0 comments on commit 3f05443

Please sign in to comment.