0

Since I can't map over a record, the best method I have found is to just use brute force:

data Item = Item {
  vendor :: Int,
  lotNumber :: Int,
  description :: String,
  reserve :: Maybe Double,
  preSaleBids :: Maybe Double,
  salePrice :: Maybe Double,
  purchaser :: Maybe Int,
  saleID :: Maybe Int
}

hsToDb :: Item -> [SqlValue]
hsToDb (Item a b c d e f g h) = [toSql a, toSql b, toSql c, toSql d, toSql e, toSql f, toSql g, toSql h]

dbToHs :: [SqlValue] -> Item
dbToHs [a,b,c,d,e,f,g,h] = Item (fromSql a) (fromSql b) (fromSql c) (fromSql d) (fromSql e) (fromSql f) (fromSql g) (fromSql h)

This code looks ugly and also requires updating if I change the length of my Item record so I was wondering whether there's a clever way of generalizing this idea.

James
  • 104
  • 1
  • 8
  • 1
    Possible duplicate of [Deserializing data form a SQL Database](https://stackoverflow.com/questions/21319802/deserializing-data-form-a-sql-database) – HTNW Jul 22 '18 at 21:02

1 Answers1

0

Depending on what library you're using the type (Int, Int, String, ...) may already have a (as in e.g. sqlite-simple) FromRow instance. If you need Item to be a new type you can use the GeneralizedNewtypeDeriving extension to get that instance for free. Barring this you could of course define your own type class/helper method to make it more DRY.

fredefox
  • 681
  • 3
  • 11