Kevin Meredith
Kevin Meredith

Reputation: 41909

Convert Type with ByteString to Text?

Using conduit, my private repo code fails to compile:

  Expected type: ConduitM
                   Text Void (Control.Monad.Trans.Resource.Internal.ResourceT IO) ()
    Actual type: ConduitM
                   ByteString
                   Void
                   (Control.Monad.Trans.Resource.Internal.ResourceT IO)
                   ()

How can I define a function?

type Helper a = ConduitM a Void (Control.Monad.Trans.Resource.Internal.ResourceT IO ()

f :: Helper ByteString -> Helper Text

Upvotes: 1

Views: 1146

Answers (2)

Michael Snoyman
Michael Snoyman

Reputation: 31305

I'd recommend using the decodeUtf8C function together with normal fusion (the .| operator). If your code looks like:

makesText .| needsByteString

Then you could rewrite it as:

makesText .| decodeUtf8C .| needsByteString

You can also choose to use decodeUtf8LenientC if you want to ignore encoding errors.

Upvotes: 2

basile-henry
basile-henry

Reputation: 1365

I don't know conduit well enough but from reading the documentation I would try something along the line of:

import Data.Void          (Void)
import Data.Text          (Text)
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
import Data.ByteString    (ByteString)
import Data.Conduit

type Helper a = ConduitM a Void (Control.Monad.Trans.Resource.Internal.ResourceT IO) ()

f :: Helper ByteString -> Helper Text
f = mapInput encodeUtf8 (pure . decodeUtf8)

Using mapInput, encodeUtf8 and decodeUtf8

This is untested, tell me if it works for you! :-)

Just tested quickly in ghci, it seems to work. You might want some proper error handling for decodeUtf8 though.

Upvotes: 2

Related Questions