Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | // Protocol Buffers for Go with Gadgets |
| 2 | // |
| 3 | // Copyright (c) 2013, The GoGo Authors. All rights reserved. |
| 4 | // http://github.com/gogo/protobuf |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | package gogoproto |
| 30 | |
| 31 | import google_protobuf "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" |
| 32 | import proto "github.com/gogo/protobuf/proto" |
| 33 | |
| 34 | func IsEmbed(field *google_protobuf.FieldDescriptorProto) bool { |
| 35 | return proto.GetBoolExtension(field.Options, E_Embed, false) |
| 36 | } |
| 37 | |
| 38 | func IsNullable(field *google_protobuf.FieldDescriptorProto) bool { |
| 39 | return proto.GetBoolExtension(field.Options, E_Nullable, true) |
| 40 | } |
| 41 | |
| 42 | func IsStdTime(field *google_protobuf.FieldDescriptorProto) bool { |
| 43 | return proto.GetBoolExtension(field.Options, E_Stdtime, false) |
| 44 | } |
| 45 | |
| 46 | func IsStdDuration(field *google_protobuf.FieldDescriptorProto) bool { |
| 47 | return proto.GetBoolExtension(field.Options, E_Stdduration, false) |
| 48 | } |
| 49 | |
| 50 | func NeedsNilCheck(proto3 bool, field *google_protobuf.FieldDescriptorProto) bool { |
| 51 | nullable := IsNullable(field) |
| 52 | if field.IsMessage() || IsCustomType(field) { |
| 53 | return nullable |
| 54 | } |
| 55 | if proto3 { |
| 56 | return false |
| 57 | } |
| 58 | return nullable || *field.Type == google_protobuf.FieldDescriptorProto_TYPE_BYTES |
| 59 | } |
| 60 | |
| 61 | func IsCustomType(field *google_protobuf.FieldDescriptorProto) bool { |
| 62 | typ := GetCustomType(field) |
| 63 | if len(typ) > 0 { |
| 64 | return true |
| 65 | } |
| 66 | return false |
| 67 | } |
| 68 | |
| 69 | func IsCastType(field *google_protobuf.FieldDescriptorProto) bool { |
| 70 | typ := GetCastType(field) |
| 71 | if len(typ) > 0 { |
| 72 | return true |
| 73 | } |
| 74 | return false |
| 75 | } |
| 76 | |
| 77 | func IsCastKey(field *google_protobuf.FieldDescriptorProto) bool { |
| 78 | typ := GetCastKey(field) |
| 79 | if len(typ) > 0 { |
| 80 | return true |
| 81 | } |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | func IsCastValue(field *google_protobuf.FieldDescriptorProto) bool { |
| 86 | typ := GetCastValue(field) |
| 87 | if len(typ) > 0 { |
| 88 | return true |
| 89 | } |
| 90 | return false |
| 91 | } |
| 92 | |
| 93 | func HasEnumDecl(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { |
| 94 | return proto.GetBoolExtension(enum.Options, E_Enumdecl, proto.GetBoolExtension(file.Options, E_EnumdeclAll, true)) |
| 95 | } |
| 96 | |
| 97 | func HasTypeDecl(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 98 | return proto.GetBoolExtension(message.Options, E_Typedecl, proto.GetBoolExtension(file.Options, E_TypedeclAll, true)) |
| 99 | } |
| 100 | |
| 101 | func GetCustomType(field *google_protobuf.FieldDescriptorProto) string { |
| 102 | if field == nil { |
| 103 | return "" |
| 104 | } |
| 105 | if field.Options != nil { |
| 106 | v, err := proto.GetExtension(field.Options, E_Customtype) |
| 107 | if err == nil && v.(*string) != nil { |
| 108 | return *(v.(*string)) |
| 109 | } |
| 110 | } |
| 111 | return "" |
| 112 | } |
| 113 | |
| 114 | func GetCastType(field *google_protobuf.FieldDescriptorProto) string { |
| 115 | if field == nil { |
| 116 | return "" |
| 117 | } |
| 118 | if field.Options != nil { |
| 119 | v, err := proto.GetExtension(field.Options, E_Casttype) |
| 120 | if err == nil && v.(*string) != nil { |
| 121 | return *(v.(*string)) |
| 122 | } |
| 123 | } |
| 124 | return "" |
| 125 | } |
| 126 | |
| 127 | func GetCastKey(field *google_protobuf.FieldDescriptorProto) string { |
| 128 | if field == nil { |
| 129 | return "" |
| 130 | } |
| 131 | if field.Options != nil { |
| 132 | v, err := proto.GetExtension(field.Options, E_Castkey) |
| 133 | if err == nil && v.(*string) != nil { |
| 134 | return *(v.(*string)) |
| 135 | } |
| 136 | } |
| 137 | return "" |
| 138 | } |
| 139 | |
| 140 | func GetCastValue(field *google_protobuf.FieldDescriptorProto) string { |
| 141 | if field == nil { |
| 142 | return "" |
| 143 | } |
| 144 | if field.Options != nil { |
| 145 | v, err := proto.GetExtension(field.Options, E_Castvalue) |
| 146 | if err == nil && v.(*string) != nil { |
| 147 | return *(v.(*string)) |
| 148 | } |
| 149 | } |
| 150 | return "" |
| 151 | } |
| 152 | |
| 153 | func IsCustomName(field *google_protobuf.FieldDescriptorProto) bool { |
| 154 | name := GetCustomName(field) |
| 155 | if len(name) > 0 { |
| 156 | return true |
| 157 | } |
| 158 | return false |
| 159 | } |
| 160 | |
| 161 | func IsEnumCustomName(field *google_protobuf.EnumDescriptorProto) bool { |
| 162 | name := GetEnumCustomName(field) |
| 163 | if len(name) > 0 { |
| 164 | return true |
| 165 | } |
| 166 | return false |
| 167 | } |
| 168 | |
| 169 | func IsEnumValueCustomName(field *google_protobuf.EnumValueDescriptorProto) bool { |
| 170 | name := GetEnumValueCustomName(field) |
| 171 | if len(name) > 0 { |
| 172 | return true |
| 173 | } |
| 174 | return false |
| 175 | } |
| 176 | |
| 177 | func GetCustomName(field *google_protobuf.FieldDescriptorProto) string { |
| 178 | if field == nil { |
| 179 | return "" |
| 180 | } |
| 181 | if field.Options != nil { |
| 182 | v, err := proto.GetExtension(field.Options, E_Customname) |
| 183 | if err == nil && v.(*string) != nil { |
| 184 | return *(v.(*string)) |
| 185 | } |
| 186 | } |
| 187 | return "" |
| 188 | } |
| 189 | |
| 190 | func GetEnumCustomName(field *google_protobuf.EnumDescriptorProto) string { |
| 191 | if field == nil { |
| 192 | return "" |
| 193 | } |
| 194 | if field.Options != nil { |
| 195 | v, err := proto.GetExtension(field.Options, E_EnumCustomname) |
| 196 | if err == nil && v.(*string) != nil { |
| 197 | return *(v.(*string)) |
| 198 | } |
| 199 | } |
| 200 | return "" |
| 201 | } |
| 202 | |
| 203 | func GetEnumValueCustomName(field *google_protobuf.EnumValueDescriptorProto) string { |
| 204 | if field == nil { |
| 205 | return "" |
| 206 | } |
| 207 | if field.Options != nil { |
| 208 | v, err := proto.GetExtension(field.Options, E_EnumvalueCustomname) |
| 209 | if err == nil && v.(*string) != nil { |
| 210 | return *(v.(*string)) |
| 211 | } |
| 212 | } |
| 213 | return "" |
| 214 | } |
| 215 | |
| 216 | func GetJsonTag(field *google_protobuf.FieldDescriptorProto) *string { |
| 217 | if field == nil { |
| 218 | return nil |
| 219 | } |
| 220 | if field.Options != nil { |
| 221 | v, err := proto.GetExtension(field.Options, E_Jsontag) |
| 222 | if err == nil && v.(*string) != nil { |
| 223 | return (v.(*string)) |
| 224 | } |
| 225 | } |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | func GetMoreTags(field *google_protobuf.FieldDescriptorProto) *string { |
| 230 | if field == nil { |
| 231 | return nil |
| 232 | } |
| 233 | if field.Options != nil { |
| 234 | v, err := proto.GetExtension(field.Options, E_Moretags) |
| 235 | if err == nil && v.(*string) != nil { |
| 236 | return (v.(*string)) |
| 237 | } |
| 238 | } |
| 239 | return nil |
| 240 | } |
| 241 | |
| 242 | type EnableFunc func(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool |
| 243 | |
| 244 | func EnabledGoEnumPrefix(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { |
| 245 | return proto.GetBoolExtension(enum.Options, E_GoprotoEnumPrefix, proto.GetBoolExtension(file.Options, E_GoprotoEnumPrefixAll, true)) |
| 246 | } |
| 247 | |
| 248 | func EnabledGoStringer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 249 | return proto.GetBoolExtension(message.Options, E_GoprotoStringer, proto.GetBoolExtension(file.Options, E_GoprotoStringerAll, true)) |
| 250 | } |
| 251 | |
| 252 | func HasGoGetters(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 253 | return proto.GetBoolExtension(message.Options, E_GoprotoGetters, proto.GetBoolExtension(file.Options, E_GoprotoGettersAll, true)) |
| 254 | } |
| 255 | |
| 256 | func IsUnion(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 257 | return proto.GetBoolExtension(message.Options, E_Onlyone, proto.GetBoolExtension(file.Options, E_OnlyoneAll, false)) |
| 258 | } |
| 259 | |
| 260 | func HasGoString(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 261 | return proto.GetBoolExtension(message.Options, E_Gostring, proto.GetBoolExtension(file.Options, E_GostringAll, false)) |
| 262 | } |
| 263 | |
| 264 | func HasEqual(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 265 | return proto.GetBoolExtension(message.Options, E_Equal, proto.GetBoolExtension(file.Options, E_EqualAll, false)) |
| 266 | } |
| 267 | |
| 268 | func HasVerboseEqual(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 269 | return proto.GetBoolExtension(message.Options, E_VerboseEqual, proto.GetBoolExtension(file.Options, E_VerboseEqualAll, false)) |
| 270 | } |
| 271 | |
| 272 | func IsStringer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 273 | return proto.GetBoolExtension(message.Options, E_Stringer, proto.GetBoolExtension(file.Options, E_StringerAll, false)) |
| 274 | } |
| 275 | |
| 276 | func IsFace(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 277 | return proto.GetBoolExtension(message.Options, E_Face, proto.GetBoolExtension(file.Options, E_FaceAll, false)) |
| 278 | } |
| 279 | |
| 280 | func HasDescription(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 281 | return proto.GetBoolExtension(message.Options, E_Description, proto.GetBoolExtension(file.Options, E_DescriptionAll, false)) |
| 282 | } |
| 283 | |
| 284 | func HasPopulate(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 285 | return proto.GetBoolExtension(message.Options, E_Populate, proto.GetBoolExtension(file.Options, E_PopulateAll, false)) |
| 286 | } |
| 287 | |
| 288 | func HasTestGen(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 289 | return proto.GetBoolExtension(message.Options, E_Testgen, proto.GetBoolExtension(file.Options, E_TestgenAll, false)) |
| 290 | } |
| 291 | |
| 292 | func HasBenchGen(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 293 | return proto.GetBoolExtension(message.Options, E_Benchgen, proto.GetBoolExtension(file.Options, E_BenchgenAll, false)) |
| 294 | } |
| 295 | |
| 296 | func IsMarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 297 | return proto.GetBoolExtension(message.Options, E_Marshaler, proto.GetBoolExtension(file.Options, E_MarshalerAll, false)) |
| 298 | } |
| 299 | |
| 300 | func IsUnmarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 301 | return proto.GetBoolExtension(message.Options, E_Unmarshaler, proto.GetBoolExtension(file.Options, E_UnmarshalerAll, false)) |
| 302 | } |
| 303 | |
| 304 | func IsStableMarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 305 | return proto.GetBoolExtension(message.Options, E_StableMarshaler, proto.GetBoolExtension(file.Options, E_StableMarshalerAll, false)) |
| 306 | } |
| 307 | |
| 308 | func IsSizer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 309 | return proto.GetBoolExtension(message.Options, E_Sizer, proto.GetBoolExtension(file.Options, E_SizerAll, false)) |
| 310 | } |
| 311 | |
| 312 | func IsProtoSizer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 313 | return proto.GetBoolExtension(message.Options, E_Protosizer, proto.GetBoolExtension(file.Options, E_ProtosizerAll, false)) |
| 314 | } |
| 315 | |
| 316 | func IsGoEnumStringer(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { |
| 317 | return proto.GetBoolExtension(enum.Options, E_GoprotoEnumStringer, proto.GetBoolExtension(file.Options, E_GoprotoEnumStringerAll, true)) |
| 318 | } |
| 319 | |
| 320 | func IsEnumStringer(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { |
| 321 | return proto.GetBoolExtension(enum.Options, E_EnumStringer, proto.GetBoolExtension(file.Options, E_EnumStringerAll, false)) |
| 322 | } |
| 323 | |
| 324 | func IsUnsafeMarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 325 | return proto.GetBoolExtension(message.Options, E_UnsafeMarshaler, proto.GetBoolExtension(file.Options, E_UnsafeMarshalerAll, false)) |
| 326 | } |
| 327 | |
| 328 | func IsUnsafeUnmarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 329 | return proto.GetBoolExtension(message.Options, E_UnsafeUnmarshaler, proto.GetBoolExtension(file.Options, E_UnsafeUnmarshalerAll, false)) |
| 330 | } |
| 331 | |
| 332 | func HasExtensionsMap(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 333 | return proto.GetBoolExtension(message.Options, E_GoprotoExtensionsMap, proto.GetBoolExtension(file.Options, E_GoprotoExtensionsMapAll, true)) |
| 334 | } |
| 335 | |
| 336 | func HasUnrecognized(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 337 | if IsProto3(file) { |
| 338 | return false |
| 339 | } |
| 340 | return proto.GetBoolExtension(message.Options, E_GoprotoUnrecognized, proto.GetBoolExtension(file.Options, E_GoprotoUnrecognizedAll, true)) |
| 341 | } |
| 342 | |
| 343 | func IsProto3(file *google_protobuf.FileDescriptorProto) bool { |
| 344 | return file.GetSyntax() == "proto3" |
| 345 | } |
| 346 | |
| 347 | func ImportsGoGoProto(file *google_protobuf.FileDescriptorProto) bool { |
| 348 | return proto.GetBoolExtension(file.Options, E_GogoprotoImport, true) |
| 349 | } |
| 350 | |
| 351 | func HasCompare(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { |
| 352 | return proto.GetBoolExtension(message.Options, E_Compare, proto.GetBoolExtension(file.Options, E_CompareAll, false)) |
| 353 | } |
| 354 | |
| 355 | func RegistersGolangProto(file *google_protobuf.FileDescriptorProto) bool { |
| 356 | return proto.GetBoolExtension(file.Options, E_GoprotoRegistration, false) |
| 357 | } |