blob: ae0453c9dad563156150ca24d38bd0037e230256 [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2010 Google Inc. All rights reserved.
4// http://code.google.com/p/goprotobuf/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32package proto
33
David Symondse37856c2011-06-22 12:52:53 +100034// Functions for writing the text protocol buffer format.
Rob Pikeaaa3a622010-03-20 22:32:34 -070035
36import (
37 "bytes"
38 "fmt"
39 "io"
David Symondse37856c2011-06-22 12:52:53 +100040 "log"
Rob Pikeaaa3a622010-03-20 22:32:34 -070041 "os"
42 "reflect"
David Symonds1d72f7a2011-08-19 18:28:52 +100043 "sort"
Rob Pikeaaa3a622010-03-20 22:32:34 -070044 "strings"
45)
46
David Symondse37856c2011-06-22 12:52:53 +100047// textWriter is an io.Writer that tracks its indentation level.
Rob Pikeaaa3a622010-03-20 22:32:34 -070048type textWriter struct {
David Symondse37856c2011-06-22 12:52:53 +100049 ind int
50 complete bool // if the current position is a complete line
51 compact bool // whether to write out as a one-liner
52 writer io.Writer
David Symonds9f402812011-04-28 18:08:44 +100053
54 c [1]byte // scratch
Rob Pikeaaa3a622010-03-20 22:32:34 -070055}
56
Rob Pikea17fdd92011-11-02 12:43:05 -070057func (w *textWriter) Write(p []byte) (n int, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -070058 n, err = len(p), nil
59
David Symonds8935abf2011-07-04 15:53:16 +100060 frags := strings.Split(string(p), "\n")
Rob Pikeaaa3a622010-03-20 22:32:34 -070061 if w.compact {
62 w.writer.Write([]byte(strings.Join(frags, " ")))
63 return
64 }
65
David Symondse37856c2011-06-22 12:52:53 +100066 for i, frag := range frags {
Rob Pikeaaa3a622010-03-20 22:32:34 -070067 if w.complete {
David Symondse37856c2011-06-22 12:52:53 +100068 for j := 0; j < w.ind; j++ {
Rob Pikeaaa3a622010-03-20 22:32:34 -070069 w.writer.Write([]byte{' ', ' '})
70 }
71 w.complete = false
72 }
73
David Symondse37856c2011-06-22 12:52:53 +100074 w.writer.Write([]byte(frag))
Rob Pikeaaa3a622010-03-20 22:32:34 -070075 if i+1 < len(frags) {
76 w.writer.Write([]byte{'\n'})
77 }
78 }
79 w.complete = len(frags[len(frags)-1]) == 0
80
81 return
82}
83
Rob Pikea17fdd92011-11-02 12:43:05 -070084func (w *textWriter) WriteByte(c byte) error {
David Symonds9f402812011-04-28 18:08:44 +100085 w.c[0] = c
86 _, err := w.Write(w.c[:])
87 return err
88}
89
David Symondse37856c2011-06-22 12:52:53 +100090func (w *textWriter) indent() { w.ind++ }
Rob Pikeaaa3a622010-03-20 22:32:34 -070091
92func (w *textWriter) unindent() {
David Symondse37856c2011-06-22 12:52:53 +100093 if w.ind == 0 {
94 log.Printf("proto: textWriter unindented too far!")
95 return
Rob Pikeaaa3a622010-03-20 22:32:34 -070096 }
David Symondse37856c2011-06-22 12:52:53 +100097 w.ind--
Rob Pikeaaa3a622010-03-20 22:32:34 -070098}
99
David Symonds9f402812011-04-28 18:08:44 +1000100func writeName(w *textWriter, props *Properties) {
101 io.WriteString(w, props.OrigName)
102 if props.Wire != "group" {
103 w.WriteByte(':')
104 }
105}
106
David Symonds1d72f7a2011-08-19 18:28:52 +1000107var (
108 messageSetType = reflect.TypeOf((*MessageSet)(nil)).Elem()
109 extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
110)
David Symondse37856c2011-06-22 12:52:53 +1000111
David Symondse9e7aaf2012-03-23 13:12:33 +1100112// raw is the interface satisfied by RawMessage.
113type raw interface {
114 Bytes() []byte
115}
116
Rob Pike97e934d2011-04-11 12:52:49 -0700117func writeStruct(w *textWriter, sv reflect.Value) {
David Symonds1d72f7a2011-08-19 18:28:52 +1000118 if sv.Type() == messageSetType {
119 writeMessageSet(w, sv.Addr().Interface().(*MessageSet))
120 return
121 }
122
Rob Pike97e934d2011-04-11 12:52:49 -0700123 st := sv.Type()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700124 sprops := GetProperties(st)
125 for i := 0; i < sv.NumField(); i++ {
David Symonds1d72f7a2011-08-19 18:28:52 +1000126 fv := sv.Field(i)
127 if name := st.Field(i).Name; strings.HasPrefix(name, "XXX_") {
128 // There's only two XXX_ fields:
129 // XXX_unrecognized []byte
130 // XXX_extensions map[int32]proto.Extension
131 // The first is handled here;
132 // the second is handled at the bottom of this function.
133 if name == "XXX_unrecognized" && !fv.IsNil() {
134 writeUnknownStruct(w, fv.Interface().([]byte))
135 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700136 continue
137 }
138 props := sprops.Prop[i]
Rob Pikeac8b1ce2011-04-11 16:14:54 -0700139 if fv.Kind() == reflect.Ptr && fv.IsNil() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700140 // Field not filled in. This could be an optional field or
141 // a required field that wasn't filled in. Either way, there
142 // isn't anything we can show for it.
143 continue
144 }
Rob Pikeac8b1ce2011-04-11 16:14:54 -0700145 if fv.Kind() == reflect.Slice && fv.IsNil() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700146 // Repeated field that is empty, or a bytes field that is unused.
147 continue
148 }
149
David Symondsaa922ff2011-07-19 14:58:06 +1000150 if props.Repeated && fv.Kind() == reflect.Slice {
151 // Repeated field.
152 for j := 0; j < fv.Len(); j++ {
153 writeName(w, props)
154 if !w.compact {
155 w.WriteByte(' ')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700156 }
David Symondsaa922ff2011-07-19 14:58:06 +1000157 writeAny(w, fv.Index(j), props)
158 w.WriteByte('\n')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700159 }
David Symondsaa922ff2011-07-19 14:58:06 +1000160 continue
Rob Pikeaaa3a622010-03-20 22:32:34 -0700161 }
162
David Symonds9f402812011-04-28 18:08:44 +1000163 writeName(w, props)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700164 if !w.compact {
David Symonds9f402812011-04-28 18:08:44 +1000165 w.WriteByte(' ')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700166 }
David Symondse9e7aaf2012-03-23 13:12:33 +1100167 if b, ok := fv.Interface().(raw); ok {
168 writeRaw(w, b.Bytes())
169 continue
170 }
David Symondse37856c2011-06-22 12:52:53 +1000171 if props.Enum != "" && tryWriteEnum(w, props.Enum, fv) {
172 // Enum written.
173 } else {
David Symonds9f402812011-04-28 18:08:44 +1000174 writeAny(w, fv, props)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700175 }
David Symondse37856c2011-06-22 12:52:53 +1000176 w.WriteByte('\n')
177 }
178
David Symonds1d72f7a2011-08-19 18:28:52 +1000179 // Extensions (the XXX_extensions field).
David Symondse37856c2011-06-22 12:52:53 +1000180 pv := sv.Addr()
181 if pv.Type().Implements(extendableProtoType) {
182 writeExtensions(w, pv)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700183 }
184}
185
David Symondse9e7aaf2012-03-23 13:12:33 +1100186// writeRaw writes an uninterpreted raw message.
187func writeRaw(w *textWriter, b []byte) {
188 w.WriteByte('<')
189 if !w.compact {
190 w.WriteByte('\n')
191 }
192 w.indent()
193 writeUnknownStruct(w, b)
194 w.unindent()
195 w.WriteByte('>')
196}
197
David Symondse37856c2011-06-22 12:52:53 +1000198// tryWriteEnum attempts to write an enum value as a symbolic constant.
199// If the enum is unregistered, nothing is written and false is returned.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700200func tryWriteEnum(w *textWriter, enum string, v reflect.Value) bool {
Rob Pikeab5b8022010-06-21 17:47:58 -0700201 v = reflect.Indirect(v)
202 if v.Type().Kind() != reflect.Int32 {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700203 return false
204 }
205 m, ok := enumNameMaps[enum]
206 if !ok {
207 return false
208 }
Rob Pike97e934d2011-04-11 12:52:49 -0700209 str, ok := m[int32(v.Int())]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700210 if !ok {
211 return false
212 }
213 fmt.Fprintf(w, str)
214 return true
215}
216
David Symondse37856c2011-06-22 12:52:53 +1000217// writeAny writes an arbitrary field.
David Symonds9f402812011-04-28 18:08:44 +1000218func writeAny(w *textWriter, v reflect.Value, props *Properties) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700219 v = reflect.Indirect(v)
220
221 // We don't attempt to serialise every possible value type; only those
222 // that can occur in protocol buffers, plus a few extra that were easy.
David Symondse37856c2011-06-22 12:52:53 +1000223 switch v.Kind() {
Rob Pike97e934d2011-04-11 12:52:49 -0700224 case reflect.Slice:
Rob Pikeaaa3a622010-03-20 22:32:34 -0700225 // Should only be a []byte; repeated fields are handled in writeStruct.
David Symonds4c95bfe2011-09-13 14:43:27 +1000226 writeString(w, string(v.Interface().([]byte)))
Rob Pike97e934d2011-04-11 12:52:49 -0700227 case reflect.String:
David Symonds4c95bfe2011-09-13 14:43:27 +1000228 writeString(w, v.String())
Rob Pike97e934d2011-04-11 12:52:49 -0700229 case reflect.Struct:
Rob Pikeaaa3a622010-03-20 22:32:34 -0700230 // Required/optional group/message.
David Symonds9f402812011-04-28 18:08:44 +1000231 var bra, ket byte = '<', '>'
232 if props != nil && props.Wire == "group" {
233 bra, ket = '{', '}'
234 }
235 w.WriteByte(bra)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700236 if !w.compact {
David Symonds9f402812011-04-28 18:08:44 +1000237 w.WriteByte('\n')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700238 }
239 w.indent()
David Symondse37856c2011-06-22 12:52:53 +1000240 writeStruct(w, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700241 w.unindent()
David Symonds9f402812011-04-28 18:08:44 +1000242 w.WriteByte(ket)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700243 default:
David Symondse37856c2011-06-22 12:52:53 +1000244 fmt.Fprint(w, v.Interface())
245 }
246}
247
David Symonds4c95bfe2011-09-13 14:43:27 +1000248// equivalent to C's isprint.
249func isprint(c byte) bool {
250 return c >= 0x20 && c < 0x7f
251}
252
253// writeString writes a string in the protocol buffer text format.
254// It is similar to strconv.Quote except we don't use Go escape sequences,
255// we treat the string as a byte sequence, and we use octal escapes.
256// These differences are to maintain interoperability with the other
257// languages' implementations of the text format.
258func writeString(w *textWriter, s string) {
259 w.WriteByte('"')
260
261 // Loop over the bytes, not the runes.
262 for i := 0; i < len(s); i++ {
263 // Divergence from C++: we don't escape apostrophes.
264 // There's no need to escape them, and the C++ parser
265 // copes with a naked apostrophe.
266 switch c := s[i]; c {
267 case '\n':
268 w.Write([]byte{'\\', 'n'})
269 case '\r':
270 w.Write([]byte{'\\', 'r'})
271 case '\t':
272 w.Write([]byte{'\\', 't'})
273 case '"':
274 w.Write([]byte{'\\', '"'})
275 case '\\':
276 w.Write([]byte{'\\', '\\'})
277 default:
278 if isprint(c) {
279 w.WriteByte(c)
280 } else {
281 fmt.Fprintf(w, "\\%03o", c)
282 }
283 }
284 }
285
286 w.WriteByte('"')
287}
288
David Symonds1d72f7a2011-08-19 18:28:52 +1000289func writeMessageSet(w *textWriter, ms *MessageSet) {
290 for _, item := range ms.Item {
291 id := *item.TypeId
292 if msd, ok := messageSetMap[id]; ok {
293 // Known message set type.
294 fmt.Fprintf(w, "[%s]: <\n", msd.name)
295 w.indent()
296
297 pb := reflect.New(msd.t.Elem())
298 if err := Unmarshal(item.Message, pb.Interface()); err != nil {
299 fmt.Fprintf(w, "/* bad message: %v */\n", err)
300 } else {
301 writeStruct(w, pb.Elem())
302 }
303 } else {
304 // Unknown type.
305 fmt.Fprintf(w, "[%d]: <\n", id)
306 w.indent()
307 writeUnknownStruct(w, item.Message)
308 }
309 w.unindent()
310 w.Write([]byte(">\n"))
311 }
312}
313
314func writeUnknownStruct(w *textWriter, data []byte) {
315 if !w.compact {
316 fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data))
317 }
318 b := NewBuffer(data)
319 for b.index < len(b.buf) {
320 x, err := b.DecodeVarint()
321 if err != nil {
322 fmt.Fprintf(w, "/* %v */\n", err)
323 return
324 }
325 wire, tag := x&7, x>>3
326 if wire == WireEndGroup {
327 w.unindent()
328 w.Write([]byte("}\n"))
329 continue
330 }
331 fmt.Fprintf(w, "tag%d", tag)
332 if wire != WireStartGroup {
333 w.WriteByte(':')
334 }
335 if !w.compact || wire == WireStartGroup {
336 w.WriteByte(' ')
337 }
338 switch wire {
339 case WireBytes:
340 buf, err := b.DecodeRawBytes(false)
341 if err == nil {
342 fmt.Fprintf(w, "%q", buf)
343 } else {
344 fmt.Fprintf(w, "/* %v */", err)
345 }
346 case WireFixed32:
347 x, err := b.DecodeFixed32()
348 writeUnknownInt(w, x, err)
349 case WireFixed64:
350 x, err := b.DecodeFixed64()
351 writeUnknownInt(w, x, err)
352 case WireStartGroup:
353 fmt.Fprint(w, "{")
354 w.indent()
355 case WireVarint:
356 x, err := b.DecodeVarint()
357 writeUnknownInt(w, x, err)
358 default:
359 fmt.Fprintf(w, "/* unknown wire type %d */", wire)
360 }
361 w.WriteByte('\n')
362 }
363}
364
Rob Pikea17fdd92011-11-02 12:43:05 -0700365func writeUnknownInt(w *textWriter, x uint64, err error) {
David Symonds1d72f7a2011-08-19 18:28:52 +1000366 if err == nil {
367 fmt.Fprint(w, x)
368 } else {
369 fmt.Fprintf(w, "/* %v */", err)
370 }
371}
372
373type int32Slice []int32
374
375func (s int32Slice) Len() int { return len(s) }
376func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
377func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
378
David Symondse37856c2011-06-22 12:52:53 +1000379// writeExtensions writes all the extensions in pv.
380// pv is assumed to be a pointer to a protocol message struct that is extendable.
381func writeExtensions(w *textWriter, pv reflect.Value) {
382 emap := extensionMaps[pv.Type().Elem()]
383 ep := pv.Interface().(extendableProto)
David Symonds1d72f7a2011-08-19 18:28:52 +1000384
385 // Order the extensions by ID.
386 // This isn't strictly necessary, but it will give us
387 // canonical output, which will also make testing easier.
388 m := ep.ExtensionMap()
389 ids := make([]int32, 0, len(m))
390 for id := range m {
391 ids = append(ids, id)
392 }
393 sort.Sort(int32Slice(ids))
394
395 for _, extNum := range ids {
396 ext := m[extNum]
David Symondse37856c2011-06-22 12:52:53 +1000397 var desc *ExtensionDesc
398 if emap != nil {
399 desc = emap[extNum]
400 }
401 if desc == nil {
David Symonds1d72f7a2011-08-19 18:28:52 +1000402 // Unknown extension.
403 writeUnknownStruct(w, ext.enc)
David Symondse37856c2011-06-22 12:52:53 +1000404 continue
405 }
406
407 pb, err := GetExtension(ep, desc)
408 if err != nil {
409 fmt.Fprintln(os.Stderr, "proto: failed getting extension: ", err)
410 continue
411 }
412
413 fmt.Fprintf(w, "[%s]:", desc.Name)
414 if !w.compact {
415 w.WriteByte(' ')
416 }
417 writeAny(w, reflect.ValueOf(pb), nil)
418 w.WriteByte('\n')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700419 }
420}
421
422func marshalText(w io.Writer, pb interface{}, compact bool) {
David Symonds03c9d412010-08-26 14:23:18 +1000423 if pb == nil {
424 w.Write([]byte("<nil>"))
425 return
426 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700427 aw := new(textWriter)
428 aw.writer = w
429 aw.complete = true
430 aw.compact = compact
431
David Symonds92dd6c12012-03-23 10:59:49 +1100432 // Reject non-pointer inputs (it's a bad practice to pass potentially large protos around by value).
Nigel Tao4ede8452011-04-28 11:27:25 +1000433 v := reflect.ValueOf(pb)
David Symonds92dd6c12012-03-23 10:59:49 +1100434 if v.Kind() != reflect.Ptr {
435 w.Write([]byte("<struct-by-value>"))
436 return
437 }
438
439 // Dereference the received pointer so we don't have outer < and >.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700440 v = reflect.Indirect(v)
David Symonds92dd6c12012-03-23 10:59:49 +1100441
David Symondsa9cda212011-04-15 01:23:17 -0700442 if v.Kind() == reflect.Struct {
443 writeStruct(aw, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700444 } else {
David Symonds9f402812011-04-28 18:08:44 +1000445 writeAny(aw, v, nil)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700446 }
447}
448
David Symondse37856c2011-06-22 12:52:53 +1000449// MarshalText writes a given protocol buffer in text format.
450// Values that are not protocol buffers can also be written, but their formatting is not guaranteed.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700451func MarshalText(w io.Writer, pb interface{}) { marshalText(w, pb, false) }
452
David Symondsd2bff3c2012-03-14 10:45:25 +1100453// MarshalTextString is the same as MarshalText, but returns the string directly.
454func MarshalTextString(pb interface{}) string {
455 var buf bytes.Buffer
456 marshalText(&buf, pb, false)
457 return buf.String()
458}
459
David Symondse37856c2011-06-22 12:52:53 +1000460// CompactText writes a given protocl buffer in compact text format (one line).
461// Values that are not protocol buffers can also be written, but their formatting is not guaranteed.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700462func CompactText(w io.Writer, pb interface{}) { marshalText(w, pb, true) }
463
464// CompactTextString is the same as CompactText, but returns the string directly.
465func CompactTextString(pb interface{}) string {
466 buf := new(bytes.Buffer)
467 marshalText(buf, pb, true)
468 return buf.String()
469}