-- |Aeson derivation combinator, internal
module Polysemy.Time.Json where

import qualified Data.Aeson as Aeson
import Data.Aeson.TH (deriveJSON)
import Data.List (dropWhileEnd)
import qualified Language.Haskell.TH.Syntax as TH

-- |Aeson codec options that remove leading and trailing underscores.
basicOptions :: Aeson.Options
basicOptions :: Options
basicOptions =
  Options
Aeson.defaultOptions { fieldLabelModifier :: String -> String
Aeson.fieldLabelModifier = forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (Char
'_' forall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char
'_' forall a. Eq a => a -> a -> Bool
==) }

-- |Derive Aeson codecs that strip underscores and unwrap data/newtype with single fields.
json :: TH.Name -> TH.Q [TH.Dec]
json :: Name -> Q [Dec]
json =
  Options -> Name -> Q [Dec]
deriveJSON Options
basicOptions { unwrapUnaryRecords :: Bool
Aeson.unwrapUnaryRecords = Bool
True }

-- |Derive Aeson codecs that strip underscores.
unaryJson :: TH.Name -> TH.Q [TH.Dec]
unaryJson :: Name -> Q [Dec]
unaryJson =
  Options -> Name -> Q [Dec]
deriveJSON Options
basicOptions