Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在GoLang测试用例中发送google.protobuf.Struct数据?_Go_Protocol Buffers_Grpc Go - Fatal编程技术网

如何在GoLang测试用例中发送google.protobuf.Struct数据?

如何在GoLang测试用例中发送google.protobuf.Struct数据?,go,protocol-buffers,grpc-go,Go,Protocol Buffers,Grpc Go,我正在使用GRPC/proto缓冲区在GoLang编写我的第一个API端点。我是刚来戈朗的新手。 下面是我为测试用例编写的文件 这就是在上面包含的v1.go文件中定义EndpointRequest对象的方式: // An v1 interface Endpoint Request object. message EndpointRequest { // data can be a complex object. google.protobuf.Struct data = 1; } 这似

我正在使用GRPC/proto缓冲区在GoLang编写我的第一个API端点。我是刚来戈朗的新手。 下面是我为测试用例编写的文件

这就是在上面包含的
v1.go
文件中定义
EndpointRequest
对象的方式:

// An v1 interface Endpoint Request object.
message EndpointRequest {
  // data can be a complex object.
  google.protobuf.Struct data = 1;
}
这似乎奏效了

但是现在,我想做一些稍微不同的事情。在我的测试用例中,不是发送一个空的
数据
对象,而是发送一个带有键/值对的映射/字典
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 

初始化
数据的方式
意味着您希望:

type Struct struct {
    A string
    C string
}
但是,定义如下:

type Struct struct {   
    // Unordered map of dynamically typed values.
    Fields map[string]*Value `protobuf:"bytes,1,rep,name=fields,proto3" json:"fields,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
    // contains filtered or unexported fields
}
很明显这里有点不匹配。您需要初始化结构的
字段
映射,并使用正确的方法设置
字段。与您显示的代码等效的代码是:

Data: &structpb.Struct{
    Fields: map[string]*structpb.Value{
        "A": &structpb.Value{
            Kind: &structpb.Value_StringValue{
                StringValue: "B",
            },
        },
        "C": &structpb.Value{
            Kind: &structpb.Value_StringValue{
                StringValue: "D",
            },
        },
    },
}
Data: &structpb.Struct{
    Fields: map[string]*structpb.Value{
        "A": &structpb.Value{
            Kind: &structpb.Value_StringValue{
                StringValue: "B",
            },
        },
        "C": &structpb.Value{
            Kind: &structpb.Value_StringValue{
                StringValue: "D",
            },
        },
    },
}