{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Headroom.Serialization
(
aesonOptions
, dropFieldPrefix
, symbolCase
, prettyPrintYAML
)
where
import Data.Aeson ( Options
, ToJSON(..)
, defaultOptions
, fieldLabelModifier
)
import qualified Data.Yaml.Pretty as YP
import RIO
import qualified RIO.Char as C
aesonOptions :: Options
aesonOptions :: Options
aesonOptions =
Options
defaultOptions { fieldLabelModifier :: String -> String
fieldLabelModifier = Char -> String -> String
symbolCase Char
'-' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
dropFieldPrefix }
dropFieldPrefix :: String -> String
dropFieldPrefix :: String -> String
dropFieldPrefix = \case
(Char
x : Char
n : String
xs) | Char -> Bool
C.isUpper Char
x Bool -> Bool -> Bool
&& Char -> Bool
C.isUpper Char
n -> Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: Char
n Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs
(Char
x : Char
n : String
xs) | Char -> Bool
C.isUpper Char
x -> Char -> Char
C.toLower Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: Char
n Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs
(Char
_ : String
xs) -> String -> String
dropFieldPrefix String
xs
[] -> []
symbolCase :: Char
-> String
-> String
symbolCase :: Char -> String -> String
symbolCase Char
sym = \case
[] -> []
(Char
x : String
xs) | Char -> Bool
C.isUpper Char
x -> Char
sym Char -> String -> String
forall a. a -> [a] -> [a]
: Char -> Char
C.toLower Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: Char -> String -> String
symbolCase Char
sym String
xs
| Bool
otherwise -> Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: Char -> String -> String
symbolCase Char
sym String
xs
prettyPrintYAML :: ToJSON a
=> a
-> Text
prettyPrintYAML :: a -> Text
prettyPrintYAML = ByteString -> Text
decodeUtf8Lenient (ByteString -> Text) -> (a -> ByteString) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config -> a -> ByteString
forall a. ToJSON a => Config -> a -> ByteString
YP.encodePretty Config
prettyConfig
where prettyConfig :: Config
prettyConfig = (Text -> Text -> Ordering) -> Config -> Config
YP.setConfCompare Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Config
YP.defConfig