unittest_custom_options_proto3.proto 10 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: benjy@google.com (Benjy Weinberger)
//  Based on original Protocol Buffers design by
//  Sanjay Ghemawat, Jeff Dean, and others.
//
// A proto file used to test the "custom options" feature of google.protobuf.

// This file is based on unittest_custom_options.proto in
// src/google/protobuf, but is modified for proto3. It could
// potentially be moved into src/google/protobuf, but currently C#
// is the only language that really needs it, as we don't support
// proto2 syntax. It's cut down significantly as proto3 only supports
// extensions for options.


syntax = "proto3";

// A custom file option (defined below).
option (file_opt1) = 9876543210;

import "google/protobuf/descriptor.proto";

// We don't put this in a package within proto2 because we need to make sure
// that the generated code doesn't depend on being in the proto2 namespace.
package protobuf_unittest;
option csharp_namespace = "UnitTest.Issues.TestProtos";

// Some simple test custom options of various types.

extend google.protobuf.FileOptions {
  uint64 file_opt1 = 7736974;
}

extend google.protobuf.MessageOptions {
  int32 message_opt1 = 7739036;
}

extend google.protobuf.FieldOptions {
  fixed64 field_opt1 = 7740936;
}

71 72 73
extend google.protobuf.OneofOptions {
  int32 oneof_opt1 = 7740111;
}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

extend google.protobuf.EnumOptions {
  sfixed32 enum_opt1 = 7753576;
}

extend google.protobuf.EnumValueOptions {
  int32 enum_value_opt1 = 1560678;
}

extend google.protobuf.ServiceOptions {
  sint64 service_opt1 = 7887650;
}

enum MethodOpt1 {
  METHODOPT1_UNSPECIFIED = 0;
  METHODOPT1_VAL1 = 1;
  METHODOPT1_VAL2 = 2;
}

extend google.protobuf.MethodOptions {
  MethodOpt1 method_opt1 = 7890860;
}

// A test message with custom options at all possible locations (and also some
// regular options, to make sure they interact nicely).
message TestMessageWithCustomOptions {
  option message_set_wire_format = false;

  option (message_opt1) = -56;

  string field1 = 1 [ctype=CORD,
                              (field_opt1)=8765432109];

  oneof AnOneof {
108
    option (oneof_opt1) = -99;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    int32 oneof_field = 2;
  }

  enum AnEnum {
    option (enum_opt1) = -789;
    ANENUM_UNSPECIFIED = 0;
    ANENUM_VAL1 = 1;
    ANENUM_VAL2 = 2 [(enum_value_opt1) = 123];
  }
}


// A test RPC service with custom options at all possible locations (and also
// some regular options, to make sure they interact nicely).
message CustomOptionFooRequest {
}

message CustomOptionFooResponse {
}

message CustomOptionFooClientMessage {
}

message CustomOptionFooServerMessage {
}

service TestServiceWithCustomOptions {
  option (service_opt1) = -9876543210;

  rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) {
    option (method_opt1) = METHODOPT1_VAL2;
  }
}



// Options of every possible field type, so we can test them all exhaustively.

message DummyMessageContainingEnum {
  enum TestEnumType {
    TEST_OPTION_ENUM_UNSPECIFIED = 0;
    TEST_OPTION_ENUM_TYPE1 = 22;
    TEST_OPTION_ENUM_TYPE2 = -23;
  }
}

message DummyMessageInvalidAsOptionType {
}

extend google.protobuf.MessageOptions {
          bool     bool_opt = 7706090;
         int32    int32_opt = 7705709;
         int64    int64_opt = 7705542;
        uint32   uint32_opt = 7704880;
        uint64   uint64_opt = 7702367;
        sint32   sint32_opt = 7701568;
        sint64   sint64_opt = 7700863;
       fixed32  fixed32_opt = 7700307;
       fixed64  fixed64_opt = 7700194;
      sfixed32 sfixed32_opt = 7698645;
      sfixed64 sfixed64_opt = 7685475;
         float    float_opt = 7675390;
        double   double_opt = 7673293;
        string   string_opt = 7673285;
         bytes    bytes_opt = 7673238;
  DummyMessageContainingEnum.TestEnumType enum_opt = 7673233;
  DummyMessageInvalidAsOptionType message_type_opt = 7665967;
}

message CustomOptionMinIntegerValues {
  option     (bool_opt) = false;
  option    (int32_opt) = -0x80000000;
  option    (int64_opt) = -0x8000000000000000;
  option   (uint32_opt) = 0;
  option   (uint64_opt) = 0;
  option   (sint32_opt) = -0x80000000;
  option   (sint64_opt) = -0x8000000000000000;
  option  (fixed32_opt) = 0;
  option  (fixed64_opt) = 0;
  option (sfixed32_opt) = -0x80000000;
  option (sfixed64_opt) = -0x8000000000000000;
}

message CustomOptionMaxIntegerValues {
  option     (bool_opt) = true;
  option    (int32_opt) = 0x7FFFFFFF;
  option    (int64_opt) = 0x7FFFFFFFFFFFFFFF;
  option   (uint32_opt) = 0xFFFFFFFF;
  option   (uint64_opt) = 0xFFFFFFFFFFFFFFFF;
  option   (sint32_opt) = 0x7FFFFFFF;
  option   (sint64_opt) = 0x7FFFFFFFFFFFFFFF;
  option  (fixed32_opt) = 0xFFFFFFFF;
  option  (fixed64_opt) = 0xFFFFFFFFFFFFFFFF;
  option (sfixed32_opt) = 0x7FFFFFFF;
  option (sfixed64_opt) = 0x7FFFFFFFFFFFFFFF;
}

message CustomOptionOtherValues {
  option  (int32_opt) = -100;  // To test sign-extension.
  option  (float_opt) = 12.3456789;
  option (double_opt) = 1.234567890123456789;
  option (string_opt) = "Hello, \"World\"";
  option  (bytes_opt) = "Hello\0World";
  option   (enum_opt) = TEST_OPTION_ENUM_TYPE2;
}

message SettingRealsFromPositiveInts {
  option  (float_opt) = 12;
  option (double_opt) = 154;
}

message SettingRealsFromNegativeInts {
  option  (float_opt) = -12;
  option  (double_opt) = -154;
}

// Options of complex message types, themselves combined and extended in
// various ways.

message ComplexOptionType1 {
  int32 foo = 1;
  int32 foo2 = 2;
  int32 foo3 = 3;
  repeated int32 foo4 = 4;
}

message ComplexOptionType2 {
  ComplexOptionType1 bar = 1;
  int32 baz = 2;

  message ComplexOptionType4 {
    int32 waldo = 1;

    extend google.protobuf.MessageOptions {
      ComplexOptionType4 complex_opt4 = 7633546;
    }
  }

  ComplexOptionType4 fred = 3;
  repeated ComplexOptionType4 barney = 4;
}

message ComplexOptionType3 {
  int32 qux = 1;
}

extend google.protobuf.MessageOptions {
  protobuf_unittest.ComplexOptionType1 complex_opt1 = 7646756;
  ComplexOptionType2 complex_opt2 = 7636949;
  ComplexOptionType3 complex_opt3 = 7636463;
}

// Note that we try various different ways of naming the same extension.
message VariousComplexOptions {
  option (.protobuf_unittest.complex_opt1).foo = 42;
  option (protobuf_unittest.complex_opt1).foo4 = 99;
  option (protobuf_unittest.complex_opt1).foo4 = 88;
  option (complex_opt2).baz = 987;
  option (complex_opt2).bar.foo = 743;
  option (ComplexOptionType2.ComplexOptionType4.complex_opt4).waldo = 1971;
  option (complex_opt2).fred.waldo = 321;
  option (complex_opt2).barney = { waldo: 101 };
  option (complex_opt2).barney = { waldo: 212 };
  option (protobuf_unittest.complex_opt3).qux = 9;
}

// ------------------------------------------------------
// Definitions for testing aggregate option parsing.
// See descriptor_unittest.cc.

// A helper type used to test aggregate option parsing
message Aggregate {
  int32 i = 1;
  string s = 2;

  // A nested object
  Aggregate sub = 3;
}

// Allow Aggregate to be used as an option at all possible locations
// in the .proto grammer.
extend google.protobuf.FileOptions      { Aggregate fileopt    = 15478479; }
extend google.protobuf.MessageOptions   { Aggregate msgopt     = 15480088; }
extend google.protobuf.FieldOptions     { Aggregate fieldopt   = 15481374; }
extend google.protobuf.EnumOptions      { Aggregate enumopt    = 15483218; }
extend google.protobuf.EnumValueOptions { Aggregate enumvalopt = 15486921; }
extend google.protobuf.ServiceOptions   { Aggregate serviceopt = 15497145; }
extend google.protobuf.MethodOptions    { Aggregate methodopt  = 15512713; }

// Try using AggregateOption at different points in the proto grammar
option (fileopt) = {
  s: 'FileAnnotation'
  // Also test the handling of comments
  /* of both types */ i: 100

  sub { s: 'NestedFileAnnotation' }
};

message AggregateMessage {
  option (msgopt) = { i:101 s:'MessageAnnotation' };
  int32 fieldname = 1 [(fieldopt) = { s:'FieldAnnotation' }];
}

service AggregateService {
  option (serviceopt) = { s:'ServiceAnnotation' };
  rpc Method (AggregateMessage) returns (AggregateMessage) {
    option (methodopt) = { s:'MethodAnnotation' };
  }
}

enum AggregateEnum {
  option (enumopt) = { s:'EnumAnnotation' };
  UNSPECIFIED = 0;
  VALUE = 1 [(enumvalopt) = { s:'EnumValueAnnotation' }];
}

// Test custom options for nested type.
message NestedOptionType {
  message NestedMessage {
    option (message_opt1) = 1001;
    int32 nested_field = 1 [(field_opt1) = 1002];
  }
  enum NestedEnum {
    UNSPECIFIED = 0;
    option (enum_opt1) = 1003;
    NESTED_ENUM_VALUE = 1 [(enum_value_opt1) = 1004];
  }
}