stackage-server/Stackage/Database/Types.hs
Konstantin Zudov 912a0175d4 Provide snapshot content as JSON
```json
$ http --json http://localhost:4000/lts-5.1
{
  "snapshot": {
    "ghc": "7.10.3",
    "created": "2016-01-30",
    "name": "lts-5.1"
  },
  "packages": [
    {
      "isCore": false,
      "name": "abstract-deque",
      "version": "0.3",
      "synopsis": "Abstract, parameterized interface to mutable Deques."
    },
    {
      "isCore": false,
      "name": "abstract-par",
      "version": "0.3.3",
      "synopsis": "Type classes generalizing the functionality of the 'monad-par' library."
    },
    ...
  ]
}

```
2016-02-02 15:50:17 +02:00

56 lines
1.5 KiB
Haskell

module Stackage.Database.Types
( SnapName (..)
, isLts
, isNightly
) where
import ClassyPrelude.Conduit
import Web.PathPieces
import Data.Aeson.Extra
import Data.Text.Read (decimal)
import Database.Persist
import Database.Persist.Sql
data SnapName = SNLts !Int !Int
| SNNightly !Day
deriving (Eq, Ord, Read, Show)
isLts :: SnapName -> Bool
isLts SNLts{} = True
isLts SNNightly{} = False
isNightly :: SnapName -> Bool
isNightly SNLts{} = False
isNightly SNNightly{} = True
instance ToJSONKey SnapName where
toJSONKey = toPathPiece
instance ToJSON SnapName where
toJSON = String . toPathPiece
instance PersistField SnapName where
toPersistValue = toPersistValue . toPathPiece
fromPersistValue v = do
t <- fromPersistValue v
case fromPathPiece t of
Nothing -> Left $ "Invalid SnapName: " ++ t
Just x -> return x
instance PersistFieldSql SnapName where
sqlType = sqlType . fmap toPathPiece
instance PathPiece SnapName where
toPathPiece (SNLts x y) = concat ["lts-", tshow x, ".", tshow y]
toPathPiece (SNNightly d) = "nightly-" ++ tshow d
fromPathPiece t0 =
nightly <|> lts
where
nightly = fmap SNNightly $ stripPrefix "nightly-" t0 >>= readMay
lts = do
t1 <- stripPrefix "lts-" t0
Right (x, t2) <- Just $ decimal t1
t3 <- stripPrefix "." t2
Right (y, "") <- Just $ decimal t3
return $ SNLts x y