-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
262 lines (228 loc) · 7.81 KB
/
resolver.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2016 The LUCI Authors.
//
// 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 textproto implements a textproto config service Resolver.
//
// It uses the "luci-go" text protobuf multi-line extension. For more
// information, see: go.chromium.org/luci/common/proto
//
// The textproto protobuf Resolver internally formats its content as a binary
// protobuf, rather than its raw text content. This has some advantages over raw
// text content caching:
// - More space-efficient.
// - Decodes faster.
// - If the config service protobuf schema differs from the application's
// compiled schema, and the schema change is responsible (adding, renaming,
// repurposing) the binary cache value will continue to load where the text
// protobuf would fail.
package textproto
import (
"bytes"
"reflect"
"strings"
"github.com/TriggerMail/luci-go/common/data/cmpbin"
"github.com/TriggerMail/luci-go/common/errors"
luciProto "github.com/TriggerMail/luci-go/common/proto"
"github.com/TriggerMail/luci-go/config"
"github.com/TriggerMail/luci-go/config/server/cfgclient"
"github.com/TriggerMail/luci-go/config/server/cfgclient/backend/format"
"github.com/golang/protobuf/proto"
)
var typeOfProtoMessage = reflect.TypeOf((*proto.Message)(nil)).Elem()
// BinaryFormat is the resolver's binary protobuf format string.
const BinaryFormat = "github.com/TriggerMail/luci-go/server/config/TextProto:binary"
// init registers this format in the registry of formats.
func init() {
registerFormat()
}
func registerFormat() {
format.Register(BinaryFormat, &Formatter{})
}
// Message is a cfgclient.Resolver that resolves config data into proto.Message
// instances by parsing the config data as a text protobuf.
func Message(out proto.Message) interface {
cfgclient.Resolver
cfgclient.FormattingResolver
} {
if out == nil {
panic("cannot pass a nil proto.Message instance")
}
return &resolver{
messageName: proto.MessageName(out),
single: out,
singleType: reflect.TypeOf(out),
}
}
// Slice is a cfgclient.MultiResolver which resolves a slice of configurations
// into a TextProto.
//
// out must be a pointer to a slice of some proto.Message implementation. If it
// isn't, this function will panic.
//
// For example:
//
// var out []*MyProtoMessages
// TextProtoSlice(&out)
func Slice(out interface{}) interface {
cfgclient.MultiResolver
cfgclient.FormattingResolver
} {
r := resolver{}
v := reflect.ValueOf(out)
if !r.loadMulti(v) {
panic(errors.Reason("%s is not a pointer to a slice of protobuf message types", v.Type()).Err())
}
return &r
}
type resolver struct {
messageName string
single proto.Message
singleType reflect.Type
multiDest reflect.Value
}
func (r *resolver) loadMulti(v reflect.Value) bool {
t := v.Type()
if t.Kind() != reflect.Ptr {
return false
}
valElem := t.Elem()
if valElem.Kind() != reflect.Slice {
return false
}
sliceContent := valElem.Elem()
if sliceContent.Kind() != reflect.Ptr {
return false
}
if !sliceContent.Implements(typeOfProtoMessage) {
return false
}
r.messageName = proto.MessageName(archetypeMessage(sliceContent))
r.singleType = sliceContent
r.multiDest = v.Elem()
return true
}
// Resolve implements cfgclient.MultiResolver.
func (r *resolver) Resolve(it *config.Config) error {
return r.resolveItem(r.single, it.Content, it.FormatSpec.Formatter)
}
// PrepareMulti implements cfgclient.MultiResolver.
func (r *resolver) PrepareMulti(size int) {
slice := reflect.MakeSlice(r.multiDest.Type(), size, size)
r.multiDest.Set(slice)
}
// ResolveItemAt implements cfgclient.MultiResolver.
func (r *resolver) ResolveItemAt(i int, it *config.Config) error {
msgV := archetypeInstance(r.singleType)
if err := r.resolveItem(msgV.Interface().(proto.Message), it.Content, it.FormatSpec.Formatter); err != nil {
return err
}
r.multiDest.Index(i).Set(msgV)
return nil
}
func (r *resolver) resolveItem(out proto.Message, content string, format string) error {
switch format {
case "":
// Not formatted (text protobuf).
if err := luciProto.UnmarshalTextML(content, out); err != nil {
return errors.Annotate(err, "failed to unmarshal text protobuf").Err()
}
return nil
case BinaryFormat:
if err := parseBinaryContent(content, out); err != nil {
return errors.Annotate(err, "failed to unmarshal binary protobuf").Err()
}
return nil
default:
return errors.Reason("unsupported content format: %q", format).Err()
}
}
func (r *resolver) Format() config.FormatSpec {
return config.FormatSpec{BinaryFormat, r.messageName}
}
// Formatter is a cfgclient.Formatter implementation bound to a specific
// protobuf message.
//
// It takes a text protobuf representation of that message as input and returns
// a binary protobuf representation as output.
type Formatter struct{}
// FormatItem implements cfgclient.Formatter.
func (f *Formatter) FormatItem(c, fd string) (string, error) {
archetype := proto.MessageType(fd)
if archetype == nil {
return "", errors.Reason("unknown proto.Message type %q in formatter data", fd).Err()
}
msg := archetypeMessage(archetype)
// Convert from config to protobuf.
if err := luciProto.UnmarshalTextML(c, msg); err != nil {
return "", errors.Annotate(err, "failed to unmarshal text protobuf content").Err()
}
// Binary format.
bc, err := makeBinaryContent(msg)
if err != nil {
return "", errors.Annotate(err, "").Err()
}
return bc, nil
}
// t is a pointer to a proto.Message instance.
func archetypeInstance(t reflect.Type) reflect.Value {
return reflect.New(t.Elem())
}
func archetypeMessage(t reflect.Type) proto.Message {
return archetypeInstance(t).Interface().(proto.Message)
}
// makeBinaryContent constructs a binary content string from text protobuf
// content.
//
// The binary content is formatted by concatenating two "cmpbin" binary values
// together:
// [Message Name] | [Marshaled Message Data]
func makeBinaryContent(msg proto.Message) (string, error) {
d, err := proto.Marshal(msg)
if err != nil {
return "", errors.Annotate(err, "failed to marshal message").Err()
}
var buf bytes.Buffer
if _, err := cmpbin.WriteString(&buf, proto.MessageName(msg)); err != nil {
return "", errors.Annotate(err, "failed to write message name").Err()
}
if _, err := cmpbin.WriteBytes(&buf, d); err != nil {
return "", errors.Annotate(err, "failed to write binary message content").Err()
}
return buf.String(), nil
}
// parseBinaryContent parses a binary content string, pulling out the message
// type and marshalled message data. It then unmarshals the specified type into
// a new message based on the archetype.
//
// If the binary content's declared type doesn't match the archetype, or if the
// binary content is invalid, an error will be returned.
func parseBinaryContent(v string, msg proto.Message) error {
r := strings.NewReader(v)
encName, _, err := cmpbin.ReadString(r)
if err != nil {
return errors.Annotate(err, "failed to read message name").Err()
}
// Construct a message for this.
if name := proto.MessageName(msg); name != encName {
return errors.Reason("message name %q doesn't match encoded name %q", name, encName).Err()
}
// We have the right message, unmarshal.
d, _, err := cmpbin.ReadBytes(r)
if err != nil {
return errors.Annotate(err, "failed to read binary message content").Err()
}
if err := proto.Unmarshal(d, msg); err != nil {
return errors.Annotate(err, "failed to unmarshal message").Err()
}
return nil
}