derive-has-field 0.0.1.1 → 0.0.1.2
raw patch · 3 files changed
+29/−3 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- derive-has-field.cabal +1/−1
- src/DeriveHasField.hs +11/−2
- test/DeriveHasFieldSpec.hs +17/−0
derive-has-field.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: derive-has-field-version: 0.0.1.1+version: 0.0.1.2 synopsis: Derive HasField instances with Template Haskell description: A Template Haskell function to derive HasField instances to utilize OverloadedRecordDot more effectively. category: Template Haskell
src/DeriveHasField.hs view
@@ -44,17 +44,21 @@ -- Build the instances let constructorNamesAndTypes :: [(Name, Type)] constructorNamesAndTypes = zip recordConstructorNames constructorInfo.constructorFields+ parentType =+ foldl'+ (\acc var -> appT acc (varT $ tyVarBndrToName var))+ (conT datatypeInfo.datatypeName)+ datatypeInfo.datatypeVars decs <- for constructorNamesAndTypes $ \(name, ty) -> let currentFieldName = nameBase name wantedFieldName = lowerFirst $ fieldModifier currentFieldName litTCurrentField = litT $ strTyLit currentFieldName litTFieldWanted = litT $ strTyLit wantedFieldName- parentTypeConstructor = conT datatypeInfo.datatypeName in if currentFieldName == wantedFieldName then fail "deriveHasField: after applying fieldModifier, field didn't change" else [d|- instance HasField $litTFieldWanted $parentTypeConstructor $(pure ty) where+ instance HasField $litTFieldWanted $parentType $(pure ty) where getField = $(appTypeE (varE $ mkName "getField") litTCurrentField) |] pure $ Foldable.concat decs@@ -63,3 +67,8 @@ lowerFirst = \case [] -> [] (x : xs) -> toLower x : xs++tyVarBndrToName :: TyVarBndr flag -> Name+tyVarBndrToName = \case+ PlainTV name _ -> name+ KindedTV name _ _ -> name
test/DeriveHasFieldSpec.hs view
@@ -26,6 +26,20 @@ , someTypeSomeEitherField = Right 0 } +data OtherType a b = OtherType+ { otherTypeField :: Maybe a+ , otherTypeOtherField :: Either a b+ }++deriveHasFieldWith (dropPrefix "otherType") ''OtherType++otherType :: OtherType Int String+otherType =+ OtherType+ { otherTypeField = Just 0+ , otherTypeOtherField = Right "hello"+ }+ spec :: Spec spec = do describe "deriveHasField" $ do@@ -34,3 +48,6 @@ someType.someOtherField `shouldBe` 0 someType.someMaybeField `shouldBe` Just 0 someType.someEitherField `shouldBe` Right 0+ it "compiles and gets the right field" $ do+ otherType.field `shouldBe` Just 0+ otherType.otherField `shouldBe` Right "hello"