我可以使用NamedRecords设置conduit-CSV的列顺序吗?

时间:2017-03-31 16:33:33

标签: haskell conduit

我的代码如下。似乎数据以随机顺序输出,这是有道理的,因为NamedRecord只是Map。有没有办法可以强制执行特定订单?

我认为一个选项是创建一个Conduit (Named a) m (Row Text)类型的管道,但似乎这是一个常见的用例,标准应该已经存在。

processor :: Monad m => Conduit (Named FalconRow) m (Named HefRow)
processor = do
  value <- await 
  case value of
    Nothing -> return ()
    Just v -> do
      let transformed = (fixRow . getNamed) v
      CL.sourceList $ Prelude.map Named transformed

runTranslation :: IO ()
runTranslation = runResourceT $ 
  transformCSV defCSVSettings 
               (sourceFile "input.csv") 
               processor
               (sinkFile "output.csv")

1 个答案:

答案 0 :(得分:0)

我最终只为此制作了自己的功能:

sortColumns :: (Monad m, DefaultOrdered a, ToNamedRecord a) => Conduit (Named a) m (Row ByteString)
sortColumns = CL.map doSort

doSort :: (DefaultOrdered a, ToNamedRecord a) => Named a -> Row ByteString
doSort value =
  let columnMap = toNamedRecord $ getNamed value
      headers = toList $ headerOrder $ getNamed value
      bsValue = map (\title -> findWithDefault "" title columnMap) headers
  in  bsValue