From 9e17e79e456acf048f55c2bd6b3dab53a3c24e6c Mon Sep 17 00:00:00 2001 From: Lucy Cifferello <12953208+elvece@users.noreply.github.com> Date: Fri, 2 Jul 2021 18:37:14 -0400 Subject: [PATCH] add read instance for emver version --- src/Handler/Marketplace.hs | 20 +++++++++++--------- src/Lib/Types/Category.hs | 5 ----- src/Lib/Types/Emver.hs | 7 +++++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Handler/Marketplace.hs b/src/Handler/Marketplace.hs index 9424910..5232dfc 100644 --- a/src/Handler/Marketplace.hs +++ b/src/Handler/Marketplace.hs @@ -169,20 +169,22 @@ getServiceListR = do , serviceListResServices = services } +-- >>> readMaybe $ "0.3.0" :: Maybe Version +-- Just 0.3.0 + getServiceR :: Handler ServiceRes getServiceR = do getParameters <- reqGetParams <$> getRequest (service, version) <- case lookup "id" getParameters of Nothing -> sendResponseStatus status404 ("id param should exist" :: Text) Just appId' -> do - versionString <- T.filter (not . isSpace) . fromMaybe "*" <$> lookupGetParam "version" - case readMaybe versionString of - Nothing -> do - sendResponseStatus status400 ("Invalid App Version Specification" :: Text) - -- default to latest - need to determine best available based on OS version? - -- runDB $ fetchLatestApp appId' >>= errOnNothing status404 "service not found" - Just v -> runDB $ fetchLatestAppAtVersion appId' v >>= errOnNothing status404 ("service at version " <> show v <> " not found") - $logInfo $ "*******************" <> show version + case lookup "version" getParameters of + -- default to latest - need to determine best available based on OS version? + Nothing -> runDB $ fetchLatestApp appId' >>= errOnNothing status404 "service not found" + Just v -> do + case readMaybe v of + Nothing -> sendResponseStatus status400 ("Invalid App Version Specification" :: Text) + Just vv -> runDB $ fetchLatestAppAtVersion appId' vv >>= errOnNothing status404 ("service at version " <> show v <> " not found") (versions, mappedVersions) <- fetchAllAppVersions (entityKey service) categories <- runDB $ fetchAppCategories (entityKey service) (appsDir, appMgrDir) <- getsYesod $ (( "apps") . resourcesDir &&& staticBinDir) . appSettings @@ -310,4 +312,4 @@ mapEntityToServiceAvailable appMgrDir appsDir domain service = do , serviceAvailableDescShort = sAppDescShort $ entityVal service , serviceAvailableVersion = appVersion , serviceAvailableIcon = icon - } \ No newline at end of file + } diff --git a/src/Lib/Types/Category.hs b/src/Lib/Types/Category.hs index 4710ff0..a0c97ca 100644 --- a/src/Lib/Types/Category.hs +++ b/src/Lib/Types/Category.hs @@ -9,8 +9,6 @@ import Database.Persist.Postgresql import Data.Aeson import Control.Monad import Yesod.Core -import Data.String.Interpolate.IsString -import qualified Data.ByteString.Lazy as BS data CategoryTitle = FEATURED | BITCOIN @@ -42,6 +40,3 @@ instance ToContent CategoryTitle where instance ToTypedContent CategoryTitle where toTypedContent = toTypedContent . toJSON -cat :: BS.ByteString -cat = [i|"featured"|] - diff --git a/src/Lib/Types/Emver.hs b/src/Lib/Types/Emver.hs index dea037c..8adf869 100644 --- a/src/Lib/Types/Emver.hs +++ b/src/Lib/Types/Emver.hs @@ -52,13 +52,16 @@ import Data.Aeson import Startlude (Hashable) -- | AppVersion is the core representation of the SemverQuad type. -newtype Version = Version { unVersion :: (Word, Word, Word, Word) } deriving (Eq, Ord, ToJSONKey, Hashable, Read) +newtype Version = Version { unVersion :: (Word, Word, Word, Word) } deriving (Eq, Ord, ToJSONKey, Hashable) instance Show Version where show (Version (x, y, z, q)) = let postfix = if q == 0 then "" else '.' : show q in show x <> "." <> show y <> "." <> show z <> postfix instance IsString Version where fromString s = either error id $ Atto.parseOnly parseVersion (T.pack s) - +instance Read Version where + readsPrec _ s = case Atto.parseOnly parseVersion (T.pack s) of + Left _ -> [] + Right a -> [(a, "")] -- | A change in the value found at 'major' implies a breaking change in the API that this version number describes major :: Version -> Word major (Version (x, _, _, _)) = x