首页 > 其他分享 >pb.go 转 proto 文件

pb.go 转 proto 文件

时间:2023-11-21 14:33:41浏览次数:24  
标签:ft proto ss pb file go line

使用python将pb.go 文件转为proto文件

编程语言不限,挺简单的,不熟悉python的可以看看逻辑,自己改下

import os
import re


# 处理字段选项
def dispose_field_type(ft: str):
   ft = ft.replace("*", "")
   if "[]" not in ft and ("int" in ft or "string" in ft):
       return ft
   if "[]" not in ft and "float" in ft:
       return "float"
   if "[]" in ft:
       return f"repeated {ft.replace("[]","")}"
   return ft


# 将Go代码转换为Proto文件
def convert_go_to_proto(go_code, packnme):
   proto_code = f"""syntax="proto3";\npackage {packname};\n"""
   state = False
   for line in go_code.split("\n"):
       line = re.sub(r"\s+", " ", line)
       if "type " in line and "struct" in line:
           message_name = line.split(" ")[1]
           proto_code += f"message {message_name} {{\n"
           state = True
       elif "}" in line and state:
           proto_code += "}\n\n"
           state = False
       elif "protobuf" in line and "import" not in line and "github.com" not in line:
           ss = line.split(" ")
           (field_name, field_type, info) = ss[1], ss[2], ss[3]
           fieldType = dispose_field_type(field_type)
           number = info.split(",")[1]
           proto_code += f"  {fieldType} {field_name} = {number};\n"

   return proto_code


def check_directory(directory):
   out = []
   for root, dirs, files in os.walk(directory):
       if "msgtype" in root:
           for file in files:
               if "pb.go" in file:
                   # print(os.path.join(root, file))
                   out.append(os.path.join(root, file))

   return out


"""
给定目录,递归所有.pb.go 文件,然后转换成proto,会在pb.go 目录生成一份,还会在当前目录proto下生成一份,需要手动创建proto
"""
if __name__ == "__main__":
   files = check_directory("E:\\codes")
   for file in files:
       result = ""
       ss = file.split("\\")
       packname = ss[len(ss) - 2]
       with open(file, "r", encoding="utf-8") as f:
           result = convert_go_to_proto(f.read(), packname)

       file2 = file.replace("pb.go", "proto")
       print(file, " -> ", file2)
       with open(file2, "w", encoding="utf-8") as f:
           f.write(result)
       ss = file2.split("\\")
       file3 = f"proto\\{ss[len(ss)-1]}"
       with open(file3, "w", encoding="utf-8") as f:
           f.write(result)
```python

标签:ft,proto,ss,pb,file,go,line
From: https://www.cnblogs.com/--just-lbk/p/17846533.html

相关文章

  • 商品购物管理与推荐系统Python+Django网页界面+协同过滤推荐算法
    一、介绍商品管理与推荐系统。本系统使用Python作为主要开发语言,前端采用HTML、CSS、BootStrap等技术搭建显示界面,后端采用Django框架处理用户的请求响应。创新点:使用协同过滤算法,以用户对商品的评分作为依据,在猜你喜欢界面中实现对当前登录用户的个性化推荐。主要功能有:系统......
  • Django自定义接口
    一、在有默认(create、list、retrieve、update、destroy)接口时自定义接口classIterationsViewSet(MyModelViewSet):queryset=uc_iterations.objects.all().order_by('id')serializer_class=IterationsSerializerpagination_class=DIYPagination#配置自定......
  • django连接mysql pycharm操作sqlite和mysql
    1如果项目使用sqlite,不需要额外配置,直接操作即可2django默认情况链接mysql,用的驱动是mysqldb模块,python3.x以后,这个模块用不了了,咱们用的全都是pymysql,需要做个替换3showmigrations:查看哪些记录更改了,但是没有同步到数据库中3如果使用mysql,需要配置如下: -1配置文件中配置......
  • MySQL主从搭建及Django实现读写分离
    mysql主从搭建#1主从同步的流程或原理1)master会将变动记录到二进制日志里面;2)master有一个I/O线程将二进制日志发送到slave;3)slave有一个I/O线程把master发送的二进制写入到relay日志里面;4)slave有一个SQL线程,按照relay日志处理slave的数据;#2在home目录下创建mys......
  • 【go】【test】benchmark
    @目录写在前面go测试基础测试fib.gotest_fib.go使用内存generate_test.go测试参数测试generate_test.go参数测试timeRestnullsort_test.go测试开始记时⌛️和测试结束计时⌛️测试参考资料写在前面相关博文个人博客首页免责声明:仅供学习交流使用!开源框架可能存在的风险和相关......
  • 【golang】【注释模版】
    @目录写在前面注释要求package结构体方法代码逻辑注释参考资料基础/标准库/第三方库golang导航编程规范算法|面试项目写在前面相关博文个人博客首页免责声明:仅供学习交流使用!开源框架可能存在的风险和相关后果将完全由用户自行承担,本人不承担任何法律责任。注释要求统......
  • goland 无法打开问题
    下载了官网最新的goland安装包,安装完成之后,goland却无法打开了。  一番排查之后,发现是之前安装的版本有一些残留的配置项。配置项中有些文件的已经不存在了,需要去手动清理 解决方法第一步:在访达中打开软件源文件打开Finder(访达)-->Applications(应用程序)-->Go......
  • Centos7安装Mongodb社区版
    Centos7安装Mongodb社区版准备环境操作系统:Centos7安装包:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.3.tgzwindows可视化工具:Studio3T(原robot3t)/DbeaverCE安装下载wget-Omongodb.tgzhttps://fastdl.mongodb.org/linux/mongodb-li......
  • go使用context.withtimtout取消一个超时操作
    3 使用context.WithTimeout:package mainimport ("context""fmt""time")func main() {timeout := 5 * time.Secondctx, cancel := context.WithTimeout(context.Background(), timeout)defer cancel()done := make(chan bool)go func() {    // ......
  • Go基础面经题
    Go基础1.基础特性Go的优势天生支持并发,性能高。单一的标准代码格式,比其他语言更具可读性。自动垃圾收集机制比Java和Python更有效,因为它与程序同时执行。Go数据类型int,string,float,bool,array,slice,map,channel,pointer,struct,interface,meth......