-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
NamedEncode for a row of columns #43
Comments
To be clear, every |
Okay, I solved it by passing the list of all language fields into the function returning an encoder, and then looking up the translation in a map: type Lang = Text
data Trans = Trans {
key :: Text,
translations :: Map Lang Text
}
transEncoder :: [Lang] -> Encoder Trans
transEncoder languagesToEncodeInColumns =
(\Trans { key, translations } -> (key, translations))
$ contrazip2 _keyEnc translationsEnc
where
translationEnc lang =
_toBytesBuilder lang
Enc.=: (encFromMap lang translatedString)
translationsEnc = foldMap translationEnc languagesToEncodeInColumns
-- | Takes a key to fetch from the map and gives back an encoder that fetches that key from the map (or empty string)
encFromMap :: Ord k => k -> Encode a -> Encode (Map k a)
encFromMap key = contramap (Map.lookup key) . orEmpty |
Then you have to collect a list of all languages that you want to encode beforehand, but you have to do that anyway to figure out which columns you need. |
Does the CSV come out as you'd expect? I'm glad you found a solution. |
it works just as expected! Maybe it could go into the examples or something? I might try to whip something up once I’ve written the decoder that can do it in reverse. |
The combinator I posted above is not really viable, I has the conceptual problem that the |
I have a datatype with a list of translations, like so:
And I want to generate a csv like
from a
[Trans]
likeBut it is not clear to me whether it’s possible to generate a
NamedEncoder a
that sets their column names based on thea
.i.e. I think I need something like
But there only is
and I’m not sure the internal encoding (heh) of
Encoder
would even allow for such a thing.A complication here is that it’s not entirely clear that every
translations
would even list the same languages, so the rows might be different for everyTrans
; maybe myTrans
should be represented differently?The text was updated successfully, but these errors were encountered: