git.iko.soy/typeable/aeson-inflections
on master help sign in
LICENSEnew file+2
since splitsnapshots
unifiedsplit
@@ -0,0 +1,2 @@
1+Copyright © 2020, Antorica LLC
2+All rights reserved
aeson-inflections.cabal+29−19
@@ -1,21 +1,31 @@
1+cabal-version: 2.4
1−name: aeson-inflections
2+name: aeson-inflections
2−version: 0.1.0.0
3+version: 0.1.0.0
4+synopsis:
3−synopsis: Collection of useful functions for aeson instances and stuff
5+ Collection of useful functions for aeson instances and stuff
6+
4−author: Author name here
7+author: Author name here
5−maintainer: example@example.com
8+maintainer: example@example.com
6−copyright: 2020 Author name here
9+license-file: LICENSE
7−category: Web
10+category: Web
8−build-type: Simple
11+build-type: Simple
9−cabal-version: >=1.10
10
12
11library
13library
12− hs-source-dirs: src
14+ hs-source-dirs: src
13− ghc-options: -Wall
15+ ghc-options: -Wall
16+ exposed-modules:
14− exposed-modules: Data.Aeson.Inflections
17+ Data.Aeson.Inflections
18+ Data.Aeson.Inflections.Generic
19+
15− build-depends: base >= 4.7 && < 5
20+ build-depends:
21+ , aeson
22+ , base >=4.12 && <5
16− , aeson
23+ , deriving-aeson >=0.2
17− , inflections
24+ , inflections
18− , lens
25+ , lens
19− , megaparsec
26+ , megaparsec >=7.0
27+ , template-haskell
20− , text
28+ , text
29+
30+ default-extensions: DataKinds
21− default-language: Haskell2010
31+ default-language: Haskell2010
src/Data/Aeson/Inflections.hs+38−2
@@ -1,10 +1,28 @@
1−module Data.Aeson.Inflections where
1+module Data.Aeson.Inflections
2+ ( defaultFieldLabelModifier
3+ , defaultFieldLabelModifier'
4+ , deriveDefaultJSON
5+ , deriveEnumJSON
6+ , dropPrefix
7+ , dropPrefixLower
8+ , dropByPrefixLength
9+ , cutPrefix
10+ , cutOptionalPrefix
11+ , toUnderscore
12+ , toDashed
13+ , toSpaced
14+ ) where
2
15
3import Control.Lens
16import Control.Lens
4import Control.Monad
17import Control.Monad
18+import Data.Aeson
19+import Data.Aeson.TH
5import Data.Char
20import Data.Char
6−import Data.Text as T (Text, pack, unpack)
21+import Data.Maybe
22+import Data.Text (Text)
23+import qualified Data.Text as T
7import Data.Void
24import Data.Void
25+import Language.Haskell.TH
8import qualified Text.Inflections as I
26import qualified Text.Inflections as I
9import Text.Megaparsec.Error
27import Text.Megaparsec.Error
10
28
@@ -17,10 +35,24 @@ defaultFieldLabelModifier = toUnderscore . dropPrefix
17defaultFieldLabelModifier' :: Text -> Text
35defaultFieldLabelModifier' :: Text -> Text
18defaultFieldLabelModifier' = T.pack . defaultFieldLabelModifier . T.unpack
36defaultFieldLabelModifier' = T.pack . defaultFieldLabelModifier . T.unpack
19
37
38+deriveDefaultJSON :: Name -> Q [Dec]
39+deriveDefaultJSON = deriveJSON defaultOptions
40+ { fieldLabelModifier = defaultFieldLabelModifier }
41+
42+deriveEnumJSON :: String -> Name -> Q [Dec]
43+deriveEnumJSON prefix = deriveJSON defaultOptions
44+ { constructorTagModifier = toUnderscore . cutPrefix prefix }
45+
20-- | Just drops prefix
46-- | Just drops prefix
21dropPrefix :: String -> String
47dropPrefix :: String -> String
22dropPrefix = dropWhile (not . isUpper)
48dropPrefix = dropWhile (not . isUpper)
23
49
50+-- | The same as @Prelude.drop@, but takes a string, instead of a length.
51+-- This makes code more explicit (easier to see what exactly we are expecting to
52+-- @drop@)
53+dropByPrefixLength :: Text -> (String -> String)
54+dropByPrefixLength t = drop (T.length t)
55+
24-- | Cuts the prefix or throws error if prefix does not match. For TH
56-- | Cuts the prefix or throws error if prefix does not match. For TH
25-- modifiers only
57-- modifiers only
26cutPrefix :: String -> String -> String
58cutPrefix :: String -> String -> String
@@ -30,6 +62,10 @@ cutPrefix pref s = if pref == take plen s
30 where
62 where
31 plen = length pref
63 plen = length pref
32
64
65+-- | Drop prefix if exists
66+cutOptionalPrefix :: Text -> Text -> Text
67+cutOptionalPrefix prefix txt = fromMaybe txt $ T.stripPrefix prefix txt
68+
33-- | Drops prefix and turns first character to lowercase
69-- | Drops prefix and turns first character to lowercase
34dropPrefixLower :: String -> String
70dropPrefixLower :: String -> String
35dropPrefixLower = over _head toLower . dropPrefix
71dropPrefixLower = over _head toLower . dropPrefix
src/Data/Aeson/Inflections/Generic.hsnew file+46
@@ -0,0 +1,46 @@
1+{-# LANGUAGE KindSignatures #-}
2+
3+module Data.Aeson.Inflections.Generic
4+ -- Reexports from Deriving.Aeson
5+ ( CustomJSON(..)
6+ , FieldLabelModifier
7+ , ConstructorTagModifier
8+ , CamelToSnake
9+ , StripPrefix
10+ , OmitNothingFields
11+
12+ , DropPrefix
13+ , DropPrefixLower
14+ , ToLower
15+ , DefaultFieldLabelModifier
16+ , DefaultJSON
17+ , DefaultEnumJSON
18+ )
19+ where
20+
21+import Data.Aeson.Inflections
22+import Data.Char
23+import Deriving.Aeson
24+import GHC.TypeLits
25+
26+data DropPrefix
27+
28+instance StringModifier DropPrefix where
29+ getStringModifier = dropPrefix
30+
31+data DropPrefixLower
32+
33+instance StringModifier DropPrefixLower where
34+ getStringModifier = dropPrefixLower
35+
36+data ToLower
37+
38+instance StringModifier ToLower where
39+ getStringModifier = map toLower
40+
41+type DefaultFieldLabelModifier = (DropPrefix, CamelToSnake)
42+
43+type DefaultJSON = CustomJSON '[FieldLabelModifier DefaultFieldLabelModifier]
44+
45+type DefaultEnumJSON (prefix :: Symbol)
46+ = CustomJSON '[ConstructorTagModifier (StripPrefix prefix, CamelToSnake)]