module Database.InfluxDB.Types.Internal
( stripPrefixOptions
, stripPrefixLower
, stripPrefixSnake
) where
import Data.Char (isUpper, toLower)
#if MIN_VERSION_aeson(0, 6, 2)
import Data.Aeson.TH (Options(..), defaultOptions)
#endif
#if MIN_VERSION_aeson(0, 6, 2)
stripPrefixOptions :: String -> Options
stripPrefixOptions name = defaultOptions
{ fieldLabelModifier = stripPrefixLower name
}
#else
stripPrefixOptions :: String -> String -> String
stripPrefixOptions = stripPrefixLower
#endif
stripPrefixLower
:: String
-> String
-> String
stripPrefixLower prefix xs = case drop (length prefix) xs of
[] -> error "Insufficient length of field name"
c:cs -> toLower c : cs
stripPrefixSnake
:: String
-> String
-> String
stripPrefixSnake prefix xs = case drop (length prefix) xs of
[] -> error "Insufficient length of field name"
cs -> toSnake cs
where
toSnake = dropWhile (== '_') . foldr f []
f c cs
| isUpper c = '_':toLower c:cs
| otherwise = c:cs