unittest_custom_options.proto 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. // Author: benjy@google.com (Benjy Weinberger)
  8. // Based on original Protocol Buffers design by
  9. // Sanjay Ghemawat, Jeff Dean, and others.
  10. //
  11. // A proto file used to test the "custom options" feature of google.protobuf.
  12. syntax = "proto2";
  13. // Some generic_services option(s) added automatically.
  14. // See: http://go/proto2-generic-services-default
  15. option cc_generic_services = true; // auto-added
  16. option java_generic_services = true; // auto-added
  17. option py_generic_services = true;
  18. // A custom file option (defined below).
  19. option (file_opt1) = 9876543210;
  20. import "google/protobuf/any.proto";
  21. import "google/protobuf/descriptor.proto";
  22. // We don't put this in a package within proto2 because we need to make sure
  23. // that the generated code doesn't depend on being in the proto2 namespace.
  24. package protobuf_unittest;
  25. // Some simple test custom options of various types.
  26. extend google.protobuf.FileOptions {
  27. optional uint64 file_opt1 = 7736974;
  28. }
  29. extend google.protobuf.MessageOptions {
  30. optional int32 message_opt1 = 7739036;
  31. }
  32. extend google.protobuf.FieldOptions {
  33. optional fixed64 field_opt1 = 7740936;
  34. // This is useful for testing that we correctly register default values for
  35. // extension options.
  36. optional int32 field_opt2 = 7753913 [default = 42];
  37. }
  38. extend google.protobuf.OneofOptions {
  39. optional int32 oneof_opt1 = 7740111;
  40. }
  41. extend google.protobuf.EnumOptions {
  42. optional sfixed32 enum_opt1 = 7753576;
  43. }
  44. extend google.protobuf.EnumValueOptions {
  45. optional int32 enum_value_opt1 = 1560678;
  46. }
  47. extend google.protobuf.ServiceOptions {
  48. optional sint64 service_opt1 = 7887650;
  49. }
  50. enum MethodOpt1 {
  51. METHODOPT1_VAL1 = 1;
  52. METHODOPT1_VAL2 = 2;
  53. }
  54. extend google.protobuf.MethodOptions {
  55. optional MethodOpt1 method_opt1 = 7890860;
  56. }
  57. // A test message with custom options at all possible locations (and also some
  58. // regular options, to make sure they interact nicely).
  59. message TestMessageWithCustomOptions {
  60. option message_set_wire_format = false;
  61. option (message_opt1) = -56;
  62. optional string field1 = 1 [ctype = CORD, (field_opt1) = 8765432109];
  63. oneof AnOneof {
  64. option (oneof_opt1) = -99;
  65. int32 oneof_field = 2;
  66. }
  67. map<string, string> map_field = 3 [(field_opt1) = 12345];
  68. enum AnEnum {
  69. option (enum_opt1) = -789;
  70. ANENUM_VAL1 = 1;
  71. ANENUM_VAL2 = 2 [(enum_value_opt1) = 123];
  72. }
  73. }
  74. // A test RPC service with custom options at all possible locations (and also
  75. // some regular options, to make sure they interact nicely).
  76. message CustomOptionFooRequest {}
  77. message CustomOptionFooResponse {}
  78. message CustomOptionFooClientMessage {}
  79. message CustomOptionFooServerMessage {}
  80. service TestServiceWithCustomOptions {
  81. option (service_opt1) = -9876543210;
  82. rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) {
  83. option (method_opt1) = METHODOPT1_VAL2;
  84. }
  85. }
  86. // Options of every possible field type, so we can test them all exhaustively.
  87. message DummyMessageContainingEnum {
  88. enum TestEnumType {
  89. TEST_OPTION_ENUM_TYPE1 = 22;
  90. TEST_OPTION_ENUM_TYPE2 = -23;
  91. }
  92. }
  93. message DummyMessageInvalidAsOptionType {}
  94. extend google.protobuf.MessageOptions {
  95. optional bool bool_opt = 7706090;
  96. optional int32 int32_opt = 7705709;
  97. optional int64 int64_opt = 7705542;
  98. optional uint32 uint32_opt = 7704880;
  99. optional uint64 uint64_opt = 7702367;
  100. optional sint32 sint32_opt = 7701568;
  101. optional sint64 sint64_opt = 7700863;
  102. optional fixed32 fixed32_opt = 7700307;
  103. optional fixed64 fixed64_opt = 7700194;
  104. optional sfixed32 sfixed32_opt = 7698645;
  105. optional sfixed64 sfixed64_opt = 7685475;
  106. optional float float_opt = 7675390;
  107. optional double double_opt = 7673293;
  108. optional string string_opt = 7673285;
  109. optional bytes bytes_opt = 7673238;
  110. optional DummyMessageContainingEnum.TestEnumType enum_opt = 7673233;
  111. optional DummyMessageInvalidAsOptionType message_type_opt = 7665967;
  112. }
  113. message CustomOptionMinIntegerValues {
  114. option (bool_opt) = false;
  115. option (int32_opt) = -0x80000000;
  116. option (int64_opt) = -0x8000000000000000;
  117. option (uint32_opt) = 0;
  118. option (uint64_opt) = 0;
  119. option (sint32_opt) = -0x80000000;
  120. option (sint64_opt) = -0x8000000000000000;
  121. option (fixed32_opt) = 0;
  122. option (fixed64_opt) = 0;
  123. option (sfixed32_opt) = -0x80000000;
  124. option (sfixed64_opt) = -0x8000000000000000;
  125. }
  126. message CustomOptionMaxIntegerValues {
  127. option (bool_opt) = true;
  128. option (int32_opt) = 0x7FFFFFFF;
  129. option (int64_opt) = 0x7FFFFFFFFFFFFFFF;
  130. option (uint32_opt) = 0xFFFFFFFF;
  131. option (uint64_opt) = 0xFFFFFFFFFFFFFFFF;
  132. option (sint32_opt) = 0x7FFFFFFF;
  133. option (sint64_opt) = 0x7FFFFFFFFFFFFFFF;
  134. option (fixed32_opt) = 0xFFFFFFFF;
  135. option (fixed64_opt) = 0xFFFFFFFFFFFFFFFF;
  136. option (sfixed32_opt) = 0x7FFFFFFF;
  137. option (sfixed64_opt) = 0x7FFFFFFFFFFFFFFF;
  138. }
  139. message CustomOptionOtherValues {
  140. option (int32_opt) = -100; // To test sign-extension.
  141. option (float_opt) = 12.3456789;
  142. option (double_opt) = 1.234567890123456789;
  143. option (string_opt) = "Hello, \"World\"";
  144. option (bytes_opt) = "Hello\0World";
  145. option (enum_opt) = TEST_OPTION_ENUM_TYPE2;
  146. }
  147. message SettingRealsFromPositiveInts {
  148. option (float_opt) = 12;
  149. option (double_opt) = 154;
  150. }
  151. message SettingRealsFromNegativeInts {
  152. option (float_opt) = -12;
  153. option (double_opt) = -154;
  154. }
  155. message SettingRealsFromInf {
  156. option (float_opt) = inf;
  157. option (double_opt) = inf;
  158. }
  159. message SettingRealsFromNegativeInf {
  160. option (float_opt) = -inf;
  161. option (double_opt) = -inf;
  162. }
  163. message SettingRealsFromNan {
  164. option (float_opt) = nan;
  165. option (double_opt) = nan;
  166. }
  167. message SettingRealsFromNegativeNan {
  168. option (float_opt) = -nan;
  169. option (double_opt) = -nan;
  170. }
  171. // Options of complex message types, themselves combined and extended in
  172. // various ways.
  173. message ComplexOptionType1 {
  174. optional int32 foo = 1;
  175. optional int32 foo2 = 2;
  176. optional int32 foo3 = 3;
  177. repeated int32 foo4 = 4;
  178. extensions 100 to max;
  179. }
  180. message ComplexOptionType2 {
  181. optional ComplexOptionType1 bar = 1;
  182. optional int32 baz = 2;
  183. message ComplexOptionType4 {
  184. optional int32 waldo = 1;
  185. extend google.protobuf.MessageOptions {
  186. optional ComplexOptionType4 complex_opt4 = 7633546;
  187. }
  188. }
  189. optional ComplexOptionType4 fred = 3;
  190. repeated ComplexOptionType4 barney = 4;
  191. extensions 100 to max;
  192. }
  193. message ComplexOptionType3 {
  194. optional int32 moo = 1;
  195. optional group ComplexOptionType5 = 2 {
  196. optional int32 plugh = 3;
  197. }
  198. }
  199. extend ComplexOptionType1 {
  200. optional int32 mooo = 7663707;
  201. optional ComplexOptionType3 corge = 7663442;
  202. }
  203. extend ComplexOptionType2 {
  204. optional int32 grault = 7650927;
  205. optional ComplexOptionType1 garply = 7649992;
  206. }
  207. extend google.protobuf.MessageOptions {
  208. optional protobuf_unittest.ComplexOptionType1 complex_opt1 = 7646756;
  209. optional ComplexOptionType2 complex_opt2 = 7636949;
  210. optional ComplexOptionType3 complex_opt3 = 7636463;
  211. optional group ComplexOpt6 = 7595468 {
  212. optional int32 xyzzy = 7593951;
  213. }
  214. }
  215. // Note that we try various different ways of naming the same extension.
  216. message VariousComplexOptions {
  217. option (.protobuf_unittest.complex_opt1).foo = 42;
  218. option (protobuf_unittest.complex_opt1).(.protobuf_unittest.mooo) = 324;
  219. option (.protobuf_unittest.complex_opt1).(protobuf_unittest.corge).moo = 876;
  220. option (protobuf_unittest.complex_opt1).foo4 = 99;
  221. option (protobuf_unittest.complex_opt1).foo4 = 88;
  222. option (complex_opt2).baz = 987;
  223. option (complex_opt2).(grault) = 654;
  224. option (complex_opt2).bar.foo = 743;
  225. option (complex_opt2).bar.(mooo) = 1999;
  226. option (complex_opt2).bar.(protobuf_unittest.corge).moo = 2008;
  227. option (complex_opt2).(garply).foo = 741;
  228. option (complex_opt2).(garply).(.protobuf_unittest.mooo) = 1998;
  229. option (complex_opt2).(protobuf_unittest.garply).(corge).moo = 2121;
  230. option (ComplexOptionType2.ComplexOptionType4.complex_opt4).waldo = 1971;
  231. option (complex_opt2).fred.waldo = 321;
  232. option (complex_opt2).barney = {
  233. waldo: 101
  234. };
  235. option (complex_opt2).barney = {
  236. waldo: 212
  237. };
  238. option (protobuf_unittest.complex_opt3).moo = 9;
  239. option (complex_opt3).complexoptiontype5.plugh = 22;
  240. option (complexopt6).xyzzy = 24;
  241. }
  242. // ------------------------------------------------------
  243. // Definitions for testing aggregate option parsing.
  244. // See descriptor_unittest.cc.
  245. message AggregateMessageSet {
  246. option message_set_wire_format = true;
  247. extensions 4 to max;
  248. }
  249. message AggregateMessageSetElement {
  250. extend AggregateMessageSet {
  251. optional AggregateMessageSetElement message_set_extension = 15447542;
  252. }
  253. optional string s = 1;
  254. }
  255. // A helper type used to test aggregate option parsing
  256. message Aggregate {
  257. optional int32 i = 1;
  258. optional string s = 2;
  259. // A nested object
  260. optional Aggregate sub = 3;
  261. // To test the parsing of extensions inside aggregate values
  262. optional google.protobuf.FileOptions file = 4;
  263. extend google.protobuf.FileOptions {
  264. optional Aggregate nested = 15476903;
  265. }
  266. // An embedded message set
  267. optional AggregateMessageSet mset = 5;
  268. // An any
  269. optional google.protobuf.Any any = 6;
  270. }
  271. // Allow Aggregate to be used as an option at all possible locations
  272. // in the .proto grammar.
  273. extend google.protobuf.FileOptions {
  274. optional Aggregate fileopt = 15478479;
  275. }
  276. extend google.protobuf.MessageOptions {
  277. optional Aggregate msgopt = 15480088;
  278. }
  279. extend google.protobuf.FieldOptions {
  280. optional Aggregate fieldopt = 15481374;
  281. }
  282. extend google.protobuf.EnumOptions {
  283. optional Aggregate enumopt = 15483218;
  284. }
  285. extend google.protobuf.EnumValueOptions {
  286. optional Aggregate enumvalopt = 15486921;
  287. }
  288. extend google.protobuf.ServiceOptions {
  289. optional Aggregate serviceopt = 15497145;
  290. }
  291. extend google.protobuf.MethodOptions {
  292. optional Aggregate methodopt = 15512713;
  293. }
  294. // Try using AggregateOption at different points in the proto grammar
  295. option (fileopt) = {
  296. s: 'FileAnnotation'
  297. // Also test the handling of comments
  298. /* of both types */
  299. i: 100
  300. sub { s: 'NestedFileAnnotation' }
  301. // Include a google.protobuf.FileOptions and recursively extend it with
  302. // another fileopt.
  303. file {
  304. [protobuf_unittest.fileopt] { s: 'FileExtensionAnnotation' }
  305. }
  306. // A message set inside an option value
  307. mset {
  308. [protobuf_unittest.AggregateMessageSetElement.message_set_extension] {
  309. s: 'EmbeddedMessageSetElement'
  310. }
  311. }
  312. any {
  313. [type.googleapis.com/protobuf_unittest.AggregateMessageSetElement] {
  314. s: 'EmbeddedMessageSetElement'
  315. }
  316. }
  317. };
  318. message AggregateMessage {
  319. option (msgopt) = {
  320. i: 101
  321. s: 'MessageAnnotation'
  322. };
  323. optional int32 fieldname = 1 [(fieldopt) = { s: 'FieldAnnotation' }];
  324. }
  325. service AggregateService {
  326. option (serviceopt) = {
  327. s: 'ServiceAnnotation'
  328. };
  329. rpc Method(AggregateMessage) returns (AggregateMessage) {
  330. option (methodopt) = {
  331. s: 'MethodAnnotation'
  332. };
  333. }
  334. }
  335. enum AggregateEnum {
  336. option (enumopt) = {
  337. s: 'EnumAnnotation'
  338. };
  339. VALUE = 1 [(enumvalopt) = { s: 'EnumValueAnnotation' }];
  340. }
  341. // Test custom options for nested type.
  342. message NestedOptionType {
  343. message NestedMessage {
  344. option (message_opt1) = 1001;
  345. optional int32 nested_field = 1 [(field_opt1) = 1002];
  346. }
  347. enum NestedEnum {
  348. option (enum_opt1) = 1003;
  349. NESTED_ENUM_VALUE = 1 [(enum_value_opt1) = 1004];
  350. }
  351. extend google.protobuf.FileOptions {
  352. optional int32 nested_extension = 7912573 [(field_opt2) = 1005];
  353. }
  354. }
  355. // Custom message option that has a required enum field.
  356. // WARNING: this is strongly discouraged!
  357. message OldOptionType {
  358. enum TestEnum {
  359. OLD_VALUE = 0;
  360. }
  361. required TestEnum value = 1;
  362. }
  363. // Updated version of the custom option above.
  364. message NewOptionType {
  365. enum TestEnum {
  366. OLD_VALUE = 0;
  367. NEW_VALUE = 1;
  368. }
  369. required TestEnum value = 1;
  370. }
  371. extend google.protobuf.MessageOptions {
  372. optional OldOptionType required_enum_opt = 106161807;
  373. }
  374. // Test message using the "required_enum_opt" option defined above.
  375. message TestMessageWithRequiredEnumOption {
  376. option (required_enum_opt) = {
  377. value: OLD_VALUE
  378. };
  379. }