Skip to content

Commit cd209f3

Browse files
committed
Change toVectorWithLabel to safe vector.
1 parent e210bee commit cd209f3

1 file changed

Lines changed: 36 additions & 34 deletions

File tree

src/DataFrame/Internal/Column.hs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ __Examples:__
233233
234234
@
235235
> import qualified Data.Vector.Unboxed as V
236-
> fromVector (V.fromList [(1 :: Int), 2, 3, 4])
236+
> fromUnboxedVector (V.fromList [(1 :: Int), 2, 3, 4])
237237
[1,2,3,4]
238238
@
239239
-}
@@ -617,42 +617,44 @@ __Examples:__
617617
exception: ...
618618
-}
619619
toVector :: forall a . Columnable a => Column -> VB.Vector a
620-
toVector = toVectorWithLabel "toVector"
620+
toVector xs = case toVectorSafe xs of
621+
Left err -> throw err
622+
Right val -> val
621623

622-
-- | An internal version of toVector that takes the calling function as an extra argument.
623-
toVectorWithLabel :: forall a . Columnable a => String -> Column -> VB.Vector a
624-
toVectorWithLabel label column@(OptionalColumn (col :: VB.Vector b)) =
624+
-- | A safe version of toVector that returns an Either type.
625+
toVectorSafe :: forall a . Columnable a => Column -> Either DataFrameException (VB.Vector a)
626+
toVectorSafe column@(OptionalColumn (col :: VB.Vector b)) =
625627
case testEquality (typeRep @a) (typeRep @b) of
626-
Just Refl -> col
627-
Nothing -> throw $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
628-
, expectedType = Right (typeRep @b)
629-
, callingFunctionName = Just label
630-
, errorColumnName = Nothing})
631-
toVectorWithLabel label (BoxedColumn (col :: VB.Vector b)) =
628+
Just Refl -> Right col
629+
Nothing -> Left $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
630+
, expectedType = Right (typeRep @b)
631+
, callingFunctionName = Just "toVectorSafe"
632+
, errorColumnName = Nothing})
633+
toVectorSafe (BoxedColumn (col :: VB.Vector b)) =
632634
case testEquality (typeRep @a) (typeRep @b) of
633-
Just Refl -> col
634-
Nothing -> throw $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
635-
, expectedType = Right (typeRep @b)
636-
, callingFunctionName = Just label
637-
, errorColumnName = Nothing})
638-
toVectorWithLabel label (UnboxedColumn (col :: VU.Vector b)) =
635+
Just Refl -> Right col
636+
Nothing -> Left $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
637+
, expectedType = Right (typeRep @b)
638+
, callingFunctionName = Just "toVectorSafe"
639+
, errorColumnName = Nothing})
640+
toVectorSafe (UnboxedColumn (col :: VU.Vector b)) =
639641
case testEquality (typeRep @a) (typeRep @b) of
640-
Just Refl -> VB.convert col
641-
Nothing -> throw $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
642-
, expectedType = Right (typeRep @b)
643-
, callingFunctionName = Just label
644-
, errorColumnName = Nothing})
645-
toVectorWithLabel label (GroupedBoxedColumn (col :: VB.Vector b)) =
642+
Just Refl -> Right $ VB.convert col
643+
Nothing -> Left $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
644+
, expectedType = Right (typeRep @b)
645+
, callingFunctionName = Just "toVectorSafe"
646+
, errorColumnName = Nothing})
647+
toVectorSafe (GroupedBoxedColumn (col :: VB.Vector b)) =
646648
case testEquality (typeRep @a) (typeRep @b) of
647-
Just Refl -> col
648-
Nothing -> throw $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
649-
, expectedType = Right (typeRep @b)
650-
, callingFunctionName = Just label
651-
, errorColumnName = Nothing})
652-
toVectorWithLabel label (GroupedUnboxedColumn (col :: VB.Vector b)) =
649+
Just Refl -> Right col
650+
Nothing -> Left $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
651+
, expectedType = Right (typeRep @b)
652+
, callingFunctionName = Just "toVectorSafe"
653+
, errorColumnName = Nothing})
654+
toVectorSafe (GroupedUnboxedColumn (col :: VB.Vector b)) =
653655
case testEquality (typeRep @a) (typeRep @b) of
654-
Just Refl -> col
655-
Nothing -> throw $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
656-
, expectedType = Right (typeRep @b)
657-
, callingFunctionName = Just label
658-
, errorColumnName = Nothing})
656+
Just Refl -> Right col
657+
Nothing -> Left $ TypeMismatchException (MkTypeErrorContext { userType = Right (typeRep @a)
658+
, expectedType = Right (typeRep @b)
659+
, callingFunctionName = Just "toVectorSafe"
660+
, errorColumnName = Nothing})

0 commit comments

Comments
 (0)