1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
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 proto2.
36
37
38// Some generic_services option(s) added automatically.
39// See:  http://go/proto2-generic-services-default
40option cc_generic_services = true;     // auto-added
41option java_generic_services = true;   // auto-added
42option py_generic_services = true;
43
44// A custom file option (defined below).
45option (file_opt1) = 9876543210;
46
47import "google/protobuf/descriptor.proto";
48
49// We don't put this in a package within proto2 because we need to make sure
50// that the generated code doesn't depend on being in the proto2 namespace.
51package protobuf_unittest;
52
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.EnumOptions {
72  optional sfixed32 enum_opt1 = 7753576;
73}
74
75extend google.protobuf.EnumValueOptions {
76  optional int32 enum_value_opt1 = 1560678;
77}
78
79extend google.protobuf.ServiceOptions {
80  optional sint64 service_opt1 = 7887650;
81}
82
83enum MethodOpt1 {
84  METHODOPT1_VAL1 = 1;
85  METHODOPT1_VAL2 = 2;
86}
87
88extend google.protobuf.MethodOptions {
89  optional MethodOpt1 method_opt1 = 7890860;
90}
91
92// A test message with custom options at all possible locations (and also some
93// regular options, to make sure they interact nicely).
94message TestMessageWithCustomOptions {
95  option message_set_wire_format = false;
96
97  option (message_opt1) = -56;
98
99  optional string field1 = 1 [ctype=CORD,
100                              (field_opt1)=8765432109];
101
102  enum AnEnum {
103    option (enum_opt1) = -789;
104
105    ANENUM_VAL1 = 1;
106    ANENUM_VAL2 = 2 [(enum_value_opt1) = 123];
107  }
108}
109
110
111// A test RPC service with custom options at all possible locations (and also
112// some regular options, to make sure they interact nicely).
113message CustomOptionFooRequest {
114}
115
116message CustomOptionFooResponse {
117}
118
119message CustomOptionFooClientMessage {
120}
121
122message CustomOptionFooServerMessage {
123}
124
125service TestServiceWithCustomOptions {
126  option (service_opt1) = -9876543210;
127
128  rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) {
129    option (method_opt1) = METHODOPT1_VAL2;
130  }
131}
132
133
134
135// Options of every possible field type, so we can test them all exhaustively.
136
137message DummyMessageContainingEnum {
138  enum TestEnumType {
139    TEST_OPTION_ENUM_TYPE1 = 22;
140    TEST_OPTION_ENUM_TYPE2 = -23;
141  }
142}
143
144message DummyMessageInvalidAsOptionType {
145}
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
222  extensions 100 to max;
223}
224
225message ComplexOptionType2 {
226  optional ComplexOptionType1 bar = 1;
227  optional int32 baz = 2;
228
229  message ComplexOptionType4 {
230    optional int32 waldo = 1;
231
232    extend google.protobuf.MessageOptions {
233      optional ComplexOptionType4 complex_opt4 = 7633546;
234    }
235  }
236
237  optional ComplexOptionType4 fred = 3;
238
239  extensions 100 to max;
240}
241
242message ComplexOptionType3 {
243  optional int32 qux = 1;
244
245  optional group ComplexOptionType5 = 2 {
246    optional int32 plugh = 3;
247  }
248}
249
250extend ComplexOptionType1 {
251  optional int32 quux = 7663707;
252  optional ComplexOptionType3 corge = 7663442;
253}
254
255extend ComplexOptionType2 {
256  optional int32 grault = 7650927;
257  optional ComplexOptionType1 garply = 7649992;
258}
259
260extend google.protobuf.MessageOptions {
261  optional protobuf_unittest.ComplexOptionType1 complex_opt1 = 7646756;
262  optional ComplexOptionType2 complex_opt2 = 7636949;
263  optional ComplexOptionType3 complex_opt3 = 7636463;
264  optional group ComplexOpt6 = 7595468 {
265    optional int32 xyzzy = 7593951;
266  }
267}
268
269// Note that we try various different ways of naming the same extension.
270message VariousComplexOptions {
271  option (.protobuf_unittest.complex_opt1).foo = 42;
272  option (protobuf_unittest.complex_opt1).(.protobuf_unittest.quux) = 324;
273  option (.protobuf_unittest.complex_opt1).(protobuf_unittest.corge).qux = 876;
274  option (complex_opt2).baz = 987;
275  option (complex_opt2).(grault) = 654;
276  option (complex_opt2).bar.foo = 743;
277  option (complex_opt2).bar.(quux) = 1999;
278  option (complex_opt2).bar.(protobuf_unittest.corge).qux = 2008;
279  option (complex_opt2).(garply).foo = 741;
280  option (complex_opt2).(garply).(.protobuf_unittest.quux) = 1998;
281  option (complex_opt2).(protobuf_unittest.garply).(corge).qux = 2121;
282  option (ComplexOptionType2.ComplexOptionType4.complex_opt4).waldo = 1971;
283  option (complex_opt2).fred.waldo = 321;
284  option (protobuf_unittest.complex_opt3).qux = 9;
285  option (complex_opt3).complexoptiontype5.plugh = 22;
286  option (complexopt6).xyzzy = 24;
287}
288
289// ------------------------------------------------------
290// Definitions for testing aggregate option parsing.
291// See descriptor_unittest.cc.
292
293message AggregateMessageSet {
294  option message_set_wire_format = true;
295  extensions 4 to max;
296}
297
298message AggregateMessageSetElement {
299  extend AggregateMessageSet {
300    optional AggregateMessageSetElement message_set_extension = 15447542;
301  }
302  optional string s = 1;
303}
304
305// A helper type used to test aggregate option parsing
306message Aggregate {
307  optional int32 i = 1;
308  optional string s = 2;
309
310  // A nested object
311  optional Aggregate sub = 3;
312
313  // To test the parsing of extensions inside aggregate values
314  optional google.protobuf.FileOptions file = 4;
315  extend google.protobuf.FileOptions {
316    optional Aggregate nested = 15476903;
317  }
318
319  // An embedded message set
320  optional AggregateMessageSet mset = 5;
321}
322
323// Allow Aggregate to be used as an option at all possible locations
324// in the .proto grammer.
325extend google.protobuf.FileOptions      { optional Aggregate fileopt    = 15478479; }
326extend google.protobuf.MessageOptions   { optional Aggregate msgopt     = 15480088; }
327extend google.protobuf.FieldOptions     { optional Aggregate fieldopt   = 15481374; }
328extend google.protobuf.EnumOptions      { optional Aggregate enumopt    = 15483218; }
329extend google.protobuf.EnumValueOptions { optional Aggregate enumvalopt = 15486921; }
330extend google.protobuf.ServiceOptions   { optional Aggregate serviceopt = 15497145; }
331extend google.protobuf.MethodOptions    { optional Aggregate methodopt  = 15512713; }
332
333// Try using AggregateOption at different points in the proto grammar
334option (fileopt) = {
335  s: 'FileAnnotation'
336  // Also test the handling of comments
337  /* of both types */ i: 100
338
339  sub { s: 'NestedFileAnnotation' }
340
341  // Include a google.protobuf.FileOptions and recursively extend it with
342  // another fileopt.
343  file {
344    [protobuf_unittest.fileopt] {
345      s:'FileExtensionAnnotation'
346    }
347  }
348
349  // A message set inside an option value
350  mset {
351    [protobuf_unittest.AggregateMessageSetElement.message_set_extension] {
352      s: 'EmbeddedMessageSetElement'
353    }
354  }
355};
356
357message AggregateMessage {
358  option (msgopt) = { i:101 s:'MessageAnnotation' };
359  optional int32 fieldname = 1 [(fieldopt) = { s:'FieldAnnotation' }];
360}
361
362service AggregateService {
363  option (serviceopt) = { s:'ServiceAnnotation' };
364  rpc Method (AggregateMessage) returns (AggregateMessage) {
365    option (methodopt) = { s:'MethodAnnotation' };
366  }
367}
368
369enum AggregateEnum {
370  option (enumopt) = { s:'EnumAnnotation' };
371  VALUE = 1 [(enumvalopt) = { s:'EnumValueAnnotation' }];
372}
373
374// Test custom options for nested type.
375message NestedOptionType {
376  message NestedMessage {
377    option (message_opt1) = 1001;
378    optional int32 nested_field = 1 [(field_opt1) = 1002];
379  }
380  enum NestedEnum {
381    option (enum_opt1) = 1003;
382    NESTED_ENUM_VALUE = 1 [(enum_value_opt1) = 1004];
383  }
384  extend google.protobuf.FileOptions {
385    optional int32 nested_extension = 7912573 [(field_opt2) = 1005];
386  }
387}
388