desc.go 21 KB

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