Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#97, make sure OneofSubfield generates well-typed code for non-Maybe #99

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Proto3/Suite/DotProto/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -868,17 +868,18 @@ messageInstD ctxt parentIdent msgIdent messageParts = do
-- Constructor y -> encodeMessageField num (Nested (Just y)) -- for embedded messages
-- Constructor y -> encodeMessageField num (ForceEmit y) -- for everything else
let mkAlt (OneofSubfield fieldNum conName _ dpType options) = do
let wrapMaybe
let isMaybe
| Prim (Named tyName) <- dpType
, Just DotProtoKindMessage <- dotProtoTypeInfoKind <$> M.lookup tyName ctxt
= HsParen . HsApp (HsVar (haskellName "Just"))
= Just DotProtoKindMessage == fmap dotProtoTypeInfoKind (M.lookup tyName ctxt)
| otherwise
= forceEmitE
= False

xE <- wrapE ctxt options dpType
. wrapMaybe
$ HsVar (unqual_ "y")
let wrapJust = HsParen . HsApp (HsVar (haskellName "Just"))

xE <- (if isMaybe then id else fmap forceEmitE)
. wrapE ctxt options dpType
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using dimap would be nice here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't see how to use dimap at all. Any hints?

Copy link
Collaborator

@themattchan themattchan Apr 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you're right -- I thought both the pre- and post- operations were enabled by isMaybe.

This might read better though:

let adapt | isMaybe = lmap wrapJust
          | otherwise = rmap (fmap forceEmitE)

xE <- adapt (wrapE ctxt options dpType) (HsVar (unqual_ "y"))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that harder to read. I guess I'd usually (. wrapJust) and (fmap forceEmitE .) rather than lmap/rmap - and in this case I'm not sure it's clearer at all. But happy to go that way if you want.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that it would be better to consolidate the adjustments predicated on isMaybe, but on second thought the extra indirection obfuscates matters.

. (if isMaybe then wrapJust else id)
$ HsVar (unqual_ "y")

pure $ alt_ (HsPApp (unqual_ conName) [patVar "y"])
(HsUnGuardedAlt (apply encodeMessageFieldE [fieldNumberE fieldNum, xE]))
Expand Down