1 module NmcTransform ( seedNmcDom
5 import Prelude hiding (lookup)
6 import Data.ByteString.Lazy (ByteString)
7 import Data.Map.Lazy (empty, lookup, delete, size, singleton
8 , foldrWithKey, insert, insertWith)
9 import Control.Monad (foldM)
10 import Data.Aeson (decode)
11 import Data.Default.Class (def)
15 -- | Perform query and return error string or parsed domain object
17 (String -> IO (Either String ByteString)) -- ^ query operation action
19 -> IO (Either String NmcDom) -- ^ error string or domain
20 queryNmcDom queryOp key = do
23 Left estr -> return $ Left estr
24 Right str -> case decode str :: Maybe NmcDom of
25 Nothing -> return $ Left $ "Unparseable value: " ++ (show str)
26 Just dom -> return $ Right dom
28 -- | Try to fetch "delegate" or "import" object and merge them into the
29 -- base domain. Original "import" element is removed, but newly
30 -- merged data may contain new "import" or "delegate", so the objects
31 -- that are about to be merged are processed recursively until there
32 -- are no more "import" and "deletage" attributes (or the depth gauge
35 (String -> IO (Either String ByteString)) -- ^ query operation action
36 -> Int -- ^ recursion counter
37 -> NmcDom -- ^ base domain
38 -> IO (Either String NmcDom) -- ^ result with merged import
39 mergeIncl queryOp depth base = do
41 mbase = mergeSelf base
42 base' = mbase {domDelegate = Nothing, domImport = Nothing}
44 if depth <= 0 then return $ Left "Nesting of imports is too deep"
45 else case ((domDelegate mbase), (domImport mbase)) of
46 (Nothing, Nothing ) -> return $ Right base'
47 (Nothing, Just keys) -> foldM mergeIncl1 (Right base') keys
48 (Just key, _ ) -> mergeIncl1 (Right def) key
50 mergeIncl1 (Left err) _ = return $ Left err -- can never happen
51 mergeIncl1 (Right acc) key = do
52 sub <- queryNmcDom queryOp key
54 Left err -> return $ Left err
55 Right sub' -> mergeIncl queryOp (depth - 1) $ sub' `merge` acc
57 -- | If there is an element in the map with key "", merge the contents
58 -- and remove this element. Do this recursively.
59 mergeSelf :: NmcDom -> NmcDom
63 base' = base {domSubmap = removeSelf map}
64 removeSelf Nothing = Nothing
65 removeSelf (Just map) = if size map' == 0 then Nothing else Just map'
66 where map' = delete "" map
71 case lookup "" map' of
73 Just sub -> (mergeSelf sub) `merge` base'
74 -- recursion depth limited by the size of the record
76 -- | transfer some elements of `base` into `sub`, notably TLSA
77 propagate :: NmcDom -> NmcDom -> NmcDom
78 propagate base sub = sub -- FIXME implement it
80 -- | Presence of some elements require removal of some others
81 normalizeDom :: NmcDom -> NmcDom
82 normalizeDom dom = foldr id dom [ translateNormalizer
86 nsNormalizer dom = case domNs dom of
88 Just ns -> def { domNs = domNs dom, domEmail = domEmail dom }
89 translateNormalizer dom = case domTranslate dom of
91 Just tr -> dom { domSubmap = Nothing }
93 -- | Merge imports and Selfs and follow the maps tree to get dom
95 (String -> IO (Either String ByteString)) -- ^ query operation action
96 -> [String] -- ^ subdomain chain
97 -> NmcDom -- ^ base domain
98 -> IO (Either String NmcDom) -- ^ fully processed result
99 descendNmcDom queryOp subdom base = do
100 base' <- mergeIncl queryOp 10 base
102 [] -> return $ fmap normalizeDom base'
105 Left err -> return base'
107 case domSubmap base'' of
108 Nothing -> return $ Right def
111 Nothing -> return $ Right def
112 Just sub -> descendNmcDom queryOp ds $ propagate base'' sub
114 -- | Initial NmcDom populated with "import" only, suitable for "descend"
116 String -- ^ domain key (without namespace prefix)
117 -> NmcDom -- ^ resulting seed domain
118 seedNmcDom dn = def { domImport = Just (["d/" ++ dn])}