message_field_unknown.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2018 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 impl
  5. import (
  6. "container/list"
  7. "reflect"
  8. "google.golang.org/protobuf/internal/encoding/wire"
  9. pref "google.golang.org/protobuf/reflect/protoreflect"
  10. )
  11. // TODO: Remove this file.
  12. var bytesType = reflect.TypeOf([]byte(nil))
  13. func makeLegacyUnknownFieldsFunc(t reflect.Type) func(p *messageDataType) pref.UnknownFields {
  14. fu, ok := t.FieldByName("XXX_unrecognized")
  15. if !ok || fu.Type != bytesType {
  16. return func(*messageDataType) pref.UnknownFields {
  17. return emptyUnknownFields{}
  18. }
  19. }
  20. fieldOffset := offsetOf(fu)
  21. return func(p *messageDataType) pref.UnknownFields {
  22. if p.p.IsNil() {
  23. return emptyUnknownFields{}
  24. }
  25. rv := p.p.Apply(fieldOffset).AsValueOf(bytesType)
  26. return (*legacyUnknownBytes)(rv.Interface().(*[]byte))
  27. }
  28. }
  29. // legacyUnknownBytes is a wrapper around XXX_unrecognized that implements
  30. // the protoreflect.UnknownFields interface. This is challenging since we are
  31. // limited to a []byte, so we do not have much flexibility in the choice
  32. // of data structure that would have been ideal.
  33. type legacyUnknownBytes []byte
  34. func (fs *legacyUnknownBytes) Len() int {
  35. // Runtime complexity: O(n)
  36. b := *fs
  37. m := map[pref.FieldNumber]bool{}
  38. for len(b) > 0 {
  39. num, _, n := wire.ConsumeField(b)
  40. m[num] = true
  41. b = b[n:]
  42. }
  43. return len(m)
  44. }
  45. func (fs *legacyUnknownBytes) Get(num pref.FieldNumber) (raw pref.RawFields) {
  46. // Runtime complexity: O(n)
  47. b := *fs
  48. for len(b) > 0 {
  49. num2, _, n := wire.ConsumeField(b)
  50. if num == num2 {
  51. raw = append(raw, b[:n]...)
  52. }
  53. b = b[n:]
  54. }
  55. return raw
  56. }
  57. func (fs *legacyUnknownBytes) Set(num pref.FieldNumber, raw pref.RawFields) {
  58. num2, _, _ := wire.ConsumeTag(raw)
  59. if len(raw) > 0 && (!raw.IsValid() || num != num2) {
  60. panic("invalid raw fields")
  61. }
  62. // Remove all current fields of num.
  63. // Runtime complexity: O(n)
  64. b := *fs
  65. out := (*fs)[:0]
  66. for len(b) > 0 {
  67. num2, _, n := wire.ConsumeField(b)
  68. if num != num2 {
  69. out = append(out, b[:n]...)
  70. }
  71. b = b[n:]
  72. }
  73. *fs = out
  74. // Append new fields of num.
  75. *fs = append(*fs, raw...)
  76. }
  77. func (fs *legacyUnknownBytes) Range(f func(pref.FieldNumber, pref.RawFields) bool) {
  78. type entry struct {
  79. num pref.FieldNumber
  80. raw pref.RawFields
  81. }
  82. // Collect up a list of all the raw fields.
  83. // We preserve the order such that the latest encountered fields
  84. // are presented at the end.
  85. //
  86. // Runtime complexity: O(n)
  87. b := *fs
  88. l := list.New()
  89. m := map[pref.FieldNumber]*list.Element{}
  90. for len(b) > 0 {
  91. num, _, n := wire.ConsumeField(b)
  92. if e, ok := m[num]; ok {
  93. x := e.Value.(*entry)
  94. x.raw = append(x.raw, b[:n]...)
  95. l.MoveToBack(e)
  96. } else {
  97. x := &entry{num: num}
  98. x.raw = append(x.raw, b[:n]...)
  99. m[num] = l.PushBack(x)
  100. }
  101. b = b[n:]
  102. }
  103. // Iterate over all the raw fields.
  104. // This ranges over a snapshot of the current state such that mutations
  105. // while ranging are not observable.
  106. //
  107. // Runtime complexity: O(n)
  108. for e := l.Front(); e != nil; e = e.Next() {
  109. x := e.Value.(*entry)
  110. if !f(x.num, x.raw) {
  111. return
  112. }
  113. }
  114. }
  115. func (fs *legacyUnknownBytes) IsSupported() bool {
  116. return true
  117. }