mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-03-31 09:46:11 +00:00
core: fix VersionRange JSON instances (#1037)
This commit is contained in:
committed by
GitHub
parent
03d73f442f
commit
b4e55146b8
@@ -2,9 +2,9 @@
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE FunctionalDependencies #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE PatternSynonyms #-}
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
|
||||
module Simplex.Messaging.Version
|
||||
@@ -27,11 +27,13 @@ module Simplex.Messaging.Version
|
||||
where
|
||||
|
||||
import Control.Applicative (optional)
|
||||
import qualified Data.Aeson.TH as J
|
||||
import qualified Data.Aeson as J
|
||||
import qualified Data.Aeson.Encoding as JE
|
||||
import Data.Aeson.Types ((.:), (.=))
|
||||
import qualified Data.Aeson.Types as JT
|
||||
import qualified Data.Attoparsec.ByteString.Char8 as A
|
||||
import Simplex.Messaging.Encoding
|
||||
import Simplex.Messaging.Encoding.String
|
||||
import Simplex.Messaging.Parsers (defaultJSON)
|
||||
import Simplex.Messaging.Version.Internal (Version (..))
|
||||
|
||||
pattern VersionRange :: Version v -> Version v -> VersionRange v
|
||||
@@ -45,6 +47,18 @@ data VersionRange v = VRange
|
||||
}
|
||||
deriving (Eq, Show)
|
||||
|
||||
instance J.FromJSON (VersionRange v) where
|
||||
parseJSON (J.Object v) = do
|
||||
minVersion <- v .: "minVersion"
|
||||
maxVersion <- v .: "maxVersion"
|
||||
pure VRange {minVersion, maxVersion}
|
||||
parseJSON invalid =
|
||||
JT.prependFailure "bad VersionRange, " (JT.typeMismatch "Object" invalid)
|
||||
|
||||
instance J.ToJSON (VersionRange v) where
|
||||
toEncoding VRange {minVersion, maxVersion} = JE.pairs $ ("minVersion" .= minVersion) <> ("maxVersion" .= maxVersion)
|
||||
toJSON VRange {minVersion, maxVersion} = J.object ["minVersion" .= minVersion, "maxVersion" .= maxVersion]
|
||||
|
||||
class VersionScope v
|
||||
|
||||
-- | construct valid version range, to be used in constants
|
||||
@@ -123,5 +137,3 @@ compatibleVersion x vr =
|
||||
|
||||
mkCompatibleIf :: a -> Bool -> Maybe (Compatible a)
|
||||
x `mkCompatibleIf` cond = if cond then Just $ Compatible_ x else Nothing
|
||||
|
||||
$(J.deriveJSON defaultJSON ''VersionRange)
|
||||
|
||||
Reference in New Issue
Block a user