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

huangapple go评论79阅读模式
英文:

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

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Golang还不熟悉。我似乎无法提供适用于adapter.ProxyWithContext的参数。根据这个链接https://pkg.go.dev/github.com/awslabs/aws-lambda-go-api-proxy@v0.13.0/gorillamux#GorillaMuxAdapter.ProxyWithContext,它应该接受API Gateway代理事件或API Gateway V2事件。

package main

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) // 执行lambda和特定的lead处理程序
}

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


我遇到了这个错误
``var req events.APIGatewayProxyRequest
cannot use req (variable of type events.APIGatewayProxyRequest) as core.SwitchableAPIGatewayRequest value in argument to adapter.ProxyWithContextcompilerIncompatibleAssign
英文:

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

</details>


# 答案1
**得分**: 1

如评论者[mkopriva](https://stackoverflow.com/users/965900/mkopriva)所指出的,你不能直接将`events.APIGatewayProxyRequest`传递给`adapter.ProxyWithContext`。

你需要创建一个新的`SwitchableAPIGatewayRequest`实例,并将其传递给`ProxyWithContext`。

这样应该可以解决你的问题:

```go

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))
}
英文:

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 &quot;github.com/awslabs/aws-lambda-go-api-proxy/core&quot;

[...]

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

huangapple
  • 本文由 发表于 2022年3月24日 23:02:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/71604856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定