Skip to content

Commit

Permalink
Move cast into Val class
Browse files Browse the repository at this point in the history
This is done so user can override the behavior of `cast` in order to
provide a more precise value in the `fail`ing case.

Closes mongodb-haskell#29.
  • Loading branch information
guibou committed Jul 7, 2021
1 parent d8e7018 commit 5654fbf
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Data/Bson.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Data.Bson (
Field(..), (=:), (=?),
Label,
-- * Value
Value(..), Val(..), fval, cast, typed, typeOfVal,
Value(..), Val(..), fval, typed, typeOfVal,
-- * Special Bson value types
Binary(..), Function(..), UUID(..), MD5(..), UserDefined(..),
Regex(..), Javascript(..), Symbol(..), MongoStamp(..), MinMaxKey(..),
Expand Down Expand Up @@ -202,15 +202,6 @@ fval f v = case v of

-- * Value conversion

cast :: (Val a, MonadFail m) => Value -> m a
-- ^ Convert Value to expected type, or fail (Nothing) if not of that type
cast v = maybe notType return castingResult
where
castingResult = cast' v
unMaybe :: Maybe a -> a
unMaybe = undefined
notType = fail $ "expected " ++ show (typeOf $ unMaybe castingResult) ++ ": " ++ show v

typed :: (Val a) => Value -> a
-- ^ Convert Value to expected type. Error if not that type.
typed = fromJust . cast
Expand All @@ -229,13 +220,24 @@ class (Typeable a, Show a, Eq a) => Val a where
valMaybe :: Maybe a -> Value
valMaybe = maybe Null val
cast' :: Value -> Maybe a
cast' = cast
cast'List :: Value -> Maybe [a]
cast'List (Array x) = mapM cast x
cast'List _ = Nothing
cast'Maybe :: Value -> Maybe (Maybe a)
cast'Maybe Null = Just Nothing
cast'Maybe v = fmap Just (cast' v)

cast :: MonadFail m => Value -> m a
-- ^ Convert Value to expected type, or fail (e.g. Nothing) if not of that type
cast v = maybe notType return castingResult
where
castingResult = cast' v
unMaybe :: Maybe a -> a
unMaybe = undefined
notType = fail $ "expected " ++ show (typeOf $ unMaybe castingResult) ++ ": " ++ show v
{-# MINIMAL val, (cast | cast') #-}

instance Val Double where
val = Float
cast' (Float x) = Just x
Expand Down

0 comments on commit 5654fbf

Please sign in to comment.