haskell-hvr / cassava

A CSV parsing and encoding library optimized for ease of use and high performance
http://hackage.haskell.org/package/cassava
BSD 3-Clause "New" or "Revised" License
222 stars 105 forks source link

Can ToField have Vectors/Functors instances? #201

Open Cmdv opened 3 years ago

Cmdv commented 3 years ago

I've been trying to make a csv that has run time generated columns.

I'm doing this with my type:

data MyType = MyType
    { callId           :: UUID.UUID
    , agentDisplayName :: Text
    , callLabels       :: Vector (Maybe Text)
    }
    deriving (Generic, Show, Eq)

But I just can't make an instance of Csv.ToField (Vector (Maybe Text))

I then tried to have a custom Csv.ToNamedRecord instance and do

instance Csv.ToNamedRecord MyType where
  toNamedRecord CallReportCSV {..} = do
    Csv.namedRecord ["callId" .= callId
                    , "Agent Display Name"
                    ]
    <> Csv.namedRecord (V.toList (convertToListFunc <$> callLabels) :: [(ByteString, ByteString)])

but then the instance for Csv.DefaultOrdered fails as I can't get at the vector as headerOrder needs to be headerOrder _:

instance Csv.DefaultOrdered MyType where
  headerOrder MyType{..} = ...

Is this the expected behaviour?

I've also tried with Map a a & [a] 😄