desc.go 22 KB

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