desc.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package filedesc
  5. import (
  6. "bytes"
  7. "fmt"
  8. "sync"
  9. "sync/atomic"
  10. "google.golang.org/protobuf/internal/descfmt"
  11. "google.golang.org/protobuf/internal/descopts"
  12. "google.golang.org/protobuf/internal/encoding/defval"
  13. "google.golang.org/protobuf/internal/encoding/messageset"
  14. "google.golang.org/protobuf/internal/genid"
  15. "google.golang.org/protobuf/internal/pragma"
  16. "google.golang.org/protobuf/internal/strs"
  17. "google.golang.org/protobuf/reflect/protoreflect"
  18. "google.golang.org/protobuf/reflect/protoregistry"
  19. )
  20. // Edition is an Enum for proto2.Edition
  21. type Edition int32
  22. // These values align with the value of Enum in descriptor.proto which allows
  23. // direct conversion between the proto enum and this enum.
  24. const (
  25. EditionUnknown Edition = 0
  26. EditionProto2 Edition = 998
  27. EditionProto3 Edition = 999
  28. Edition2023 Edition = 1000
  29. EditionUnsupported Edition = 100000
  30. )
  31. // The types in this file may have a suffix:
  32. // • L0: Contains fields common to all descriptors (except File) and
  33. // must be initialized up front.
  34. // • L1: Contains fields specific to a descriptor and
  35. // must be initialized up front. If the associated proto uses Editions, the
  36. // Editions features must always be resolved. If not explicitly set, the
  37. // appropriate default must be resolved and set.
  38. // • L2: Contains fields that are lazily initialized when constructing
  39. // from the raw file descriptor. When constructing as a literal, the L2
  40. // fields must be initialized up front.
  41. //
  42. // The types are exported so that packages like reflect/protodesc can
  43. // directly construct descriptors.
  44. type (
  45. File struct {
  46. fileRaw
  47. L1 FileL1
  48. once uint32 // atomically set if L2 is valid
  49. mu sync.Mutex // protects L2
  50. L2 *FileL2
  51. }
  52. FileL1 struct {
  53. Syntax protoreflect.Syntax
  54. Edition Edition // Only used if Syntax == Editions
  55. Path string
  56. Package protoreflect.FullName
  57. Enums Enums
  58. Messages Messages
  59. Extensions Extensions
  60. Services Services
  61. EditionFeatures EditionFeatures
  62. }
  63. FileL2 struct {
  64. Options func() protoreflect.ProtoMessage
  65. Imports FileImports
  66. Locations SourceLocations
  67. }
  68. EditionFeatures struct {
  69. // IsFieldPresence is true if field_presence is EXPLICIT
  70. // https://protobuf.dev/editions/features/#field_presence
  71. IsFieldPresence bool
  72. // IsFieldPresence is true if field_presence is LEGACY_REQUIRED
  73. // https://protobuf.dev/editions/features/#field_presence
  74. IsLegacyRequired bool
  75. // IsOpenEnum is true if enum_type is OPEN
  76. // https://protobuf.dev/editions/features/#enum_type
  77. IsOpenEnum bool
  78. // IsPacked is true if repeated_field_encoding is PACKED
  79. // https://protobuf.dev/editions/features/#repeated_field_encoding
  80. IsPacked bool
  81. // IsUTF8Validated is true if utf_validation is VERIFY
  82. // https://protobuf.dev/editions/features/#utf8_validation
  83. IsUTF8Validated bool
  84. // IsDelimitedEncoded is true if message_encoding is DELIMITED
  85. // https://protobuf.dev/editions/features/#message_encoding
  86. IsDelimitedEncoded bool
  87. // IsJSONCompliant is true if json_format is ALLOW
  88. // https://protobuf.dev/editions/features/#json_format
  89. IsJSONCompliant bool
  90. // GenerateLegacyUnmarshalJSON determines if the plugin generates the
  91. // UnmarshalJSON([]byte) error method for enums.
  92. GenerateLegacyUnmarshalJSON bool
  93. }
  94. )
  95. func (fd *File) ParentFile() protoreflect.FileDescriptor { return fd }
  96. func (fd *File) Parent() protoreflect.Descriptor { return nil }
  97. func (fd *File) Index() int { return 0 }
  98. func (fd *File) Syntax() protoreflect.Syntax { return fd.L1.Syntax }
  99. func (fd *File) Name() protoreflect.Name { return fd.L1.Package.Name() }
  100. func (fd *File) FullName() protoreflect.FullName { return fd.L1.Package }
  101. func (fd *File) IsPlaceholder() bool { return false }
  102. func (fd *File) Options() protoreflect.ProtoMessage {
  103. if f := fd.lazyInit().Options; f != nil {
  104. return f()
  105. }
  106. return descopts.File
  107. }
  108. func (fd *File) Path() string { return fd.L1.Path }
  109. func (fd *File) Package() protoreflect.FullName { return fd.L1.Package }
  110. func (fd *File) Imports() protoreflect.FileImports { return &fd.lazyInit().Imports }
  111. func (fd *File) Enums() protoreflect.EnumDescriptors { return &fd.L1.Enums }
  112. func (fd *File) Messages() protoreflect.MessageDescriptors { return &fd.L1.Messages }
  113. func (fd *File) Extensions() protoreflect.ExtensionDescriptors { return &fd.L1.Extensions }
  114. func (fd *File) Services() protoreflect.ServiceDescriptors { return &fd.L1.Services }
  115. func (fd *File) SourceLocations() protoreflect.SourceLocations { return &fd.lazyInit().Locations }
  116. func (fd *File) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, fd) }
  117. func (fd *File) ProtoType(protoreflect.FileDescriptor) {}
  118. func (fd *File) ProtoInternal(pragma.DoNotImplement) {}
  119. func (fd *File) lazyInit() *FileL2 {
  120. if atomic.LoadUint32(&fd.once) == 0 {
  121. fd.lazyInitOnce()
  122. }
  123. return fd.L2
  124. }
  125. func (fd *File) lazyInitOnce() {
  126. fd.mu.Lock()
  127. if fd.L2 == nil {
  128. fd.lazyRawInit() // recursively initializes all L2 structures
  129. }
  130. atomic.StoreUint32(&fd.once, 1)
  131. fd.mu.Unlock()
  132. }
  133. // GoPackagePath is a pseudo-internal API for determining the Go package path
  134. // that this file descriptor is declared in.
  135. //
  136. // WARNING: This method is exempt from the compatibility promise and may be
  137. // removed in the future without warning.
  138. func (fd *File) GoPackagePath() string {
  139. return fd.builder.GoPackagePath
  140. }
  141. type (
  142. Enum struct {
  143. Base
  144. L1 EnumL1
  145. L2 *EnumL2 // protected by fileDesc.once
  146. }
  147. EnumL1 struct {
  148. eagerValues bool // controls whether EnumL2.Values is already populated
  149. EditionFeatures EditionFeatures
  150. }
  151. EnumL2 struct {
  152. Options func() protoreflect.ProtoMessage
  153. Values EnumValues
  154. ReservedNames Names
  155. ReservedRanges EnumRanges
  156. }
  157. EnumValue struct {
  158. Base
  159. L1 EnumValueL1
  160. }
  161. EnumValueL1 struct {
  162. Options func() protoreflect.ProtoMessage
  163. Number protoreflect.EnumNumber
  164. }
  165. )
  166. func (ed *Enum) Options() protoreflect.ProtoMessage {
  167. if f := ed.lazyInit().Options; f != nil {
  168. return f()
  169. }
  170. return descopts.Enum
  171. }
  172. func (ed *Enum) Values() protoreflect.EnumValueDescriptors {
  173. if ed.L1.eagerValues {
  174. return &ed.L2.Values
  175. }
  176. return &ed.lazyInit().Values
  177. }
  178. func (ed *Enum) ReservedNames() protoreflect.Names { return &ed.lazyInit().ReservedNames }
  179. func (ed *Enum) ReservedRanges() protoreflect.EnumRanges { return &ed.lazyInit().ReservedRanges }
  180. func (ed *Enum) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, ed) }
  181. func (ed *Enum) ProtoType(protoreflect.EnumDescriptor) {}
  182. func (ed *Enum) lazyInit() *EnumL2 {
  183. ed.L0.ParentFile.lazyInit() // implicitly initializes L2
  184. return ed.L2
  185. }
  186. func (ed *EnumValue) Options() protoreflect.ProtoMessage {
  187. if f := ed.L1.Options; f != nil {
  188. return f()
  189. }
  190. return descopts.EnumValue
  191. }
  192. func (ed *EnumValue) Number() protoreflect.EnumNumber { return ed.L1.Number }
  193. func (ed *EnumValue) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, ed) }
  194. func (ed *EnumValue) ProtoType(protoreflect.EnumValueDescriptor) {}
  195. type (
  196. Message struct {
  197. Base
  198. L1 MessageL1
  199. L2 *MessageL2 // protected by fileDesc.once
  200. }
  201. MessageL1 struct {
  202. Enums Enums
  203. Messages Messages
  204. Extensions Extensions
  205. IsMapEntry bool // promoted from google.protobuf.MessageOptions
  206. IsMessageSet bool // promoted from google.protobuf.MessageOptions
  207. EditionFeatures EditionFeatures
  208. }
  209. MessageL2 struct {
  210. Options func() protoreflect.ProtoMessage
  211. Fields Fields
  212. Oneofs Oneofs
  213. ReservedNames Names
  214. ReservedRanges FieldRanges
  215. RequiredNumbers FieldNumbers // must be consistent with Fields.Cardinality
  216. ExtensionRanges FieldRanges
  217. ExtensionRangeOptions []func() protoreflect.ProtoMessage // must be same length as ExtensionRanges
  218. }
  219. Field struct {
  220. Base
  221. L1 FieldL1
  222. }
  223. FieldL1 struct {
  224. Options func() protoreflect.ProtoMessage
  225. Number protoreflect.FieldNumber
  226. Cardinality protoreflect.Cardinality // must be consistent with Message.RequiredNumbers
  227. Kind protoreflect.Kind
  228. StringName stringName
  229. IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto
  230. IsWeak bool // promoted from google.protobuf.FieldOptions
  231. HasPacked bool // promoted from google.protobuf.FieldOptions
  232. IsPacked bool // promoted from google.protobuf.FieldOptions
  233. HasEnforceUTF8 bool // promoted from google.protobuf.FieldOptions
  234. EnforceUTF8 bool // promoted from google.protobuf.FieldOptions
  235. Default defaultValue
  236. ContainingOneof protoreflect.OneofDescriptor // must be consistent with Message.Oneofs.Fields
  237. Enum protoreflect.EnumDescriptor
  238. Message protoreflect.MessageDescriptor
  239. EditionFeatures EditionFeatures
  240. }
  241. Oneof struct {
  242. Base
  243. L1 OneofL1
  244. }
  245. OneofL1 struct {
  246. Options func() protoreflect.ProtoMessage
  247. Fields OneofFields // must be consistent with Message.Fields.ContainingOneof
  248. EditionFeatures EditionFeatures
  249. }
  250. )
  251. func (md *Message) Options() protoreflect.ProtoMessage {
  252. if f := md.lazyInit().Options; f != nil {
  253. return f()
  254. }
  255. return descopts.Message
  256. }
  257. func (md *Message) IsMapEntry() bool { return md.L1.IsMapEntry }
  258. func (md *Message) Fields() protoreflect.FieldDescriptors { return &md.lazyInit().Fields }
  259. func (md *Message) Oneofs() protoreflect.OneofDescriptors { return &md.lazyInit().Oneofs }
  260. func (md *Message) ReservedNames() protoreflect.Names { return &md.lazyInit().ReservedNames }
  261. func (md *Message) ReservedRanges() protoreflect.FieldRanges { return &md.lazyInit().ReservedRanges }
  262. func (md *Message) RequiredNumbers() protoreflect.FieldNumbers { return &md.lazyInit().RequiredNumbers }
  263. func (md *Message) ExtensionRanges() protoreflect.FieldRanges { return &md.lazyInit().ExtensionRanges }
  264. func (md *Message) ExtensionRangeOptions(i int) protoreflect.ProtoMessage {
  265. if f := md.lazyInit().ExtensionRangeOptions[i]; f != nil {
  266. return f()
  267. }
  268. return descopts.ExtensionRange
  269. }
  270. func (md *Message) Enums() protoreflect.EnumDescriptors { return &md.L1.Enums }
  271. func (md *Message) Messages() protoreflect.MessageDescriptors { return &md.L1.Messages }
  272. func (md *Message) Extensions() protoreflect.ExtensionDescriptors { return &md.L1.Extensions }
  273. func (md *Message) ProtoType(protoreflect.MessageDescriptor) {}
  274. func (md *Message) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, md) }
  275. func (md *Message) lazyInit() *MessageL2 {
  276. md.L0.ParentFile.lazyInit() // implicitly initializes L2
  277. return md.L2
  278. }
  279. // IsMessageSet is a pseudo-internal API for checking whether a message
  280. // should serialize in the proto1 message format.
  281. //
  282. // WARNING: This method is exempt from the compatibility promise and may be
  283. // removed in the future without warning.
  284. func (md *Message) IsMessageSet() bool {
  285. return md.L1.IsMessageSet
  286. }
  287. func (fd *Field) Options() protoreflect.ProtoMessage {
  288. if f := fd.L1.Options; f != nil {
  289. return f()
  290. }
  291. return descopts.Field
  292. }
  293. func (fd *Field) Number() protoreflect.FieldNumber { return fd.L1.Number }
  294. func (fd *Field) Cardinality() protoreflect.Cardinality { return fd.L1.Cardinality }
  295. func (fd *Field) Kind() protoreflect.Kind {
  296. return fd.L1.Kind
  297. }
  298. func (fd *Field) HasJSONName() bool { return fd.L1.StringName.hasJSON }
  299. func (fd *Field) JSONName() string { return fd.L1.StringName.getJSON(fd) }
  300. func (fd *Field) TextName() string { return fd.L1.StringName.getText(fd) }
  301. func (fd *Field) HasPresence() bool {
  302. if fd.L1.Cardinality == protoreflect.Repeated {
  303. return false
  304. }
  305. explicitFieldPresence := fd.Syntax() == protoreflect.Editions && fd.L1.EditionFeatures.IsFieldPresence
  306. return fd.Syntax() == protoreflect.Proto2 || explicitFieldPresence || fd.L1.Message != nil || fd.L1.ContainingOneof != nil
  307. }
  308. func (fd *Field) HasOptionalKeyword() bool {
  309. return (fd.L0.ParentFile.L1.Syntax == protoreflect.Proto2 && fd.L1.Cardinality == protoreflect.Optional && fd.L1.ContainingOneof == nil) || fd.L1.IsProto3Optional
  310. }
  311. func (fd *Field) IsPacked() bool {
  312. if fd.L1.Cardinality != protoreflect.Repeated {
  313. return false
  314. }
  315. switch fd.L1.Kind {
  316. case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind:
  317. return false
  318. }
  319. if fd.L0.ParentFile.L1.Syntax == protoreflect.Editions {
  320. return fd.L1.EditionFeatures.IsPacked
  321. }
  322. if fd.L0.ParentFile.L1.Syntax == protoreflect.Proto3 {
  323. // proto3 repeated fields are packed by default.
  324. return !fd.L1.HasPacked || fd.L1.IsPacked
  325. }
  326. return fd.L1.IsPacked
  327. }
  328. func (fd *Field) IsExtension() bool { return false }
  329. func (fd *Field) IsWeak() bool { return fd.L1.IsWeak }
  330. func (fd *Field) IsList() bool { return fd.Cardinality() == protoreflect.Repeated && !fd.IsMap() }
  331. func (fd *Field) IsMap() bool { return fd.Message() != nil && fd.Message().IsMapEntry() }
  332. func (fd *Field) MapKey() protoreflect.FieldDescriptor {
  333. if !fd.IsMap() {
  334. return nil
  335. }
  336. return fd.Message().Fields().ByNumber(genid.MapEntry_Key_field_number)
  337. }
  338. func (fd *Field) MapValue() protoreflect.FieldDescriptor {
  339. if !fd.IsMap() {
  340. return nil
  341. }
  342. return fd.Message().Fields().ByNumber(genid.MapEntry_Value_field_number)
  343. }
  344. func (fd *Field) HasDefault() bool { return fd.L1.Default.has }
  345. func (fd *Field) Default() protoreflect.Value { return fd.L1.Default.get(fd) }
  346. func (fd *Field) DefaultEnumValue() protoreflect.EnumValueDescriptor { return fd.L1.Default.enum }
  347. func (fd *Field) ContainingOneof() protoreflect.OneofDescriptor { return fd.L1.ContainingOneof }
  348. func (fd *Field) ContainingMessage() protoreflect.MessageDescriptor {
  349. return fd.L0.Parent.(protoreflect.MessageDescriptor)
  350. }
  351. func (fd *Field) Enum() protoreflect.EnumDescriptor {
  352. return fd.L1.Enum
  353. }
  354. func (fd *Field) Message() protoreflect.MessageDescriptor {
  355. if fd.L1.IsWeak {
  356. if d, _ := protoregistry.GlobalFiles.FindDescriptorByName(fd.L1.Message.FullName()); d != nil {
  357. return d.(protoreflect.MessageDescriptor)
  358. }
  359. }
  360. return fd.L1.Message
  361. }
  362. func (fd *Field) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, fd) }
  363. func (fd *Field) ProtoType(protoreflect.FieldDescriptor) {}
  364. // EnforceUTF8 is a pseudo-internal API to determine whether to enforce UTF-8
  365. // validation for the string field. This exists for Google-internal use only
  366. // since proto3 did not enforce UTF-8 validity prior to the open-source release.
  367. // If this method does not exist, the default is to enforce valid UTF-8.
  368. //
  369. // WARNING: This method is exempt from the compatibility promise and may be
  370. // removed in the future without warning.
  371. func (fd *Field) EnforceUTF8() bool {
  372. if fd.L0.ParentFile.L1.Syntax == protoreflect.Editions {
  373. return fd.L1.EditionFeatures.IsUTF8Validated
  374. }
  375. if fd.L1.HasEnforceUTF8 {
  376. return fd.L1.EnforceUTF8
  377. }
  378. return fd.L0.ParentFile.L1.Syntax == protoreflect.Proto3
  379. }
  380. func (od *Oneof) IsSynthetic() bool {
  381. return od.L0.ParentFile.L1.Syntax == protoreflect.Proto3 && len(od.L1.Fields.List) == 1 && od.L1.Fields.List[0].HasOptionalKeyword()
  382. }
  383. func (od *Oneof) Options() protoreflect.ProtoMessage {
  384. if f := od.L1.Options; f != nil {
  385. return f()
  386. }
  387. return descopts.Oneof
  388. }
  389. func (od *Oneof) Fields() protoreflect.FieldDescriptors { return &od.L1.Fields }
  390. func (od *Oneof) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, od) }
  391. func (od *Oneof) ProtoType(protoreflect.OneofDescriptor) {}
  392. type (
  393. Extension struct {
  394. Base
  395. L1 ExtensionL1
  396. L2 *ExtensionL2 // protected by fileDesc.once
  397. }
  398. ExtensionL1 struct {
  399. Number protoreflect.FieldNumber
  400. Extendee protoreflect.MessageDescriptor
  401. Cardinality protoreflect.Cardinality
  402. Kind protoreflect.Kind
  403. EditionFeatures EditionFeatures
  404. }
  405. ExtensionL2 struct {
  406. Options func() protoreflect.ProtoMessage
  407. StringName stringName
  408. IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto
  409. IsPacked bool // promoted from google.protobuf.FieldOptions
  410. Default defaultValue
  411. Enum protoreflect.EnumDescriptor
  412. Message protoreflect.MessageDescriptor
  413. }
  414. )
  415. func (xd *Extension) Options() protoreflect.ProtoMessage {
  416. if f := xd.lazyInit().Options; f != nil {
  417. return f()
  418. }
  419. return descopts.Field
  420. }
  421. func (xd *Extension) Number() protoreflect.FieldNumber { return xd.L1.Number }
  422. func (xd *Extension) Cardinality() protoreflect.Cardinality { return xd.L1.Cardinality }
  423. func (xd *Extension) Kind() protoreflect.Kind { return xd.L1.Kind }
  424. func (xd *Extension) HasJSONName() bool { return xd.lazyInit().StringName.hasJSON }
  425. func (xd *Extension) JSONName() string { return xd.lazyInit().StringName.getJSON(xd) }
  426. func (xd *Extension) TextName() string { return xd.lazyInit().StringName.getText(xd) }
  427. func (xd *Extension) HasPresence() bool { return xd.L1.Cardinality != protoreflect.Repeated }
  428. func (xd *Extension) HasOptionalKeyword() bool {
  429. return (xd.L0.ParentFile.L1.Syntax == protoreflect.Proto2 && xd.L1.Cardinality == protoreflect.Optional) || xd.lazyInit().IsProto3Optional
  430. }
  431. func (xd *Extension) IsPacked() bool { return xd.lazyInit().IsPacked }
  432. func (xd *Extension) IsExtension() bool { return true }
  433. func (xd *Extension) IsWeak() bool { return false }
  434. func (xd *Extension) IsList() bool { return xd.Cardinality() == protoreflect.Repeated }
  435. func (xd *Extension) IsMap() bool { return false }
  436. func (xd *Extension) MapKey() protoreflect.FieldDescriptor { return nil }
  437. func (xd *Extension) MapValue() protoreflect.FieldDescriptor { return nil }
  438. func (xd *Extension) HasDefault() bool { return xd.lazyInit().Default.has }
  439. func (xd *Extension) Default() protoreflect.Value { return xd.lazyInit().Default.get(xd) }
  440. func (xd *Extension) DefaultEnumValue() protoreflect.EnumValueDescriptor {
  441. return xd.lazyInit().Default.enum
  442. }
  443. func (xd *Extension) ContainingOneof() protoreflect.OneofDescriptor { return nil }
  444. func (xd *Extension) ContainingMessage() protoreflect.MessageDescriptor { return xd.L1.Extendee }
  445. func (xd *Extension) Enum() protoreflect.EnumDescriptor { return xd.lazyInit().Enum }
  446. func (xd *Extension) Message() protoreflect.MessageDescriptor { return xd.lazyInit().Message }
  447. func (xd *Extension) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, xd) }
  448. func (xd *Extension) ProtoType(protoreflect.FieldDescriptor) {}
  449. func (xd *Extension) ProtoInternal(pragma.DoNotImplement) {}
  450. func (xd *Extension) lazyInit() *ExtensionL2 {
  451. xd.L0.ParentFile.lazyInit() // implicitly initializes L2
  452. return xd.L2
  453. }
  454. type (
  455. Service struct {
  456. Base
  457. L1 ServiceL1
  458. L2 *ServiceL2 // protected by fileDesc.once
  459. }
  460. ServiceL1 struct{}
  461. ServiceL2 struct {
  462. Options func() protoreflect.ProtoMessage
  463. Methods Methods
  464. }
  465. Method struct {
  466. Base
  467. L1 MethodL1
  468. }
  469. MethodL1 struct {
  470. Options func() protoreflect.ProtoMessage
  471. Input protoreflect.MessageDescriptor
  472. Output protoreflect.MessageDescriptor
  473. IsStreamingClient bool
  474. IsStreamingServer bool
  475. }
  476. )
  477. func (sd *Service) Options() protoreflect.ProtoMessage {
  478. if f := sd.lazyInit().Options; f != nil {
  479. return f()
  480. }
  481. return descopts.Service
  482. }
  483. func (sd *Service) Methods() protoreflect.MethodDescriptors { return &sd.lazyInit().Methods }
  484. func (sd *Service) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, sd) }
  485. func (sd *Service) ProtoType(protoreflect.ServiceDescriptor) {}
  486. func (sd *Service) ProtoInternal(pragma.DoNotImplement) {}
  487. func (sd *Service) lazyInit() *ServiceL2 {
  488. sd.L0.ParentFile.lazyInit() // implicitly initializes L2
  489. return sd.L2
  490. }
  491. func (md *Method) Options() protoreflect.ProtoMessage {
  492. if f := md.L1.Options; f != nil {
  493. return f()
  494. }
  495. return descopts.Method
  496. }
  497. func (md *Method) Input() protoreflect.MessageDescriptor { return md.L1.Input }
  498. func (md *Method) Output() protoreflect.MessageDescriptor { return md.L1.Output }
  499. func (md *Method) IsStreamingClient() bool { return md.L1.IsStreamingClient }
  500. func (md *Method) IsStreamingServer() bool { return md.L1.IsStreamingServer }
  501. func (md *Method) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, md) }
  502. func (md *Method) ProtoType(protoreflect.MethodDescriptor) {}
  503. func (md *Method) ProtoInternal(pragma.DoNotImplement) {}
  504. // Surrogate files are can be used to create standalone descriptors
  505. // where the syntax is only information derived from the parent file.
  506. var (
  507. SurrogateProto2 = &File{L1: FileL1{Syntax: protoreflect.Proto2}, L2: &FileL2{}}
  508. SurrogateProto3 = &File{L1: FileL1{Syntax: protoreflect.Proto3}, L2: &FileL2{}}
  509. )
  510. type (
  511. Base struct {
  512. L0 BaseL0
  513. }
  514. BaseL0 struct {
  515. FullName protoreflect.FullName // must be populated
  516. ParentFile *File // must be populated
  517. Parent protoreflect.Descriptor
  518. Index int
  519. }
  520. )
  521. func (d *Base) Name() protoreflect.Name { return d.L0.FullName.Name() }
  522. func (d *Base) FullName() protoreflect.FullName { return d.L0.FullName }
  523. func (d *Base) ParentFile() protoreflect.FileDescriptor {
  524. if d.L0.ParentFile == SurrogateProto2 || d.L0.ParentFile == SurrogateProto3 {
  525. return nil // surrogate files are not real parents
  526. }
  527. return d.L0.ParentFile
  528. }
  529. func (d *Base) Parent() protoreflect.Descriptor { return d.L0.Parent }
  530. func (d *Base) Index() int { return d.L0.Index }
  531. func (d *Base) Syntax() protoreflect.Syntax { return d.L0.ParentFile.Syntax() }
  532. func (d *Base) IsPlaceholder() bool { return false }
  533. func (d *Base) ProtoInternal(pragma.DoNotImplement) {}
  534. type stringName struct {
  535. hasJSON bool
  536. once sync.Once
  537. nameJSON string
  538. nameText string
  539. }
  540. // InitJSON initializes the name. It is exported for use by other internal packages.
  541. func (s *stringName) InitJSON(name string) {
  542. s.hasJSON = true
  543. s.nameJSON = name
  544. }
  545. func (s *stringName) lazyInit(fd protoreflect.FieldDescriptor) *stringName {
  546. s.once.Do(func() {
  547. if fd.IsExtension() {
  548. // For extensions, JSON and text are formatted the same way.
  549. var name string
  550. if messageset.IsMessageSetExtension(fd) {
  551. name = string("[" + fd.FullName().Parent() + "]")
  552. } else {
  553. name = string("[" + fd.FullName() + "]")
  554. }
  555. s.nameJSON = name
  556. s.nameText = name
  557. } else {
  558. // Format the JSON name.
  559. if !s.hasJSON {
  560. s.nameJSON = strs.JSONCamelCase(string(fd.Name()))
  561. }
  562. // Format the text name.
  563. s.nameText = string(fd.Name())
  564. if fd.Kind() == protoreflect.GroupKind {
  565. s.nameText = string(fd.Message().Name())
  566. }
  567. }
  568. })
  569. return s
  570. }
  571. func (s *stringName) getJSON(fd protoreflect.FieldDescriptor) string { return s.lazyInit(fd).nameJSON }
  572. func (s *stringName) getText(fd protoreflect.FieldDescriptor) string { return s.lazyInit(fd).nameText }
  573. func DefaultValue(v protoreflect.Value, ev protoreflect.EnumValueDescriptor) defaultValue {
  574. dv := defaultValue{has: v.IsValid(), val: v, enum: ev}
  575. if b, ok := v.Interface().([]byte); ok {
  576. // Store a copy of the default bytes, so that we can detect
  577. // accidental mutations of the original value.
  578. dv.bytes = append([]byte(nil), b...)
  579. }
  580. return dv
  581. }
  582. func unmarshalDefault(b []byte, k protoreflect.Kind, pf *File, ed protoreflect.EnumDescriptor) defaultValue {
  583. var evs protoreflect.EnumValueDescriptors
  584. if k == protoreflect.EnumKind {
  585. // If the enum is declared within the same file, be careful not to
  586. // blindly call the Values method, lest we bind ourselves in a deadlock.
  587. if e, ok := ed.(*Enum); ok && e.L0.ParentFile == pf {
  588. evs = &e.L2.Values
  589. } else {
  590. evs = ed.Values()
  591. }
  592. // If we are unable to resolve the enum dependency, use a placeholder
  593. // enum value since we will not be able to parse the default value.
  594. if ed.IsPlaceholder() && protoreflect.Name(b).IsValid() {
  595. v := protoreflect.ValueOfEnum(0)
  596. ev := PlaceholderEnumValue(ed.FullName().Parent().Append(protoreflect.Name(b)))
  597. return DefaultValue(v, ev)
  598. }
  599. }
  600. v, ev, err := defval.Unmarshal(string(b), k, evs, defval.Descriptor)
  601. if err != nil {
  602. panic(err)
  603. }
  604. return DefaultValue(v, ev)
  605. }
  606. type defaultValue struct {
  607. has bool
  608. val protoreflect.Value
  609. enum protoreflect.EnumValueDescriptor
  610. bytes []byte
  611. }
  612. func (dv *defaultValue) get(fd protoreflect.FieldDescriptor) protoreflect.Value {
  613. // Return the zero value as the default if unpopulated.
  614. if !dv.has {
  615. if fd.Cardinality() == protoreflect.Repeated {
  616. return protoreflect.Value{}
  617. }
  618. switch fd.Kind() {
  619. case protoreflect.BoolKind:
  620. return protoreflect.ValueOfBool(false)
  621. case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
  622. return protoreflect.ValueOfInt32(0)
  623. case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
  624. return protoreflect.ValueOfInt64(0)
  625. case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
  626. return protoreflect.ValueOfUint32(0)
  627. case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
  628. return protoreflect.ValueOfUint64(0)
  629. case protoreflect.FloatKind:
  630. return protoreflect.ValueOfFloat32(0)
  631. case protoreflect.DoubleKind:
  632. return protoreflect.ValueOfFloat64(0)
  633. case protoreflect.StringKind:
  634. return protoreflect.ValueOfString("")
  635. case protoreflect.BytesKind:
  636. return protoreflect.ValueOfBytes(nil)
  637. case protoreflect.EnumKind:
  638. if evs := fd.Enum().Values(); evs.Len() > 0 {
  639. return protoreflect.ValueOfEnum(evs.Get(0).Number())
  640. }
  641. return protoreflect.ValueOfEnum(0)
  642. }
  643. }
  644. if len(dv.bytes) > 0 && !bytes.Equal(dv.bytes, dv.val.Bytes()) {
  645. // TODO: Avoid panic if we're running with the race detector
  646. // and instead spawn a goroutine that periodically resets
  647. // this value back to the original to induce a race.
  648. panic(fmt.Sprintf("detected mutation on the default bytes for %v", fd.FullName()))
  649. }
  650. return dv.val
  651. }