diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 04:31:54 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-02-12 04:31:54 +0100 |
commit | 709f60ab694c2e0429f4585512f14a2c133f7d9c (patch) | |
tree | f825a7e7d3a3fbfe45fc25b6349a183cc31114aa | |
parent | 92f403a2c7c6c61f16bf472ad75a7539bc7f2786 (diff) |
`Only [Field] -> `Only (N.NonEmpty Field)`
-rw-r--r-- | app/Main.hs | 16 | ||||
-rw-r--r-- | json2sql.cabal | 1 |
2 files changed, 11 insertions, 6 deletions
diff --git a/app/Main.hs b/app/Main.hs index 23f2c9e..3cb3b79 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,17 +1,20 @@ {-# LANGUAGE BlockArguments #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-} module Main where import Control.Exception (Exception, throw) import Control.Monad (void) +import Control.Monad.Combinators.NonEmpty qualified as PN import Data.Aeson qualified as J import Data.Aeson.Key qualified as JK import Data.Aeson.KeyMap qualified as JM import Data.ByteString.Lazy.Char8 qualified as LB import Data.Char (isSpace) import Data.List (foldl', intercalate, isSuffixOf) +import Data.List.NonEmpty qualified as N import Data.Maybe (catMaybes, fromMaybe, mapMaybe) import Data.Set qualified as S import Data.String (IsString (fromString)) @@ -65,7 +68,8 @@ instance Show Query where ] where showFieldSelector All = "*" - showFieldSelector (Only fs) = intercalate ", " (map showField fs) + showFieldSelector (Only (N.toList -> fs)) = + intercalate ", " (map showField fs) showField (Qualified c k) = c <> "." <> T.unpack k showField (Unqualified k) = T.unpack k showCollection c = c @@ -104,7 +108,7 @@ instance Show Query where data FieldSelector = All - | Only [Field] + | Only (N.NonEmpty Field) deriving (Show) data Field @@ -224,7 +228,7 @@ instance IsString Query where void $ lexeme $ P.string "*" pure All, do - Only <$> P.sepBy1 field comma + Only <$> PN.sepBy1 field comma ] field :: P.Parsec Void String Field @@ -369,10 +373,10 @@ select (Only fs) vs = Qualified c k -> Just $ J.Object $ JM.singleton (JK.fromText (T.pack c <> "." <> k)) J.Null Unqualified k -> Just $ J.Object $ JM.singleton (JK.fromText k) J.Null ) - fs + (N.toList fs) -select' :: [Field] -> Record J.Value -> Record J.Value -select' fs (Record c (J.Object kvs)) = +select' :: N.NonEmpty Field -> Record J.Value -> Record J.Value +select' (N.toList -> fs) (Record c (J.Object kvs)) = Record c . J.Object $ JM.fromList . mapMaybe match . JM.toList $ kvs diff --git a/json2sql.cabal b/json2sql.cabal index 3a39f88..5851d0e 100644 --- a/json2sql.cabal +++ b/json2sql.cabal @@ -33,6 +33,7 @@ executable json2sql gitlib-libgit2, megaparsec, mtl, + parser-combinators, tagged, text, unliftio, |