packages feed

barbies-th 0.1.9 → 0.1.10

raw patch · 6 files changed

+39/−7 lines, 6 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

+ Barbies.TH: bnestedFieldNames :: (FieldNamesB b, IsString a) => b (Const (NonEmpty a))
+ Barbies.TH.Config: [switchName] :: DeclareBareBConfig -> Q Name
+ Barbies.TH.Config: [wrapperName] :: DeclareBareBConfig -> Q Name
- Barbies.TH.Config: DeclareBareBConfig :: [Name] -> (String -> Maybe String) -> (String -> Maybe String) -> (String -> String) -> DeclareBareBConfig
+ Barbies.TH.Config: DeclareBareBConfig :: [Name] -> (String -> Maybe String) -> (String -> Maybe String) -> (String -> String) -> Q Name -> Q Name -> DeclareBareBConfig

Files

CHANGELOG.md view
@@ -1,5 +1,10 @@ # Revision history for barbies-th +## 0.1.10++* Added `bnestedFieldNames` to `FieldNamesB`+* Made the names of the type parameters customisable via `DeclareBareBConfig`+ ## 0.1.9  * Added `passthroughBareB`
barbies-th.cabal view
@@ -1,6 +1,6 @@ cabal-version:       2.4 name:                barbies-th-version:             0.1.9+version:             0.1.10 synopsis:            Create strippable HKD via TH description:         Please see Data.Barbie.TH homepage:            https://github.com/fumieval/barbies-th@@ -9,8 +9,13 @@ license-file:        LICENSE author:              Fumiaki Kinoshita maintainer:          fumiexcel@gmail.com-copyright:           Copyright (c) 2021 Fumiaki Kinoshita+copyright:           Copyright (c) 2022 Fumiaki Kinoshita category:            Data, Generics, Data Structures+tested-with:+  GHC ==8.8.4+   || ==8.10.7+   || ==9.0.1+   || ==9.2.2 extra-source-files:   CHANGELOG.md   README.md@@ -28,7 +33,7 @@     Data.Barbie.TH   other-extensions:    RankNTypes, PolyKinds, DataKinds, KindSignatures, TemplateHaskell, TypeFamilies   build-depends:       base >= 4.12 && <4.17-    , template-haskell >= 2.14 && <2.18+    , template-haskell >= 2.14 && <2.19     , barbies ^>= 2.0.1     , split ^>= 0.2   hs-source-dirs:      src
src/Barbies/TH.hs view
@@ -27,9 +27,11 @@  import Language.Haskell.TH hiding (cxt) import Language.Haskell.TH.Syntax (VarBangType, Name(..), mkOccName, occString)+import Data.Bifunctor (first) import Data.String import Data.Foldable (foldl') import Data.List (partition, nub)+import qualified Data.List.NonEmpty as NE import Barbies import Barbies.Constraints import Barbies.Bare@@ -68,6 +70,9 @@   -- | A collection of field names.   bfieldNames :: IsString a => b (Const a) +  -- | A collection of field names, prefixed by the names of the parent.+  bnestedFieldNames :: IsString a => b (Const (NE.NonEmpty a))+ -- | Transform a regular Haskell record declaration into HKD form. -- 'BareB', 'FieldNamesB', 'FunctorB', 'DistributiveB', -- 'TraversableB', 'ApplicativeB' and 'ConstraintsB' instances are@@ -106,8 +111,8 @@     go otherBarbieNames (DataD _ dataName0 tvbs _ [con@(RecC nDataCon mangledfields)] classes) = do       let dataName = mkName $ barbieName $ nameBase dataName0       let fields = [(unmangle name, c, t) | (name, c, t) <- mangledfields]-      nSwitch <- newName "sw"-      nWrap <- newName "h"+      nSwitch <- switchName+      nWrap <- wrapperName       let xs = varNames "x" fields       let ys = varNames "y" fields       -- 'mapMembers' applies one of two functions to elements of a list@@ -125,10 +130,15 @@       let transformed = transformCon otherBarbieNames nSwitch nWrap con       let reconE = foldl' appE (conE nDataCon)           -- field names for FieldNamesB+          strLit str = [|fromString $(litE $ StringL str)|]           fieldNamesE = reconE $ mapMembers-            (\(name,_,_) -> [|Const $ fromString $(litE $ StringL $ nameBase name)|])+            (\(name,_,_) -> conE 'Const `appE` strLit (nameBase name))             (\_ -> [|bfieldNames|])             fields+          nestedFieldNamesE = reconE $ mapMembers+            (\(name,_,_) -> [|Const $ pure $(strLit $ nameBase name)|])+            (\(name,_,_) -> [|first (NE.cons $(strLit $ nameBase name)) `bmap` bnestedFieldNames|])+            fields           accessors = reconE $ mapMembers             (\name -> [|LensB                 $(varE name)@@ -188,7 +198,9 @@           bstrip $(conP nDataCon $ map varP xs)             = $(reconE $ mapMembers (appE (varE 'runIdentity)) (appE (varE 'bstrip)) (varE <$> xs))           {-# INLINE bstrip #-}-        instance FieldNamesB $(pure coveredType) where bfieldNames = $(fieldNamesE)+        instance FieldNamesB $(pure coveredType) where+          bfieldNames = $(fieldNamesE)+          bnestedFieldNames = $(nestedFieldNamesE)         instance AccessorsB $(pure coveredType) where baccessors = $(accessors)         instance FunctorB $(pure coveredType) where           bmap f $(conP nDataCon $ map varP xs)
src/Barbies/TH/Config.hs view
@@ -14,6 +14,10 @@   -- ^ generate a type synonym for the 'Barbies.Bare.Covered' type?   , barbieName :: String -> String   -- ^ modify the name of the datatype+  , switchName :: Q Name+  -- ^ the name of the type parameter to toggle between Bare and covered+  , wrapperName :: Q Name+  -- ^ the name of the type parameter of the wrapper for each field   }  -- | Does not define any type synonyms@@ -23,6 +27,8 @@   , bareName = const Nothing   , coveredName = const Nothing   , barbieName = id+  , switchName = newName "sw"+  , wrapperName = newName "h"   }  -- | Defines a synonym for the bare type with the same name.@@ -33,4 +39,6 @@   , bareName = Just   , coveredName = Just . (++"H")   , barbieName = (++"B")+  , switchName = newName "sw"+  , wrapperName = newName "h"   }
tests/Main.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-}
tests/passthrough.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-}