From: Eugene Crosser Date: Thu, 8 May 2014 22:45:03 +0000 (+0400) Subject: make "map" parser detect format errors X-Git-Tag: 0.9.0.1~4 X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=851c71d42654a1620ea98d2dfe751d2e7fbc961f;p=pdns-pipe-nmc.git make "map" parser detect format errors --- diff --git a/NmcDom.hs b/NmcDom.hs index 8bfb6f3..2fdd6b9 100644 --- a/NmcDom.hs +++ b/NmcDom.hs @@ -18,7 +18,7 @@ import Data.Vector ((!), length) 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 @@ -78,21 +78,16 @@ takeMap :: Object -> Parser (Maybe (Map String NmcDom)) takeMap o = case H.lookup "map" o of Nothing -> pure Nothing - Just (Object mo) -> do - raw <- (parseJSON (Object mo) :: Parser (Maybe (Map String NmcDom))) - let result = fmap splitup raw - 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) -- preserve "self" map entry - nest ([k], v) = (k, v) - nest (k:ks, v) = - nest (ks, def { domSubmap = Just (M.singleton k v) }) + 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)) diff --git a/SPEC.md b/SPEC.md index 4a0fb4a..57940a5 100644 --- a/SPEC.md +++ b/SPEC.md @@ -325,9 +325,9 @@ the current object. ``` Note: When a key contains dots ".", it is converted to a nested -map. If empty element appears as a result of split, such as when -a dot is at the beginning or at the end of the key, or there are -consequitive dots, such elemets are ignored. For example, +map. Empty elements in the result of split, such as when a dot is +at the beginning or at the end of the key, or there are +consequitive dots, are diagnosed as erroneous data. ``` "map": { "www.uk": { "alias" : "www.example.co.uk" } diff --git a/d/extra1 b/d/extra1 index b1fc213..b1fdc0a 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","smtp.eu":{"ip6":"dead::0002"},"smtp..eu.":{"ip6":"dead::0003"},"eu":{"map":{"smtp":{"ip6":["dead::0001"]}}}}} +{"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"},"smtp.eu":{"ip6":"dead::0003"},"eu":{"map":{"smtp":{"ip6":["dead::0001"]}}}}}