【protobuf】ProtoBuf——proto3语法详解、oneof类型、map类型、默认值、通讯录添加联系方式和备注的功能实现

ProtoBuf

在这里插入图片描述

  

5. proto3语法详解

5.5 oneof类型

  如果消息中有很多可选字段,但是将来只会有一个字段被设置,那么就可以利用oneof类型加强这一行为,也能有节约内存的效果。

  我们定义了一个名为 other_contact 的 oneof 字段,其中包含两个可选字段:qq 和 weixin。这两个字段只能有一个会被设置。

 oneof other_contact
 { // 其他联系⽅式:多选⼀
     string qq = 1;
     string weixin = 2;
 }

  同时也要注意:

  可选字段的编号不可与非可选字段编号相同,以防冲突。

  oneof 中不能使用 repeated 字段。

  未来设置 oneof 字段值时,若设置多个,仅保留最后一次设置的成员,之前设置的会自动清除。

  

  我们向我们的通讯录中添加oneof类型作为一种联系方式选择:

syntax = "proto3";
package contacts;

import "google/protobuf/any.proto"; // 引⼊ any.proto ⽂件

// 地址
message Address
{
    string home_address = 1; // 家庭地址
    string unit_address = 2; // 单位地址
}

// 联系⼈
message PeopleInfo
{
    string name = 1; // 姓名
    int32 age = 2;   // 年龄
    message Phone
    {
        string number = 1; // 电话号码
        enum PhoneType
        {
            MP = 0;  // 移动电话
            TEL = 1; // 固定电话
        }
        PhoneType type = 2; // 类型
    }

    repeated Phone phone = 3; // 电话

    google.protobuf.Any data = 4;

    oneof other_contact
    { // 其他联系⽅式:多选⼀
        string qq = 5;
        string weixin = 6;
    }
}

// 通讯录
message Contacts
{
    repeated PeopleInfo contacts = 1;
}

  
  protoc编译文件:

protoc --cpp_out=. contacts.proto

  
  查看contacts.pb.h文件,这就是自动生成的有关oneo的函数:

在这里插入图片描述
  

  判断类:

  _internal_has_qq:判断是否设置了 qq 字段。

  has_qq:通过调用 _internal_has_qq 获取是否设置了 qq 字段的信息。

  
  设置类:

  set_has_qq:设置 _oneof_case _ [0] 为 kQq 。

  set_qq:在未设置 qq 时进行清理和初始化操作,然后设置 qq 的值。

  _internal_set_qq:设置 qq 的值。

  set_allocated_qq:根据传入的 qq 指针进行相应的设置和清理操作。

  
  清除类:

  clear_qq:在 _internal_has_qq 为真时,销毁 qq 字段并清除相关设置。

  
  获取类:

  qq:获取 qq 字段的值。

  mutable_qq:获取可修改的 qq 字段。

  _internal_qq:在设置了 qq 时获取其值,否则返回空字符串。

  _internal_mutable_qq:在未设置 qq 时进行清理和初始化,然后返回可修改的 qq 字段。

  release_qq:在设置了 qq 时释放并清除相关设置,未设置时返回空指针。

  
  更新 write.cc :

#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
 * 新增联系⼈
 */
void AddPeopleInfo(PeopleInfo *people_info_ptr)
{
    cout << "-------------新增联系⼈-------------" << endl;
    cout << "请输⼊联系⼈姓名: ";
    string name;
    getline(cin, name);
    people_info_ptr->set_name(name);
    cout << "请输⼊联系⼈年龄: ";
    int age;
    cin >> age;
    people_info_ptr->set_age(age);
    cin.ignore(256, '\n');
    for (int i = 1;; i++)
    {
        cout << "请输⼊联系⼈电话" << i << "(只输⼊回⻋完成电话新增): ";
        string number;
        getline(cin, number);
        if (number.empty())
        {
            break;
        }
        PeopleInfo_Phone *phone = people_info_ptr->add_phone();
        phone->set_number(number);
        cout << "选择此电话类型 (1、移动电话 2、固定电话) : ";
        int type;
        cin >> type;
        cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
            break;
        case 2:
            phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
            break;
        default:
            cout << "⾮法选择,使⽤默认值!" << endl;
            break;
        }
    }
    Address address;
    cout << "请输⼊联系⼈家庭地址: ";
    string home_address;
    getline(cin, home_address);
    address.set_home_address(home_address);
    cout << "请输⼊联系⼈单位地址: ";
    string unit_address;
    getline(cin, unit_address);
    address.set_unit_address(unit_address);
    google::protobuf::Any *data = people_info_ptr->mutable_data();
    data->PackFrom(address);
    cout << "选择添加⼀个其他联系⽅式 (1、qq号 2、微信号) : ";
    int other_contact;
    cin >> other_contact;
    cin.ignore(256, '\n');
    if (1 == other_contact)
    {
        cout << "请输⼊qq号: ";
        string qq;
        getline(cin, qq);
        people_info_ptr->set_qq(qq);
    }
    else if (2 == other_contact)
    {
        cout << "请输⼊微信号: ";
        string weixin;
        getline(cin, weixin);
        people_info_ptr->set_weixin(weixin);
    }
    else
    {
        cout << "⾮法选择,该项设置失败!" << endl;
    }
    cout << "-----------添加联系⼈成功-----------" << endl;
}

int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " CONTACTS_FILE" << endl;
        return -1;
    }
    Contacts contacts;

    // 先读取已存在的 contacts
    fstream input(argv[1], ios::in | ios::binary);
    if (!input)
    {
        cout << argv[1] << ": File not found. Creating a new file." << endl;
    }
    else if (!contacts.ParseFromIstream(&input))
    {
        cerr << "Failed to parse contacts." << endl;
        input.close();
        return -1;
    }
    // 新增⼀个联系⼈
    AddPeopleInfo(contacts.add_contacts());
    // 向磁盘⽂件写⼊新的 contacts
    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    if (!contacts.SerializeToOstream(&output))
    {
        cerr << "Failed to write contacts." << endl;
        input.close();
        output.close();
        return -1;
    }
    input.close();
    output.close();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}

  
  更新 read.cc :

#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
 * 打印联系⼈列表
 */
void PrintfContacts(const Contacts &contacts)
{
    for (int i = 0; i < contacts.contacts_size(); ++i)
    {
        const PeopleInfo &people = contacts.contacts(i);
        cout << "------------联系⼈" << i + 1 << "------------" << endl;
        cout << "姓名:" << people.name() << endl;
        cout << "年龄:" << people.age() << endl;
        int j = 1;
        for (const PeopleInfo_Phone &phone : people.phone())
        {
            cout << "电话" << j++ << ": " << phone.number();
            cout << " (" << phone.PhoneType_Name(phone.type()) << ")" << endl;
        }
        if (people.has_data() && people.data().Is<Address>())
        {
            Address address;
            people.data().UnpackTo(&address);
            if (!address.home_address().empty())
            {
                cout << "家庭地址:" << address.home_address() << endl;
            }
            if (!address.unit_address().empty())
            {
                cout << "单位地址:" << address.unit_address() << endl;
            }
        }
        /* if (people.has_qq()) {
        } else if (people.has_weixin()) {
        } */
        switch (people.other_contact_case())
        {
        case PeopleInfo::OtherContactCase::kQq:
            cout << "qq号: " << people.qq() << endl;
            break;
        case PeopleInfo::OtherContactCase::kWeixin:
            cout << "微信号: " << people.weixin() << endl;
            break;
        case PeopleInfo::OtherContactCase::OTHER_CONTACT_NOT_SET:
            break;
        }
    }
}
int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << "CONTACTS_FILE" << endl;
        return -1;
    }
    // 以⼆进制⽅式读取 contacts
    Contacts contacts;
    fstream input(argv[1], ios::in | ios::binary);
    if (!contacts.ParseFromIstream(&input))
    {
        cerr << "Failed to parse contacts." << endl;
        input.close();
        return -1;
    }
    // 打印 contacts
    PrintfContacts(contacts);
    input.close();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}

  
  makfile编译:

all:write read
write:write.cc contacts.pb.cc
	g++ -o $@ $^ -std=c++11 -lprotobuf
read:read.cc contacts.pb.cc
	g++ -o $@ $^ -std=c++11 -lprotobuf
	
.PHONY:clean
clean:
	rm -f write read

在这里插入图片描述
在这里插入图片描述

  

5.6 map类型

  语法支持创建⼀个关联映射字段,也就是可以使用map类型去声明字段类型,格式为:

map<key_type, value_type> map_field = N;

  注意:

  key_type 可以是除 float 和 bytes 类型之外的任何标量类型,如 int 、 string 、 bool 等。

  value_type 则没有太多限制,可以是任何类型,包括自定义类型、其他复合类型等。

  map 字段不能用 repeated 修饰,所以不能写成类似于 repeated map<int, string> user_ages 这样的形式。

  map 中存储的元素是无序的。

  
  我们可以这样定义备注字段类型:

map<string, string> remark = 7; // 备注

  
  我们向我们的通讯录中添加map类型作为备注:

  
  protoc编译文件:

protoc --cpp_out=. contacts.proto

  
  查看contacts.pb.h文件,这就是自动生成的有关map的函数:

在这里插入图片描述
  

  与 remark 相关的函数:

  _internal_remark_size :获取 remark 的大小。

  remark_size :通过调用 _internal_remark_size 获取 remark 的大小。

  clear_remark :清空 remark 。

  _internal_remark :获取 remark 的内部映射。

  remark :通过特定的插入点获取 remark 的内部映射。

  _internal_mutable_remark :获取可修改的 remark 映射。

  mutable_remark :通过特定的插入点获取可修改的 remark 映射。

  
  更新 write.cc :

#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
 * 新增联系⼈
 */
void AddPeopleInfo(PeopleInfo *people_info_ptr)
{
    cout << "-------------新增联系⼈-------------" << endl;
    cout << "请输⼊联系⼈姓名: ";
    string name;
    getline(cin, name);
    people_info_ptr->set_name(name);
    cout << "请输⼊联系⼈年龄: ";
    int age;
    cin >> age;
    people_info_ptr->set_age(age);
    cin.ignore(256, '\n');
    for (int i = 1;; i++)
    {
        cout << "请输⼊联系⼈电话" << i << "(只输⼊回⻋完成电话新增): ";
        string number;
        getline(cin, number);
        if (number.empty())
        {
            break;
        }
        PeopleInfo_Phone *phone = people_info_ptr->add_phone();
        phone->set_number(number);
        cout << "选择此电话类型 (1、移动电话 2、固定电话) : ";
        int type;
        cin >> type;
        cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
            break;
        case 2:
            phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
            break;
        default:
            cout << "⾮法选择,使⽤默认值!" << endl;
            break;
        }
    }
    Address address;
    cout << "请输⼊联系⼈家庭地址: ";
    string home_address;
    getline(cin, home_address);
    address.set_home_address(home_address);
    cout << "请输⼊联系⼈单位地址: ";
    string unit_address;
    getline(cin, unit_address);
    address.set_unit_address(unit_address);
    google::protobuf::Any *data = people_info_ptr->mutable_data();
    data->PackFrom(address);
    cout << "选择添加⼀个其他联系⽅式 (1、qq号 2、微信号) : ";
    int other_contact;
    cin >> other_contact;
    cin.ignore(256, '\n');
    if (1 == other_contact)
    {
        cout << "请输⼊qq号: ";
        string qq;
        getline(cin, qq);
        people_info_ptr->set_qq(qq);
    }
    else if (2 == other_contact)
    {
        cout << "请输⼊微信号: ";
        string weixin;
        getline(cin, weixin);
        people_info_ptr->set_weixin(weixin);
    }
    else
    {
        cout << "⾮法选择,该项设置失败!" << endl;
    }
    for (int i = 1;; i++)
    {
        cout << "请输⼊备注" << i << "标题 (只输⼊回⻋完成备注新增): ";
        string remark_key;
        getline(cin, remark_key);
        if (remark_key.empty())
        {
            break;
        }
        cout << "请输⼊备注" << i << "内容: ";
        string remark_value;
        getline(cin, remark_value);
        people_info_ptr->mutable_remark()->insert({remark_key, remark_value});
    }
    cout << "-----------添加联系⼈成功-----------" << endl;
}
int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " CONTACTS_FILE" << endl;
        return -1;
    }
    Contacts contacts;

    // 先读取已存在的 contacts
    fstream input(argv[1], ios::in | ios::binary);
    if (!input)
    {
        cout << argv[1] << ": File not found. Creating a new file." << endl;
    }
    else if (!contacts.ParseFromIstream(&input))
    {
        cerr << "Failed to parse contacts." << endl;
        input.close();
        return -1;
    }
    // 新增⼀个联系⼈
    AddPeopleInfo(contacts.add_contacts());
    // 向磁盘⽂件写⼊新的 contacts
    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    if (!contacts.SerializeToOstream(&output))
    {
        cerr << "Failed to write contacts." << endl;
        input.close();
        output.close();
        return -1;
    }
    input.close();
    output.close();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}

  
  更新 read.cc :

#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
 * 打印联系⼈列表
 */
void PrintfContacts(const Contacts &contacts)
{
    for (int i = 0; i < contacts.contacts_size(); ++i)
    {
        const PeopleInfo &people = contacts.contacts(i);
        cout << "------------联系⼈" << i + 1 << "------------" << endl;
        cout << "姓名:" << people.name() << endl;
        cout << "年龄:" << people.age() << endl;
        int j = 1;
        for (const PeopleInfo_Phone &phone : people.phone())
        {
            cout << "电话" << j++ << ": " << phone.number();
            cout << " (" << phone.PhoneType_Name(phone.type()) << ")" << endl;
        }
        if (people.has_data() && people.data().Is<Address>())
        {
            Address address;
            people.data().UnpackTo(&address);
            if (!address.home_address().empty())
            {
                cout << "家庭地址:" << address.home_address() << endl;
            }
            if (!address.unit_address().empty())
            {
                cout << "单位地址:" << address.unit_address() << endl;
            }
        }
        /* if (people.has_qq()) {
        } else if (people.has_weixin()) {
        } */
        switch (people.other_contact_case())
        {
        case PeopleInfo::OtherContactCase::kQq:
            cout << "qq号: " << people.qq() << endl;
            break;
        case PeopleInfo::OtherContactCase::kWeixin:
            cout << "微信号: " << people.weixin() << endl;
            break;
        case PeopleInfo::OtherContactCase::OTHER_CONTACT_NOT_SET:
            break;
        }
        if (people.remark_size())
        {
            cout << "备注信息: " << endl;
        }
        for (auto it = people.remark().cbegin(); it != people.remark().cend();
             ++it)
        {
            cout << " " << it->first << ": " << it->second << endl;
        }
    }
}
int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << "CONTACTS_FILE" << endl;
        return -1;
    }
    // 以⼆进制⽅式读取 contacts
    Contacts contacts;
    fstream input(argv[1], ios::in | ios::binary);
    if (!contacts.ParseFromIstream(&input))
    {
        cerr << "Failed to parse contacts." << endl;
        input.close();
        return -1;
    }
    // 打印 contacts
    PrintfContacts(contacts);
    input.close();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}

  
  makfile编译:

all:write read
write:write.cc contacts.pb.cc
	g++ -o $@ $^ -std=c++11 -lprotobuf
read:read.cc contacts.pb.cc
	g++ -o $@ $^ -std=c++11 -lprotobuf
	
.PHONY:clean
clean:
	rm -f write read

在这里插入图片描述

  

5.7 默认值

  对于 proto3 的语法,message 中的字段默认由 singular 修饰。当被 singular 修饰的字段在序列化时未被设置值,protobuf 的序列化方法不会对该字段进行编码。同理,在反序列化时,若在反序列化序列中未找到 message 中某一字段的值,protobuf 则会用该字段的默认值来填充此字段。

  在反序列化消息时,不同类型字段在二进制序列中未包含时所采用的默认值规则:

类型默认值
字符串默认空字符串
字节默认空字节
布尔值默认 false
数值类型默认 0
枚举默认第一个定义且值为 0 的枚举值
消息字段未设置
repeated字段默认空列表
对于 消息字段 、 oneof 字段 和 any 字段在 C++ 和 Java 中可以通过 has_ 方法检测是否被设置

            

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Protobuf.js 是一个 JavaScript 库,它可以让您在 JavaScript 环境中使用 Protocol Buffers 编码和解码数据。 在 Protocol Buffers 中,oneof 关键字允许您定义一个消息类型中的一组字段中只能有一个字段有值。 在 protobuf.js 中,可以通过一下几种方式实现 oneof: 1.在 .proto 文件中定义 oneof 字段 ```protobuf syntax = "proto3"; message ExampleMessage { oneof example_oneof { string field1 = 1; int32 field2 = 2; bool field3 = 3; } } ``` 2.使用 protobuf.js 的 API 解析 .proto 文件, 然后获取 oneof 的值。 ```javascript const protobuf = require("protobufjs"); protobuf.load("example.proto", (err, root) => { if (err) throw err; // Obtain a message type const ExampleMessage = root.lookupType("ExampleMessage"); // Encode a message (note that the oneof field is not set) const message = ExampleMessage.create({ field1: "example value" }); // Verify that only one field is set console.log(ExampleMessage.oneofs.example_oneof.decode(message.example_oneof)); // console.log(field1) // Update the message and verify again message.field2 = 100; console.log(ExampleMessage.oneofs.example_oneof.decode(message.example_oneof)); //console.log(field2) }); ``` 总结: protobuf.js 中 oneof实现方式就是在 .proto 文件中定义 oneof 字段, 然后使用 protobuf.js 的 API 解析 .proto 文件,获取 oneof 的值。 ### 回答2: protobuf.js是一个用于生成和解析Protocol Buffers数据的JavaScript库。oneofProtocol Buffers中的一个特殊字段修饰符,表示只能有一个字段可以被设置并包含在消息中。 在protobuf.js中实现oneof,需要遵循以下步骤: 1. 首先,在定义Protocol Buffers消息类型时,使用oneof修饰符来标记需要实现oneof的字段。例如: ``` syntax = "proto3"; message MyMessage { oneof data { string name = 1; int32 age = 2; bool married = 3; } } ``` 2. 使用protobuf.js的编译器将上述定义的.proto文件编译为可用的JavaScript模块。例如,通过命令行运行以下命令: ``` pbjs -t static-module -w commonjs -o mymessage.js mymessage.proto ``` 这将生成一个名为mymessage.js的JavaScript模块,其中包含生成的代码和方法。 3. 在JavaScript代码中,引入生成的模块,然后使用相关方法创建和操作消息对象。例如: ``` const protobuf = require('./mymessage'); // 创建一个新的MyMessage对象 const myMessage = new protobuf.MyMessage(); // 设置name字段的值 myMessage.name = 'John'; // 或者,设置age字段的值 myMessage.age = 25; // 或者,设置married字段的值 myMessage.married = true; // 访问已设置的字段的值 console.log(myMessage.name); // 输出 'John' // 序列化消息对象为字节流 const bytes = protobuf.MyMessage.encode(myMessage).finish(); // 反序列化字节流为消息对象 const decodedMessage = protobuf.MyMessage.decode(bytes); ``` 通过以上步骤,我们可以在protobuf.js中实现oneof。在创建消息对象时,只能设置一个oneof字段的值,并且可以通过访问已设置字段的值来检查当前设置的字段。同时,protobuf.js提供了方法来进行消息对象的序列化和反序列化操作。 ### 回答3: protobuf.js是一个用于处理Protocol Buffers数据的JavaScript库。在protobuf.js中,可以使用oneof关键字来实现oneof语义。 oneof语义用于在多个字段中只能选择一个进行编码和解码。在protobuf.js中,可以通过以下步骤来实现oneof。 首先,在定义消息类型时,使用oneof关键字来声明一个oneof组: ```protobuf message MyMessage { oneof my_field { int32 number = 1; string text = 2; bool flag = 3; } } ``` 在上述示例中,我们定义了一个名为MyMessage的消息类型,其中包含一个oneof组名为my_field。该组包含了三个字段:number、text和flag。 然后,根据需要选择其中一个字段进行编码和解码。在protobuf.js中,可以通过以下方式设置和获取字段值: ```javascript const message = MyMessage.create(); message.number = 10; // 设置number字段的值 console.log(message.number); // 获取number字段的值 ``` 在上述示例中,我们通过设置message.number来给number字段赋值,通过message.number来获取number字段的值。 需要注意的是,由于oneof语义的限制,不同字段之间是互斥的。即当设置一个字段的值时,其他字段的值将被清空。 通过以上步骤,我们可以使用protobuf.js来实现oneof语义。当编码和解码消息时,protobuf.js会自动处理oneof语义,确保只有一个字段被设置和读取。 总结起来,使用protobuf.js可以方便地实现oneof语义,通过oneof关键字声明一个组,并根据需要设置和获取字段的值。这样就可以实现在多个字段中只能选择一个进行编码和解码的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鳄鱼麻薯球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值