helper.go 16 KB

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