aws-lambda-haskell-runtime icon indicating copy to clipboard operation
aws-lambda-haskell-runtime copied to clipboard

Code generation with generateLambdaDispatcher leads to warnings (Update: Addressed by upcoming 4.0.0)

Open andys8 opened this issue 4 years ago • 7 comments

With

generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions

I'm currently seeing these warnings:

/app/Main.hs:58:1: warning: [-Wunused-matches]
    Defined but not used: ‘executionUuid’
   |
58 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/app/Main.hs:58:1: warning: [-Wunused-matches]
    Defined but not used: ‘eventObject’
   |
58 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/app/Main.hs:58:1: warning: [-Wunused-matches]
    Defined but not used: ‘contextObject’
   |
58 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/app/Main.hs:58:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature:
      run :: Applicative f =>
             LambdaOptions context
             -> f (Either
                     aws-lambda-haskell-runtime-3.0.1:Aws.Lambda.Runtime.Common.LambdaError
                     b)
   |
58 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/app/Main.hs:58:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: main :: IO ()
   |
58 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Is it possible to get rid of them?

Version 3.0.1

andys8 avatar Jul 16 '20 23:07 andys8

Okay, I think the actual issue, is that the handler function is not picked up and the generated code looks like tihs:

app/Main.hs:62:1-67: Splicing declarations
    generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
  ======>
    main = (runLambda initializeContext) run
    run
      LambdaOptions {functionHandler = functionHandler,
                     contextObject = contextObject, eventObject = eventObject,
                     executionUuid = executionUuid}
      = case functionHandler of {
          _ -> ((pure
                   . (Left
                        . (aws-lambda-haskell-runtime-3.0.1:Aws.Lambda.Runtime.Common.ApiGatewayLambdaError
                             . (mkApiGatewayResponse 500 . toApiGatewayResponseBody))))
                  $ ("Handler "
                       <> (functionHandler <> " does not exist on project"))) }

Will look into it

andys8 avatar Jul 17 '20 00:07 andys8

I tried to inline the generated code, but it looks like that's not possible because ApiGatewayLambdaError is not exposed.

https://github.com/theam/aws-lambda-haskell-runtime/blob/8aea720ff2299e35918ed36c0c1609d7623f7f36/src/Aws/Lambda/Runtime/Common.hs#L77

andys8 avatar Jul 17 '20 00:07 andys8

This (minimal) apigateway example:

initializeContext :: IO ()
initializeContext = return ()

handler
  :: ApiGatewayRequest Text
  -> Context ()
  -> IO (Either (ApiGatewayResponse Text) (ApiGatewayResponse Text))
handler request context = return $ Right $ mkApiGatewayResponse 200 "OK"

generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions

results in:

    main = (runLambda initializeContext) run
    run
      LambdaOptions {functionHandler = functionHandler,
                     contextObject = contextObject, eventObject = eventObject,
                     executionUuid = executionUuid}
      = case functionHandler of {
          _ -> ((pure
                   . (Left
                        . (aws-lambda-haskell-runtime-3.0.1:Aws.Lambda.Runtime.Common.ApiGatewayLambdaError
                             . (mkApiGatewayResponse 500 . toApiGatewayResponseBody))))
                  $ ("Handler "
                       <> (functionHandler <> " does not exist on project"))) }

andys8 avatar Jul 17 '20 00:07 andys8

Okay, I think the issue might actually be, that the formatting of the type signature is in multiple lines. The project is formatted with brittany and :: is not in the same line as handler. Because of this, it won't be picked up!

https://github.com/theam/aws-lambda-haskell-runtime/blob/1eda275e2c4c0f600bcfbae6f1b0979926d630dc/src/Aws/Lambda/Meta/Discover.hs#L65

Formatting it like this, is working.

type Req = ApiGatewayRequest Text
type Res = ApiGatewayResponse Text

handler :: Req -> Context () -> IO (Either Res Res)
handler request context = return $ Right $ mkApiGatewayResponse 200 "OK"

andys8 avatar Jul 17 '20 00:07 andys8

With the handler being recognized there are less warnings:

/app/app/Main.hs:68:1: error: [-Wunused-matches, -Werror=unused-matches]
    Defined but not used: ‘executionUuid’
   |
68 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/app/app/Main.hs:68:1: error: [-Wmissing-signatures, -Werror=missing-signatures]
    Top-level binding with no type signature:
      run :: LambdaOptions Token.Handle
             -> IO
                  (Either
                     aws-lambda-haskell-runtime-3.0.1:Aws.Lambda.Runtime.Common.LambdaError
                     LambdaResult)
   |
68 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/app/app/Main.hs:68:1: error: [-Wmissing-signatures, -Werror=missing-signatures]
    Top-level binding with no type signature: main :: IO ()
   |
68 | generateLambdaDispatcher UseWithAPIGateway defaultDispatcherOptions
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sorry for spamming comments :)

andys8 avatar Jul 17 '20 00:07 andys8

Hey! Thanks so much for writing such a detailed issue!

I'm currently working on #81 so all Template Haskell code will go away, and with it, all the warnings :)

NickSeagull avatar Jul 17 '20 09:07 NickSeagull

Nice. I had a look at the changes, and it looks like the search for "handler ::" will also be removed, and therefore a fix is probably not necessary.

I'd leave this issue open for documentation and close it with the rel are of 4.0.0. Looking forward to it.

andys8 avatar Jul 17 '20 09:07 andys8