-- |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 = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (Char
'_' Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char
'_' Char -> Char -> Bool
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