git.iko.soy/typeable/aeson-inflections
on master help sign in
Cleanup sso (#4648)
de6eb74Alexey Uimanov
Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
aeson-inflections.cabalnew file+21
@@ -0,0 +1,21 @@
1+name: aeson-inflections
2+version: 0.1.0.0
3+synopsis: Collection of useful functions for aeson instances and stuff
4+author: Author name here
5+maintainer: example@example.com
6+copyright: 2020 Author name here
7+category: Web
8+build-type: Simple
9+cabal-version: >=1.10
10+
11+library
12+ hs-source-dirs: src
13+ ghc-options: -Wall
14+ exposed-modules: Data.Aeson.Inflections
15+ build-depends: base >= 4.7 && < 5
16+ , aeson
17+ , inflections
18+ , lens
19+ , megaparsec
20+ , text
21+ default-language: Haskell2010
src/Data/Aeson/Inflections.hsnew file+52
@@ -0,0 +1,52 @@
1+module Data.Aeson.Inflections where
2+
3+import Control.Lens
4+import Control.Monad
5+import Data.Char
6+import Data.Text as T (Text, pack, unpack)
7+import Data.Void
8+import qualified Text.Inflections as I
9+import Text.Megaparsec.Error
10+
11+
12+-- | Drops until first uppercase character and apply 'toUnderscore' to
13+-- the rest
14+defaultFieldLabelModifier :: String -> String
15+defaultFieldLabelModifier = toUnderscore . dropPrefix
16+
17+defaultFieldLabelModifier' :: Text -> Text
18+defaultFieldLabelModifier' = T.pack . defaultFieldLabelModifier . T.unpack
19+
20+-- | Just drops prefix
21+dropPrefix :: String -> String
22+dropPrefix = dropWhile (not . isUpper)
23+
24+-- | Cuts the prefix or throws error if prefix does not match. For TH
25+-- modifiers only
26+cutPrefix :: String -> String -> String
27+cutPrefix pref s = if pref == take plen s
28+ then drop plen s
29+ else error "Prefix does not match!"
30+ where
31+ plen = length pref
32+
33+-- | Drops prefix and turns first character to lowercase
34+dropPrefixLower :: String -> String
35+dropPrefixLower = over _head toLower . dropPrefix
36+
37+toUnderscore :: String -> String
38+toUnderscore = fromInflection I.toUnderscore
39+
40+toDashed :: String -> String
41+toDashed = fromInflection I.toDashed
42+
43+fromInflection
44+ :: (Text -> Either (ParseErrorBundle Text Void) Text)
45+ -> String
46+ -> String
47+fromInflection f = either error' T.unpack . f . T.pack
48+ where
49+ error' = error . show . I.InflectionParsingFailed
50+
51+toSpaced :: String -> String
52+toSpaced = fromInflection (I.toHumanized False <=< I.toUnderscore)