From 709f60ab694c2e0429f4585512f14a2c133f7d9c Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Mon, 12 Feb 2024 04:31:54 +0100 Subject: `Only [Field] -> `Only (N.NonEmpty Field)` --- app/Main.hs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'app') 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 -- cgit v1.2.3