diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 05:30:47 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 05:30:47 +0100 |
commit | 9e79b37ce9f6f863d50659d1c51620d544cc5b5c (patch) | |
tree | 50d6dac8c580e0bcefc16fceeb84acee6e0f0d5a /app/Field.hs | |
parent | 0ae2a8ebbc88e013d2a79ee727da82931002ef96 (diff) |
refactor `Record`
Diffstat (limited to 'app/Field.hs')
-rw-r--r-- | app/Field.hs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/app/Field.hs b/app/Field.hs new file mode 100644 index 0000000..68baec7 --- /dev/null +++ b/app/Field.hs @@ -0,0 +1,32 @@ +module 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) |