, NmcRRService(..)
, NmcRRI2p(..)
, NmcRRTls(..)
- , emptyNmcDom
+ , NmcRRDs(..)
, mergeNmcDom
) where
import qualified Data.HashMap.Strict as H (lookup)
import Data.Aeson
import Data.Aeson.Types
+import Data.Default.Class
-- Variant of Aeson's `.:?` that interprets a String as a
-- single-element list, so it is possible to have either
merge _ b = b
data NmcRRI2p = NmcRRI2p
- { i2pDestination :: String
- , i2pName :: String
- , i2pB32 :: String
+ { i2pDestination :: Maybe String
+ , i2pName :: Maybe String
+ , i2pB32 :: Maybe String
} deriving (Show, Eq)
instance FromJSON NmcRRI2p where
parseJSON (Object o) = NmcRRI2p
- <$> o .: "destination"
- <*> o .: "name"
- <*> o .: "b32"
+ <$> o .:? "destination"
+ <*> o .:? "name"
+ <*> o .:? "b32"
parseJSON _ = empty
instance Mergeable NmcRRI2p where
, domDs :: Maybe [NmcRRDs]
, domMx :: Maybe [String] -- Synthetic
, domSrv :: Maybe [String] -- Synthetic
+ , domTlsa :: Maybe [String] -- Synthetic
} deriving (Show, Eq)
+instance Default NmcDom where
+ def = NmcDom Nothing Nothing Nothing Nothing Nothing Nothing Nothing
+ Nothing Nothing Nothing Nothing Nothing Nothing Nothing
+ Nothing Nothing Nothing Nothing Nothing Nothing Nothing
+
instance FromJSON NmcDom where
-- Wherever we expect a domain object, there may be a string
-- containing IPv4 address. Interpret it as such.
-- Question: shall we try to recognize IPv6 addresses too?
parseJSON (String s) =
return $ if isIPv4 s'
- then emptyNmcDom { domIp = Just [s'] }
- else emptyNmcDom
+ then def { domIp = Just [s'] }
+ else def
where
s' = unpack s
isIPv4 x = all isNibble $ splitOn "." x
<*> o .:? "ds"
<*> return Nothing -- domMx not parsed
<*> return Nothing -- domSrv not parsed
+ <*> return Nothing -- domTlsa not parsed
parseJSON _ = empty
instance Mergeable NmcDom where
, domDs = mergelm domDs
, domMx = mergelm domMx
, domSrv = mergelm domSrv
+ , domTlsa = mergelm domTlsa
}
where
mergelm x = merge (x sub) (x dom)
mergeNmcDom :: NmcDom -> NmcDom -> NmcDom
mergeNmcDom = merge
-
-emptyNmcDom = NmcDom Nothing Nothing Nothing Nothing Nothing Nothing
- Nothing Nothing Nothing Nothing Nothing Nothing
- Nothing Nothing Nothing Nothing Nothing Nothing
- Nothing Nothing