{-# LANGUAGE RankNTypes #-}
module Database.Bolt.Lens
( exact
, field
, prop
)
where
import Data.Functor.Contravariant (Contravariant (..))
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Text (Text)
import qualified Database.Bolt as B
type Getter s a = forall f. (Functor f, Contravariant f) => (a -> f a) -> (s -> f s)
type Fold s a = forall f. (Applicative f, Contravariant f) => (a -> f a) -> (s -> f s)
exact :: B.RecordValue a => Fold B.Value a
exact = to B.exact . _Just
field :: B.RecordValue a => Text -> Fold B.Record a
field key = ix key . exact
prop :: B.RecordValue a => Text -> Fold B.Node a
prop key = to B.nodeProps . ix key . exact
to :: (s -> a) -> Getter s a
to f g = contramap f . g . f
_Just :: Fold (Maybe a) a
_Just f s =
case s of
Just a -> s <$ f a
Nothing -> pure s
ix :: Ord k => k -> Fold (Map k v) v
ix k = to (M.lookup k) . _Just