Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lens example in tutorial does not work #664

Open
tdimiduk opened this issue Mar 11, 2023 · 1 comment
Open

Lens example in tutorial does not work #664

tdimiduk opened this issue Mar 11, 2023 · 1 comment
Labels

Comments

@tdimiduk
Copy link
Contributor

The lens generation example in the docs appears to be malformed. UserId seems to be used as a data constructor in the pattern match, but it is not one:

Prelude> :load Tutorial.hs 
[1 of 1] Compiling Main             ( Tutorial.hs, interpreted )

Tutorial.hs:39:10: error:
    Not in scope: data constructor ‘UserId’
    Perhaps you meant ‘User’ (line 8)
   |
39 |         (UserId (LensFor addressForUserId)) =

Where I have made a file called Tutorial.hs that pulls in the relevant lines from tutorials 1 and 2

Tutorial.hs

{-#  LANGUAGE ImpredicativeTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
import Database.Beam
import GHC.Int
import Data.Text (Text)

data UserT f
    = User
    { _userEmail     :: Columnar f Text
    , _userFirstName :: Columnar f Text
    , _userLastName  :: Columnar f Text
    , _userPassword  :: Columnar f Text }
    deriving Generic
type User = UserT Identity
type UserId = PrimaryKey UserT Identity

data AddressT f = Address
                { _addressId    :: C f Int32
                , _addressLine1 :: C f Text
                , _addressLine2 :: C f (Maybe Text)
                , _addressCity  :: C f Text
                , _addressState :: C f Text
                , _addressZip   :: C f Text
                , _addressForUser :: PrimaryKey UserT f }
                  deriving (Generic, Beamable)

type Address = AddressT Identity
deriving instance Show (PrimaryKey UserT Identity)
deriving instance Show Address

instance Table AddressT where
    data PrimaryKey AddressT f = AddressId (Columnar f Int32) deriving (Generic, Beamable)
    primaryKey = AddressId . _addressId
type AddressId = PrimaryKey AddressT Identity -- For convenience

Address (LensFor addressId)    (LensFor addressLine1)
        (LensFor addressLine2) (LensFor addressCity)
        (LensFor addressState) (LensFor addressZip)
        (UserId (LensFor addressForUserId)) =
        tableLenses

User (LensFor userEmail)    (LensFor userFirstName)
     (LensFor userLastName) (LensFor userPassword) =
     tableLenses
@kmicklas kmicklas added the docs label Jun 17, 2023
@augusto
Copy link

augusto commented Oct 28, 2023

This looks like the same issue as in #659. I followed the example on the other issue and was able to generate lenses using micro lens, but got stuck trying to generate a lens for the foreign key between Address and User. This is the code I've got

addressId :: Lens Address Address Int32 Int32
addressLine1 :: Lens Address Address Text Text
addressLine2 :: Lens Address Address (Maybe Text) (Maybe Text)
addressCity :: Lens Address Address Text Text
addressState :: Lens Address Address Text Text
addressZip :: Lens Address Address Text Text
--addressForUser :: Lens Address Address ??? ???
Address (LensFor addressId) _ _ _ _ _ _ = tableLenses
Address _ (LensFor addressLine1) _ _ _ _ _ = tableLenses
Address _ _ (LensFor addressLine2) _ _ _ _ = tableLenses
Address _ _ _ (LensFor addressCity) _ _ _  = tableLenses
Address _ _ _ _ (LensFor addressState) _ _  = tableLenses
Address _ _ _ _ _ (LensFor addressZip) _  = tableLenses
-- Address _ _ _ _ _ _ (LensFor addressForUser) = tableLenses

userEmail :: Lens User User Text Text
userFirstName :: Lens User User Text Text
userLastName :: Lens User User Text Text
userPassword :: Lens User User Text Text
User (LensFor userEmail) _ _ _ = tableLenses
User _ (LensFor userFirstName) _ _ = tableLenses
User _ _ (LensFor userLastName) _ = tableLenses
User _ _ _ (LensFor userPassword) = tableLenses

I'm getting my head around Haskell, but does this mean that Beam cannot create lenses without using a library or templates with GHC>9.2 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants