aboutsummaryrefslogtreecommitdiffstats
path: root/src/Store/Query/Field.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-02-13 02:07:20 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-02-13 02:10:56 +0100
commit950eea3ba04e94cf3d5797f9b5d32b2621c89b55 (patch)
tree2e6aee5b7f571ca8022181689d5650a8c1b82f03 /src/Store/Query/Field.hs
parentb110c5904d4b252d0adbb7fbfabd3270a7844fd3 (diff)
refactor library
Diffstat (limited to 'src/Store/Query/Field.hs')
-rw-r--r--src/Store/Query/Field.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/Store/Query/Field.hs b/src/Store/Query/Field.hs
new file mode 100644
index 0000000..69a0983
--- /dev/null
+++ b/src/Store/Query/Field.hs
@@ -0,0 +1,32 @@
+module Store.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)