首页 > 代码库 > 多语言 公用的类型定义方式 Cap‘n Proto

多语言 公用的类型定义方式 Cap‘n Proto

最近在折腾一个中间件的时候 ,用到了 Cap’n Proto

简单了解了一下, declaration names should use camelCase and must not contain underscores # 及定义类型的时候不能使用 小写或下滑线, 需要使用驼峰类型的,好吧 Alkaid 的最爱啊

有时间了解一下 protobuffer or thrift ..

就这些了,然后一些简单的测试代码:

import os
import capnp

this_dir = os.path.dirname(__file__)
node = capnp.load(os.path.join(this_dir, ‘node.capnp‘))


member_01 = node.Member.new_message()

member_01.node ="node1"
member_01.ip = "127.0.0.1"

f = open(‘node1.bin‘,‘w‘)

member_01.write( f)

f = open(‘node1.bin‘, ‘r‘)

member_01 = node.Member.read(f)
print(member_01.node)
print(member_01.ip)
#  filename  node.capnp
     @0x934efea7f017fff0;

 struct Member {
     node            @0 : Text;
     alias           @1 :List(Text);
     ip              @2 :Text;
     port            @3 :UInt32;
     inet            @4 :UInt32;
     clock           @5 :UInt32;
     state           @6 :Void;
     numOfVnodes   @7 :UInt32;
     grpLevelO1     @8 :List(Text);
     grpLevelO2     @9:List(Text);
 }

 struct ReduantNode {
     node             @0 : Text;
     avaliable        @1 : Bool;
     canReadRepair    @2 : Bool ;
     role             @3 : List(Text);
 }


 struct VnodeidNodes {
     id             @0 :UInt32 =0;
     vnodeIdFrom    @1: UInt32 = 0;
     vnodeIdTo      @2 : UInt32 =0;
     nodes          @3 : List(ReduantNode);
 }

 struct RingGroup {     
     indexFrom         @0 : UInt32 =0;
     indexTo           @1 : UInt32 =0;
     vnodeidNodesList @2: List(VnodeidNodes);
 }


 struct  RingInfo {
     checksum        @0: Int32 = -1;
     firstVnodeId  @1 :UInt32 = 0;
     lastVnodeId   @2 :UInt32 = 0;
     ringGroupList @3: List(RingGroup);  #list of groups
     members         @4 : List(Member);      #cluster-members
 }

 struct NodeState {
     node  @0 : Text;
     state @1 :  Text;
     ringHashNew @2  :Text = "-1";
     ringHashOld @3  :Text = "-1";
     whenIs @4 :UInt32 = 0;
     error   @5 :UInt32 = 0;
 }

多语言 公用的类型定义方式 Cap‘n Proto