eitherFromPersistValues :: (PersistEntity record) => EntityDef -> [DB.Field] -> Either T.Text (Entity record)
eitherFromPersistValues entDef doc = case mKey of
   Nothing  -> addDetail $ Left $ "could not find _id field: "
   Just kpv -> do
      body <- addDetail (fromPersistValues (map snd $ orderPersistValues (toEmbedEntityDef entDef) castDoc))
      key <- keyFromValues [kpv]
      return $ Entity key body
  where
    addDetail :: Either Text a -> Either Text a
    addDetail = mapLeft (\msg -> msg `mappend` " for doc: " `mappend` T.pack (show doc))
    castDoc = assocListFromDoc doc
    -- normally _id is the first field
    mKey = lookup id_ castDoc

-- | unlike many SQL databases, MongoDB makes no guarantee of the ordering
-- of the fields returned in the document.
-- Ordering might be maintained if persistent were the only user of the db,
-- but other tools may be using MongoDB.
--
-- Persistent creates a Haskell record from a list of PersistValue
-- But most importantly it puts all PersistValues in the proper order
eitherFromPersistValues :: (PersistEntity record) => EntityDef -> [DB.Field] -> Either T.Text (Entity record)
eitherFromPersistValues entDef doc = case mKey of
   Nothing  -> addDetail $ Left $ "could not find _id field: "
   Just kpv -> do
      body <- addDetail (fromPersistValues (map snd $ orderPersistValues (toEmbedEntityDef entDef) castDoc))
      key <- keyFromValues [kpv]
      return $ Entity key body
  where
    addDetail :: Either Text a -> Either Text a
    addDetail = mapLeft (\msg -> msg `mappend` " for doc: " `mappend` T.pack (show doc))
    castDoc = assocListFromDoc doc
    -- normally _id is the first field
    mKey = lookup id_ castDoc

-- | unlike many SQL databases, MongoDB makes no guarantee of the ordering
-- of the fields returned in the document.
-- Ordering might be maintained if persistent were the only user of the db,
-- but other tools may be using MongoDB.
--
-- Persistent creates a Haskell record from a list of PersistValue
-- But most importantly it puts all PersistValues in the proper order
eitherFromPersistValues entDef doc = case mKey of
   Nothing  -> addDetail $ Left $ "could not find _id field: "
   Just kpv -> do
      body <- addDetail (fromPersistValues (map snd $ orderPersistValues (toEmbedEntityDef entDef) castDoc))
      key <- keyFromValues [kpv]
      return $ Entity key body
  where
    addDetail :: Either Text a -> Either Text a
    addDetail = mapLeft (\msg -> msg `mappend` " for doc: " `mappend` T.pack (show doc))
    castDoc = assocListFromDoc doc
    -- normally _id is the first field
    mKey = lookup id_ castDoc

-- | unlike many SQL databases, MongoDB makes no guarantee of the ordering
-- of the fields returned in the document.
-- Ordering might be maintained if persistent were the only user of the db,
-- but other tools may be using MongoDB.
--
-- Persistent creates a Haskell record from a list of PersistValue
-- But most importantly it puts all PersistValues in the proper order