) where
import Prelude hiding (length)
-import Control.Applicative ((<$>), (<*>), empty, pure)
+import Control.Applicative ((<$>), (<*>), liftA2, empty, pure)
import Data.Char
import Data.Text (Text, unpack)
import Data.List (union)
import qualified Data.Vector as V (singleton)
import Data.Map (Map, unionWith, foldrWithKey)
import qualified Data.Map as M (singleton, empty, insert, insertWith)
-import qualified Data.HashMap.Strict as H (lookup)
+import qualified Data.HashMap.Strict as H (lookup, foldrWithKey)
import Data.Aeson
import Data.Aeson.Types
import Data.Default.Class
Just _ -> empty
makeSubmap :: Object -> Parser (Maybe (Map String NmcDom))
-makeSubmap o = ((.).(.)) merge merge <$> takeTls o <*> takeSrv o <*> takeMap o
+makeSubmap o = takeTls o `fmerge` takeSrv o `fmerge` takeMap o
+ where fmerge = liftA2 merge
takeMap :: Object -> Parser (Maybe (Map String NmcDom))
takeMap o =
case H.lookup "map" o of
Nothing -> pure Nothing
- Just (Object mo) -> do
- unsplit <- (parseJSON (Object mo) :: Parser (Maybe (Map String NmcDom)))
- let result = fmap splitup unsplit
- return result
- where
- splitup :: Map String NmcDom -> Map String NmcDom
- splitup x = foldrWithKey stow M.empty x
- stow fqdn sdom acc = M.insertWith merge fqdn' sdom' acc
- where
- (fqdn', sdom') = nest (filter (/= "") (splitOnDots fqdn), sdom)
- splitOnDots s = splitOn "." s
- nest ([], v) = (fqdn, v) -- can split result be empty?
- nest ([k], v) = (k, v)
- nest (k:ks, v) =
- nest (ks, def { domSubmap = Just (M.singleton k v) })
- _ -> empty
+ Just (Object mo) -> H.foldrWithKey addmapentry (pure (Just M.empty)) mo
+ where
+ addmapentry "" v acc = parseJSON v >>= inject acc ""
+ addmapentry k v acc = nest (splitOn "." (unpack k)) v acc
+ nest [] v acc = empty -- does not happen as a result of splitOn
+ nest [""] v acc = empty -- empty element of fqdn forbidden
+ nest [d] v acc = parseJSON v >>= inject acc d
+ nest (d:ds) v acc =
+ nest ds v acc >>= (inject acc d) . (\r -> def { domSubmap = r })
+ inject acc d r = (fmap.fmap) (M.insertWith merge d r) acc
+ _ -> empty
takeSrv :: Object -> Parser (Maybe (Map String NmcDom))
takeSrv o =
data NmcRRTlsa = NmcRRTlsa
{ tlsMatchType :: Int -- 0:exact 1:sha256 2:sha512
, tlsMatchValue :: String
- , tlsIncSubdoms :: Int -- 1:enforce on subdoms 0:no
+ , tlsIncSubdoms :: Bool -- enforce on subdoms?
} deriving (Show, Eq)
instance FromJSON NmcRRTlsa where
if length a == 3 then NmcRRTlsa
<$> parseJSON (a ! 0)
<*> parseJSON (a ! 1)
- <*> parseJSON (a ! 2)
+ <*> case (a ! 2) of
+ Number 0 -> return False
+ Number 1 -> return True
+ _ -> empty
else empty
parseJSON _ = empty