DB2Proto

记录一下

定义person.proto文件,

package tutorial;
message Person {
  required int32 id = 1;
  optional string name = 2;
  optional string email = 3;
}


#include "ProtoImporter.h"

using namespace google::protobuf;

void reflectionFill(Message* message, const FieldDescriptor* descriptor, const Reflection* reflection, std::map<string, string> row)
{
    assert(descriptor != NULL);
    //current not support for repeated label
    if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) {
        return;
    }

    const char* name = descriptor->name().c_str();

    switch (descriptor->type()) {
    case FieldDescriptor::TYPE_FIXED64:
    case FieldDescriptor::TYPE_INT64:
        reflection->SetInt64(message, descriptor, (long long)row[name].c_str()); break;
    case FieldDescriptor::TYPE_UINT64:
        reflection->SetInt64(message, descriptor, (unsigned long long)row[name].c_str()); break;

    case FieldDescriptor::TYPE_FIXED32:
    case FieldDescriptor::TYPE_INT32:
        reflection->SetInt32(message, descriptor, (int)row[name].c_str()); break;
    case FieldDescriptor::TYPE_UINT32:
        reflection->SetInt32(message, descriptor, (unsigned int)row[name].c_str()); break;

    case FieldDescriptor::TYPE_STRING:
        reflection->SetString(message, descriptor, (std::string)row[name]); break;

    case FieldDescriptor::TYPE_DOUBLE:
        reflection->SetDouble(message, descriptor, (double)atof(row[name].c_str())); break;
    case FieldDescriptor::TYPE_FLOAT:
        reflection->SetFloat(message, descriptor, (float)atof(row[name].c_str())); break;
    case FieldDescriptor::TYPE_BOOL:
        reflection->SetBool(message, descriptor, (bool)row[name].c_str()); break;
    default: std::cerr << "not support type " << descriptor->type() << std::endl;
    }
}

void convert(std::map<string, string> rowInfo, std::string& data)
{
    google::protobuf::Message* message = sProtoImporter.createDynamicMessage("Person");

    //Message* message = createMessage(message_type);
    const Reflection* reflection = message->GetReflection();
    const Descriptor* descriptor = message->GetDescriptor();

    for (int i = 0; i < descriptor->field_count(); ++i) 
    {
        reflectionFill(message, descriptor->field(i), reflection, rowInfo);
    }
    message->SerializeToString(&data);

    delete message;
}

void TestProto()
{
    // sql::row rowInfo;
    std::map<string, string> rowInfo;
    rowInfo["id"] = "20001";
    rowInfo["name"] = "testname";
    rowInfo["email"] = "aaa@163.com";

    std::string data;
    if (sProtoImporter.Import("person.proto"))
    {
        convert(rowInfo, data);
    }

    printf("get: %s", data.c_str());

    return;
}

 

### Protocol Buffers 和 Proto 文件格式简介 #### 什么是 Protocol Buffers? Protocol Buffers 是一种高效的结构化数据序列化方法,最初由 Google 开发。它类似于 XML 或 JSON,但更小、更快、也更简单[^1]。由于其高效性和跨平台特性,广泛应用于网络通信以及存储复杂数据。 #### .proto 文件的作用 `.proto` 文件是用来描述 Protocol Buffers 数据结构的语言文件。开发者可以在 `.proto` 文件中定义消息类型及其字段,这些定义会被编译器转换成目标语言的具体类或结构体。通过这种方式,不同编程环境之间能够轻松实现数据交互[^2]。 #### 基本语法组件 以下是 `.proto` 文件的一些基本组成部分: - **Package Declaration**: 使用 `package` 关键字声明命名空间,用于避免不同类型间的名称冲突。例如: ```protobuf package foo.bar; ``` 这样做的目的是确保即使两个不同的项目都定义了一个名为 `Open` 的消息类型也不会发生混淆[^3]。 - **Message Definition**: 定义具体的消息格式。每条消息都是一个小的记录,包含一系列名值对。 ```protobuf message Person { required string name = 1; required int32 id = 2; optional string email = 3; } ``` - **Field Rules and Types**: - 字段可以标记为 `required`, `optional`, 或者 `repeated` 来指定它们的行为。 - 支持多种内置数据类型如整数 (`int32`, `uint64`)、浮点数 (`float`, `double`)、布尔型 (`bool`) 及字符串 (`string`) 等。 #### 编译过程概述 为了使应用程序能读写上述定义好的消息格式,需借助特定于所使用的编程语言版本的 protoc 编译工具来处理 `.proto` 文件。该工具会生成对应的目标代码片段以便进一步集成到软件工程之中。 ```python import person_pb2 as addressbook_pb2 person = addressbook_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "jdoe@example.com" print(person) ``` 以上展示了 Python 中如何利用已生成的模块创建并操作一个简单的 `Person` 对象实例。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值