V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
joesonw
V2EX  ›  分享创造

分享一个合并 protobuf 的小工具

  •  2
     
  •   joesonw · 356 天前 · 1526 次点击
    这是一个创建于 356 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://github.com/joesonw/oneproto

    用来把多个 protobuf 合并为一个(如果 package 不同,通过 nested message 来区别命名空间)

    用来解决项目大了, protobuf 之间难免会产生相互引用. 但是放在不同的文件中, 维护性又更好, 于是产生了这个小工具

    例子

    user.proto

    syntax = 'proto3';
    option go_package = 'generated/proto;pb';
    
    package example.com.users;
    
    import "proto/group.proto";
    
    message User {
        string id = 1;
        string name = 2;
        string email = 3;
        string password = 4;
        string created_at = 5;
        string updated_at = 6;
        repeated groups.Group groups = 7;
    }
    

    group.proto

    syntax = 'proto3';
    option go_package = 'generated/proto;pb';
    
    package example.com.groups;
    
    import "proto/user.proto";
    
    message Group {
      string name = 1;
      repeated users.User users = 2;
    }
    

    产生的文件

    syntax = 'proto3';
    option go_package = 'generated/proto;pb';
    
    package example.com;
    
    message groups {
        message Group {
            string name = 1;
            repeated users.User users = 2;
        }
    
    }
    
    message users {
        message User {
            string id = 1;
            string name = 2;
            string email = 3;
            string password = 4;
            string created_at = 5;
            string updated_at = 6;
            repeated groups.Group groups = 7;
        }
    
    }
    

    希望能帮助到有需要的人

    1 条回复    2023-05-14 20:26:52 +08:00
    taofoo
        1
    taofoo  
       353 天前
    这两个我弄 swagger 的 Openapi.yaml 文件做过类似的 哈哈
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1775 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:46 · PVG 08:46 · LAX 17:46 · JFK 20:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.