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 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 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 = { waldo: 101 }; 289 option (complex_opt2).barney = { waldo: 212 }; 290 option (protobuf_unittest.complex_opt3).qux = 9; 291 option (complex_opt3).complexoptiontype5.plugh = 22; 292 option (complexopt6).xyzzy = 24; 293} 294 295// ------------------------------------------------------ 296// Definitions for testing aggregate option parsing. 297// See descriptor_unittest.cc. 298 299message AggregateMessageSet { 300 option message_set_wire_format = true; 301 extensions 4 to max; 302} 303 304message AggregateMessageSetElement { 305 extend AggregateMessageSet { 306 optional AggregateMessageSetElement message_set_extension = 15447542; 307 } 308 optional string s = 1; 309} 310 311// A helper type used to test aggregate option parsing 312message Aggregate { 313 optional int32 i = 1; 314 optional string s = 2; 315 316 // A nested object 317 optional Aggregate sub = 3; 318 319 // To test the parsing of extensions inside aggregate values 320 optional google.protobuf.FileOptions file = 4; 321 extend google.protobuf.FileOptions { 322 optional Aggregate nested = 15476903; 323 } 324 325 // An embedded message set 326 optional AggregateMessageSet mset = 5; 327} 328 329// Allow Aggregate to be used as an option at all possible locations 330// in the .proto grammer. 331extend google.protobuf.FileOptions { optional Aggregate fileopt = 15478479; } 332extend google.protobuf.MessageOptions { optional Aggregate msgopt = 15480088; } 333extend google.protobuf.FieldOptions { optional Aggregate fieldopt = 15481374; } 334extend google.protobuf.EnumOptions { optional Aggregate enumopt = 15483218; } 335extend google.protobuf.EnumValueOptions { optional Aggregate enumvalopt = 15486921; } 336extend google.protobuf.ServiceOptions { optional Aggregate serviceopt = 15497145; } 337extend google.protobuf.MethodOptions { optional Aggregate methodopt = 15512713; } 338 339// Try using AggregateOption at different points in the proto grammar 340option (fileopt) = { 341 s: 'FileAnnotation' 342 // Also test the handling of comments 343 /* of both types */ i: 100 344 345 sub { s: 'NestedFileAnnotation' } 346 347 // Include a google.protobuf.FileOptions and recursively extend it with 348 // another fileopt. 349 file { 350 [protobuf_unittest.fileopt] { 351 s:'FileExtensionAnnotation' 352 } 353 } 354 355 // A message set inside an option value 356 mset { 357 [protobuf_unittest.AggregateMessageSetElement.message_set_extension] { 358 s: 'EmbeddedMessageSetElement' 359 } 360 } 361}; 362 363message AggregateMessage { 364 option (msgopt) = { i:101 s:'MessageAnnotation' }; 365 optional int32 fieldname = 1 [(fieldopt) = { s:'FieldAnnotation' }]; 366} 367 368service AggregateService { 369 option (serviceopt) = { s:'ServiceAnnotation' }; 370 rpc Method (AggregateMessage) returns (AggregateMessage) { 371 option (methodopt) = { s:'MethodAnnotation' }; 372 } 373} 374 375enum AggregateEnum { 376 option (enumopt) = { s:'EnumAnnotation' }; 377 VALUE = 1 [(enumvalopt) = { s:'EnumValueAnnotation' }]; 378} 379 380// Test custom options for nested type. 381message NestedOptionType { 382 message NestedMessage { 383 option (message_opt1) = 1001; 384 optional int32 nested_field = 1 [(field_opt1) = 1002]; 385 } 386 enum NestedEnum { 387 option (enum_opt1) = 1003; 388 NESTED_ENUM_VALUE = 1 [(enum_value_opt1) = 1004]; 389 } 390 extend google.protobuf.FileOptions { 391 optional int32 nested_extension = 7912573 [(field_opt2) = 1005]; 392 } 393} 394