Internal.hs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. module Codec.ActivityStream.Internal (commonOpts, commonOptsCC) where
  2. import Data.Aeson.TH
  3. import Data.Char
  4. toCamelCaseUpper :: String -> String
  5. toCamelCaseUpper = toCamelCase True
  6. toCamelCaseLower :: String -> String
  7. toCamelCaseLower = toCamelCase False
  8. toCamelCase :: Bool -> String -> String
  9. toCamelCase = go
  10. where go _ "" = ""
  11. go _ ('-':cs) = go True cs
  12. go True (c:cs) = toUpper c : go False cs
  13. go False (c:cs) = c : go False cs
  14. fromCamelCase :: String -> String
  15. fromCamelCase (c:cs)
  16. | isUpper c = toLower c : go cs
  17. | otherwise = go (c:cs)
  18. where go "" = ""
  19. go (c:cs)
  20. | c == ' ' = go cs
  21. | isUpper c = '-' : toLower c : go cs
  22. | otherwise = c : go cs
  23. commonOpts :: String -> Options
  24. commonOpts prefix = defaultOptions
  25. { fieldLabelModifier = drop (length prefix)
  26. , omitNothingFields = True
  27. }
  28. commonOptsCC :: String -> Options
  29. commonOptsCC prefix = defaultOptions
  30. { fieldLabelModifier = fromCamelCase . drop (length prefix)
  31. , constructorTagModifier = fromCamelCase
  32. , omitNothingFields = True
  33. }