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