evoke 0.2021.9.14 → 0.2022.5.2
raw patch · 4 files changed
+68/−25 lines, 4 filesdep ~textPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: text
API changes (from Hackage documentation)
Files
- README.markdown +11/−4
- evoke.cabal +2/−1
- src/lib/Evoke/Generator/Common.hs +33/−16
- src/test/Main.hs +22/−4
README.markdown view
@@ -196,20 +196,27 @@ would become `first-name`. - `--camel`: Convert field names into `camelCase`. For example `FirstName`- would become `firstName`. This is usually combined with `--strip PREFIX`.+ would become `firstName`. This is usually combined with `--prefix PREFIX`. - `--snake`: Convert field names into `snake_case`. For example `firstName` would become `first_name`. -- `--strip PREFIX`: Removes the given prefix from field names. If the field+- `--prefix PREFIX`: Removes the given prefix from field names. If the field name doesn't begin with the prefix, an error will be thrown. For example,- with `--strip person` the field name `personFirstName` would become+ with `--prefix person` the field name `personFirstName` would become `FirstName`. +- `--suffix SUFFIX`: Same as `--prefix` except for suffixes.+ - `--title`: Convert field names into `TitleCase`. For example `firstName` would become `FirstName`. -Options are processed in order. So for example `--strip person --kebab` would+- `--rename OLD:NEW`: Renames the given `OLD` field into the given `NEW` one.+ This can be useful to use keywords as field names. For example,+ `--rename type_:type` will change a field called `type_` into a field called+ `type`.++Options are processed in order. So for example `--prefix person --kebab` would change `personFirstName` into `first-name`. ### Arbitrary
evoke.cabal view
@@ -1,7 +1,7 @@ cabal-version: >= 1.10 name: evoke-version: 0.2021.9.14+version: 0.2022.5.2 synopsis: A GHC plugin to derive instances. description: Evoke is a GHC plugin to derive instances. @@ -20,6 +20,7 @@ build-depends: base >= 4.14 && < 4.15 , ghc >= 8.10.1 && < 8.11+ , text >= 1.2.3 && < 1.3 || >= 2.0 && < 2.1 default-language: Haskell2010 exposed-modules: Evoke
src/lib/Evoke/Generator/Common.hs view
@@ -14,6 +14,7 @@ import qualified Data.IORef as IORef import qualified Data.List as List import qualified Data.Maybe as Maybe+import qualified Data.Text as Text import qualified Evoke.Hs as Hs import qualified Evoke.Hsc as Hsc import qualified Evoke.Type.Constructor as Constructor@@ -41,24 +42,40 @@ [ Console.Option [] ["kebab"] (Console.NoArg $ pure . kebab) "" , Console.Option [] ["camel"] (Console.NoArg $ pure . lower) "" , Console.Option [] ["snake"] (Console.NoArg $ pure . snake) ""- , Console.Option- []- ["strip"]- (Console.ReqArg- (\prefix s1 -> case List.stripPrefix prefix s1 of- Nothing ->- Hsc.throwError srcSpan- . Ghc.text- $ show prefix- <> " is not a prefix of "- <> show s1- Just s2 -> pure s2- )- "PREFIX"- )- ""+ , Console.Option [] ["prefix", "strip"] (Console.ReqArg (stripPrefix srcSpan) "PREFIX" ) ""+ , Console.Option [] ["suffix"] (Console.ReqArg (stripSuffix srcSpan) "SUFFIX" ) "" , Console.Option [] ["title"] (Console.NoArg $ pure . upper) ""+ , Console.Option [] ["rename"] (Console.ReqArg (rename srcSpan) "OLD:NEW") "" ]++stripPrefix :: Ghc.SrcSpan -> String -> String -> Ghc.Hsc String+stripPrefix srcSpan prefix s1 = case List.stripPrefix prefix s1 of+ Nothing ->+ Hsc.throwError srcSpan+ . Ghc.text+ $ show prefix+ <> " is not a prefix of "+ <> show s1+ Just s2 -> pure s2++stripSuffix :: Ghc.SrcSpan -> String -> String -> Ghc.Hsc String+stripSuffix srcSpan suffix s1 = case Text.stripSuffix (Text.pack suffix) (Text.pack s1) of+ Nothing ->+ Hsc.throwError srcSpan+ . Ghc.text+ $ show suffix+ <> " is not a suffix of "+ <> show s1+ Just s2 -> pure $ Text.unpack s2++rename :: Ghc.SrcSpan -> String -> String -> Ghc.Hsc String+rename loc arg str =+ case Text.splitOn (Text.singleton ':') $ Text.pack arg of+ [old, new] | not (Text.null old || Text.null new) ->+ pure $ if Text.pack str == old+ then Text.unpack new+ else str+ _ -> Hsc.throwError loc . Ghc.text $ show arg <> " is invalid" -- | Applies all the monadic functions in order beginning with some starting -- value.
src/test/Main.hs view
@@ -35,6 +35,8 @@ T13C1 { t13c1f1 = 15, t13c1f2 = 16 } [Aeson.aesonQQ| { "t13c1f1": 15, "t13c1f2": 16 } |] , testToJSON T14C1 { t14c1f1 = Just 17 } [Aeson.aesonQQ| { "t14c1f1": 17 } |]+ , testToJSON T15C1 { t15c1f1 = 18 } [Aeson.aesonQQ| { "t15c1": 18 } |]+ , testToJSON T16C1 { t16c1f1 = 19 } [Aeson.aesonQQ| { "one": 19 } |] , checkInstances (Proxy.Proxy :: Proxy.Proxy T1) , checkInstances (Proxy.Proxy :: Proxy.Proxy T2) , checkInstances (Proxy.Proxy :: Proxy.Proxy T3)@@ -49,6 +51,8 @@ , checkInstances (Proxy.Proxy :: Proxy.Proxy (T12 X Int X Int X)) , checkInstances (Proxy.Proxy :: Proxy.Proxy T13) , checkInstances (Proxy.Proxy :: Proxy.Proxy T14)+ , checkInstances (Proxy.Proxy :: Proxy.Proxy T15)+ , checkInstances (Proxy.Proxy :: Proxy.Proxy T16) ] checkInstances@@ -115,21 +119,21 @@ { t6c1f1 :: Int } deriving (Eq, Show)- deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --strip t6c1"+ deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --prefix t6c1" -- stripped then capitalized newtype T7 = T7C1 { t7c1f1 :: Int } deriving (Eq, Show)- deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --strip t7c1 --title"+ deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --prefix t7c1 --title" -- capitalized then stripped newtype T8 = T8C1 { t8c1f1 :: Int } deriving (Eq, Show)- deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --title --strip T8c1"+ deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --title --prefix T8c1" -- phantom type newtype T9 a = T9C1@@ -150,7 +154,7 @@ { t11c1f1A :: Int } deriving (Eq, Show)- deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --strip t11c1f1 --camel"+ deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --prefix t11c1f1 --camel" -- multiple type variables data T12 a b c d e = T12C1@@ -173,3 +177,17 @@ } deriving (Eq, Show) deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke"++-- stripped suffix+newtype T15 = T15C1+ { t15c1f1 :: Int+ }+ deriving (Eq, Show)+ deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --suffix f1"++-- renamed+newtype T16 = T16C1+ { t16c1f1 :: Int+ }+ deriving (Eq, Show)+ deriving (Arbitrary, FromJSON, ToJSON, ToSchema) via "Evoke --rename t16c1f1:one"