@@ -13,7 +13,9 @@ import Data.Aeson
13
13
import qualified Data.Aeson as A
14
14
import qualified Data.Binary as Binary
15
15
import Data.Binary.Instances ()
16
- import Functora.Prelude
16
+ import Data.Char (isLower , toLower )
17
+ import Data.Typeable (typeRep )
18
+ import Functora.Prelude hiding (toLower )
17
19
import qualified GHC.Generics as Generics
18
20
import qualified Text.URI as URI
19
21
import Toml (HasCodec , HasItemCodec )
@@ -32,11 +34,34 @@ genericTomlCodec =
32
34
Toml. genericCodecWithOptions
33
35
Toml. TomlOptions
34
36
{ Toml. tomlOptionsFieldModifier = \ proxy ->
35
- Toml. stripTypeNamePrefix proxy . \ case
37
+ stripTypeNamePrefix proxy . \ case
36
38
(' _' : xs) -> xs
37
39
xs -> xs
38
40
}
39
41
42
+ stripTypeNamePrefix :: forall a . (Typeable a ) => Proxy a -> String -> String
43
+ stripTypeNamePrefix _ fieldName =
44
+ case stripPrefix (headToLower $ typeName @ a ) fieldName of
45
+ Just rest -> leaveIfEmpty rest
46
+ Nothing -> leaveIfEmpty (dropWhile isLower fieldName)
47
+ where
48
+ headToLower :: String -> String
49
+ headToLower = \ case
50
+ [] -> error " Cannot use 'headToLower' on empty Text"
51
+ x : xs -> toLower x : xs
52
+ -- if all lower case then leave field as it is
53
+ leaveIfEmpty :: String -> String
54
+ leaveIfEmpty rest = if null rest then fieldName else headToLower rest
55
+
56
+ typeName :: forall a . (Typeable a ) => String
57
+ typeName =
58
+ fromMaybe mempty
59
+ . safeHead
60
+ . words
61
+ . show
62
+ . typeRep
63
+ $ Proxy @ a
64
+
40
65
instance A. ToJSON URI where
41
66
toJSON =
42
67
A. toJSON . URI. render
0 commit comments