diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 05:39:19 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 05:39:19 +0100 |
commit | 33faca6f99dc207e81497297c205a1ff29ae2f33 (patch) | |
tree | a73ecda14cf14b5ff3e0c21d92f8f388fec57ff0 /app/Record.hs | |
parent | 504dc557cc041f7427f8b717cf3f7f5e7524f467 (diff) |
dissolve `unique`
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) |