Skip to content

Commit

Permalink
Merge pull request #23 from kutyel/feature-add-strip-suffix
Browse files Browse the repository at this point in the history
Add StripSuffix modifier
  • Loading branch information
fumieval authored Nov 23, 2024
2 parents c1556e3 + 314e5de commit cea5989
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ main = BL.putStrLn $ encode testData

* `Prefixed str` = `CustomJSON '[FieldLabelModifier (StripPrefix str)]`
* `PrefixedSnake str` = `CustomJSON '[FieldLabelModifier (StripPrefix str, CamelToSnake)]`
* `Suffixed str` = `CustomJSON '[FieldLabelModifier (StripSuffix str)]`
* `SuffixedSnake str` = `CustomJSON '[FieldLabelModifier (StripSuffix str, CamelToSnake)]`
* `Snake` = `CustomJSON '[FieldLabelModifier '[StripPrefix str, CamelToSnake]]`
* `Vanilla` = `CustomJSON '[]`

Expand Down
20 changes: 15 additions & 5 deletions src/Deriving/Aeson.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
--------------------
-- | Type-directed aeson instance CustomJSONisation
Expand All @@ -27,6 +27,7 @@ module Deriving.Aeson
, SumTwoElemArray
-- * Name modifiers
, StripPrefix
, StripSuffix
, CamelTo
, CamelToKebab
, CamelToSnake
Expand All @@ -38,7 +39,7 @@ module Deriving.Aeson
, FromJSON
, ToJSON
, Generic
)where
) where

import Data.Aeson
import Data.Coerce
Expand Down Expand Up @@ -86,6 +87,9 @@ data UnwrapUnaryRecords
-- | Strip prefix @t@. If it doesn't have the prefix, keep it as-is.
data StripPrefix t

-- | Strip suffix @t@. If it doesn't have the suffix, keep it as-is.
data StripSuffix t

-- | Generic CamelTo constructor taking in a separator char
data CamelTo (separator :: Symbol)

Expand All @@ -105,6 +109,12 @@ class StringModifier t where
instance KnownSymbol k => StringModifier (StripPrefix k) where
getStringModifier = fromMaybe <*> stripPrefix (symbolVal (Proxy @k))

stripSuffix :: Eq a => [a] -> [a] -> Maybe [a]
stripSuffix a b = reverse <$> stripPrefix (reverse a) (reverse b)

instance KnownSymbol k => StringModifier (StripSuffix k) where
getStringModifier = fromMaybe <*> stripSuffix (symbolVal (Proxy @k))

instance StringModifier '[] where
getStringModifier = id

Expand Down
8 changes: 8 additions & 0 deletions src/Deriving/Aeson/Stock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
module Deriving.Aeson.Stock
( Prefixed
, PrefixedSnake
, Suffixed
, SuffixedSnake
, Snake
, Vanilla
-- * Reexports
Expand All @@ -21,6 +23,12 @@ type Prefixed str = CustomJSON '[FieldLabelModifier (StripPrefix str)]
-- | Strip @str@ prefices and convert from CamelCase to snake_case
type PrefixedSnake str = CustomJSON '[FieldLabelModifier '[StripPrefix str, CamelToSnake]]

-- | Field names are suffixed by @str@; strip them from JSON representation
type Suffixed str = CustomJSON '[FieldLabelModifier (StripSuffix str)]

-- | Strip @str@ suffixes and convert from CamelCase to snake_case
type SuffixedSnake str = CustomJSON '[FieldLabelModifier '[StripSuffix str, CamelToSnake]]

-- | Convert from CamelCase to snake_case
type Snake = CustomJSON '[FieldLabelModifier CamelToSnake]

Expand Down
5 changes: 5 additions & 0 deletions tests/test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ data Foo = Foo { fooFoo :: Int, fooBar :: Int }
deriving (FromJSON, ToJSON)
via Prefixed "foo" Foo

data Something = Something { somethingKun :: Int, somethingElseKun :: Int }
deriving Generic
deriving (FromJSON, ToJSON) via Suffixed "Kun" Something

testData :: [User]
testData = [User 42 "Alice" Nothing "human", User 43 "Bob" (Just "xyz") "bot"]

Expand Down Expand Up @@ -57,6 +61,7 @@ data MultipleFieldRenames = MultipleFieldRenames
main = do
BL.putStrLn $ encode testData
BL.putStrLn $ encode $ Foo 0 1
BL.putStrLn $ encode $ Something 0 1

assertEq
(toJSON RenamedCtorOptA)
Expand Down

0 comments on commit cea5989

Please sign in to comment.