X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=blobdiff_plain;f=NmcDom.hs;h=79402e7da1b8f185421842552195c06a8b895812;hp=8153a1dd7dad2d0b3128b455cf465bf0a93aa381;hb=aa91db8940672a57169b1bb400fa3c1a1e9dd335;hpb=25e3fdd1a8580a4436088d3f66c14cfa321ad52f diff --git a/NmcDom.hs b/NmcDom.hs index 8153a1d..79402e7 100644 --- a/NmcDom.hs +++ b/NmcDom.hs @@ -4,7 +4,7 @@ module NmcDom ( NmcDom(..) , NmcRRService(..) , NmcRRI2p(..) , NmcRRTls(..) - , emptyNmcDom + , NmcRRDs(..) , mergeNmcDom ) where @@ -19,6 +19,7 @@ import Data.Map (Map, unionWith) 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 @@ -80,16 +81,16 @@ instance Mergeable NmcRRService where 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 @@ -156,14 +157,19 @@ data NmcDom = NmcDom { domService :: Maybe [NmcRRService] , domSrv :: 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 + 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 @@ -226,8 +232,3 @@ instance Mergeable NmcDom where 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