-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.hs
51 lines (45 loc) · 1.86 KB
/
example.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# LANGUAGE DataKinds, DeriveGeneric, DerivingVia, FlexibleInstances #-}
{-# LANGUAGE TypeOperators, MultiParamTypeClasses #-}
module Main where
import Data.Aeson
import Data.Aeson.Generic.DerivingVia
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Reflection
import GHC.Generics
import Data.DerivingIso
import Data.Semigroup (Last(..), First(..), Dual(..))
data MyConfig = MyConfig { mcNameOfProcess :: String
, mcArgsToProcess :: [String]
}
deriving (Read, Show, Eq, Ord, Generic)
deriving (ToJSON, FromJSON)
via WithOptions '[ FieldLabelModifier '[CamelTo2 "_" , Drop 2]
, ConstructorTagModifier '[CamelTo2 "_"]
]
MyConfig
deriving (Semigroup)
via MyConfig `SameRepAs` (Last String, [String])
data Init
instance Reifies Init (String -> String) where
reflect _ = init
data OtherConfig = OtherConfig { otrNameOfProcess :: Maybe String
, otrArgsToProcess :: [String]
}
deriving (Read, Show, Eq, Ord, Generic)
deriving (ToJSON, FromJSON)
via WithOptions '[ FieldLabelModifier '[CamelTo2 "-"]
, ConstructorTagModifier '[CamelTo2 "-", UserDefined Init]
, SumEnc TwoElemArr
, TagSingleConstructors 'True
, OmitNothingFields 'True
]
OtherConfig
main :: IO ()
main = do
LBS.putStrLn $ encode $ MyConfig "foo" ["bar", "baz"]
LBS.putStrLn $ encode $ OtherConfig Nothing ["bar", "baz"]
{-
ghci> main
{"args_to_process":["bar","baz"],"name_of_process":"foo"}
["other-config",{"otr-args-to-process":["bar","baz"]}]
-}