dynamic.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 dynamicpb creates protocol buffer messages using runtime type information.
  5. package dynamicpb
  6. import (
  7. "math"
  8. "google.golang.org/protobuf/internal/errors"
  9. pref "google.golang.org/protobuf/reflect/protoreflect"
  10. protoimpl "google.golang.org/protobuf/runtime/protoimpl"
  11. )
  12. // A Message is a dynamically constructed protocol buffer message.
  13. //
  14. // Message implements the proto.Message interface, and may be used with all
  15. // standard proto package functions such as Marshal, Unmarshal, and so forth.
  16. //
  17. // Message also implements the protoreflect.Message interface. See the protoreflect
  18. // package documentation for that interface for how to get and set fields and
  19. // otherwise interact with the contents of a Message.
  20. //
  21. // Reflection API functions which construct messages, such as NewField,
  22. // return new dynamic messages of the appropriate type. Functions which take
  23. // messages, such as Set for a message-value field, will accept any message
  24. // with a compatible type.
  25. //
  26. // Operations which modify a Message are not safe for concurrent use.
  27. type Message struct {
  28. typ messageType
  29. known map[pref.FieldNumber]pref.Value
  30. ext map[pref.FieldNumber]pref.FieldDescriptor
  31. unknown pref.RawFields
  32. }
  33. // NewMessage creates a new message with the provided descriptor.
  34. func NewMessage(desc pref.MessageDescriptor) *Message {
  35. return &Message{
  36. typ: messageType{desc},
  37. known: make(map[pref.FieldNumber]pref.Value),
  38. ext: make(map[pref.FieldNumber]pref.FieldDescriptor),
  39. }
  40. }
  41. // ProtoReflect implements the protoreflect.ProtoMessage interface.
  42. func (m *Message) ProtoReflect() pref.Message {
  43. return m
  44. }
  45. // String returns a string representation of a message.
  46. func (m *Message) String() string {
  47. return protoimpl.X.MessageStringOf(m)
  48. }
  49. // Descriptor returns the message descriptor.
  50. func (m *Message) Descriptor() pref.MessageDescriptor {
  51. return m.typ.desc
  52. }
  53. // Type returns the message type.
  54. func (m *Message) Type() pref.MessageType {
  55. return m.typ
  56. }
  57. // New returns a newly allocated empty message with the same descriptor.
  58. // See protoreflect.Message for details.
  59. func (m *Message) New() pref.Message {
  60. return m.Type().New()
  61. }
  62. // Interface returns the message.
  63. // See protoreflect.Message for details.
  64. func (m *Message) Interface() pref.ProtoMessage {
  65. return m
  66. }
  67. // Range visits every populated field in undefined order.
  68. // See protoreflect.Message for details.
  69. func (m *Message) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
  70. for num, v := range m.known {
  71. fd := m.ext[num]
  72. if fd == nil {
  73. fd = m.Descriptor().Fields().ByNumber(num)
  74. if !isSet(fd, v) {
  75. continue
  76. }
  77. }
  78. if !f(fd, v) {
  79. return
  80. }
  81. }
  82. }
  83. // Range reports whether a field is populated.
  84. // See protoreflect.Message for details.
  85. func (m *Message) Has(fd pref.FieldDescriptor) bool {
  86. m.checkField(fd)
  87. if fd.IsExtension() {
  88. return m.ext[fd.Number()] == fd
  89. }
  90. v, ok := m.known[fd.Number()]
  91. if !ok {
  92. return false
  93. }
  94. return isSet(fd, v)
  95. }
  96. // Clear clears a field.
  97. // See protoreflect.Message for details.
  98. func (m *Message) Clear(fd pref.FieldDescriptor) {
  99. m.checkField(fd)
  100. num := fd.Number()
  101. delete(m.known, num)
  102. delete(m.ext, num)
  103. }
  104. // Get returns the value of a field.
  105. // See protoreflect.Message for details.
  106. func (m *Message) Get(fd pref.FieldDescriptor) pref.Value {
  107. m.checkField(fd)
  108. num := fd.Number()
  109. if fd.IsExtension() {
  110. if fd != m.ext[num] {
  111. return fd.(pref.ExtensionTypeDescriptor).Type().Zero()
  112. }
  113. return m.known[num]
  114. }
  115. if v, ok := m.known[num]; ok {
  116. return v
  117. }
  118. switch {
  119. case fd.IsMap():
  120. return pref.ValueOfMap(&dynamicMap{desc: fd})
  121. case fd.IsList():
  122. return pref.ValueOfList(emptyList{desc: fd})
  123. case fd.Message() != nil:
  124. return pref.ValueOfMessage(&Message{typ: messageType{fd.Message()}})
  125. case fd.Kind() == pref.BytesKind:
  126. return pref.ValueOfBytes(append([]byte(nil), fd.Default().Bytes()...))
  127. default:
  128. return fd.Default()
  129. }
  130. }
  131. // Mutable returns a mutable reference to a repeated, map, or message field.
  132. // See protoreflect.Message for details.
  133. func (m *Message) Mutable(fd pref.FieldDescriptor) pref.Value {
  134. m.checkField(fd)
  135. if !fd.IsMap() && !fd.IsList() && fd.Message() == nil {
  136. panic(errors.New("%v: getting mutable reference to non-composite type", fd.FullName()))
  137. }
  138. if m.known == nil {
  139. panic(errors.New("%v: modification of read-only message", fd.FullName()))
  140. }
  141. num := fd.Number()
  142. if fd.IsExtension() {
  143. if fd != m.ext[num] {
  144. m.ext[num] = fd
  145. m.known[num] = fd.(pref.ExtensionTypeDescriptor).Type().New()
  146. }
  147. return m.known[num]
  148. }
  149. if v, ok := m.known[num]; ok {
  150. return v
  151. }
  152. m.clearOtherOneofFields(fd)
  153. m.known[num] = m.NewField(fd)
  154. if fd.IsExtension() {
  155. m.ext[num] = fd
  156. }
  157. return m.known[num]
  158. }
  159. // Set stores a value in a field.
  160. // See protoreflect.Message for details.
  161. func (m *Message) Set(fd pref.FieldDescriptor, v pref.Value) {
  162. m.checkField(fd)
  163. if m.known == nil {
  164. panic(errors.New("%v: modification of read-only message", fd.FullName()))
  165. }
  166. if fd.IsExtension() {
  167. if !fd.(pref.ExtensionTypeDescriptor).Type().IsValidValue(v) {
  168. panic(errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface()))
  169. }
  170. m.ext[fd.Number()] = fd
  171. } else {
  172. typecheck(fd, v)
  173. }
  174. m.clearOtherOneofFields(fd)
  175. m.known[fd.Number()] = v
  176. }
  177. func (m *Message) clearOtherOneofFields(fd pref.FieldDescriptor) {
  178. od := fd.ContainingOneof()
  179. if od == nil {
  180. return
  181. }
  182. num := fd.Number()
  183. for i := 0; i < od.Fields().Len(); i++ {
  184. if n := od.Fields().Get(i).Number(); n != num {
  185. delete(m.known, n)
  186. }
  187. }
  188. }
  189. // NewField returns a new value for assignable to the field of a given descriptor.
  190. // See protoreflect.Message for details.
  191. func (m *Message) NewField(fd pref.FieldDescriptor) pref.Value {
  192. m.checkField(fd)
  193. switch {
  194. case fd.IsExtension():
  195. return fd.(pref.ExtensionTypeDescriptor).Type().New()
  196. case fd.IsMap():
  197. return pref.ValueOfMap(&dynamicMap{
  198. desc: fd,
  199. mapv: make(map[interface{}]pref.Value),
  200. })
  201. case fd.IsList():
  202. return pref.ValueOfList(&dynamicList{desc: fd})
  203. case fd.Message() != nil:
  204. return pref.ValueOfMessage(NewMessage(fd.Message()).ProtoReflect())
  205. default:
  206. return fd.Default()
  207. }
  208. }
  209. // WhichOneof reports which field in a oneof is populated, returning nil if none are populated.
  210. // See protoreflect.Message for details.
  211. func (m *Message) WhichOneof(od pref.OneofDescriptor) pref.FieldDescriptor {
  212. for i := 0; i < od.Fields().Len(); i++ {
  213. fd := od.Fields().Get(i)
  214. if m.Has(fd) {
  215. return fd
  216. }
  217. }
  218. return nil
  219. }
  220. // GetUnknown returns the raw unknown fields.
  221. // See protoreflect.Message for details.
  222. func (m *Message) GetUnknown() pref.RawFields {
  223. return m.unknown
  224. }
  225. // SetUnknown sets the raw unknown fields.
  226. // See protoreflect.Message for details.
  227. func (m *Message) SetUnknown(r pref.RawFields) {
  228. if m.known == nil {
  229. panic(errors.New("%v: modification of read-only message", m.typ.desc.FullName()))
  230. }
  231. m.unknown = r
  232. }
  233. func (m *Message) checkField(fd pref.FieldDescriptor) {
  234. if fd.IsExtension() && fd.ContainingMessage().FullName() == m.Descriptor().FullName() {
  235. if _, ok := fd.(pref.ExtensionTypeDescriptor); !ok {
  236. panic(errors.New("%v: extension field descriptor does not implement ExtensionTypeDescriptor", fd.FullName()))
  237. }
  238. return
  239. }
  240. if fd.Parent() == m.Descriptor() {
  241. return
  242. }
  243. fields := m.Descriptor().Fields()
  244. index := fd.Index()
  245. if index >= fields.Len() || fields.Get(index) != fd {
  246. panic(errors.New("%v: field descriptor does not belong to this message", fd.FullName()))
  247. }
  248. }
  249. type messageType struct {
  250. desc pref.MessageDescriptor
  251. }
  252. // NewMessageType creates a new MessageType with the provided descriptor.
  253. //
  254. // MessageTypes created by this package are equal if their descriptors are equal.
  255. // That is, if md1 == md2, then NewMessageType(md1) == NewMessageType(md2).
  256. func NewMessageType(desc pref.MessageDescriptor) pref.MessageType {
  257. return messageType{desc}
  258. }
  259. func (mt messageType) New() pref.Message { return NewMessage(mt.desc) }
  260. func (mt messageType) Zero() pref.Message { return NewMessage(mt.desc) }
  261. func (mt messageType) Descriptor() pref.MessageDescriptor { return mt.desc }
  262. type emptyList struct {
  263. desc pref.FieldDescriptor
  264. }
  265. func (x emptyList) Len() int { return 0 }
  266. func (x emptyList) Get(n int) pref.Value { panic(errors.New("out of range")) }
  267. func (x emptyList) Set(n int, v pref.Value) { panic(errors.New("modification of immutable list")) }
  268. func (x emptyList) Append(v pref.Value) { panic(errors.New("modification of immutable list")) }
  269. func (x emptyList) Truncate(n int) { panic(errors.New("modification of immutable list")) }
  270. func (x emptyList) NewElement() pref.Value {
  271. return newListEntry(x.desc)
  272. }
  273. type dynamicList struct {
  274. desc pref.FieldDescriptor
  275. list []pref.Value
  276. }
  277. func (x *dynamicList) Len() int {
  278. return len(x.list)
  279. }
  280. func (x *dynamicList) Get(n int) pref.Value {
  281. return x.list[n]
  282. }
  283. func (x *dynamicList) Set(n int, v pref.Value) {
  284. typecheckSingular(x.desc, v)
  285. x.list[n] = v
  286. }
  287. func (x *dynamicList) Append(v pref.Value) {
  288. typecheckSingular(x.desc, v)
  289. x.list = append(x.list, v)
  290. }
  291. func (x *dynamicList) Truncate(n int) {
  292. // Zero truncated elements to avoid keeping data live.
  293. for i := n; i < len(x.list); i++ {
  294. x.list[i] = pref.Value{}
  295. }
  296. x.list = x.list[:n]
  297. }
  298. func (x *dynamicList) NewElement() pref.Value {
  299. return newListEntry(x.desc)
  300. }
  301. type dynamicMap struct {
  302. desc pref.FieldDescriptor
  303. mapv map[interface{}]pref.Value
  304. }
  305. func (x *dynamicMap) Get(k pref.MapKey) pref.Value { return x.mapv[k.Interface()] }
  306. func (x *dynamicMap) Set(k pref.MapKey, v pref.Value) {
  307. typecheckSingular(x.desc.MapKey(), k.Value())
  308. typecheckSingular(x.desc.MapValue(), v)
  309. x.mapv[k.Interface()] = v
  310. }
  311. func (x *dynamicMap) Has(k pref.MapKey) bool { return x.Get(k).IsValid() }
  312. func (x *dynamicMap) Clear(k pref.MapKey) { delete(x.mapv, k.Interface()) }
  313. func (x *dynamicMap) Len() int { return len(x.mapv) }
  314. func (x *dynamicMap) NewValue() pref.Value {
  315. if md := x.desc.MapValue().Message(); md != nil {
  316. return pref.ValueOfMessage(NewMessage(md).ProtoReflect())
  317. }
  318. return x.desc.MapValue().Default()
  319. }
  320. func (x *dynamicMap) Range(f func(pref.MapKey, pref.Value) bool) {
  321. for k, v := range x.mapv {
  322. if !f(pref.ValueOf(k).MapKey(), v) {
  323. return
  324. }
  325. }
  326. }
  327. func isSet(fd pref.FieldDescriptor, v pref.Value) bool {
  328. switch {
  329. case fd.IsMap():
  330. return v.Map().Len() > 0
  331. case fd.IsList():
  332. return v.List().Len() > 0
  333. case fd.ContainingOneof() != nil:
  334. return true
  335. case fd.Syntax() == pref.Proto3:
  336. switch fd.Kind() {
  337. case pref.BoolKind:
  338. return v.Bool()
  339. case pref.EnumKind:
  340. return v.Enum() != 0
  341. case pref.Int32Kind, pref.Sint32Kind, pref.Int64Kind, pref.Sint64Kind, pref.Sfixed32Kind, pref.Sfixed64Kind:
  342. return v.Int() != 0
  343. case pref.Uint32Kind, pref.Uint64Kind, pref.Fixed32Kind, pref.Fixed64Kind:
  344. return v.Uint() != 0
  345. case pref.FloatKind, pref.DoubleKind:
  346. return v.Float() != 0 || math.Signbit(v.Float())
  347. case pref.StringKind:
  348. return v.String() != ""
  349. case pref.BytesKind:
  350. return len(v.Bytes()) > 0
  351. }
  352. }
  353. return true
  354. }
  355. func typecheck(fd pref.FieldDescriptor, v pref.Value) {
  356. if err := typeIsValid(fd, v); err != nil {
  357. panic(err)
  358. }
  359. }
  360. func typeIsValid(fd pref.FieldDescriptor, v pref.Value) error {
  361. switch {
  362. case fd.IsMap():
  363. if mapv, ok := v.Interface().(*dynamicMap); !ok || mapv.desc != fd {
  364. return errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface())
  365. }
  366. return nil
  367. case fd.IsList():
  368. switch list := v.Interface().(type) {
  369. case *dynamicList:
  370. if list.desc == fd {
  371. return nil
  372. }
  373. case emptyList:
  374. if list.desc == fd {
  375. return nil
  376. }
  377. }
  378. return errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface())
  379. default:
  380. return singularTypeIsValid(fd, v)
  381. }
  382. }
  383. func typecheckSingular(fd pref.FieldDescriptor, v pref.Value) {
  384. if err := singularTypeIsValid(fd, v); err != nil {
  385. panic(err)
  386. }
  387. }
  388. func singularTypeIsValid(fd pref.FieldDescriptor, v pref.Value) error {
  389. vi := v.Interface()
  390. var ok bool
  391. switch fd.Kind() {
  392. case pref.BoolKind:
  393. _, ok = vi.(bool)
  394. case pref.EnumKind:
  395. // We could check against the valid set of enum values, but do not.
  396. _, ok = vi.(pref.EnumNumber)
  397. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  398. _, ok = vi.(int32)
  399. case pref.Uint32Kind, pref.Fixed32Kind:
  400. _, ok = vi.(uint32)
  401. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  402. _, ok = vi.(int64)
  403. case pref.Uint64Kind, pref.Fixed64Kind:
  404. _, ok = vi.(uint64)
  405. case pref.FloatKind:
  406. _, ok = vi.(float32)
  407. case pref.DoubleKind:
  408. _, ok = vi.(float64)
  409. case pref.StringKind:
  410. _, ok = vi.(string)
  411. case pref.BytesKind:
  412. _, ok = vi.([]byte)
  413. case pref.MessageKind, pref.GroupKind:
  414. var m pref.Message
  415. m, ok = vi.(pref.Message)
  416. if ok && m.Descriptor().FullName() != fd.Message().FullName() {
  417. return errors.New("%v: assigning invalid message type %v", fd.FullName(), m.Descriptor().FullName())
  418. }
  419. if dm, ok := vi.(*Message); ok && dm.known == nil {
  420. return errors.New("%v: assigning invalid zero-value message", fd.FullName())
  421. }
  422. }
  423. if !ok {
  424. return errors.New("%v: assigning invalid type %T", fd.FullName(), v.Interface())
  425. }
  426. return nil
  427. }
  428. func newListEntry(fd pref.FieldDescriptor) pref.Value {
  429. switch fd.Kind() {
  430. case pref.BoolKind:
  431. return pref.ValueOfBool(false)
  432. case pref.EnumKind:
  433. return pref.ValueOfEnum(fd.Enum().Values().Get(0).Number())
  434. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  435. return pref.ValueOfInt32(0)
  436. case pref.Uint32Kind, pref.Fixed32Kind:
  437. return pref.ValueOfUint32(0)
  438. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  439. return pref.ValueOfInt64(0)
  440. case pref.Uint64Kind, pref.Fixed64Kind:
  441. return pref.ValueOfUint64(0)
  442. case pref.FloatKind:
  443. return pref.ValueOfFloat32(0)
  444. case pref.DoubleKind:
  445. return pref.ValueOfFloat64(0)
  446. case pref.StringKind:
  447. return pref.ValueOfString("")
  448. case pref.BytesKind:
  449. return pref.ValueOfBytes(nil)
  450. case pref.MessageKind, pref.GroupKind:
  451. return pref.ValueOfMessage(NewMessage(fd.Message()).ProtoReflect())
  452. }
  453. panic(errors.New("%v: unknown kind %v", fd.FullName(), fd.Kind()))
  454. }
  455. // extensionType is a dynamic protoreflect.ExtensionType.
  456. type extensionType struct {
  457. desc extensionTypeDescriptor
  458. }
  459. // NewExtensionType creates a new ExtensionType with the provided descriptor.
  460. //
  461. // Dynamic ExtensionTypes with the same descriptor compare as equal. That is,
  462. // if xd1 == xd2, then NewExtensionType(xd1) == NewExtensionType(xd2).
  463. //
  464. // The InterfaceOf and ValueOf methods of the extension type are defined as:
  465. //
  466. // func (xt extensionType) ValueOf(iv interface{}) protoreflect.Value {
  467. // return protoreflect.ValueOf(iv)
  468. // }
  469. //
  470. // func (xt extensionType) InterfaceOf(v protoreflect.Value) interface{} {
  471. // return v.Interface()
  472. // }
  473. //
  474. // The Go type used by the proto.GetExtension and proto.SetExtension functions
  475. // is determined by these methods, and is therefore equivalent to the Go type
  476. // used to represent a protoreflect.Value. See the protoreflect.Value
  477. // documentation for more details.
  478. func NewExtensionType(desc pref.ExtensionDescriptor) pref.ExtensionType {
  479. if xt, ok := desc.(pref.ExtensionTypeDescriptor); ok {
  480. desc = xt.Descriptor()
  481. }
  482. return extensionType{extensionTypeDescriptor{desc}}
  483. }
  484. func (xt extensionType) New() pref.Value {
  485. switch {
  486. case xt.desc.IsMap():
  487. return pref.ValueOfMap(&dynamicMap{
  488. desc: xt.desc,
  489. mapv: make(map[interface{}]pref.Value),
  490. })
  491. case xt.desc.IsList():
  492. return pref.ValueOfList(&dynamicList{desc: xt.desc})
  493. case xt.desc.Message() != nil:
  494. return pref.ValueOfMessage(NewMessage(xt.desc.Message()))
  495. default:
  496. return xt.desc.Default()
  497. }
  498. }
  499. func (xt extensionType) Zero() pref.Value {
  500. switch {
  501. case xt.desc.IsMap():
  502. return pref.ValueOfMap(&dynamicMap{desc: xt.desc})
  503. case xt.desc.Cardinality() == pref.Repeated:
  504. return pref.ValueOfList(emptyList{desc: xt.desc})
  505. case xt.desc.Message() != nil:
  506. return pref.ValueOfMessage(&Message{typ: messageType{xt.desc.Message()}})
  507. default:
  508. return xt.desc.Default()
  509. }
  510. }
  511. func (xt extensionType) TypeDescriptor() pref.ExtensionTypeDescriptor {
  512. return xt.desc
  513. }
  514. func (xt extensionType) ValueOf(iv interface{}) pref.Value {
  515. v := pref.ValueOf(iv)
  516. typecheck(xt.desc, v)
  517. return v
  518. }
  519. func (xt extensionType) InterfaceOf(v pref.Value) interface{} {
  520. typecheck(xt.desc, v)
  521. return v.Interface()
  522. }
  523. func (xt extensionType) IsValidInterface(iv interface{}) bool {
  524. return typeIsValid(xt.desc, pref.ValueOf(iv)) == nil
  525. }
  526. func (xt extensionType) IsValidValue(v pref.Value) bool {
  527. return typeIsValid(xt.desc, v) == nil
  528. }
  529. type extensionTypeDescriptor struct {
  530. pref.ExtensionDescriptor
  531. }
  532. func (xt extensionTypeDescriptor) Type() pref.ExtensionType {
  533. return extensionType{xt}
  534. }
  535. func (xt extensionTypeDescriptor) Descriptor() pref.ExtensionDescriptor {
  536. return xt.ExtensionDescriptor
  537. }