module Data.Hermes.Decoder.Path ( dot , removePath , withPath , withPathIndex ) where import Control.Monad.Reader (local) import Data.Maybe (fromMaybe) import Data.Text (Text) import qualified Data.Text as T import Data.Hermes.Decoder.Types (Decoder, HermesEnv(hPath)) withPath :: Text -> Decoder a -> Decoder a withPath :: forall a. Text -> Decoder a -> Decoder a withPath Text key = forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a local forall a b. (a -> b) -> a -> b $ \HermesEnv st -> HermesEnv st { hPath :: Text hPath = HermesEnv -> Text hPath HermesEnv st forall a. Semigroup a => a -> a -> a <> Text key } {-# INLINE withPath #-} removePath :: Text -> Decoder a -> Decoder a removePath :: forall a. Text -> Decoder a -> Decoder a removePath Text key = forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a local forall a b. (a -> b) -> a -> b $ \HermesEnv st -> HermesEnv st { hPath :: Text hPath = forall a. a -> Maybe a -> a fromMaybe (HermesEnv -> Text hPath HermesEnv st) (Text -> Text -> Maybe Text T.stripSuffix Text key forall a b. (a -> b) -> a -> b $ HermesEnv -> Text hPath HermesEnv st) } {-# INLINE removePath #-} withPathIndex :: Int -> Decoder a -> Decoder a withPathIndex :: forall a. Int -> Decoder a -> Decoder a withPathIndex Int idx = forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a local forall a b. (a -> b) -> a -> b $ \HermesEnv st -> HermesEnv st { hPath :: Text hPath = forall a. a -> Maybe a -> a fromMaybe (HermesEnv -> Text hPath HermesEnv st) (Text -> Text -> Maybe Text T.stripSuffix (forall {a}. Show a => a -> Text showInt forall a b. (a -> b) -> a -> b $ forall a. Ord a => a -> a -> a max Int 0 (Int idx forall a. Num a => a -> a -> a - Int 1)) forall a b. (a -> b) -> a -> b $ HermesEnv -> Text hPath HermesEnv st) forall a. Semigroup a => a -> a -> a <> forall {a}. Show a => a -> Text showInt Int idx } where showInt :: a -> Text showInt a i = Text -> Text dot forall b c a. (b -> c) -> (a -> b) -> a -> c . String -> Text T.pack forall a b. (a -> b) -> a -> b $ forall a. Show a => a -> String show a i {-# INLINE withPathIndex #-} dot :: Text -> Text dot :: Text -> Text dot = Char -> Text -> Text T.cons Char '/' {-# INLINE dot #-}