爱意满满的作品展示区。
joesonw

分享一个合并 protobuf 的小工具

  •  2
     
  •   joesonw · May 11, 2023 · 2418 views
    This topic created in 1105 days ago, the information mentioned may be changed or developed.

    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;
        }
    
    }
    

    希望能帮助到有需要的人

    2 replies    2024-11-26 11:11:31 +08:00
    taofoo
        1
    taofoo  
       May 14, 2023
    这两个我弄 swagger 的 Openapi.yaml 文件做过类似的 哈哈
    424778940
        2
    424778940  
       Nov 26, 2024
    能说一下具体用法吗? template 的作用是?

    我这边这样用最后得到的 msg.proto 跟 temp_messages.proto 内容一模一样
    oneproto -T temp_messages.proto -O msg.proto -P sss.sss.messages -I. -I../../../../common/protob 1.proto 2.proto
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1493 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 16:59 · PVG 00:59 · LAX 09:59 · JFK 12:59
    ♥ Do have faith in what you're doing.