我正在使用 GRPC/proto-buffers 在 GoLang 中编写我的第一个 API 端点。我对 GoLang 比较陌生。以下是我为测试用例编写的文件
package my_package
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/structpb"
"github.com/MyTeam/myproject/cmd/eventstream/setup"
v1handler "github.com/MyTeam/myproject/internal/handlers/myproject/v1"
v1interface "github.com/MyTeam/myproject/proto/.gen/go/myteam/myproject/v1"
)
func TestEndpoint(t *testing.T) {
conf := &setup.Config{}
// Initialize our API handlers
myhandler := v1handler.New(&v1handler.Config{})
t.Run("Success", func(t *testing.T) {
res, err := myhandler.Endpoint(context.Background(), &v1interface.EndpointRequest{
Data: &structpb.Struct{},
})
require.Nil(t, err)
// Assert we got what we want.
require.Equal(t, "Ok", res.Text)
})
}
这是在上面包含的文件EndpointRequest中定义对象的方式:v1.go
// An v1 interface Endpoint Request object.
message EndpointRequest {
// data can be a complex object.
google.protobuf.Struct data = 1;
}
这似乎有效。
但现在,我想做一些稍微不同的事情。在我的测试用例中data,我想发送一个带有键/值对的地图/字典,而不是发送一个空对象A: "B", C: "D"。我该怎么做?如果我替换Data: &structpb.Struct{}为Data: &structpb.Struct{A: "B", C: "D"},我会得到编译器错误:
invalid field name "A" in struct initializer
invalid field name "C" in struct initializer
繁星coding
相关分类