Full Example of Proto Definition

Proto Example

The following example is from Language Guide (proto3)

  1. // Statement proto use syntax
  2. syntax = "proto3";
  3. // proto package name
  4. package demo;
  5. // golang package name
  6. option go_package = "github. om/zeroicro/example/proto/demo";
  7. // Structure
  8. // SearchRequest Message definition specifies three fields (name/value), each field corresponds to each of the data to be included in such messages.Each field has a name and type.
  9. message SearchRequest {
  10. string = 1;
  11. int32 page_number = 2;
  12. int32 result_per_page = 3;
  13. }
  14. // If required. Roto file add annotations, use C/C++ style // / *...*/ syntax.
  15. /* SearchRequest presents a search query, with identification options to
  16. * indicating which results to include in the response. */
  17. message SearchRequest {
  18. string = 1;
  19. int32 page_number = 2; // Which page number do we want?
  20. int32 result_per_page = 3; // Number of results to return per page.
  21. }
  22. // Keep the field writing sample
  23. // If you update the message type by completely removing or annoying a field, future users will be free to update the field number to
  24. // that type.If users load later older versions of the same .proto (including data breaks, privacy bugs etc.),
  25. // this may cause serious problems.One way to ensure that this does not happen is to reserve the field number
  26. // (and/or the name that may also cause JSON serialization problems) that has been deleted.If any future user attempts to use these words
  27. // segment identifier, the protocol buffer compiler will complain.
  28. message Foo {
  29. reserved 2, 15, 9 to 11;
  30. reserved "foo", "bar";
  31. }
  32. // Enums
  33. // In defining message type, you may want a field to have only one predefined list of values.For example, suppose you want to add a corpus field for each
  34. // SearchRequest where the body can be UNIVERSAL, WEB, IMAGES, LOCAL,
  35. //NEWS, PRODUCTS or VIDEO.For this, you simply need to add enum in the message definition and add a constant to each possible value.
  36. // In the following example, we've added a field named Corpus (containing all possible values) and Corpus type:
  37. / / as you see, the first constant map of the Corpus enumeration to zeroeach item must contain a first constant to zero as its
  38. // first element.The reason is the following:
  39. //
  40. // 1. Must have a zero value so that we can use 0 as a default value for numbers.
  41. // Zero must be the first element to be compatible with proto2 in which the first value is always the default.
  42. enum Corpus {
  43. CORPUS_UNSPECIFIED = 0;
  44. CORPUS_UNIVERSAL = 1;
  45. CORPUS_WEB = 2;
  46. CORPUS_IMAGES = 3;
  47. CORPUS_LOCAL = 4;
  48. CORPUS_NEWS = 5;
  49. CORPUS_PRODUCTS = 6;
  50. CORPUS_VIDEO = 7;
  51. }
  52. message SearchRequest {
  53. string query = 1;
  54. int32 page_number = 2;
  55. int32 result_per_page = 3;
  56. Corpus corps = 4;
  57. }
  58. // Use other message type
  59. // You can use other message types as field type.message SearchResponse {
  60. repeated Result results = 1;
  61. }
  62. message Result {
  63. string url = 1;
  64. string title = 2;
  65. repeated string snippets = 3;
  66. }
  67. message SearchResponse {
  68. message Result {
  69. string url = 1;
  70. string title = 2;
  71. repeated string snippets = 3;
  72. }
  73. repeated Result results = 1;
  74. }
  75. // map
  76. message SearchResponse {
  77. map<string, Project> projects = 1;
  78. }For example, if you want to use the method of accepting SearchRequest
  79. // and return SearchResponse to define RPC services, it can be defined in a .proto file as shown below in:
  80. service SearchService 6
  81. rpc Search(SearchRequest) returns (Search);
  82. }