Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 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 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
Joe Tsai | 2d5a169 | 2018-10-29 02:10:42 -0700 | [diff] [blame] | 8 | "container/list" |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 9 | "reflect" |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 10 | "sort" |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 11 | |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 12 | "github.com/golang/protobuf/v2/internal/encoding/wire" |
| 13 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 14 | ) |
| 15 | |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 16 | var bytesType = reflect.TypeOf([]byte(nil)) |
| 17 | |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 18 | func makeLegacyUnknownFieldsFunc(t reflect.Type) func(p *messageDataType) pref.UnknownFields { |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 19 | fu, ok := t.FieldByName("XXX_unrecognized") |
| 20 | if !ok || fu.Type != bytesType { |
| 21 | return nil |
| 22 | } |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 23 | fieldOffset := offsetOf(fu) |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 24 | unkFunc := func(p *messageDataType) pref.UnknownFields { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 25 | if p.p.IsNil() { |
| 26 | return emptyUnknownFields{} |
| 27 | } |
| 28 | rv := p.p.Apply(fieldOffset).AsValueOf(bytesType) |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 29 | return (*legacyUnknownBytes)(rv.Interface().(*[]byte)) |
| 30 | } |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 31 | extFunc := makeLegacyExtensionMapFunc(t) |
| 32 | if extFunc != nil { |
| 33 | return func(p *messageDataType) pref.UnknownFields { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 34 | if p.p.IsNil() { |
| 35 | return emptyUnknownFields{} |
| 36 | } |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 37 | return &legacyUnknownBytesAndExtensionMap{ |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 38 | unkFunc(p), extFunc(p), p.mi.PBType.ExtensionRanges(), |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | } |
| 42 | return unkFunc |
| 43 | } |
| 44 | |
| 45 | // legacyUnknownBytesAndExtensionMap is a wrapper around both XXX_unrecognized |
| 46 | // and also the extension field map. |
| 47 | type legacyUnknownBytesAndExtensionMap struct { |
| 48 | u pref.UnknownFields |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 49 | x legacyExtensionFieldsIface |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 50 | r pref.FieldRanges |
| 51 | } |
| 52 | |
| 53 | func (fs *legacyUnknownBytesAndExtensionMap) Len() int { |
| 54 | n := fs.u.Len() |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 55 | fs.x.Range(func(_ pref.FieldNumber, x ExtensionFieldV1) bool { |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 56 | if len(x.Raw) > 0 { |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 57 | n++ |
| 58 | } |
| 59 | return true |
| 60 | }) |
| 61 | return n |
| 62 | } |
| 63 | |
| 64 | func (fs *legacyUnknownBytesAndExtensionMap) Get(num pref.FieldNumber) (raw pref.RawFields) { |
| 65 | if fs.r.Has(num) { |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 66 | return fs.x.Get(num).Raw |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 67 | } |
| 68 | return fs.u.Get(num) |
| 69 | } |
| 70 | |
| 71 | func (fs *legacyUnknownBytesAndExtensionMap) Set(num pref.FieldNumber, raw pref.RawFields) { |
| 72 | if fs.r.Has(num) { |
| 73 | x := fs.x.Get(num) |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 74 | x.Raw = raw |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 75 | fs.x.Set(num, x) |
| 76 | return |
| 77 | } |
| 78 | fs.u.Set(num, raw) |
| 79 | } |
| 80 | |
| 81 | func (fs *legacyUnknownBytesAndExtensionMap) Range(f func(pref.FieldNumber, pref.RawFields) bool) { |
| 82 | // Range over unknown fields not in the extension range. |
| 83 | // Create a closure around f to capture whether iteration terminated early. |
| 84 | var stop bool |
| 85 | fs.u.Range(func(n pref.FieldNumber, b pref.RawFields) bool { |
| 86 | stop = stop || !f(n, b) |
| 87 | return !stop |
| 88 | }) |
| 89 | if stop { |
| 90 | return |
| 91 | } |
| 92 | |
| 93 | // Range over unknown fields in the extension range in ascending order |
| 94 | // to ensure protoreflect.UnknownFields.Range remains deterministic. |
| 95 | type entry struct { |
| 96 | num pref.FieldNumber |
| 97 | raw pref.RawFields |
| 98 | } |
| 99 | var xs []entry |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 100 | fs.x.Range(func(n pref.FieldNumber, x ExtensionFieldV1) bool { |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 101 | if len(x.Raw) > 0 { |
| 102 | xs = append(xs, entry{n, x.Raw}) |
Joe Tsai | 95b0290 | 2018-10-31 18:23:42 -0700 | [diff] [blame] | 103 | } |
| 104 | return true |
| 105 | }) |
| 106 | sort.Slice(xs, func(i, j int) bool { return xs[i].num < xs[j].num }) |
| 107 | for _, x := range xs { |
| 108 | if !f(x.num, x.raw) { |
| 109 | return |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func (fs *legacyUnknownBytesAndExtensionMap) IsSupported() bool { |
| 115 | return true |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // legacyUnknownBytes is a wrapper around XXX_unrecognized that implements |
| 119 | // the protoreflect.UnknownFields interface. This is challenging since we are |
| 120 | // limited to a []byte, so we do not have much flexibility in the choice |
| 121 | // of data structure that would have been ideal. |
| 122 | type legacyUnknownBytes []byte |
| 123 | |
| 124 | func (fs *legacyUnknownBytes) Len() int { |
| 125 | // Runtime complexity: O(n) |
| 126 | b := *fs |
| 127 | m := map[pref.FieldNumber]bool{} |
| 128 | for len(b) > 0 { |
| 129 | num, _, n := wire.ConsumeField(b) |
| 130 | m[num] = true |
| 131 | b = b[n:] |
| 132 | } |
| 133 | return len(m) |
| 134 | } |
| 135 | |
| 136 | func (fs *legacyUnknownBytes) Get(num pref.FieldNumber) (raw pref.RawFields) { |
| 137 | // Runtime complexity: O(n) |
| 138 | b := *fs |
| 139 | for len(b) > 0 { |
| 140 | num2, _, n := wire.ConsumeField(b) |
| 141 | if num == num2 { |
| 142 | raw = append(raw, b[:n]...) |
| 143 | } |
| 144 | b = b[n:] |
| 145 | } |
| 146 | return raw |
| 147 | } |
| 148 | |
| 149 | func (fs *legacyUnknownBytes) Set(num pref.FieldNumber, raw pref.RawFields) { |
| 150 | num2, _, _ := wire.ConsumeTag(raw) |
| 151 | if len(raw) > 0 && (!raw.IsValid() || num != num2) { |
| 152 | panic("invalid raw fields") |
| 153 | } |
| 154 | |
| 155 | // Remove all current fields of num. |
| 156 | // Runtime complexity: O(n) |
| 157 | b := *fs |
| 158 | out := (*fs)[:0] |
| 159 | for len(b) > 0 { |
| 160 | num2, _, n := wire.ConsumeField(b) |
| 161 | if num != num2 { |
| 162 | out = append(out, b[:n]...) |
| 163 | } |
| 164 | b = b[n:] |
| 165 | } |
| 166 | *fs = out |
| 167 | |
| 168 | // Append new fields of num. |
| 169 | *fs = append(*fs, raw...) |
| 170 | } |
| 171 | |
| 172 | func (fs *legacyUnknownBytes) Range(f func(pref.FieldNumber, pref.RawFields) bool) { |
| 173 | type entry struct { |
| 174 | num pref.FieldNumber |
| 175 | raw pref.RawFields |
| 176 | } |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 177 | |
| 178 | // Collect up a list of all the raw fields. |
| 179 | // We preserve the order such that the latest encountered fields |
| 180 | // are presented at the end. |
| 181 | // |
| 182 | // Runtime complexity: O(n) |
| 183 | b := *fs |
Joe Tsai | 2d5a169 | 2018-10-29 02:10:42 -0700 | [diff] [blame] | 184 | l := list.New() |
| 185 | m := map[pref.FieldNumber]*list.Element{} |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 186 | for len(b) > 0 { |
| 187 | num, _, n := wire.ConsumeField(b) |
Joe Tsai | 2d5a169 | 2018-10-29 02:10:42 -0700 | [diff] [blame] | 188 | if e, ok := m[num]; ok { |
| 189 | x := e.Value.(*entry) |
| 190 | x.raw = append(x.raw, b[:n]...) |
| 191 | l.MoveToBack(e) |
| 192 | } else { |
| 193 | x := &entry{num: num} |
| 194 | x.raw = append(x.raw, b[:n]...) |
| 195 | m[num] = l.PushBack(x) |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 196 | } |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 197 | b = b[n:] |
| 198 | } |
| 199 | |
| 200 | // Iterate over all the raw fields. |
| 201 | // This ranges over a snapshot of the current state such that mutations |
| 202 | // while ranging are not observable. |
| 203 | // |
| 204 | // Runtime complexity: O(n) |
Joe Tsai | 2d5a169 | 2018-10-29 02:10:42 -0700 | [diff] [blame] | 205 | for e := l.Front(); e != nil; e = e.Next() { |
| 206 | x := e.Value.(*entry) |
Joe Tsai | e2afdc2 | 2018-10-25 14:06:56 -0700 | [diff] [blame] | 207 | if !f(x.num, x.raw) { |
| 208 | return |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | func (fs *legacyUnknownBytes) IsSupported() bool { |
| 214 | return true |
| 215 | } |