• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// Author: benjy@google.com (Benjy Weinberger)
32//  Based on original Protocol Buffers design by
33//  Sanjay Ghemawat, Jeff Dean, and others.
34//
35// A proto file used to test the "custom options" feature of google.protobuf.
36
37syntax = "proto2";
38
39// Some generic_services option(s) added automatically.
40// See:  http://go/proto2-generic-services-default
41option cc_generic_services = true;    // auto-added
42option java_generic_services = true;  // auto-added
43option py_generic_services = true;
44
45// A custom file option (defined below).
46option (file_opt1) = 9876543210;
47
48import "google/protobuf/descriptor.proto";
49
50// We don't put this in a package within proto2 because we need to make sure
51// that the generated code doesn't depend on being in the proto2 namespace.
52package protobuf_unittest;
53
54// Some simple test custom options of various types.
55
56extend google.protobuf.FileOptions {
57  optional uint64 file_opt1 = 7736974;
58}
59
60extend google.protobuf.MessageOptions {
61  optional int32 message_opt1 = 7739036;
62}
63
64extend google.protobuf.FieldOptions {
65  optional fixed64 field_opt1 = 7740936;
66  // This is useful for testing that we correctly register default values for
67  // extension options.
68  optional int32 field_opt2 = 7753913 [default = 42];
69}
70
71extend google.protobuf.OneofOptions {
72  optional int32 oneof_opt1 = 7740111;
73}
74
75extend google.protobuf.EnumOptions {
76  optional sfixed32 enum_opt1 = 7753576;
77}
78
79extend google.protobuf.EnumValueOptions {
80  optional int32 enum_value_opt1 = 1560678;
81}
82
83extend google.protobuf.ServiceOptions {
84  optional sint64 service_opt1 = 7887650;
85}
86
87enum MethodOpt1 {
88  METHODOPT1_VAL1 = 1;
89  METHODOPT1_VAL2 = 2;
90}
91
92extend google.protobuf.MethodOptions {
93  optional MethodOpt1 method_opt1 = 7890860;
94}
95
96// A test message with custom options at all possible locations (and also some
97// regular options, to make sure they interact nicely).
98message TestMessageWithCustomOptions {
99  option message_set_wire_format = false;
100  option (message_opt1) = -56;
101
102  optional string field1 = 1 [ctype = CORD, (field_opt1) = 8765432109];
103
104  oneof AnOneof {
105    option (oneof_opt1) = -99;
106
107    int32 oneof_field = 2;
108  }
109
110  enum AnEnum {
111    option (enum_opt1) = -789;
112
113    ANENUM_VAL1 = 1;
114    ANENUM_VAL2 = 2 [(enum_value_opt1) = 123];
115  }
116}
117
118// A test RPC service with custom options at all possible locations (and also
119// some regular options, to make sure they interact nicely).
120message CustomOptionFooRequest {}
121
122message CustomOptionFooResponse {}
123
124message CustomOptionFooClientMessage {}
125
126message CustomOptionFooServerMessage {}
127
128service TestServiceWithCustomOptions {
129  option (service_opt1) = -9876543210;
130
131  rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) {
132    option (method_opt1) = METHODOPT1_VAL2;
133  }
134}
135
136// Options of every possible field type, so we can test them all exhaustively.
137
138message DummyMessageContainingEnum {
139  enum TestEnumType {
140    TEST_OPTION_ENUM_TYPE1 = 22;
141    TEST_OPTION_ENUM_TYPE2 = -23;
142  }
143}
144
145message DummyMessageInvalidAsOptionType {}
146
147extend google.protobuf.MessageOptions {
148  optional bool bool_opt = 7706090;
149  optional int32 int32_opt = 7705709;
150  optional int64 int64_opt = 7705542;
151  optional uint32 uint32_opt = 7704880;
152  optional uint64 uint64_opt = 7702367;
153  optional sint32 sint32_opt = 7701568;
154  optional sint64 sint64_opt = 7700863;
155  optional fixed32 fixed32_opt = 7700307;
156  optional fixed64 fixed64_opt = 7700194;
157  optional sfixed32 sfixed32_opt = 7698645;
158  optional sfixed64 sfixed64_opt = 7685475;
159  optional float float_opt = 7675390;
160  optional double double_opt = 7673293;
161  optional string string_opt = 7673285;
162  optional bytes bytes_opt = 7673238;
163  optional DummyMessageContainingEnum.TestEnumType enum_opt = 7673233;
164  optional DummyMessageInvalidAsOptionType message_type_opt = 7665967;
165}
166
167message CustomOptionMinIntegerValues {
168  option (bool_opt) = false;
169  option (int32_opt) = -0x80000000;
170  option (int64_opt) = -0x8000000000000000;
171  option (uint32_opt) = 0;
172  option (uint64_opt) = 0;
173  option (sint32_opt) = -0x80000000;
174  option (sint64_opt) = -0x8000000000000000;
175  option (fixed32_opt) = 0;
176  option (fixed64_opt) = 0;
177  option (sfixed32_opt) = -0x80000000;
178  option (sfixed64_opt) = -0x8000000000000000;
179}
180
181message CustomOptionMaxIntegerValues {
182  option (bool_opt) = true;
183  option (int32_opt) = 0x7FFFFFFF;
184  option (int64_opt) = 0x7FFFFFFFFFFFFFFF;
185  option (uint32_opt) = 0xFFFFFFFF;
186  option (uint64_opt) = 0xFFFFFFFFFFFFFFFF;
187  option (sint32_opt) = 0x7FFFFFFF;
188  option (sint64_opt) = 0x7FFFFFFFFFFFFFFF;
189  option (fixed32_opt) = 0xFFFFFFFF;
190  option (fixed64_opt) = 0xFFFFFFFFFFFFFFFF;
191  option (sfixed32_opt) = 0x7FFFFFFF;
192  option (sfixed64_opt) = 0x7FFFFFFFFFFFFFFF;
193}
194
195message CustomOptionOtherValues {
196  option (int32_opt) = -100;  // To test sign-extension.
197  option (float_opt) = 12.3456789;
198  option (double_opt) = 1.234567890123456789;
199  option (string_opt) = "Hello, \"World\"";
200  option (bytes_opt) = "Hello\0World";
201  option (enum_opt) = TEST_OPTION_ENUM_TYPE2;
202}
203
204message SettingRealsFromPositiveInts {
205  option (float_opt) = 12;
206  option (double_opt) = 154;
207}
208
209message SettingRealsFromNegativeInts {
210  option (float_opt) = -12;
211  option (double_opt) = -154;
212}
213
214// Options of complex message types, themselves combined and extended in
215// various ways.
216
217message ComplexOptionType1 {
218  optional int32 foo = 1;
219  optional int32 foo2 = 2;
220  optional int32 foo3 = 3;
221  repeated int32 foo4 = 4;
222
223  extensions 100 to max;
224}
225
226message ComplexOptionType2 {
227  optional ComplexOptionType1 bar = 1;
228  optional int32 baz = 2;
229
230  message ComplexOptionType4 {
231    optional int32 waldo = 1;
232
233    extend google.protobuf.MessageOptions {
234      optional ComplexOptionType4 complex_opt4 = 7633546;
235    }
236  }
237
238  optional ComplexOptionType4 fred = 3;
239  repeated ComplexOptionType4 barney = 4;
240
241  extensions 100 to max;
242}
243
244message ComplexOptionType3 {
245  optional int32 qux = 1;
246
247  optional group ComplexOptionType5 = 2 {
248    optional int32 plugh = 3;
249  }
250}
251
252extend ComplexOptionType1 {
253  optional int32 quux = 7663707;
254  optional ComplexOptionType3 corge = 7663442;
255}
256
257extend ComplexOptionType2 {
258  optional int32 grault = 7650927;
259  optional ComplexOptionType1 garply = 7649992;
260}
261
262extend google.protobuf.MessageOptions {
263  optional protobuf_unittest.ComplexOptionType1 complex_opt1 = 7646756;
264  optional ComplexOptionType2 complex_opt2 = 7636949;
265  optional ComplexOptionType3 complex_opt3 = 7636463;
266  optional group ComplexOpt6 = 7595468 {
267    optional int32 xyzzy = 7593951;
268  }
269}
270
271// Note that we try various different ways of naming the same extension.
272message VariousComplexOptions {
273  option (.protobuf_unittest.complex_opt1).foo = 42;
274  option (protobuf_unittest.complex_opt1).(.protobuf_unittest.quux) = 324;
275  option (.protobuf_unittest.complex_opt1).(protobuf_unittest.corge).qux = 876;
276  option (protobuf_unittest.complex_opt1).foo4 = 99;
277  option (protobuf_unittest.complex_opt1).foo4 = 88;
278  option (complex_opt2).baz = 987;
279  option (complex_opt2).(grault) = 654;
280  option (complex_opt2).bar.foo = 743;
281  option (complex_opt2).bar.(quux) = 1999;
282  option (complex_opt2).bar.(protobuf_unittest.corge).qux = 2008;
283  option (complex_opt2).(garply).foo = 741;
284  option (complex_opt2).(garply).(.protobuf_unittest.quux) = 1998;
285  option (complex_opt2).(protobuf_unittest.garply).(corge).qux = 2121;
286  option (ComplexOptionType2.ComplexOptionType4.complex_opt4).waldo = 1971;
287  option (complex_opt2).fred.waldo = 321;
288  option (complex_opt2).barney = {
289    waldo: 101
290  };
291  option (complex_opt2).barney = {
292    waldo: 212
293  };
294  option (protobuf_unittest.complex_opt3).qux = 9;
295  option (complex_opt3).complexoptiontype5.plugh = 22;
296  option (complexopt6).xyzzy = 24;
297}
298
299// ------------------------------------------------------
300// Definitions for testing aggregate option parsing.
301// See descriptor_unittest.cc.
302
303message AggregateMessageSet {
304  option message_set_wire_format = true;
305
306  extensions 4 to max;
307}
308
309message AggregateMessageSetElement {
310  extend AggregateMessageSet {
311    optional AggregateMessageSetElement message_set_extension = 15447542;
312  }
313  optional string s = 1;
314}
315
316// A helper type used to test aggregate option parsing
317message Aggregate {
318  optional int32 i = 1;
319  optional string s = 2;
320
321  // A nested object
322  optional Aggregate sub = 3;
323
324  // To test the parsing of extensions inside aggregate values
325  optional google.protobuf.FileOptions file = 4;
326  extend google.protobuf.FileOptions {
327    optional Aggregate nested = 15476903;
328  }
329
330  // An embedded message set
331  optional AggregateMessageSet mset = 5;
332}
333
334// Allow Aggregate to be used as an option at all possible locations
335// in the .proto grammar.
336extend google.protobuf.FileOptions {
337  optional Aggregate fileopt = 15478479;
338}
339extend google.protobuf.MessageOptions {
340  optional Aggregate msgopt = 15480088;
341}
342extend google.protobuf.FieldOptions {
343  optional Aggregate fieldopt = 15481374;
344}
345extend google.protobuf.EnumOptions {
346  optional Aggregate enumopt = 15483218;
347}
348extend google.protobuf.EnumValueOptions {
349  optional Aggregate enumvalopt = 15486921;
350}
351extend google.protobuf.ServiceOptions {
352  optional Aggregate serviceopt = 15497145;
353}
354extend google.protobuf.MethodOptions {
355  optional Aggregate methodopt = 15512713;
356}
357
358// Try using AggregateOption at different points in the proto grammar
359option (fileopt) = {
360  s: 'FileAnnotation'
361  // Also test the handling of comments
362  /* of both types */
363  i: 100
364
365  sub { s: 'NestedFileAnnotation' }
366
367  // Include a google.protobuf.FileOptions and recursively extend it with
368  // another fileopt.
369  file {
370    [protobuf_unittest.fileopt] { s: 'FileExtensionAnnotation' }
371  }
372
373  // A message set inside an option value
374  mset {
375    [protobuf_unittest.AggregateMessageSetElement.message_set_extension] {
376      s: 'EmbeddedMessageSetElement'
377    }
378  }
379};
380
381message AggregateMessage {
382  option (msgopt) = {
383    i: 101
384    s: 'MessageAnnotation'
385  };
386
387  optional int32 fieldname = 1 [(fieldopt) = { s: 'FieldAnnotation' }];
388}
389
390service AggregateService {
391  option (serviceopt) = {
392    s: 'ServiceAnnotation'
393  };
394
395  rpc Method(AggregateMessage) returns (AggregateMessage) {
396    option (methodopt) = {
397      s: 'MethodAnnotation'
398    };
399  }
400}
401
402enum AggregateEnum {
403  option (enumopt) = {
404    s: 'EnumAnnotation'
405  };
406
407  VALUE = 1 [(enumvalopt) = { s: 'EnumValueAnnotation' }];
408}
409
410// Test custom options for nested type.
411message NestedOptionType {
412  message NestedMessage {
413    option (message_opt1) = 1001;
414
415    optional int32 nested_field = 1 [(field_opt1) = 1002];
416  }
417  enum NestedEnum {
418    option (enum_opt1) = 1003;
419
420    NESTED_ENUM_VALUE = 1 [(enum_value_opt1) = 1004];
421  }
422  extend google.protobuf.FileOptions {
423    optional int32 nested_extension = 7912573 [(field_opt2) = 1005];
424  }
425}
426
427// Custom message option that has a required enum field.
428// WARNING: this is strongly discouraged!
429message OldOptionType {
430  enum TestEnum { OLD_VALUE = 0; }
431  required TestEnum value = 1;
432}
433
434// Updated version of the custom option above.
435message NewOptionType {
436  enum TestEnum {
437    OLD_VALUE = 0;
438    NEW_VALUE = 1;
439  }
440  required TestEnum value = 1;
441}
442
443extend google.protobuf.MessageOptions {
444  optional OldOptionType required_enum_opt = 106161807;
445}
446
447// Test message using the "required_enum_opt" option defined above.
448message TestMessageWithRequiredEnumOption {
449  option (required_enum_opt) = {
450    value: OLD_VALUE
451  };
452}
453