[web-devel] [Yesod] how to write PersistFIeld instance?
Michael Snoyman
michael at snoyman.com
Thu Feb 3 18:14:22 CET 2011
On Thu, Feb 3, 2011 at 6:34 PM, Katsutoshi Itoh <cutsea110 at gmail.com> wrote:
> I'd like to introduce serializable data type like this:
>
> data Role = Teacher | Student
> deriving (Read, Show, Eq, Ord)
>
> share2 mkPersist (mkMigrate "migrateAll") [$persist|
> User
> ident String
> :
> :
> role Role Maybe Update
> :
> |]
>
> instance PersistField Role where
> ???
> Would you teach me how to write this instance?
Check out the Haskellers codebase[1]: Persistent includes a TH
function called derivePersistField[2] which does this automatically
for you based on Show/Read instances. I personally find this the best
approach to the problem. It's more efficient to serialize to integers
(and you could use an Enum instance for that), but it's less
future-proof.
Michael
[1] https://github.com/snoyberg/haskellers/blob/master/Model.hs
[2] http://hackage.haskell.org/packages/archive/persistent/0.3.1.3/doc/html/Database-Persist-TH.html#v:derivePersistField
More information about the web-devel
mailing list