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