desc.go 28 KB

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