]> www.average.org Git - pdns-pipe-nmc.git/commitdiff
make "map" parser detect format errors
authorEugene Crosser <crosser@average.org>
Thu, 8 May 2014 22:45:03 +0000 (02:45 +0400)
committerEugene Crosser <crosser@average.org>
Thu, 8 May 2014 22:45:03 +0000 (02:45 +0400)
NmcDom.hs
SPEC.md
d/extra1

index 8bfb6f358bde32dbcf037ee59b77f97c978f904e..2fdd6b99a3b41df75dd71c210d23aa2e29992796 100644 (file)
--- 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 4a0fb4ac7fdf82a890821236345580353d9b828e..57940a5beddc51f243a927ee1e824067e8cf7d1d 100644 (file)
--- 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" }
index b1fc2138733c132c3242fdf5c4e7d744abb3e2d6..b1fdc0a3d44fa2f81dab568f884f3ba9963c412f 100644 (file)
--- 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"]}}}}}