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 29package gogoproto 30 31import google_protobuf "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" 32import proto "github.com/gogo/protobuf/proto" 33 34func IsEmbed(field *google_protobuf.FieldDescriptorProto) bool { 35 return proto.GetBoolExtension(field.Options, E_Embed, false) 36} 37 38func IsNullable(field *google_protobuf.FieldDescriptorProto) bool { 39 return proto.GetBoolExtension(field.Options, E_Nullable, true) 40} 41 42func IsStdTime(field *google_protobuf.FieldDescriptorProto) bool { 43 return proto.GetBoolExtension(field.Options, E_Stdtime, false) 44} 45 46func IsStdDuration(field *google_protobuf.FieldDescriptorProto) bool { 47 return proto.GetBoolExtension(field.Options, E_Stdduration, false) 48} 49 50func IsStdDouble(field *google_protobuf.FieldDescriptorProto) bool { 51 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.DoubleValue" 52} 53 54func IsStdFloat(field *google_protobuf.FieldDescriptorProto) bool { 55 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.FloatValue" 56} 57 58func IsStdInt64(field *google_protobuf.FieldDescriptorProto) bool { 59 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.Int64Value" 60} 61 62func IsStdUInt64(field *google_protobuf.FieldDescriptorProto) bool { 63 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.UInt64Value" 64} 65 66func IsStdInt32(field *google_protobuf.FieldDescriptorProto) bool { 67 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.Int32Value" 68} 69 70func IsStdUInt32(field *google_protobuf.FieldDescriptorProto) bool { 71 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.UInt32Value" 72} 73 74func IsStdBool(field *google_protobuf.FieldDescriptorProto) bool { 75 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.BoolValue" 76} 77 78func IsStdString(field *google_protobuf.FieldDescriptorProto) bool { 79 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.StringValue" 80} 81 82func IsStdBytes(field *google_protobuf.FieldDescriptorProto) bool { 83 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) && *field.TypeName == ".google.protobuf.BytesValue" 84} 85 86func IsStdType(field *google_protobuf.FieldDescriptorProto) bool { 87 return (IsStdTime(field) || IsStdDuration(field) || 88 IsStdDouble(field) || IsStdFloat(field) || 89 IsStdInt64(field) || IsStdUInt64(field) || 90 IsStdInt32(field) || IsStdUInt32(field) || 91 IsStdBool(field) || 92 IsStdString(field) || IsStdBytes(field)) 93} 94 95func IsWktPtr(field *google_protobuf.FieldDescriptorProto) bool { 96 return proto.GetBoolExtension(field.Options, E_Wktpointer, false) 97} 98 99func NeedsNilCheck(proto3 bool, field *google_protobuf.FieldDescriptorProto) bool { 100 nullable := IsNullable(field) 101 if field.IsMessage() || IsCustomType(field) { 102 return nullable 103 } 104 if proto3 { 105 return false 106 } 107 return nullable || *field.Type == google_protobuf.FieldDescriptorProto_TYPE_BYTES 108} 109 110func IsCustomType(field *google_protobuf.FieldDescriptorProto) bool { 111 typ := GetCustomType(field) 112 if len(typ) > 0 { 113 return true 114 } 115 return false 116} 117 118func IsCastType(field *google_protobuf.FieldDescriptorProto) bool { 119 typ := GetCastType(field) 120 if len(typ) > 0 { 121 return true 122 } 123 return false 124} 125 126func IsCastKey(field *google_protobuf.FieldDescriptorProto) bool { 127 typ := GetCastKey(field) 128 if len(typ) > 0 { 129 return true 130 } 131 return false 132} 133 134func IsCastValue(field *google_protobuf.FieldDescriptorProto) bool { 135 typ := GetCastValue(field) 136 if len(typ) > 0 { 137 return true 138 } 139 return false 140} 141 142func HasEnumDecl(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { 143 return proto.GetBoolExtension(enum.Options, E_Enumdecl, proto.GetBoolExtension(file.Options, E_EnumdeclAll, true)) 144} 145 146func HasTypeDecl(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 147 return proto.GetBoolExtension(message.Options, E_Typedecl, proto.GetBoolExtension(file.Options, E_TypedeclAll, true)) 148} 149 150func GetCustomType(field *google_protobuf.FieldDescriptorProto) string { 151 if field == nil { 152 return "" 153 } 154 if field.Options != nil { 155 v, err := proto.GetExtension(field.Options, E_Customtype) 156 if err == nil && v.(*string) != nil { 157 return *(v.(*string)) 158 } 159 } 160 return "" 161} 162 163func GetCastType(field *google_protobuf.FieldDescriptorProto) string { 164 if field == nil { 165 return "" 166 } 167 if field.Options != nil { 168 v, err := proto.GetExtension(field.Options, E_Casttype) 169 if err == nil && v.(*string) != nil { 170 return *(v.(*string)) 171 } 172 } 173 return "" 174} 175 176func GetCastKey(field *google_protobuf.FieldDescriptorProto) string { 177 if field == nil { 178 return "" 179 } 180 if field.Options != nil { 181 v, err := proto.GetExtension(field.Options, E_Castkey) 182 if err == nil && v.(*string) != nil { 183 return *(v.(*string)) 184 } 185 } 186 return "" 187} 188 189func GetCastValue(field *google_protobuf.FieldDescriptorProto) string { 190 if field == nil { 191 return "" 192 } 193 if field.Options != nil { 194 v, err := proto.GetExtension(field.Options, E_Castvalue) 195 if err == nil && v.(*string) != nil { 196 return *(v.(*string)) 197 } 198 } 199 return "" 200} 201 202func IsCustomName(field *google_protobuf.FieldDescriptorProto) bool { 203 name := GetCustomName(field) 204 if len(name) > 0 { 205 return true 206 } 207 return false 208} 209 210func IsEnumCustomName(field *google_protobuf.EnumDescriptorProto) bool { 211 name := GetEnumCustomName(field) 212 if len(name) > 0 { 213 return true 214 } 215 return false 216} 217 218func IsEnumValueCustomName(field *google_protobuf.EnumValueDescriptorProto) bool { 219 name := GetEnumValueCustomName(field) 220 if len(name) > 0 { 221 return true 222 } 223 return false 224} 225 226func GetCustomName(field *google_protobuf.FieldDescriptorProto) string { 227 if field == nil { 228 return "" 229 } 230 if field.Options != nil { 231 v, err := proto.GetExtension(field.Options, E_Customname) 232 if err == nil && v.(*string) != nil { 233 return *(v.(*string)) 234 } 235 } 236 return "" 237} 238 239func GetEnumCustomName(field *google_protobuf.EnumDescriptorProto) string { 240 if field == nil { 241 return "" 242 } 243 if field.Options != nil { 244 v, err := proto.GetExtension(field.Options, E_EnumCustomname) 245 if err == nil && v.(*string) != nil { 246 return *(v.(*string)) 247 } 248 } 249 return "" 250} 251 252func GetEnumValueCustomName(field *google_protobuf.EnumValueDescriptorProto) string { 253 if field == nil { 254 return "" 255 } 256 if field.Options != nil { 257 v, err := proto.GetExtension(field.Options, E_EnumvalueCustomname) 258 if err == nil && v.(*string) != nil { 259 return *(v.(*string)) 260 } 261 } 262 return "" 263} 264 265func GetJsonTag(field *google_protobuf.FieldDescriptorProto) *string { 266 if field == nil { 267 return nil 268 } 269 if field.Options != nil { 270 v, err := proto.GetExtension(field.Options, E_Jsontag) 271 if err == nil && v.(*string) != nil { 272 return (v.(*string)) 273 } 274 } 275 return nil 276} 277 278func GetMoreTags(field *google_protobuf.FieldDescriptorProto) *string { 279 if field == nil { 280 return nil 281 } 282 if field.Options != nil { 283 v, err := proto.GetExtension(field.Options, E_Moretags) 284 if err == nil && v.(*string) != nil { 285 return (v.(*string)) 286 } 287 } 288 return nil 289} 290 291type EnableFunc func(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool 292 293func EnabledGoEnumPrefix(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { 294 return proto.GetBoolExtension(enum.Options, E_GoprotoEnumPrefix, proto.GetBoolExtension(file.Options, E_GoprotoEnumPrefixAll, true)) 295} 296 297func EnabledGoStringer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 298 return proto.GetBoolExtension(message.Options, E_GoprotoStringer, proto.GetBoolExtension(file.Options, E_GoprotoStringerAll, true)) 299} 300 301func HasGoGetters(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 302 return proto.GetBoolExtension(message.Options, E_GoprotoGetters, proto.GetBoolExtension(file.Options, E_GoprotoGettersAll, true)) 303} 304 305func IsUnion(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 306 return proto.GetBoolExtension(message.Options, E_Onlyone, proto.GetBoolExtension(file.Options, E_OnlyoneAll, false)) 307} 308 309func HasGoString(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 310 return proto.GetBoolExtension(message.Options, E_Gostring, proto.GetBoolExtension(file.Options, E_GostringAll, false)) 311} 312 313func HasEqual(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 314 return proto.GetBoolExtension(message.Options, E_Equal, proto.GetBoolExtension(file.Options, E_EqualAll, false)) 315} 316 317func HasVerboseEqual(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 318 return proto.GetBoolExtension(message.Options, E_VerboseEqual, proto.GetBoolExtension(file.Options, E_VerboseEqualAll, false)) 319} 320 321func IsStringer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 322 return proto.GetBoolExtension(message.Options, E_Stringer, proto.GetBoolExtension(file.Options, E_StringerAll, false)) 323} 324 325func IsFace(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 326 return proto.GetBoolExtension(message.Options, E_Face, proto.GetBoolExtension(file.Options, E_FaceAll, false)) 327} 328 329func HasDescription(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 330 return proto.GetBoolExtension(message.Options, E_Description, proto.GetBoolExtension(file.Options, E_DescriptionAll, false)) 331} 332 333func HasPopulate(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 334 return proto.GetBoolExtension(message.Options, E_Populate, proto.GetBoolExtension(file.Options, E_PopulateAll, false)) 335} 336 337func HasTestGen(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 338 return proto.GetBoolExtension(message.Options, E_Testgen, proto.GetBoolExtension(file.Options, E_TestgenAll, false)) 339} 340 341func HasBenchGen(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 342 return proto.GetBoolExtension(message.Options, E_Benchgen, proto.GetBoolExtension(file.Options, E_BenchgenAll, false)) 343} 344 345func IsMarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 346 return proto.GetBoolExtension(message.Options, E_Marshaler, proto.GetBoolExtension(file.Options, E_MarshalerAll, false)) 347} 348 349func IsUnmarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 350 return proto.GetBoolExtension(message.Options, E_Unmarshaler, proto.GetBoolExtension(file.Options, E_UnmarshalerAll, false)) 351} 352 353func IsStableMarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 354 return proto.GetBoolExtension(message.Options, E_StableMarshaler, proto.GetBoolExtension(file.Options, E_StableMarshalerAll, false)) 355} 356 357func IsSizer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 358 return proto.GetBoolExtension(message.Options, E_Sizer, proto.GetBoolExtension(file.Options, E_SizerAll, false)) 359} 360 361func IsProtoSizer(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 362 return proto.GetBoolExtension(message.Options, E_Protosizer, proto.GetBoolExtension(file.Options, E_ProtosizerAll, false)) 363} 364 365func IsGoEnumStringer(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { 366 return proto.GetBoolExtension(enum.Options, E_GoprotoEnumStringer, proto.GetBoolExtension(file.Options, E_GoprotoEnumStringerAll, true)) 367} 368 369func IsEnumStringer(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool { 370 return proto.GetBoolExtension(enum.Options, E_EnumStringer, proto.GetBoolExtension(file.Options, E_EnumStringerAll, false)) 371} 372 373func IsUnsafeMarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 374 return proto.GetBoolExtension(message.Options, E_UnsafeMarshaler, proto.GetBoolExtension(file.Options, E_UnsafeMarshalerAll, false)) 375} 376 377func IsUnsafeUnmarshaler(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 378 return proto.GetBoolExtension(message.Options, E_UnsafeUnmarshaler, proto.GetBoolExtension(file.Options, E_UnsafeUnmarshalerAll, false)) 379} 380 381func HasExtensionsMap(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 382 return proto.GetBoolExtension(message.Options, E_GoprotoExtensionsMap, proto.GetBoolExtension(file.Options, E_GoprotoExtensionsMapAll, true)) 383} 384 385func HasUnrecognized(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 386 return proto.GetBoolExtension(message.Options, E_GoprotoUnrecognized, proto.GetBoolExtension(file.Options, E_GoprotoUnrecognizedAll, true)) 387} 388 389func IsProto3(file *google_protobuf.FileDescriptorProto) bool { 390 return file.GetSyntax() == "proto3" 391} 392 393func ImportsGoGoProto(file *google_protobuf.FileDescriptorProto) bool { 394 return proto.GetBoolExtension(file.Options, E_GogoprotoImport, true) 395} 396 397func HasCompare(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 398 return proto.GetBoolExtension(message.Options, E_Compare, proto.GetBoolExtension(file.Options, E_CompareAll, false)) 399} 400 401func RegistersGolangProto(file *google_protobuf.FileDescriptorProto) bool { 402 return proto.GetBoolExtension(file.Options, E_GoprotoRegistration, false) 403} 404 405func HasMessageName(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 406 return proto.GetBoolExtension(message.Options, E_Messagename, proto.GetBoolExtension(file.Options, E_MessagenameAll, false)) 407} 408 409func HasSizecache(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 410 return proto.GetBoolExtension(message.Options, E_GoprotoSizecache, proto.GetBoolExtension(file.Options, E_GoprotoSizecacheAll, true)) 411} 412 413func HasUnkeyed(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { 414 return proto.GetBoolExtension(message.Options, E_GoprotoUnkeyed, proto.GetBoolExtension(file.Options, E_GoprotoUnkeyedAll, true)) 415} 416