From 1f0961535a0b6cdadb01c9896c391704f6ff8a74 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Thu, 17 Apr 2014 02:02:41 +0400 Subject: [PATCH] split map keys on dots --- NmcTransform.hs | 27 +++++++++++++++++++++++++-- d/extra1 | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/NmcTransform.hs b/NmcTransform.hs index c753f80..dc5f0ff 100644 --- a/NmcTransform.hs +++ b/NmcTransform.hs @@ -4,7 +4,9 @@ module NmcTransform ( seedNmcDom import Prelude hiding (lookup) import Data.ByteString.Lazy (ByteString) -import Data.Map (empty, lookup, delete, size, singleton) +import Data.Text.Lazy (splitOn, pack, unpack) +import Data.Map.Lazy (empty, lookup, delete, size, singleton + , foldrWithKey, insert, insertWith) import Control.Monad (foldM) import Data.Aeson (decode) @@ -33,7 +35,7 @@ mergeImport :: -> IO (Either String NmcDom) -- ^ result with merged import mergeImport queryOp depth base = do let - mbase = (expandSrv . mergeSelf) base + mbase = (expandSrv . splitSubdoms . mergeSelf) base base' = mbase {domImport = Nothing} -- print base if depth <= 0 then return $ Left "Nesting of imports is too deep" @@ -96,6 +98,27 @@ expandSrv base = && srvPort sr == 25 then Just [(show (srvPrio sr)) ++ " " ++ (srvHost sr)] else Nothing + +-- | Convert map elements of the form "subN...sub2.sub1.dom.bit" +-- into nested map and merge it +splitSubdoms :: NmcDom -> NmcDom +splitSubdoms base = + let + base' = base { domMap = Nothing } + in + case domMap base of + Nothing -> base' + Just sdmap -> (emptyNmcDom { domMap = Just sdmap' }) `mergeNmcDom` base' + where + sdmap' = foldrWithKey stow empty sdmap + stow fqdn sdom acc = insertWith mergeNmcDom fqdn' sdom' acc + where + (fqdn', sdom') = + nest (map unpack (splitOn (pack ".") (pack fqdn)), sdom) + nest ([], v) = (fqdn, v) -- can split result be empty? + nest ([k], v) = (k, v) + nest (k:ks, v) = + nest (ks, emptyNmcDom { domMap = Just (singleton k v) }) -- | Presence of some elements require removal of some others normalizeDom :: NmcDom -> NmcDom diff --git a/d/extra1 b/d/extra1 index a2a8a5d..32ec73e 100644 --- a/d/extra1 +++ b/d/extra1 @@ -1 +1 @@ -{"service":[["smtp", "tcp", 0, 0, 25, "mail.host.com."]],"import":["d/extra2","d/extra3"],"ip":["1.2.3.4"],"alias":"extra1alias","map":{"mail":"1.1.1.1","www":"1.1.1.1"}} +{"service":[["smtp", "tcp", 0, 0, 25, "mail.host.com."]],"import":["d/extra2","d/extra3"],"ip":["1.2.3.4"],"alias":"extra1alias","map":{"mail":"1.1.1.1","www":"1.1.1.1","smtp.eu":{"ip6":"dead::0002"},"eu":{"map":{"smtp":{"ip6":["dead::0001"]}}}}} -- 2.43.0