{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StrictData #-}
module Headroom.Configuration.Enrich
(
Enrich(..)
, ValueType(..)
, withArray
, withText
, replaceEmptyValue
)
where
import Data.Aeson ( ToJSON(..) )
import Headroom.Serialization ( prettyPrintYAML )
import RIO
import qualified RIO.Map as M
import qualified RIO.Text as T
import qualified RIO.Text.Partial as TP
newtype Enrich = Enrich
{ Enrich -> Text -> Text
enrich :: Text -> Text
}
instance Semigroup Enrich where
Enrich Text -> Text
fnA <> :: Enrich -> Enrich -> Enrich
<> Enrich Text -> Text
fnB = (Text -> Text) -> Enrich
Enrich ((Text -> Text) -> Enrich) -> (Text -> Text) -> Enrich
forall a b. (a -> b) -> a -> b
$ Text -> Text
fnA (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
fnB
instance Monoid Enrich where
mempty :: Enrich
mempty = (Text -> Text) -> Enrich
Enrich Text -> Text
forall a. a -> a
id
data ValueType
= Array
| String
deriving (ValueType -> ValueType -> Bool
(ValueType -> ValueType -> Bool)
-> (ValueType -> ValueType -> Bool) -> Eq ValueType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ValueType -> ValueType -> Bool
$c/= :: ValueType -> ValueType -> Bool
== :: ValueType -> ValueType -> Bool
$c== :: ValueType -> ValueType -> Bool
Eq, Int -> ValueType -> ShowS
[ValueType] -> ShowS
ValueType -> String
(Int -> ValueType -> ShowS)
-> (ValueType -> String)
-> ([ValueType] -> ShowS)
-> Show ValueType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ValueType] -> ShowS
$cshowList :: [ValueType] -> ShowS
show :: ValueType -> String
$cshow :: ValueType -> String
showsPrec :: Int -> ValueType -> ShowS
$cshowsPrec :: Int -> ValueType -> ShowS
Show)
withArray :: ToJSON a
=> [a]
-> Text
-> (ValueType, Text)
withArray :: [a] -> Text -> (ValueType, Text)
withArray [a]
list Text
field = (ValueType
Array, Text -> [a] -> Text
forall a. ToJSON a => Text -> a -> Text
asYAML Text
field [a]
list)
withText :: Text
-> Text
-> (ValueType, Text)
withText :: Text -> Text -> (ValueType, Text)
withText Text
text Text
field = (ValueType
String, Text -> Text -> Text
forall a. ToJSON a => Text -> a -> Text
asYAML Text
field Text
text)
replaceEmptyValue :: Text
-> (Text -> (ValueType, Text))
-> Enrich
replaceEmptyValue :: Text -> (Text -> (ValueType, Text)) -> Enrich
replaceEmptyValue Text
field Text -> (ValueType, Text)
replaceFn = (Text -> Text) -> Enrich
Enrich ((Text -> Text) -> Enrich) -> (Text -> Text) -> Enrich
forall a b. (a -> b) -> a -> b
$ \Text
doc -> do
Text -> Text -> Text -> Text
TP.replace Text
old Text
new Text
doc
where
(ValueType
tpe, Text
new) = Text -> (ValueType, Text)
replaceFn Text
field
old :: Text
old = Text
field Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ValueType -> Text
emptyValue ValueType
tpe
asYAML :: ToJSON a => Text -> a -> Text
asYAML :: Text -> a -> Text
asYAML Text
field a
value = Text -> Text
T.stripEnd (Text -> Text) -> (Map Text a -> Text) -> Map Text a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map Text a -> Text
forall a. ToJSON a => a -> Text
prettyPrintYAML (Map Text a -> Text) -> Map Text a -> Text
forall a b. (a -> b) -> a -> b
$ [(Text, a)] -> Map Text a
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Text
field, a
value)]
emptyValue :: ValueType -> Text
emptyValue :: ValueType -> Text
emptyValue ValueType
Array = Text
"[]"
emptyValue ValueType
String = Text
"\"\""