blob: a8866874e8fbf45cf143a2609d8ca3ec7e6f3506 [file] [log] [blame]
Damien Neilc37adef2019-04-01 13:49:56 -07001// 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
5package impl
6
7import (
8 "sort"
9 "sync/atomic"
10
Damien Neilc37adef2019-04-01 13:49:56 -070011 proto "google.golang.org/protobuf/proto"
12 pref "google.golang.org/protobuf/reflect/protoreflect"
13 piface "google.golang.org/protobuf/runtime/protoiface"
14)
15
16type marshalOptions uint
17
18const (
19 marshalAllowPartial marshalOptions = 1 << iota
20 marshalDeterministic
21 marshalUseCachedSize
22)
23
24func newMarshalOptions(opts piface.MarshalOptions) marshalOptions {
25 var o marshalOptions
26 if opts.AllowPartial {
27 o |= marshalAllowPartial
28 }
29 if opts.Deterministic {
30 o |= marshalDeterministic
31 }
32 if opts.UseCachedSize {
33 o |= marshalUseCachedSize
34 }
35 return o
36}
37
38func (o marshalOptions) Options() proto.MarshalOptions {
39 return proto.MarshalOptions{
40 AllowPartial: o.AllowPartial(),
41 Deterministic: o.Deterministic(),
42 UseCachedSize: o.UseCachedSize(),
43 }
44}
45
46func (o marshalOptions) AllowPartial() bool { return o&marshalAllowPartial != 0 }
47func (o marshalOptions) Deterministic() bool { return o&marshalDeterministic != 0 }
48func (o marshalOptions) UseCachedSize() bool { return o&marshalUseCachedSize != 0 }
49
50// size is protoreflect.Methods.Size.
Joe Tsai4fe96632019-05-22 05:12:36 -040051func (mi *MessageInfo) size(msg pref.ProtoMessage) (size int) {
Damien Neilc37adef2019-04-01 13:49:56 -070052 return mi.sizePointer(pointerOfIface(msg), 0)
53}
54
Joe Tsai4fe96632019-05-22 05:12:36 -040055func (mi *MessageInfo) sizePointer(p pointer, opts marshalOptions) (size int) {
Damien Neilc37adef2019-04-01 13:49:56 -070056 mi.init()
57 if p.IsNil() {
58 return 0
59 }
60 if opts.UseCachedSize() && mi.sizecacheOffset.IsValid() {
61 return int(atomic.LoadInt32(p.Apply(mi.sizecacheOffset).Int32()))
62 }
63 return mi.sizePointerSlow(p, opts)
64}
65
Joe Tsai4fe96632019-05-22 05:12:36 -040066func (mi *MessageInfo) sizePointerSlow(p pointer, opts marshalOptions) (size int) {
Damien Neilc37adef2019-04-01 13:49:56 -070067 if mi.extensionOffset.IsValid() {
68 e := p.Apply(mi.extensionOffset).Extensions()
69 size += mi.sizeExtensions(e, opts)
70 }
71 for _, f := range mi.fieldsOrdered {
72 fptr := p.Apply(f.offset)
73 if f.isPointer && fptr.Elem().IsNil() {
74 continue
75 }
76 if f.funcs.size == nil {
77 continue
78 }
79 size += f.funcs.size(fptr, f.tagsize, opts)
80 }
81 if mi.unknownOffset.IsValid() {
82 u := *p.Apply(mi.unknownOffset).Bytes()
83 size += len(u)
84 }
85 if mi.sizecacheOffset.IsValid() {
86 atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size))
87 }
88 return size
89}
90
91// marshalAppend is protoreflect.Methods.MarshalAppend.
Joe Tsai4fe96632019-05-22 05:12:36 -040092func (mi *MessageInfo) marshalAppend(b []byte, msg pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) {
Damien Neilc37adef2019-04-01 13:49:56 -070093 return mi.marshalAppendPointer(b, pointerOfIface(msg), newMarshalOptions(opts))
94}
95
Joe Tsai4fe96632019-05-22 05:12:36 -040096func (mi *MessageInfo) marshalAppendPointer(b []byte, p pointer, opts marshalOptions) ([]byte, error) {
Damien Neilc37adef2019-04-01 13:49:56 -070097 mi.init()
98 if p.IsNil() {
99 return b, nil
100 }
101 var err error
Damien Neilc37adef2019-04-01 13:49:56 -0700102 // The old marshaler encodes extensions at beginning.
103 if mi.extensionOffset.IsValid() {
104 e := p.Apply(mi.extensionOffset).Extensions()
105 // TODO: Special handling for MessageSet?
106 b, err = mi.appendExtensions(b, e, opts)
Damien Neil8c86fc52019-06-19 09:28:29 -0700107 if err != nil {
Damien Neilc37adef2019-04-01 13:49:56 -0700108 return b, err
109 }
110 }
111 for _, f := range mi.fieldsOrdered {
112 fptr := p.Apply(f.offset)
113 if f.isPointer && fptr.Elem().IsNil() {
114 continue
115 }
116 if f.funcs.marshal == nil {
117 continue
118 }
119 b, err = f.funcs.marshal(b, fptr, f.wiretag, opts)
Damien Neil8c86fc52019-06-19 09:28:29 -0700120 if err != nil {
Damien Neilc37adef2019-04-01 13:49:56 -0700121 return b, err
122 }
123 }
124 if mi.unknownOffset.IsValid() {
125 u := *p.Apply(mi.unknownOffset).Bytes()
126 b = append(b, u...)
127 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700128 return b, nil
Damien Neilc37adef2019-04-01 13:49:56 -0700129}
130
Joe Tsai89d49632019-06-04 16:20:00 -0700131func (mi *MessageInfo) sizeExtensions(ext *map[int32]ExtensionField, opts marshalOptions) (n int) {
Damien Neilc37adef2019-04-01 13:49:56 -0700132 if ext == nil {
133 return 0
134 }
Joe Tsai89d49632019-06-04 16:20:00 -0700135 for _, x := range *ext {
136 xi := mi.extensionFieldInfo(x.GetType())
137 if xi.funcs.size == nil {
Damien Neilc37adef2019-04-01 13:49:56 -0700138 continue
139 }
Joe Tsai89d49632019-06-04 16:20:00 -0700140 n += xi.funcs.size(x.GetValue(), xi.tagsize, opts)
Damien Neilc37adef2019-04-01 13:49:56 -0700141 }
142 return n
143}
144
Joe Tsai89d49632019-06-04 16:20:00 -0700145func (mi *MessageInfo) appendExtensions(b []byte, ext *map[int32]ExtensionField, opts marshalOptions) ([]byte, error) {
Damien Neilc37adef2019-04-01 13:49:56 -0700146 if ext == nil {
147 return b, nil
148 }
149
150 switch len(*ext) {
151 case 0:
152 return b, nil
153 case 1:
154 // Fast-path for one extension: Don't bother sorting the keys.
155 var err error
Joe Tsai89d49632019-06-04 16:20:00 -0700156 for _, x := range *ext {
157 xi := mi.extensionFieldInfo(x.GetType())
158 b, err = xi.funcs.marshal(b, x.GetValue(), xi.wiretag, opts)
Damien Neilc37adef2019-04-01 13:49:56 -0700159 }
160 return b, err
161 default:
162 // Sort the keys to provide a deterministic encoding.
163 // Not sure this is required, but the old code does it.
164 keys := make([]int, 0, len(*ext))
165 for k := range *ext {
166 keys = append(keys, int(k))
167 }
168 sort.Ints(keys)
169 var err error
Damien Neilc37adef2019-04-01 13:49:56 -0700170 for _, k := range keys {
Joe Tsai89d49632019-06-04 16:20:00 -0700171 x := (*ext)[int32(k)]
172 xi := mi.extensionFieldInfo(x.GetType())
173 b, err = xi.funcs.marshal(b, x.GetValue(), xi.wiretag, opts)
Damien Neil8c86fc52019-06-19 09:28:29 -0700174 if err != nil {
Damien Neilc37adef2019-04-01 13:49:56 -0700175 return b, err
176 }
177 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700178 return b, nil
Damien Neilc37adef2019-04-01 13:49:56 -0700179 }
180}