forked from google/agi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic.go
243 lines (221 loc) · 6.9 KB
/
dynamic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pack
import (
"fmt"
"io"
"math"
"sort"
"strings"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)
// Dynamic is a dynamic proto message.
type Dynamic struct {
Desc *descriptor.DescriptorProto
Fields map[string]interface{}
types *types
}
func (Dynamic) ProtoMessage() {}
func (d *Dynamic) Reset() {
d.Fields = map[string]interface{}{}
}
func (d Dynamic) String() string {
return fmt.Sprintf("%+v", d)
}
// Format implements fmt.Formatter to print the full value chain.
func (d Dynamic) Format(f fmt.State, r rune) {
switch r {
case 'v':
fields := []string{}
for n, v := range d.Fields {
suffix := ""
if bytes, ok := v.([]byte); ok && len(bytes) > 32 {
suffix = fmt.Sprintf(" (truncated %v bytes)", len(bytes))
v = bytes[:32]
}
if f.Flag('+') {
fields = append(fields, fmt.Sprintf("%v: %+v%v", n, v, suffix))
} else {
fields = append(fields, fmt.Sprintf("%v%v", v, suffix))
}
}
// TODO: we should sort the fields before formatting, so that we can exit out early, as soon
// as the requested number of chars has been reached.
sort.Strings(fields)
joined := strings.Join(fields, ", ")
if width, hasWidth := f.Width(); hasWidth && len(joined) > width {
joined = joined[0:width] + "..."
}
fmt.Fprintf(f, "%vᵈ{%s}", d.Desc.GetName(), joined)
}
}
func newDynamic(desc *descriptor.DescriptorProto, types *types) *Dynamic {
return &Dynamic{
Desc: desc,
Fields: map[string]interface{}{},
types: types,
}
}
func (d *Dynamic) Unmarshal(data []byte) error {
buf := proto.NewBuffer(data)
fields := map[int]*descriptor.FieldDescriptorProto{}
for _, f := range d.Desc.GetField() {
fields[int(f.GetNumber())] = f
}
for {
u, err := buf.DecodeVarint()
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
return err
}
wire, tag := int(u&0x7), int(u>>3)
if tag <= 0 {
return fmt.Errorf("illegal tag %d (wire type %d)", tag, wire)
}
val, err := d.read(wire, buf)
if err != nil {
return err
}
f, ok := fields[tag]
if !ok {
continue
}
wire = d.wireType(f.GetType())
if f.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED && wire != proto.WireBytes {
buf := proto.NewBuffer(val.([]byte))
arr := []interface{}{}
for {
val, err := d.read(wire, buf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
return err
}
el, err := d.unpack(val, f)
if err != nil {
return err
}
arr = append(arr, el)
}
val = arr
} else if f.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED {
arr, _ := d.Fields[f.GetName()].([]interface{})
if val, err = d.unpack(val, f); err != nil {
return err
}
val = append(arr, val)
} else {
if val, err = d.unpack(val, f); err != nil {
return err
}
}
d.Fields[f.GetName()] = val
}
return nil
}
func (d *Dynamic) wireType(t descriptor.FieldDescriptorProto_Type) int {
switch t {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
descriptor.FieldDescriptorProto_TYPE_FIXED64,
descriptor.FieldDescriptorProto_TYPE_SFIXED64:
return proto.WireFixed64
case descriptor.FieldDescriptorProto_TYPE_FLOAT,
descriptor.FieldDescriptorProto_TYPE_FIXED32,
descriptor.FieldDescriptorProto_TYPE_SFIXED32:
return proto.WireFixed32
case descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_SINT32,
descriptor.FieldDescriptorProto_TYPE_SINT64,
descriptor.FieldDescriptorProto_TYPE_BOOL,
descriptor.FieldDescriptorProto_TYPE_ENUM:
return proto.WireVarint
default:
return proto.WireBytes
}
}
func (d *Dynamic) read(wire int, buf *proto.Buffer) (interface{}, error) {
switch wire {
case proto.WireVarint:
return buf.DecodeVarint()
case proto.WireFixed64:
return buf.DecodeFixed64()
case proto.WireFixed32:
return buf.DecodeFixed32()
case proto.WireBytes:
return buf.DecodeRawBytes(true)
case proto.WireStartGroup, proto.WireEndGroup:
return nil, fmt.Errorf("wire groups are deprecated and not supported by dynamic")
default:
return nil, fmt.Errorf("unknown wire type %v", wire)
}
}
func (d *Dynamic) unpack(data interface{}, f *descriptor.FieldDescriptorProto) (interface{}, error) {
switch f.GetType() {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
return math.Float64frombits(data.(uint64)), nil
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
return math.Float32frombits((uint32)(data.(uint64))), nil
case descriptor.FieldDescriptorProto_TYPE_INT64:
return (int64)(data.(uint64)), nil
case descriptor.FieldDescriptorProto_TYPE_UINT64:
return data, nil
case descriptor.FieldDescriptorProto_TYPE_INT32:
return (int32)(data.(uint64)), nil
case descriptor.FieldDescriptorProto_TYPE_FIXED64:
return data, nil
case descriptor.FieldDescriptorProto_TYPE_FIXED32:
return (uint32)(data.(uint64)), nil
case descriptor.FieldDescriptorProto_TYPE_BOOL:
return data, nil
case descriptor.FieldDescriptorProto_TYPE_STRING:
return string(data.([]byte)), nil
case descriptor.FieldDescriptorProto_TYPE_GROUP:
return nil, fmt.Errorf("wire groups are deprecated and not supported by dynamic")
case descriptor.FieldDescriptorProto_TYPE_BYTES:
return data, nil
case descriptor.FieldDescriptorProto_TYPE_UINT32:
return uint32(data.(uint64)), nil
case descriptor.FieldDescriptorProto_TYPE_ENUM:
return data, nil
case descriptor.FieldDescriptorProto_TYPE_SFIXED32:
return (int32)(data.(uint64)), nil
case descriptor.FieldDescriptorProto_TYPE_SFIXED64:
return (int64)(data.(uint64)), nil
case descriptor.FieldDescriptorProto_TYPE_SINT32:
return (int32)(decodeZigzag(data.(uint64))), nil
case descriptor.FieldDescriptorProto_TYPE_SINT64:
return (int64)(decodeZigzag(data.(uint64))), nil
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
tyname := strings.TrimLeft(f.GetTypeName(), ".")
if subty, ok := d.types.byName[tyname]; ok {
msg := subty.create()
if err := proto.Unmarshal(data.([]byte), msg); err != nil {
return nil, err
}
return msg, nil
}
return nil, fmt.Errorf("Unknown type: %v", tyname)
}
return nil, fmt.Errorf("Unknown field type: %v", f.GetType())
}
func decodeZigzag(x uint64) uint64 {
return (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
}