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
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))
```
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" }
-{"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"]}}}}}