diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 10:05:02 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 10:25:37 +0100 |
commit | 68566ca5a376f8508fdd1c5eff3155cde7929850 (patch) | |
tree | 3573f5b5fe392d6b46f08ef259a2be65baf77308 /app/Query/Field.hs | |
parent | 33faca6f99dc207e81497297c205a1ff29ae2f33 (diff) |
refactor `Query`
Diffstat (limited to 'app/Query/Field.hs')
-rw-r--r-- | app/Query/Field.hs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/app/Query/Field.hs b/app/Query/Field.hs new file mode 100644 index 0000000..cdb977a --- /dev/null +++ b/app/Query/Field.hs @@ -0,0 +1,32 @@ +module Query.Field + ( Field (..), + toString, + prefix, + ) +where + +import Data.Aeson qualified as J +import Data.Aeson.Key qualified as JK +import Data.Aeson.KeyMap qualified as JM +import Data.List (intercalate) +import Data.List.NonEmpty qualified as N +import Data.Text qualified as T + +data Field + = Qualified Collection (N.NonEmpty T.Text) + | Unqualified (N.NonEmpty T.Text) + deriving (Show) + +toString :: Field -> String +toString (Qualified c ks) = intercalate "." (c : map T.unpack (N.toList ks)) +toString (Unqualified ks) = intercalate "." (map T.unpack (N.toList ks)) + +type Collection = FilePath + +prefix :: Field -> J.Value -> J.Value +prefix (Qualified c ks) = prefix' (T.pack c : N.toList ks) +prefix (Unqualified ks) = prefix' (N.toList ks) + +prefix' :: [T.Text] -> J.Value -> J.Value +prefix' ks v = + foldr ((J.Object .) . JM.singleton) v (map JK.fromText ks) |