Protobuf Reflection 反射使用

2024-06-22 08:04

本文主要是介绍Protobuf Reflection 反射使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Protobuf Reflection 反射使用

源码地址:https://github.com/Michaelzhouisnotwhite/DemoCXXCode/blob/main/src/protobuf-reflection/main.cpp
首先创建一个proto文件:

syntax = "proto3";// option optimize_for = LITE_RUNTIME; // 不使用MessageLitepackage pb;message Data {uint64 id = 1;string value = 2;
}
message DataList {repeated Data data = 1;int64 uid = 2;
}

NOTE 如何生成pb.c文件不赘述了

如何实现反射:

创建一个动态读取proto的importer

google::protobuf::compiler::DiskSourceTree source_tree{};// proto文件夹
source_tree.MapPath("proto",PROTO_ROOT_PATH);google::protobuf::compiler::Importer importer(&source_tree, nullptr);
const auto* desp = importer.Import("proto/test_msg.proto"); // 从文件夹导入文件

获得proto中的pb.DataList消息

const auto* desp_pool = desp->pool(); // 列出文件中所有描述器
const auto* msg_desp = desp_pool->FindMessageTypeByName("pb.DataList"); // 找到messagegoogle::protobuf::DynamicMessageFactory factory; // 创建工厂
auto* msg = factory.GetPrototype(msg_desp)->New(); // 得到目标message然后创建msg->ParseFromString(data_list_raw_data); // 解析

动态解析所有message中的field

auto parse_msg_field(const google::protobuf::Message* msg, int recurtion_level) -> void {const auto* ref = msg->GetReflection(); // 得到message的反射const auto* desp2 = msg->GetDescriptor(); // 得到message的描述器auto prefix = std::string("  ", recurtion_level);for (int i = 0; i < desp2->field_count(); ++i) {auto field = desp2->field(i);fmt::print("{}", prefix);fmt::print("{}   {}   {}    {}", field->number(), field->type_name(),field->cpp_type_name(), field->name());if (field->message_type()) {// fmt::println("current field is a message type");}switch (field->cpp_type()) {case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {fmt::println("   {}", ref->GetInt64(*msg, field));}break;case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {fmt::println("   {}", ref->GetString(*msg, field));}break;case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {fmt::println("   {}", ref->GetUInt64(*msg, field));}break;case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {fmt::println("");if (field->is_repeated()) {for (int rid = 0; rid < ref->FieldSize(*msg, field); rid++) {parse_msg_field(&ref->GetRepeatedMessage(*msg, field, rid),recurtion_level + 1);}}}break;default: {fmt::print("{}", prefix);fmt::println("can't parse type: {} {}", field->type_name(), field->name());}break;}}
}

这篇关于Protobuf Reflection 反射使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1083718

相关文章

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML

JAVA虚拟机中 -D, -X, -XX ,-server参数使用

《JAVA虚拟机中-D,-X,-XX,-server参数使用》本文主要介绍了JAVA虚拟机中-D,-X,-XX,-server参数使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录一、-D参数二、-X参数三、-XX参数总结:在Java开发过程中,对Java虚拟机(JVM)的启动参数进

Java中使用注解校验手机号格式的详细指南

《Java中使用注解校验手机号格式的详细指南》在现代的Web应用开发中,数据校验是一个非常重要的环节,本文将详细介绍如何在Java中使用注解对手机号格式进行校验,感兴趣的小伙伴可以了解下... 目录1. 引言2. 数据校验的重要性3. Java中的数据校验框架4. 使用注解校验手机号格式4.1 @NotBl

Python使用DeepSeek进行联网搜索功能详解

《Python使用DeepSeek进行联网搜索功能详解》Python作为一种非常流行的编程语言,结合DeepSeek这一高性能的深度学习工具包,可以方便地处理各种深度学习任务,本文将介绍一下如何使用P... 目录一、环境准备与依赖安装二、DeepSeek简介三、联网搜索与数据集准备四、实践示例:图像分类1.

Linux系统之authconfig命令的使用解读

《Linux系统之authconfig命令的使用解读》authconfig是一个用于配置Linux系统身份验证和账户管理设置的命令行工具,主要用于RedHat系列的Linux发行版,它提供了一系列选项... 目录linux authconfig命令的使用基本语法常用选项示例总结Linux authconfi

Windows server服务器使用blat命令行发送邮件

《Windowsserver服务器使用blat命令行发送邮件》在linux平台的命令行下可以使用mail命令来发送邮件,windows平台没有内置的命令,但可以使用开源的blat,其官方主页为ht... 目录下载blatBAT命令行示例备注总结在linux平台的命令行下可以使用mail命令来发送邮件,Win

Tomcat的下载安装与使用教程

《Tomcat的下载安装与使用教程》本文介绍了Tomcat的下载、安装和使用方法,包括在本机和云服务器上部署Tomcat的过程,以及解决启动失败问题的方法... 目录Tomcat的下载安装与使用Tomcat的下载与安装Tomcat在本机运行使用Tomcat在php云服务器上的使用总结Tomcat的下载安装与