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 8// Author: benjy@google.com (Benjy Weinberger) 9// Based on original Protocol Buffers design by 10// Sanjay Ghemawat, Jeff Dean, and others. 11// 12// A proto file used to test the "custom options" feature of google.protobuf. 13 14syntax = "proto2"; 15 16// Some generic_services option(s) added automatically. 17// See: http://go/proto2-generic-services-default 18option cc_generic_services = true; // auto-added 19option java_generic_services = true; // auto-added 20option py_generic_services = true; 21 22// A custom file option (defined below). 23option (file_opt1) = 9876543210; 24 25import "google/protobuf/any.proto"; 26import "google/protobuf/descriptor.proto"; 27 28// We don't put this in a package within proto2 because we need to make sure 29// that the generated code doesn't depend on being in the proto2 namespace. 30package protobuf_unittest; 31 32// Some simple test custom options of various types. 33 34extend google.protobuf.FileOptions { 35 optional uint64 file_opt1 = 7736974; 36} 37 38extend google.protobuf.MessageOptions { 39 optional int32 message_opt1 = 7739036; 40} 41 42extend google.protobuf.FieldOptions { 43 optional fixed64 field_opt1 = 7740936; 44 // This is useful for testing that we correctly register default values for 45 // extension options. 46 optional int32 field_opt2 = 7753913 [default = 42]; 47} 48 49extend google.protobuf.OneofOptions { 50 optional int32 oneof_opt1 = 7740111; 51} 52 53extend google.protobuf.EnumOptions { 54 optional sfixed32 enum_opt1 = 7753576; 55} 56 57extend google.protobuf.EnumValueOptions { 58 optional int32 enum_value_opt1 = 1560678; 59} 60 61extend google.protobuf.ServiceOptions { 62 optional sint64 service_opt1 = 7887650; 63} 64 65enum MethodOpt1 { 66 METHODOPT1_VAL1 = 1; 67 METHODOPT1_VAL2 = 2; 68} 69 70extend google.protobuf.MethodOptions { 71 optional MethodOpt1 method_opt1 = 7890860; 72} 73 74// A test message with custom options at all possible locations (and also some 75// regular options, to make sure they interact nicely). 76message TestMessageWithCustomOptions { 77 option message_set_wire_format = false; 78 option (message_opt1) = -56; 79 80 optional string field1 = 1 [ctype = CORD, (field_opt1) = 8765432109]; 81 82 oneof AnOneof { 83 option (oneof_opt1) = -99; 84 85 int32 oneof_field = 2; 86 } 87 88 map<string, string> map_field = 3 [(field_opt1) = 12345]; 89 90 enum AnEnum { 91 option (enum_opt1) = -789; 92 93 ANENUM_VAL1 = 1; 94 ANENUM_VAL2 = 2 [(enum_value_opt1) = 123]; 95 } 96} 97 98// A test RPC service with custom options at all possible locations (and also 99// some regular options, to make sure they interact nicely). 100message CustomOptionFooRequest {} 101 102message CustomOptionFooResponse {} 103 104message CustomOptionFooClientMessage {} 105 106message CustomOptionFooServerMessage {} 107 108service TestServiceWithCustomOptions { 109 option (service_opt1) = -9876543210; 110 111 rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) { 112 option (method_opt1) = METHODOPT1_VAL2; 113 } 114} 115 116// Options of every possible field type, so we can test them all exhaustively. 117 118message DummyMessageContainingEnum { 119 enum TestEnumType { 120 TEST_OPTION_ENUM_TYPE1 = 22; 121 TEST_OPTION_ENUM_TYPE2 = -23; 122 } 123} 124 125message DummyMessageInvalidAsOptionType {} 126 127extend google.protobuf.MessageOptions { 128 optional bool bool_opt = 7706090; 129 optional int32 int32_opt = 7705709; 130 optional int64 int64_opt = 7705542; 131 optional uint32 uint32_opt = 7704880; 132 optional uint64 uint64_opt = 7702367; 133 optional sint32 sint32_opt = 7701568; 134 optional sint64 sint64_opt = 7700863; 135 optional fixed32 fixed32_opt = 7700307; 136 optional fixed64 fixed64_opt = 7700194; 137 optional sfixed32 sfixed32_opt = 7698645; 138 optional sfixed64 sfixed64_opt = 7685475; 139 optional float float_opt = 7675390; 140 optional double double_opt = 7673293; 141 optional string string_opt = 7673285; 142 optional bytes bytes_opt = 7673238; 143 optional DummyMessageContainingEnum.TestEnumType enum_opt = 7673233; 144 optional DummyMessageInvalidAsOptionType message_type_opt = 7665967; 145} 146 147message CustomOptionMinIntegerValues { 148 option (bool_opt) = false; 149 option (int32_opt) = -0x80000000; 150 option (int64_opt) = -0x8000000000000000; 151 option (uint32_opt) = 0; 152 option (uint64_opt) = 0; 153 option (sint32_opt) = -0x80000000; 154 option (sint64_opt) = -0x8000000000000000; 155 option (fixed32_opt) = 0; 156 option (fixed64_opt) = 0; 157 option (sfixed32_opt) = -0x80000000; 158 option (sfixed64_opt) = -0x8000000000000000; 159} 160 161message CustomOptionMaxIntegerValues { 162 option (bool_opt) = true; 163 option (int32_opt) = 0x7FFFFFFF; 164 option (int64_opt) = 0x7FFFFFFFFFFFFFFF; 165 option (uint32_opt) = 0xFFFFFFFF; 166 option (uint64_opt) = 0xFFFFFFFFFFFFFFFF; 167 option (sint32_opt) = 0x7FFFFFFF; 168 option (sint64_opt) = 0x7FFFFFFFFFFFFFFF; 169 option (fixed32_opt) = 0xFFFFFFFF; 170 option (fixed64_opt) = 0xFFFFFFFFFFFFFFFF; 171 option (sfixed32_opt) = 0x7FFFFFFF; 172 option (sfixed64_opt) = 0x7FFFFFFFFFFFFFFF; 173} 174 175message CustomOptionOtherValues { 176 option (int32_opt) = -100; // To test sign-extension. 177 option (float_opt) = 12.3456789; 178 option (double_opt) = 1.234567890123456789; 179 option (string_opt) = "Hello, \"World\""; 180 option (bytes_opt) = "Hello\0World"; 181 option (enum_opt) = TEST_OPTION_ENUM_TYPE2; 182} 183 184message SettingRealsFromPositiveInts { 185 option (float_opt) = 12; 186 option (double_opt) = 154; 187} 188 189message SettingRealsFromNegativeInts { 190 option (float_opt) = -12; 191 option (double_opt) = -154; 192} 193 194message SettingRealsFromInf { 195 option (float_opt) = inf; 196 option (double_opt) = inf; 197} 198 199message SettingRealsFromNegativeInf { 200 option (float_opt) = -inf; 201 option (double_opt) = -inf; 202} 203 204message SettingRealsFromNan { 205 option (float_opt) = nan; 206 option (double_opt) = nan; 207} 208 209message SettingRealsFromNegativeNan { 210 option (float_opt) = -nan; 211 option (double_opt) = -nan; 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 moo = 1; 246 247 optional group ComplexOptionType5 = 2 { 248 optional int32 plugh = 3; 249 } 250} 251 252extend ComplexOptionType1 { 253 optional int32 mooo = 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.mooo) = 324; 275 option (.protobuf_unittest.complex_opt1).(protobuf_unittest.corge).moo = 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.(mooo) = 1999; 282 option (complex_opt2).bar.(protobuf_unittest.corge).moo = 2008; 283 option (complex_opt2).(garply).foo = 741; 284 option (complex_opt2).(garply).(.protobuf_unittest.mooo) = 1998; 285 option (complex_opt2).(protobuf_unittest.garply).(corge).moo = 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).moo = 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 // An any 334 optional google.protobuf.Any any = 6; 335} 336 337// Allow Aggregate to be used as an option at all possible locations 338// in the .proto grammar. 339extend google.protobuf.FileOptions { 340 optional Aggregate fileopt = 15478479; 341} 342extend google.protobuf.MessageOptions { 343 optional Aggregate msgopt = 15480088; 344} 345extend google.protobuf.FieldOptions { 346 optional Aggregate fieldopt = 15481374; 347} 348extend google.protobuf.EnumOptions { 349 optional Aggregate enumopt = 15483218; 350} 351extend google.protobuf.EnumValueOptions { 352 optional Aggregate enumvalopt = 15486921; 353} 354extend google.protobuf.ServiceOptions { 355 optional Aggregate serviceopt = 15497145; 356} 357extend google.protobuf.MethodOptions { 358 optional Aggregate methodopt = 15512713; 359} 360 361// Try using AggregateOption at different points in the proto grammar 362option (fileopt) = { 363 s: 'FileAnnotation' 364 // Also test the handling of comments 365 /* of both types */ 366 i: 100 367 368 sub { s: 'NestedFileAnnotation' } 369 370 // Include a google.protobuf.FileOptions and recursively extend it with 371 // another fileopt. 372 file { 373 [protobuf_unittest.fileopt] { s: 'FileExtensionAnnotation' } 374 } 375 376 // A message set inside an option value 377 mset { 378 [protobuf_unittest.AggregateMessageSetElement.message_set_extension] { 379 s: 'EmbeddedMessageSetElement' 380 } 381 } 382 383 any { 384 [type.googleapis.com/protobuf_unittest.AggregateMessageSetElement] { 385 s: 'EmbeddedMessageSetElement' 386 } 387 } 388}; 389 390message AggregateMessage { 391 option (msgopt) = { 392 i: 101 393 s: 'MessageAnnotation' 394 }; 395 396 optional int32 fieldname = 1 [(fieldopt) = { s: 'FieldAnnotation' }]; 397} 398 399service AggregateService { 400 option (serviceopt) = { 401 s: 'ServiceAnnotation' 402 }; 403 404 rpc Method(AggregateMessage) returns (AggregateMessage) { 405 option (methodopt) = { 406 s: 'MethodAnnotation' 407 }; 408 } 409} 410 411enum AggregateEnum { 412 option (enumopt) = { 413 s: 'EnumAnnotation' 414 }; 415 416 VALUE = 1 [(enumvalopt) = { s: 'EnumValueAnnotation' }]; 417} 418 419// Test custom options for nested type. 420message NestedOptionType { 421 message NestedMessage { 422 option (message_opt1) = 1001; 423 424 optional int32 nested_field = 1 [(field_opt1) = 1002]; 425 } 426 enum NestedEnum { 427 option (enum_opt1) = 1003; 428 429 NESTED_ENUM_VALUE = 1 [(enum_value_opt1) = 1004]; 430 } 431 extend google.protobuf.FileOptions { 432 optional int32 nested_extension = 7912573 [(field_opt2) = 1005]; 433 } 434} 435 436// Custom message option that has a required enum field. 437// WARNING: this is strongly discouraged! 438message OldOptionType { 439 enum TestEnum { 440 OLD_VALUE = 0; 441 } 442 required TestEnum value = 1; 443} 444 445// Updated version of the custom option above. 446message NewOptionType { 447 enum TestEnum { 448 OLD_VALUE = 0; 449 NEW_VALUE = 1; 450 } 451 required TestEnum value = 1; 452} 453 454extend google.protobuf.MessageOptions { 455 optional OldOptionType required_enum_opt = 106161807; 456} 457 458// Test message using the "required_enum_opt" option defined above. 459message TestMessageWithRequiredEnumOption { 460 option (required_enum_opt) = { 461 value: OLD_VALUE 462 }; 463} 464