Как получить внешний ключ в protobufPython

Программы на Python
Anonymous
Как получить внешний ключ в protobuf

Сообщение Anonymous »

Я читаю данные из двух файлов прототипов:
file.proto: это оболочка
file2.proto: здесь есть все столбцы
file.proto:

Код: Выделить всё

syntax = "proto3";

package com.oracle;

import "file2.proto";

option go_package = "github.com/cle/sdk/go_sdk";

// This is the inbound message intended to inform the Oracle of new answers to be persisted
message AnswerUpdateRequest {
Entity entity = 1;
repeated Answer answers = 2;
}

// This is the outbound message informing Oracle subscribers of new answers
message AnswersUpdated {
Entity entity = 1;
repeated Answer answers = 2;
}
file2.proto:

Код: Выделить всё

syntax = "proto3";

package com.oracle;

import "google/protobuf/timestamp.proto";

option go_package = "github.com/embroker/oracle/sdk/go_sdk";

message Entity {
Type type = 1;
string id = 2;

enum Type {
ORGANIZATION = 0;
USER = 1;
APPLICATION = 2;
}
}

message AnswerSource {
Type type = 1;
string id = 2;

enum Type {
UNKNOWN = 0;
USER = 1;
DOCUMENT = 2;
EXTERNAL = 3;
}
}

message Answer {
string key = 1;
AnswerSource source = 2;
google.protobuf.Timestamp provided_at = 3;
google.protobuf.Timestamp received_at = 4;
AnswerFieldType type = 5;
Value value = 6;

message Value {
oneof value {
string text = 1;
float decimal = 2;
// ...
}
}
}

enum AnswerFieldType {
ANSWER_FIELD_TYPE_UNSTRUCTURED = 0;  // Can be useful for LLM purposes
ANSWER_FIELD_TYPE_TEXT = 1;
ANSWER_FIELD_TYPE_INTEGER = 2;
ANSWER_FIELD_TYPE_BOOLEAN = 3;
ANSWER_FIELD_TYPE_DECIMAL = 4;
ANSWER_FIELD_TYPE_DATE = 5;
ANSWER_FIELD_TYPE_ADDRESS = 6;
}
Моя функция Python для сопоставления с прототипом

Код: Выделить всё

import file.proto
import file2.proto
def create_answer_update_request(json_data):
data = json_data
answer_update_request = events_pb2.AnswerUpdateRequest()

entity = answer_update_request.entity
entity.type = model_pb2.Entity.Type.Value(data["answerUpdateRequest"]["entity"]["type"])
entity.id = data["answerUpdateRequest"]["entity"]["id"]

for answer_data in data["answerUpdateRequest"]["answers"]:
answer = Answer()
answer.key = answer_data['key']

source = AnswerSource()
source.type = AnswerSource.Type.Value(answer_data['source']['type'])
source.id = answer_data['source']['id']
answer.source.CopyFrom(source)

provided_at_datetime = datetime.fromisoformat(answer_data['provided_at'])
answer.provided_at.FromDatetime(provided_at_datetime)
received_at_datetime = datetime.fromisoformat(answer_data['received_at'])
answer.received_at.FromDatetime(received_at_datetime)
answer.type = AnswerFieldType.Value(f"ANSWER_FIELD_TYPE_{answer_data['type']}")
value = Answer.Value()
value.text = answer_data['value']['text']
answer.value.CopyFrom(value)

answer_update_request.answers.append(answer)
return answer_update_request.SerializeToString()
При десериализации данных я не получаю оболочку:
Ожидаемый результат:

Код: Выделить всё

{
"answerUpdateRequest": {
"entity": {
"type": "ORGANIZATION",
"id": "UU12334ID"
},
"answers": [
{
"key": "legal_company_name",
"source": {
"type": "DOCUMENT",
"id": "3ea20f68e73ec | DocumentType.application"
},
"provided_at": "2024-05-02T15:54:15.941988",
"received_at": "2024-05-02T15:54:15.945350",
"type": "TEXT",
"value": {
"text": "Cicne Law, LLC"
}
},
{
"key": "company_website_ind",
"source": {
"type": "DOCUMENT",
"id": "3ea20440-83fb-43c0-b409-1dd8f68e73ec | DocumentType.application"
},
"provided_at": "2024-05-02T15:54:15.941988",
"received_at": "2024-05-02T15:54:15.945365",
"type": "BOOLEAN",
"value": {
"text": "Yes"
}

]
}

}
Ошибка:
Я не получаю «answerUpdateRequest» в окончательном выводе, в остальном у меня все работает так, как ожидалось, как это получить?

Подробнее здесь: https://stackoverflow.com/questions/784 ... a-protobuf

Вернуться в «Python»