cannot use req (variable of type events.APIGatewayProxyRequest) as core.SwitchableAPIGatewayRequest value in argument to adapter.ProxyWithContext

Viewed 331

I'm new to Golang. I can't seem to supply the argument needed to work with adapter.ProxyWithContext. According to this https://pkg.go.dev/github.com/awslabs/aws-lambda-go-api-proxy@v0.13.0/gorillamux#GorillaMuxAdapter.ProxyWithContext it should accept API Gateway proxy event or API Gateway V2 event.


import (
    "context"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    routerProxy "github.com/awslabs/aws-lambda-go-api-proxy/gorillamux"
)

var adapter *routerProxy.GorillaMuxAdapter

func main() {
    cfg := config.NewConfig()
    s := app.NewApp(cfg, log)

    adapter = routerProxy.New(s.SetupRoutes())
    lambda.Start(lambdaHandler) // execute lambda and the specific lead handler
}

func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    c, err := adapter.ProxyWithContext(ctx, req)
    return c, err
}


i'm having this error: 
``var req events.APIGatewayProxyRequest
cannot use req (variable of type events.APIGatewayProxyRequest) as core.SwitchableAPIGatewayRequest value in argument to adapter.ProxyWithContextcompilerIncompatibleAssign
1 Answers

As commenter mkopriva noted, you can not directly pass the events.APIGatewayProxyRequest to adapter.ProxyWithContext.

You need to create a new instance of SwitchableAPIGatewayRequest and pass it to ProxyWithContext.

This should fix your issue:


import "github.com/awslabs/aws-lambda-go-api-proxy/core"

[...]

func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    return adapter.ProxyWithContext(ctx, *core.NewSwitchableAPIGatewayRequestV1(&req))
}
Related