brendanhay / gogol

A comprehensive Google Services SDK for Haskell.
Other
280 stars 105 forks source link

How do I access fields in gogol records? Lens and regular fieldselectors don't work #179

Open tonyalaribe opened 2 years ago

tonyalaribe commented 2 years ago

Hi, this is likely a trivial question, but i've been banging my head on this for a bit too long.

How do I access the fields in a Pubsub RecievedMessage record?

I had this line of code before the updates:

let rmMsg = msg ^? rmMessage . _Just . psData . _Just

But this doesn't work anymore.

So i saw tried what I saw from the generic-lens package and from diving into gogol-pubsub codebase:

let rmMsg = msg ^? #message . _Just . #data . _Just

Which gives the error:

    • No instance for (IsLabel
                         "message"
                         (p0 (Maybe a0) (f0 (Maybe b0))
                          -> ReceivedMessage -> Const (First ByteString) ReceivedMessage))
        arising from the overloaded label ‘#message’
        (maybe you haven't applied a function to enough arguments?)
    • In the first argument of ‘(.)’, namely ‘#message’
      In the second argument of ‘(^?)’, namely
        ‘#message . _Just . #data . _Just’
      In the expression: msg ^? #message . _Just . #data . _Just
    |
102 |           let rmMsg = msg ^? #message . _Just . #data . _Just
    |                              ^^^^^^^^

Is it that the lenses are not derived on the fields?

Next, I tried to fall back on regular haskell

let rmMsg = fromMaybe "" $ maybe (Nothing) (data') (message msg)

But then I get an error about the NoFieldSelectors extension. So it seems the fieldselectors are not exposed, so I can't query using them either.

   • Variable not in scope: data' :: a0 -> Maybe (Maybe ByteString)
    • Perhaps you want to add ‘data'’ to the import list
      in the import of ‘Gogol.PubSub.Types’
      (src/ProcessMessage.hs:12:1-74).
      NB: ‘data'’ is a field selector belonging to the type ‘PubsubMessage’
      that has been suppressed by NoFieldSelectors
    |
104 |           let rmMsg = fromMaybe "" $ maybe (Nothing) (data') (message msg)
    |                                                       ^^^^^
tonyalaribe commented 2 years ago

I was able to get this to work with lenses, but like this:

let rmMsg = msg ^? field @"message" . _Just . field @"data'" . _Just . _Base64

field @"blabla" The intended way for accessing record fields on gogol?

endgame commented 1 year ago

Yes. Or #message, using -XOverloadedLabels.