Grpc 将 RepeatedCompositeContainer 转换为字典时出现的问题。

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

Grpc Conversion issue from RepeatedCompositeContainer to dict

问题

"RepeatedCompositeContainer"

关于这个,我有一个问题
"google._upb._message.RepeatedCompositeContainer" 对象没有属性 "file"
这个错误出现了

我写了这段代码

convert_dict = MessageToDict(user.results, descriptor_pool=True)

我想将 "RepeatedCompositeContainer" 转换成字典,但是出现了错误,如你所见

我期望将 "RepeatedCompositeContainer" 转换成字典

英文:

RepeatedCompositeContainer

About This I have one question
google._upb._message.RepeatedCompositeContainer' object has no attribute 'file'

this error is coming

I have write this code

convert_dict = MessageToDict(user.results, descriptor_pool=True)

i want to convert RepeatedCompositeContainer into the dict and this is throwing me error as you see

i am expecting to convert RepeatedCompositeContainer to dict

答案1

得分: 1

foo.proto:

语法 = "proto3";

 foo;

消息 X {
    重复 字符串 words = 1;
}

和:

protoc \
--proto_path=${PWD} \
--python_out=${PWD} \
--pyi_out=${PWD} \
${PWD}/foo.proto

main.py:

from google.protobuf.json_format import MessageToDict

from foo_pb2 import X


x = X()
x.words.extend(["A","B","C"])
print(x)

# 正确(成功)
try:
    d=MessageToDict(x)
    print(d["words"])
except AttributeError:
    pass

# 不正确(失败)
try:
    d=MessageToDict(x.words)
    print(d)
except AttributeError:
    print("无法将消息字段转换为字典")

产生的输出:

words: "A"
words: "B"
words: "C"

['A', 'B', 'C']
无法将消息字段转换为字典
英文:

For example:

foo.proto:

syntax = "proto3";

package foo;

message X {
    repeated string words = 1;
}

And:

protoc \
--proto_path=${PWD} \
--python_out=${PWD} \
--pyi_out=${PWD} \
${PWD}/foo.proto

main.py:

from google.protobuf.json_format import MessageToDict

from foo_pb2 import X


x = X()
x.words.extend(["A","B","C"])
print(x)

# Correct (succeeds)
try:
    d=MessageToDict(x)
    print(d["words"])
except AttributeError:
    pass

# Incorrect (fails)
try:
    d=MessageToDict(x.words)
    print(d)
except AttributeError:
    print("unable to convert Message Field to dict")

Yields:

words: "A"
words: "B"
words: "C"

['A', 'B', 'C']
unable to convert Message Field to dict

答案2

得分: 0

以下是你要翻译的代码部分:

user.proto

syntax = "proto3";
package user_proto;
import "google/protobuf/empty.proto";
service UserController {
    rpc List(UserListRequest) returns (UserListResponse) {}
}
message User {
    int32 id = 1;
    string name = 2;
    string email = 3;
    string groups = 4;
}
message UserListRequest {
}
message UserListResponse {
    repeated User results = 1;
}

userprotoserializer

from config_routing.models import User
from django_socio_grpc import proto_serializers
from config_routing.grpc import config_routing_pb2

class UserProtoSerializer(proto_serializers.ModelProtoSerializer):
    class Meta:
        model = User
        proto_class = user_pb2.User
        proto_class_list = user_pb2.UserListResponse
        fields = '__all__'


server.py

def List(self, request, context):
        user = User.objects.using('secondary').all()
        serializer = UserProtoSerializer(user, many=True)
        print(serializer)
        return serializer.message

client.py 

stub = user_pb2_grpc.UserControllerStub(self.channel)
user = stub.List(user_pb2.UserListRequest())
convert_dict = MessageToDict(user)
print(convert_dict["result"])

this shows me error 
AttributeError: file

希望这可以帮助你解决问题。如果你需要更多帮助,请告诉我。

英文:
user.proto
syntax = "proto3";
package user_proto;
import "google/protobuf/empty.proto";
service UserController {
rpc List(UserListRequest) returns (UserListResponse) {}
}
message User {
int32 id = 1;
string name = 2;
string email = 3;
string groups = 4;
}
message UserListRequest {
}
message UserListResponse {
repeated User results = 1;
}
userprotoserializer
from config_routing.models import User
from django_socio_grpc import proto_serializers
from config_routing.grpc import config_routing_pb2
class UserProtoSerializer(proto_serializers.ModelProtoSerializer):
class Meta:
model = User
proto_class = user_pb2.User
proto_class_list = user_pb2.UserListResponse
fields = '__all__'
server.py
def List(self, request, context):
user = User.objects.using('secondary').all()
serializer = UserProtoSerializer(user, many=True)
print(serializer)
return serializer.message
client.py 
stub = user_pb2_grpc.UserControllerStub(self.channel)
user = stub.List(user_pb2.UserListRequest())
convert_dict = MessageToDict(user)
print(convert_dict["result"])
this shows me error 
AttributeError: file

Please look into this issue

huangapple
  • 本文由 发表于 2023年5月6日 20:31:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188901.html
匿名

发表评论

匿名网友

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

确定