@@ -17,15 +17,31 @@ import qualified Data.List as L
1717import qualified Data.Text as T
1818import Data.Attoparsec.ByteString.Lazy
1919import Text.Read (readMaybe)
20+import Data.Typeable (typeRep, Proxy(Proxy))
21+import ProjectM36.ConcreteTypeRep
22+import Data.Either (either)
2023
2124data CsvImportError = CsvParseError String |
2225 AttributeMappingError RelationalError |
26+ UnsupportedAtomTypeError AttributeName |
2327 HeaderAttributeMismatchError (S.Set AttributeName)
2428 deriving (Show)
2529
2630csvDecodeOptions :: DecodeOptions
2731csvDecodeOptions = DecodeOptions {decDelimiter = fromIntegral (ord ',')}
2832
33+makeAtom :: AttributeName -> AtomType -> T.Text -> Either CsvImportError Atom
34+makeAtom attrName (AtomType cTypeRep) strIn = if unCTR cTypeRep == typeRep (Proxy :: Proxy Int) then
35+ either (Left . AttributeMappingError . ParseError . T.pack) (Right . Atom) ((fromText strIn) :: Either String Int)
36+ else
37+ Left $ AttributeMappingError (ParseError strIn)
38+ {- where readAtomable :: (Atomable a) => Proxy a -> T.Text -> Either CsvImportError Atom
39+ readAtomable _ strIn' = case fromText strIn of
40+ Right val -> Right $ Atom (val :: a)
41+ Left err -> Left $ AttributeMappingError (ParseError (T.pack err))-}
42+makeAtom attrName _ _ = Left $ UnsupportedAtomTypeError attrName
43+
44+
2945csvAsRelation :: BS.ByteString -> Attributes -> Either CsvImportError Relation
3046csvAsRelation inString attrs = case parse (csvWithHeader csvDecodeOptions) inString of
3147 Fail _ _ err -> Left (CsvParseError err)
@@ -37,10 +53,7 @@ csvAsRelation inString attrs = case parse (csvWithHeader csvDecodeOptions) inStr
3753 attrNameSet = S.fromList (V.toList attrNames)
3854 headerSet = S.fromList (V.toList strHeader)
3955 makeTupleList :: HM.HashMap AttributeName String -> [Either CsvImportError Atom]
40− makeTupleList tupMap = V.toList $ V.map (\attr -> makeAtom (A.atomType attr) (tupMap HM.! (A.attributeName attr))) attrs
41− makeAtom aType strIn = case readMaybe strIn of
42− Nothing -> Left $ AttributeMappingError (ParseError
43− Right atom -> Right atom
56+ makeTupleList tupMap = V.toList $ V.map (\attr -> makeAtom (A.attributeName attr) (A.atomType attr) (T.pack $ tupMap HM.! (A.attributeName attr))) attrs
4457 case attrNameSet == headerSet of
4558 False -> Left $ HeaderAttributeMismatchError (S.difference attrNameSet headerSet)
4659 True -> do