diff options
Diffstat (limited to 'app/Record.hs')
-rw-r--r-- | app/Record.hs | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/app/Record.hs b/app/Record.hs index d23d289..30e98ec 100644 --- a/app/Record.hs +++ b/app/Record.hs @@ -4,6 +4,7 @@ module Record toValue, lookup, Records, + lookups, select, disjointUnion, disjointUnions, @@ -16,7 +17,7 @@ import Data.Aeson.Key qualified as JK import Data.Aeson.KeyMap qualified as JM import Data.List (foldl') import Data.List.NonEmpty qualified as N -import Data.Maybe (mapMaybe) +import Data.Maybe (fromMaybe, mapMaybe) import Data.Text qualified as T import Exception (DuplicateField (DuplicateField)) import Field qualified as F @@ -48,18 +49,19 @@ lookup' (k : ks) (J.Object kvs) = type Records a = [Record a] +lookups :: F.Field -> Records J.Value -> Maybe J.Value +lookups f rs = + case mapMaybe (lookup f) rs of + [] -> Nothing + [v] -> Just v + (_ : _) -> throw (DuplicateField (F.toString f)) + select :: [F.Field] -> Records J.Value -> J.Value select fs rs = foldl' union (J.Object JM.empty) - (map (\f -> F.prefix f (select' rs f)) fs) - -select' :: Records J.Value -> F.Field -> J.Value -select' rs f = case mapMaybe (lookup f) rs of - [] -> J.Null - [v] -> v - (_ : _) -> throw (DuplicateField (F.toString f)) + (map (\f -> F.prefix f ((fromMaybe J.Null (lookups f rs)))) fs) union :: J.Value -> J.Value -> J.Value union (J.Object r) (J.Object s) = J.Object (JM.unionWith union r s) |