Home | History | Annotate | Download | only in protobuf
      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 (a] 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 // A custom file option (defined below).
     39 option (file_opt1) = 9876543210;
     40 
     41 import "google/protobuf/descriptor.proto";
     42 
     43 // We don't put this in a package within proto2 because we need to make sure
     44 // that the generated code doesn't depend on being in the proto2 namespace.
     45 package protobuf_unittest;
     46 
     47 
     48 // Some simple test custom options of various types.
     49 
     50 extend google.protobuf.FileOptions {
     51   optional uint64 file_opt1 = 7736974;
     52 }
     53 
     54 extend google.protobuf.MessageOptions {
     55   optional int32 message_opt1 = 7739036;
     56 }
     57 
     58 extend google.protobuf.FieldOptions {
     59   optional fixed64 field_opt1 = 7740936;
     60   // This is useful for testing that we correctly register default values for
     61   // extension options.
     62   optional int32 field_opt2 = 7753913 [default=42];
     63 }
     64 
     65 extend google.protobuf.EnumOptions {
     66   optional sfixed32 enum_opt1 = 7753576;
     67 }
     68 
     69 extend google.protobuf.EnumValueOptions {
     70   optional int32 enum_value_opt1 = 1560678;
     71 }
     72 
     73 extend google.protobuf.ServiceOptions {
     74   optional sint64 service_opt1 = 7887650;
     75 }
     76 
     77 enum MethodOpt1 {
     78   METHODOPT1_VAL1 = 1;
     79   METHODOPT1_VAL2 = 2;
     80 }
     81 
     82 extend google.protobuf.MethodOptions {
     83   optional MethodOpt1 method_opt1 = 7890860;
     84 }
     85 
     86 // A test message with custom options at all possible locations (and also some
     87 // regular options, to make sure they interact nicely).
     88 message TestMessageWithCustomOptions {
     89   option message_set_wire_format = false;
     90 
     91   option (message_opt1) = -56;
     92 
     93   optional string field1 = 1 [ctype=CORD,
     94                               (field_opt1)=8765432109];
     95 
     96   enum AnEnum {
     97     option (enum_opt1) = -789;
     98 
     99     ANENUM_VAL1 = 1;
    100     ANENUM_VAL2 = 2 [(enum_value_opt1) = 123];
    101   }
    102 }
    103 
    104 
    105 // A test RPC service with custom options at all possible locations (and also
    106 // some regular options, to make sure they interact nicely).
    107 message CustomOptionFooRequest {
    108 }
    109 
    110 message CustomOptionFooResponse {
    111 }
    112 
    113 service TestServiceWithCustomOptions {
    114   option (service_opt1) = -9876543210;
    115 
    116   rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) {
    117     option (method_opt1) = METHODOPT1_VAL2;
    118   }
    119 }
    120 
    121 
    122 
    123 // Options of every possible field type, so we can test them all exhaustively.
    124 
    125 message DummyMessageContainingEnum {
    126   enum TestEnumType {
    127     TEST_OPTION_ENUM_TYPE1 = 22;
    128     TEST_OPTION_ENUM_TYPE2 = -23;
    129   }
    130 }
    131 
    132 message DummyMessageInvalidAsOptionType {
    133 }
    134 
    135 extend google.protobuf.MessageOptions {
    136   optional         bool     bool_opt = 7706090;
    137   optional        int32    int32_opt = 7705709;
    138   optional        int64    int64_opt = 7705542;
    139   optional       uint32   uint32_opt = 7704880;
    140   optional       uint64   uint64_opt = 7702367;
    141   optional       sint32   sint32_opt = 7701568;
    142   optional       sint64   sint64_opt = 7700863;
    143   optional      fixed32  fixed32_opt = 7700307;
    144   optional      fixed64  fixed64_opt = 7700194;
    145   optional     sfixed32 sfixed32_opt = 7698645;
    146   optional     sfixed64 sfixed64_opt = 7685475;
    147   optional        float    float_opt = 7675390;
    148   optional       double   double_opt = 7673293;
    149   optional       string   string_opt = 7673285;
    150   optional        bytes    bytes_opt = 7673238;
    151   optional DummyMessageContainingEnum.TestEnumType enum_opt = 7673233;
    152   optional DummyMessageInvalidAsOptionType message_type_opt = 7665967;
    153 }
    154 
    155 message CustomOptionMinIntegerValues {
    156   option     (bool_opt) = false;
    157   option    (int32_opt) = -0x80000000;
    158   option    (int64_opt) = -0x8000000000000000;
    159   option   (uint32_opt) = 0;
    160   option   (uint64_opt) = 0;
    161   option   (sint32_opt) = -0x80000000;
    162   option   (sint64_opt) = -0x8000000000000000;
    163   option  (fixed32_opt) = 0;
    164   option  (fixed64_opt) = 0;
    165   option (sfixed32_opt) = -0x80000000;
    166   option (sfixed64_opt) = -0x8000000000000000;
    167 }
    168 
    169 message CustomOptionMaxIntegerValues {
    170   option     (bool_opt) = true;
    171   option    (int32_opt) = 0x7FFFFFFF;
    172   option    (int64_opt) = 0x7FFFFFFFFFFFFFFF;
    173   option   (uint32_opt) = 0xFFFFFFFF;
    174   option   (uint64_opt) = 0xFFFFFFFFFFFFFFFF;
    175   option   (sint32_opt) = 0x7FFFFFFF;
    176   option   (sint64_opt) = 0x7FFFFFFFFFFFFFFF;
    177   option  (fixed32_opt) = 0xFFFFFFFF;
    178   option  (fixed64_opt) = 0xFFFFFFFFFFFFFFFF;
    179   option (sfixed32_opt) = 0x7FFFFFFF;
    180   option (sfixed64_opt) = 0x7FFFFFFFFFFFFFFF;
    181 }
    182 
    183 message CustomOptionOtherValues {
    184   option  (int32_opt) = -100;  // To test sign-extension.
    185   option  (float_opt) = 12.3456789;
    186   option (double_opt) = 1.234567890123456789;
    187   option (string_opt) = "Hello, \"World\"";
    188   option  (bytes_opt) = "Hello\0World";
    189   option   (enum_opt) = TEST_OPTION_ENUM_TYPE2;
    190 }
    191 
    192 message SettingRealsFromPositiveInts {
    193   option  (float_opt) = 12;
    194   option (double_opt) = 154;
    195 }
    196 
    197 message SettingRealsFromNegativeInts {
    198   option  (float_opt) = -12;
    199   option  (double_opt) = -154;
    200 }
    201 
    202 // Options of complex message types, themselves combined and extended in
    203 // various ways.
    204 
    205 message ComplexOptionType1 {
    206   optional int32 foo = 1;
    207   optional int32 foo2 = 2;
    208   optional int32 foo3 = 3;
    209 
    210   extensions 100 to max;
    211 }
    212 
    213 message ComplexOptionType2 {
    214   optional ComplexOptionType1 bar = 1;
    215   optional int32 baz = 2;
    216 
    217   message ComplexOptionType4 {
    218     optional int32 waldo = 1;
    219 
    220     extend google.protobuf.MessageOptions {
    221       optional ComplexOptionType4 complex_opt4 = 7633546;
    222     }
    223   }
    224 
    225   optional ComplexOptionType4 fred = 3;
    226 
    227   extensions 100 to max;
    228 }
    229 
    230 message ComplexOptionType3 {
    231   optional int32 qux = 1;
    232 
    233   optional group ComplexOptionType5 = 2 {
    234     optional int32 plugh = 3;
    235   }
    236 }
    237 
    238 extend ComplexOptionType1 {
    239   optional int32 quux = 7663707;
    240   optional ComplexOptionType3 corge = 7663442;
    241 }
    242 
    243 extend ComplexOptionType2 {
    244   optional int32 grault = 7650927;
    245   optional ComplexOptionType1 garply = 7649992;
    246 }
    247 
    248 extend google.protobuf.MessageOptions {
    249   optional protobuf_unittest.ComplexOptionType1 complex_opt1 = 7646756;
    250   optional ComplexOptionType2 complex_opt2 = 7636949;
    251   optional ComplexOptionType3 complex_opt3 = 7636463;
    252   optional group ComplexOpt6 = 7595468 {
    253     optional int32 xyzzy = 7593951;
    254   }
    255 }
    256 
    257 // Note that we try various different ways of naming the same extension.
    258 message VariousComplexOptions {
    259   option (.protobuf_unittest.complex_opt1).foo = 42;
    260   option (protobuf_unittest.complex_opt1).(.protobuf_unittest.quux) = 324;
    261   option (.protobuf_unittest.complex_opt1).(protobuf_unittest.corge).qux = 876;
    262   option (complex_opt2).baz = 987;
    263   option (complex_opt2).(grault) = 654;
    264   option (complex_opt2).bar.foo = 743;
    265   option (complex_opt2).bar.(quux) = 1999;
    266   option (complex_opt2).bar.(protobuf_unittest.corge).qux = 2008;
    267   option (complex_opt2).(garply).foo = 741;
    268   option (complex_opt2).(garply).(.protobuf_unittest.quux) = 1998;
    269   option (complex_opt2).(protobuf_unittest.garply).(corge).qux = 2121;
    270   option (ComplexOptionType2.ComplexOptionType4.complex_opt4).waldo = 1971;
    271   option (complex_opt2).fred.waldo = 321;
    272   option (protobuf_unittest.complex_opt3).qux = 9;
    273   option (complex_opt3).complexoptiontype5.plugh = 22;
    274   option (complexopt6).xyzzy = 24;
    275 }
    276