【问题标题】:Protocol Buffer协议缓冲区
【发布时间】:2017-05-06 18:44:53
【问题描述】:

我正试图了解协议缓冲区。我理解了基本的东西,但现在我试图将以下 javascript 对象定义为 .proto。使用Node.js gRPC Library

const listsSchema = new Schema({
    id: { type: String, required: true },
  id_str: { type: String, required: true, index: { unique: true } },
  name: { type: String, required: true },
  description: { type: String, required: true },
  slug: { type: String, required: true },
  createdAt: { type: Date, 'default': Date.now },
  updatedAt: { type: Date, 'default': Date.now },
  track: { type: Boolean, 'default': false },
  lastTrackedTweet: [
    { 
      id: { type: String, required: true },
      id_str: { type: String, required: true },
      createdAt: { type: Date, 'default': Date.now },
      updatedAt: { type: Date, 'default': Date.now }
        }
    ]
});

这是我想出的,但被拒绝了。

syntax = "proto3";

package lists;

service Lists {
  rpc FindLists (ListRequest) returns (ListReply) {}
}

message ListRequest {
  struct params = 1;
}

message ListReply {
  repeated struct List = 1;
}

message List {
 int64 id = 1;
 string id_str = 2;
 string name = 3;
 string description = 4;
 string slug = 5;
 Timestamp createdAt = 6;
 Timestamp updatedAt = 7;
 bool track = 8;
 repeated struct LastTrackedTweet = 9;
}

message LastTrackedTweet {
  int64 id = 1;
  string id_str = 2;
  Timestamp createdAt = 3;
  Timestamp updatedAt = 4;
}

【问题讨论】:

    标签: node.js protocol-buffers grpc proto


    【解决方案1】:

    我想通了。高级示例的文档不太好:

    syntax = "proto3";
    
    package lists;
    
    service Lists {
      rpc FindLists (ListRequest) returns (ListReply) {}
    }
    
    message ListRequest {
     string query = 1
    }
    
    message ListReply {
      repeated List list = 1;
    }
    
    message List {
     int64 id = 1;
     string id_str = 2;
     string name = 3;
     string description = 4;
     string slug = 5;
     string createdAt = 6;
     string updatedAt = 7;
     bool track = 8;
     repeated LastTrackedTweet lastTrackedTweet = 9;
    }
    
    message LastTrackedTweet {
      int64 id = 1;
      string id_str = 2;
      string createdAt = 3;
      string updatedAt = 4;
    }
    

    【讨论】: