Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 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 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "sort" |
| 9 | "sync/atomic" |
| 10 | |
| 11 | "google.golang.org/protobuf/internal/errors" |
| 12 | proto "google.golang.org/protobuf/proto" |
| 13 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 14 | piface "google.golang.org/protobuf/runtime/protoiface" |
| 15 | ) |
| 16 | |
| 17 | type marshalOptions uint |
| 18 | |
| 19 | const ( |
| 20 | marshalAllowPartial marshalOptions = 1 << iota |
| 21 | marshalDeterministic |
| 22 | marshalUseCachedSize |
| 23 | ) |
| 24 | |
| 25 | func newMarshalOptions(opts piface.MarshalOptions) marshalOptions { |
| 26 | var o marshalOptions |
| 27 | if opts.AllowPartial { |
| 28 | o |= marshalAllowPartial |
| 29 | } |
| 30 | if opts.Deterministic { |
| 31 | o |= marshalDeterministic |
| 32 | } |
| 33 | if opts.UseCachedSize { |
| 34 | o |= marshalUseCachedSize |
| 35 | } |
| 36 | return o |
| 37 | } |
| 38 | |
| 39 | func (o marshalOptions) Options() proto.MarshalOptions { |
| 40 | return proto.MarshalOptions{ |
| 41 | AllowPartial: o.AllowPartial(), |
| 42 | Deterministic: o.Deterministic(), |
| 43 | UseCachedSize: o.UseCachedSize(), |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func (o marshalOptions) AllowPartial() bool { return o&marshalAllowPartial != 0 } |
| 48 | func (o marshalOptions) Deterministic() bool { return o&marshalDeterministic != 0 } |
| 49 | func (o marshalOptions) UseCachedSize() bool { return o&marshalUseCachedSize != 0 } |
| 50 | |
| 51 | // size is protoreflect.Methods.Size. |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 52 | func (mi *MessageInfo) size(msg pref.ProtoMessage) (size int) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 53 | return mi.sizePointer(pointerOfIface(msg), 0) |
| 54 | } |
| 55 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 56 | func (mi *MessageInfo) sizePointer(p pointer, opts marshalOptions) (size int) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 57 | mi.init() |
| 58 | if p.IsNil() { |
| 59 | return 0 |
| 60 | } |
| 61 | if opts.UseCachedSize() && mi.sizecacheOffset.IsValid() { |
| 62 | return int(atomic.LoadInt32(p.Apply(mi.sizecacheOffset).Int32())) |
| 63 | } |
| 64 | return mi.sizePointerSlow(p, opts) |
| 65 | } |
| 66 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 67 | func (mi *MessageInfo) sizePointerSlow(p pointer, opts marshalOptions) (size int) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 68 | if mi.extensionOffset.IsValid() { |
| 69 | e := p.Apply(mi.extensionOffset).Extensions() |
| 70 | size += mi.sizeExtensions(e, opts) |
| 71 | } |
| 72 | for _, f := range mi.fieldsOrdered { |
| 73 | fptr := p.Apply(f.offset) |
| 74 | if f.isPointer && fptr.Elem().IsNil() { |
| 75 | continue |
| 76 | } |
| 77 | if f.funcs.size == nil { |
| 78 | continue |
| 79 | } |
| 80 | size += f.funcs.size(fptr, f.tagsize, opts) |
| 81 | } |
| 82 | if mi.unknownOffset.IsValid() { |
| 83 | u := *p.Apply(mi.unknownOffset).Bytes() |
| 84 | size += len(u) |
| 85 | } |
| 86 | if mi.sizecacheOffset.IsValid() { |
| 87 | atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size)) |
| 88 | } |
| 89 | return size |
| 90 | } |
| 91 | |
| 92 | // marshalAppend is protoreflect.Methods.MarshalAppend. |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 93 | func (mi *MessageInfo) marshalAppend(b []byte, msg pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 94 | return mi.marshalAppendPointer(b, pointerOfIface(msg), newMarshalOptions(opts)) |
| 95 | } |
| 96 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 97 | func (mi *MessageInfo) marshalAppendPointer(b []byte, p pointer, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 98 | mi.init() |
| 99 | if p.IsNil() { |
| 100 | return b, nil |
| 101 | } |
| 102 | var err error |
| 103 | var nerr errors.NonFatal |
| 104 | // The old marshaler encodes extensions at beginning. |
| 105 | if mi.extensionOffset.IsValid() { |
| 106 | e := p.Apply(mi.extensionOffset).Extensions() |
| 107 | // TODO: Special handling for MessageSet? |
| 108 | b, err = mi.appendExtensions(b, e, opts) |
| 109 | if !nerr.Merge(err) { |
| 110 | return b, err |
| 111 | } |
| 112 | } |
| 113 | for _, f := range mi.fieldsOrdered { |
| 114 | fptr := p.Apply(f.offset) |
| 115 | if f.isPointer && fptr.Elem().IsNil() { |
| 116 | continue |
| 117 | } |
| 118 | if f.funcs.marshal == nil { |
| 119 | continue |
| 120 | } |
| 121 | b, err = f.funcs.marshal(b, fptr, f.wiretag, opts) |
| 122 | if !nerr.Merge(err) { |
| 123 | return b, err |
| 124 | } |
| 125 | } |
| 126 | if mi.unknownOffset.IsValid() { |
| 127 | u := *p.Apply(mi.unknownOffset).Bytes() |
| 128 | b = append(b, u...) |
| 129 | } |
| 130 | return b, nerr.E |
| 131 | } |
| 132 | |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame^] | 133 | func (mi *MessageInfo) sizeExtensions(ext *map[int32]ExtensionField, opts marshalOptions) (n int) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 134 | if ext == nil { |
| 135 | return 0 |
| 136 | } |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame^] | 137 | for _, x := range *ext { |
| 138 | xi := mi.extensionFieldInfo(x.GetType()) |
| 139 | if xi.funcs.size == nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 140 | continue |
| 141 | } |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame^] | 142 | n += xi.funcs.size(x.GetValue(), xi.tagsize, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 143 | } |
| 144 | return n |
| 145 | } |
| 146 | |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame^] | 147 | func (mi *MessageInfo) appendExtensions(b []byte, ext *map[int32]ExtensionField, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 148 | if ext == nil { |
| 149 | return b, nil |
| 150 | } |
| 151 | |
| 152 | switch len(*ext) { |
| 153 | case 0: |
| 154 | return b, nil |
| 155 | case 1: |
| 156 | // Fast-path for one extension: Don't bother sorting the keys. |
| 157 | var err error |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame^] | 158 | for _, x := range *ext { |
| 159 | xi := mi.extensionFieldInfo(x.GetType()) |
| 160 | b, err = xi.funcs.marshal(b, x.GetValue(), xi.wiretag, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 161 | } |
| 162 | return b, err |
| 163 | default: |
| 164 | // Sort the keys to provide a deterministic encoding. |
| 165 | // Not sure this is required, but the old code does it. |
| 166 | keys := make([]int, 0, len(*ext)) |
| 167 | for k := range *ext { |
| 168 | keys = append(keys, int(k)) |
| 169 | } |
| 170 | sort.Ints(keys) |
| 171 | var err error |
| 172 | var nerr errors.NonFatal |
| 173 | for _, k := range keys { |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame^] | 174 | x := (*ext)[int32(k)] |
| 175 | xi := mi.extensionFieldInfo(x.GetType()) |
| 176 | b, err = xi.funcs.marshal(b, x.GetValue(), xi.wiretag, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 177 | if !nerr.Merge(err) { |
| 178 | return b, err |
| 179 | } |
| 180 | } |
| 181 | return b, nerr.E |
| 182 | } |
| 183 | } |