From aa100741449b426c1f55c0c396936b60ec962873 Mon Sep 17 00:00:00 2001 From: Lucy Cifferello <12953208+elvece@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:56:08 -0400 Subject: [PATCH] fix serialization and deserialization of devices jsonb database field --- src/Lib/Types/Manifest.hs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Lib/Types/Manifest.hs b/src/Lib/Types/Manifest.hs index 8b2fe0b..60d8f44 100644 --- a/src/Lib/Types/Manifest.hs +++ b/src/Lib/Types/Manifest.hs @@ -11,7 +11,7 @@ import Data.String.Interpolate.IsString (i) import Data.Text qualified as T import Lib.Types.Core (PkgId, OsArch) import Lib.Types.Emver (Version (..), VersionRange) -import Startlude (ByteString, Eq, Generic, Hashable, Maybe (..), Monad ((>>=)), Read, Show, Text, for, pure, readMaybe, ($), Int, (.), (<>), String, map) +import Startlude (ByteString, Eq, Generic, Hashable, Maybe (..), Monad ((>>=)), Read, Show, Text, for, pure, readMaybe, ($), Int, (.), (<>), String, map, otherwise, show) import Data.Aeson ( eitherDecode, eitherDecodeStrict, @@ -35,6 +35,7 @@ import Database.Persist.Class ( PersistField(..) ) import Data.Aeson.Key ( fromText ) import Data.Maybe (maybe) + data PackageManifest = PackageManifest { packageManifestId :: !PkgId , packageManifestTitle :: !Text @@ -101,7 +102,9 @@ data PackageDevice = PackageDevice (HashMap Text RegexPattern) deriving (Show, Eq) instance ToJSON PackageDevice where - toJSON (PackageDevice hashMap) = object (toJSONKeyValuePairs hashMap) + toJSON (PackageDevice hashMap) + | HM.null hashMap = object [] -- Empty object when the HashMap is empty + | otherwise = object (toJSONKeyValuePairs hashMap) where toJSONKeyValuePairs = map toKeyValue . HM.toList toKeyValue (key, value) = fromText key .= toJSON value @@ -111,9 +114,9 @@ instance FromJSON PackageDevice where pure $ PackageDevice hashMap instance PersistField PackageDevice where - toPersistValue = PersistByteString . BL.toStrict . encode - fromPersistValue (PersistByteString bs) = case eitherDecode (BL.fromStrict bs) of - Left err -> Left $ TE.decodeUtf8 bs <> ": " <> T.pack err + toPersistValue = PersistText . T.pack . show . toJSON + fromPersistValue (PersistText t) = case eitherDecodeStrict (TE.encodeUtf8 t) of + Left err -> Left $ T.pack err Right val -> Right val fromPersistValue _ = Left "Invalid JSON value in database"