You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

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