From: Eugene Crosser Date: Mon, 14 Apr 2014 14:32:07 +0000 (+0400) Subject: deep merge maps X-Git-Tag: 0.9.0.0~67 X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=4b7ba1ac6879c5277fc45d52b3f7b783d881b9b9;p=pdns-pipe-nmc.git deep merge maps --- diff --git a/NmcDom.hs b/NmcDom.hs index adb3789..7eb4ee6 100644 --- a/NmcDom.hs +++ b/NmcDom.hs @@ -13,7 +13,7 @@ import Data.Text (Text, unpack) import Data.List as L (union) import Data.List.Split import Data.Char -import Data.Map as M (Map, lookup, delete, size, union) +import Data.Map as M (Map, lookup, delete, size, unionWith) import Data.Vector (toList,(!),length, singleton) import Control.Applicative ((<$>), (<*>), empty, pure) import Data.Aeson @@ -37,8 +37,8 @@ obj .:/ key = case H.lookup key obj of class Mergeable a where merge :: a -> a -> a -- bias towads second arg -instance Ord k => Mergeable (Map k a) where - merge mx my = M.union my mx +instance (Ord k, Mergeable a) => Mergeable (Map k a) where + merge mx my = M.unionWith merge my mx -- instance Mergeable String where -- merge _ b = b diff --git a/d/extra1 b/d/extra1 index b0fdcf5..19bfa43 100644 --- a/d/extra1 +++ b/d/extra1 @@ -1 +1 @@ -{"service":[["smtp", "tcp", 0, 0, 25, "mail.host.com."]],"import":"d/extra2","ip":["1.2.3.4"],"alias":"extra1alias"} +{"service":[["smtp", "tcp", 0, 0, 25, "mail.host.com."]],"import":"d/extra2","ip":["1.2.3.4"],"alias":"extra1alias","map":{"mail":"1.1.1.1","www":"1.1.1.1"}} diff --git a/d/extra2 b/d/extra2 index c899105..78de00e 100644 --- a/d/extra2 +++ b/d/extra2 @@ -1 +1 @@ -{"ip":["5.6.7.8"],"alias":"extra2alias","service":[["imap", "tcp", 0, 0, 143, "mail.host.com."]]} +{"ip":["5.6.7.8"],"alias":"extra2alias","service":[["imap", "tcp", 0, 0, 143, "mail.host.com."]],"map":{"ftp":"2.2.2.2","www":"2.2.2.2"}}