Question

I'm writing a CRUD interface for my Yesod-based site, but I'm having a problem with some values. In particular, I am trying to CRUD on values like:

data Value = Value { valueId :: UUID
                   , otherStuff :: Stuff
                   }

I am having trouble figuring out a good way to create one of these using the tools Yesod and Aeson provide. When I used formlets, in the distant past, I would use the monad transformer version of the form to do something along the lines of:

Value <$> (liftIO getUUID)
      <*> textField Nothing

(I'm probably not getting the formlets syntax exactly right, but hopefully the Haskellers out there get the gist). This had the effect of making a form that did not include a UUID field and a form parser that ran the IO action to make the data at post-time.

I'm not sure how to proceed. I considered the same kind of idea with Aeson parsing, but attoparsec (the underlying parser) is not a monad transformer either.

Can anybody think of any more options other than having an intermediate data representation?

Was it helpful?

Solution

If what you're trying to do is call an IO action in an Applicative form, there is a trick you can use:

lift (liftIO action)

The issue is that an Applicative can't be an instance of MonadIO, but it can be an instance of MonadTrans.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top