blob: d781c68e60decde74d10d0c633cc82615d6a9128 [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
Rob Pike97e934d2011-04-11 12:52:49 -0700112func writeStruct(w *textWriter, sv reflect.Value) {
David Symonds1d72f7a2011-08-19 18:28:52 +1000113 if sv.Type() == messageSetType {
114 writeMessageSet(w, sv.Addr().Interface().(*MessageSet))
115 return
116 }
117
Rob Pike97e934d2011-04-11 12:52:49 -0700118 st := sv.Type()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700119 sprops := GetProperties(st)
120 for i := 0; i < sv.NumField(); i++ {
David Symonds1d72f7a2011-08-19 18:28:52 +1000121 fv := sv.Field(i)
122 if name := st.Field(i).Name; strings.HasPrefix(name, "XXX_") {
123 // There's only two XXX_ fields:
124 // XXX_unrecognized []byte
125 // XXX_extensions map[int32]proto.Extension
126 // The first is handled here;
127 // the second is handled at the bottom of this function.
128 if name == "XXX_unrecognized" && !fv.IsNil() {
129 writeUnknownStruct(w, fv.Interface().([]byte))
130 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700131 continue
132 }
133 props := sprops.Prop[i]
Rob Pikeac8b1ce2011-04-11 16:14:54 -0700134 if fv.Kind() == reflect.Ptr && fv.IsNil() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700135 // Field not filled in. This could be an optional field or
136 // a required field that wasn't filled in. Either way, there
137 // isn't anything we can show for it.
138 continue
139 }
Rob Pikeac8b1ce2011-04-11 16:14:54 -0700140 if fv.Kind() == reflect.Slice && fv.IsNil() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700141 // Repeated field that is empty, or a bytes field that is unused.
142 continue
143 }
144
David Symondsaa922ff2011-07-19 14:58:06 +1000145 if props.Repeated && fv.Kind() == reflect.Slice {
146 // Repeated field.
147 for j := 0; j < fv.Len(); j++ {
148 writeName(w, props)
149 if !w.compact {
150 w.WriteByte(' ')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700151 }
David Symondsaa922ff2011-07-19 14:58:06 +1000152 writeAny(w, fv.Index(j), props)
153 w.WriteByte('\n')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700154 }
David Symondsaa922ff2011-07-19 14:58:06 +1000155 continue
Rob Pikeaaa3a622010-03-20 22:32:34 -0700156 }
157
David Symonds9f402812011-04-28 18:08:44 +1000158 writeName(w, props)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700159 if !w.compact {
David Symonds9f402812011-04-28 18:08:44 +1000160 w.WriteByte(' ')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700161 }
David Symondse37856c2011-06-22 12:52:53 +1000162 if props.Enum != "" && tryWriteEnum(w, props.Enum, fv) {
163 // Enum written.
164 } else {
David Symonds9f402812011-04-28 18:08:44 +1000165 writeAny(w, fv, props)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700166 }
David Symondse37856c2011-06-22 12:52:53 +1000167 w.WriteByte('\n')
168 }
169
David Symonds1d72f7a2011-08-19 18:28:52 +1000170 // Extensions (the XXX_extensions field).
David Symondse37856c2011-06-22 12:52:53 +1000171 pv := sv.Addr()
172 if pv.Type().Implements(extendableProtoType) {
173 writeExtensions(w, pv)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700174 }
175}
176
David Symondse37856c2011-06-22 12:52:53 +1000177// tryWriteEnum attempts to write an enum value as a symbolic constant.
178// If the enum is unregistered, nothing is written and false is returned.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700179func tryWriteEnum(w *textWriter, enum string, v reflect.Value) bool {
Rob Pikeab5b8022010-06-21 17:47:58 -0700180 v = reflect.Indirect(v)
181 if v.Type().Kind() != reflect.Int32 {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700182 return false
183 }
184 m, ok := enumNameMaps[enum]
185 if !ok {
186 return false
187 }
Rob Pike97e934d2011-04-11 12:52:49 -0700188 str, ok := m[int32(v.Int())]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700189 if !ok {
190 return false
191 }
192 fmt.Fprintf(w, str)
193 return true
194}
195
David Symondse37856c2011-06-22 12:52:53 +1000196// writeAny writes an arbitrary field.
David Symonds9f402812011-04-28 18:08:44 +1000197func writeAny(w *textWriter, v reflect.Value, props *Properties) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700198 v = reflect.Indirect(v)
199
200 // We don't attempt to serialise every possible value type; only those
201 // that can occur in protocol buffers, plus a few extra that were easy.
David Symondse37856c2011-06-22 12:52:53 +1000202 switch v.Kind() {
Rob Pike97e934d2011-04-11 12:52:49 -0700203 case reflect.Slice:
Rob Pikeaaa3a622010-03-20 22:32:34 -0700204 // Should only be a []byte; repeated fields are handled in writeStruct.
David Symonds4c95bfe2011-09-13 14:43:27 +1000205 writeString(w, string(v.Interface().([]byte)))
Rob Pike97e934d2011-04-11 12:52:49 -0700206 case reflect.String:
David Symonds4c95bfe2011-09-13 14:43:27 +1000207 writeString(w, v.String())
Rob Pike97e934d2011-04-11 12:52:49 -0700208 case reflect.Struct:
Rob Pikeaaa3a622010-03-20 22:32:34 -0700209 // Required/optional group/message.
David Symonds9f402812011-04-28 18:08:44 +1000210 var bra, ket byte = '<', '>'
211 if props != nil && props.Wire == "group" {
212 bra, ket = '{', '}'
213 }
214 w.WriteByte(bra)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700215 if !w.compact {
David Symonds9f402812011-04-28 18:08:44 +1000216 w.WriteByte('\n')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700217 }
218 w.indent()
David Symondse37856c2011-06-22 12:52:53 +1000219 writeStruct(w, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700220 w.unindent()
David Symonds9f402812011-04-28 18:08:44 +1000221 w.WriteByte(ket)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700222 default:
David Symondse37856c2011-06-22 12:52:53 +1000223 fmt.Fprint(w, v.Interface())
224 }
225}
226
David Symonds4c95bfe2011-09-13 14:43:27 +1000227// equivalent to C's isprint.
228func isprint(c byte) bool {
229 return c >= 0x20 && c < 0x7f
230}
231
232// writeString writes a string in the protocol buffer text format.
233// It is similar to strconv.Quote except we don't use Go escape sequences,
234// we treat the string as a byte sequence, and we use octal escapes.
235// These differences are to maintain interoperability with the other
236// languages' implementations of the text format.
237func writeString(w *textWriter, s string) {
238 w.WriteByte('"')
239
240 // Loop over the bytes, not the runes.
241 for i := 0; i < len(s); i++ {
242 // Divergence from C++: we don't escape apostrophes.
243 // There's no need to escape them, and the C++ parser
244 // copes with a naked apostrophe.
245 switch c := s[i]; c {
246 case '\n':
247 w.Write([]byte{'\\', 'n'})
248 case '\r':
249 w.Write([]byte{'\\', 'r'})
250 case '\t':
251 w.Write([]byte{'\\', 't'})
252 case '"':
253 w.Write([]byte{'\\', '"'})
254 case '\\':
255 w.Write([]byte{'\\', '\\'})
256 default:
257 if isprint(c) {
258 w.WriteByte(c)
259 } else {
260 fmt.Fprintf(w, "\\%03o", c)
261 }
262 }
263 }
264
265 w.WriteByte('"')
266}
267
David Symonds1d72f7a2011-08-19 18:28:52 +1000268func writeMessageSet(w *textWriter, ms *MessageSet) {
269 for _, item := range ms.Item {
270 id := *item.TypeId
271 if msd, ok := messageSetMap[id]; ok {
272 // Known message set type.
273 fmt.Fprintf(w, "[%s]: <\n", msd.name)
274 w.indent()
275
276 pb := reflect.New(msd.t.Elem())
277 if err := Unmarshal(item.Message, pb.Interface()); err != nil {
278 fmt.Fprintf(w, "/* bad message: %v */\n", err)
279 } else {
280 writeStruct(w, pb.Elem())
281 }
282 } else {
283 // Unknown type.
284 fmt.Fprintf(w, "[%d]: <\n", id)
285 w.indent()
286 writeUnknownStruct(w, item.Message)
287 }
288 w.unindent()
289 w.Write([]byte(">\n"))
290 }
291}
292
293func writeUnknownStruct(w *textWriter, data []byte) {
294 if !w.compact {
295 fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data))
296 }
297 b := NewBuffer(data)
298 for b.index < len(b.buf) {
299 x, err := b.DecodeVarint()
300 if err != nil {
301 fmt.Fprintf(w, "/* %v */\n", err)
302 return
303 }
304 wire, tag := x&7, x>>3
305 if wire == WireEndGroup {
306 w.unindent()
307 w.Write([]byte("}\n"))
308 continue
309 }
310 fmt.Fprintf(w, "tag%d", tag)
311 if wire != WireStartGroup {
312 w.WriteByte(':')
313 }
314 if !w.compact || wire == WireStartGroup {
315 w.WriteByte(' ')
316 }
317 switch wire {
318 case WireBytes:
319 buf, err := b.DecodeRawBytes(false)
320 if err == nil {
321 fmt.Fprintf(w, "%q", buf)
322 } else {
323 fmt.Fprintf(w, "/* %v */", err)
324 }
325 case WireFixed32:
326 x, err := b.DecodeFixed32()
327 writeUnknownInt(w, x, err)
328 case WireFixed64:
329 x, err := b.DecodeFixed64()
330 writeUnknownInt(w, x, err)
331 case WireStartGroup:
332 fmt.Fprint(w, "{")
333 w.indent()
334 case WireVarint:
335 x, err := b.DecodeVarint()
336 writeUnknownInt(w, x, err)
337 default:
338 fmt.Fprintf(w, "/* unknown wire type %d */", wire)
339 }
340 w.WriteByte('\n')
341 }
342}
343
Rob Pikea17fdd92011-11-02 12:43:05 -0700344func writeUnknownInt(w *textWriter, x uint64, err error) {
David Symonds1d72f7a2011-08-19 18:28:52 +1000345 if err == nil {
346 fmt.Fprint(w, x)
347 } else {
348 fmt.Fprintf(w, "/* %v */", err)
349 }
350}
351
352type int32Slice []int32
353
354func (s int32Slice) Len() int { return len(s) }
355func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
356func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
357
David Symondse37856c2011-06-22 12:52:53 +1000358// writeExtensions writes all the extensions in pv.
359// pv is assumed to be a pointer to a protocol message struct that is extendable.
360func writeExtensions(w *textWriter, pv reflect.Value) {
361 emap := extensionMaps[pv.Type().Elem()]
362 ep := pv.Interface().(extendableProto)
David Symonds1d72f7a2011-08-19 18:28:52 +1000363
364 // Order the extensions by ID.
365 // This isn't strictly necessary, but it will give us
366 // canonical output, which will also make testing easier.
367 m := ep.ExtensionMap()
368 ids := make([]int32, 0, len(m))
369 for id := range m {
370 ids = append(ids, id)
371 }
372 sort.Sort(int32Slice(ids))
373
374 for _, extNum := range ids {
375 ext := m[extNum]
David Symondse37856c2011-06-22 12:52:53 +1000376 var desc *ExtensionDesc
377 if emap != nil {
378 desc = emap[extNum]
379 }
380 if desc == nil {
David Symonds1d72f7a2011-08-19 18:28:52 +1000381 // Unknown extension.
382 writeUnknownStruct(w, ext.enc)
David Symondse37856c2011-06-22 12:52:53 +1000383 continue
384 }
385
386 pb, err := GetExtension(ep, desc)
387 if err != nil {
388 fmt.Fprintln(os.Stderr, "proto: failed getting extension: ", err)
389 continue
390 }
391
392 fmt.Fprintf(w, "[%s]:", desc.Name)
393 if !w.compact {
394 w.WriteByte(' ')
395 }
396 writeAny(w, reflect.ValueOf(pb), nil)
397 w.WriteByte('\n')
Rob Pikeaaa3a622010-03-20 22:32:34 -0700398 }
399}
400
401func marshalText(w io.Writer, pb interface{}, compact bool) {
David Symonds03c9d412010-08-26 14:23:18 +1000402 if pb == nil {
403 w.Write([]byte("<nil>"))
404 return
405 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700406 aw := new(textWriter)
407 aw.writer = w
408 aw.complete = true
409 aw.compact = compact
410
Nigel Tao4ede8452011-04-28 11:27:25 +1000411 v := reflect.ValueOf(pb)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700412 // We should normally be passed a struct, or a pointer to a struct,
413 // and we don't want the outer < and > in that case.
414 v = reflect.Indirect(v)
David Symondsa9cda212011-04-15 01:23:17 -0700415 if v.Kind() == reflect.Struct {
416 writeStruct(aw, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700417 } else {
David Symonds9f402812011-04-28 18:08:44 +1000418 writeAny(aw, v, nil)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700419 }
420}
421
David Symondse37856c2011-06-22 12:52:53 +1000422// MarshalText writes a given protocol buffer in text format.
423// Values that are not protocol buffers can also be written, but their formatting is not guaranteed.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700424func MarshalText(w io.Writer, pb interface{}) { marshalText(w, pb, false) }
425
David Symondsd2bff3c2012-03-14 10:45:25 +1100426// MarshalTextString is the same as MarshalText, but returns the string directly.
427func MarshalTextString(pb interface{}) string {
428 var buf bytes.Buffer
429 marshalText(&buf, pb, false)
430 return buf.String()
431}
432
David Symondse37856c2011-06-22 12:52:53 +1000433// CompactText writes a given protocl buffer in compact text format (one line).
434// Values that are not protocol buffers can also be written, but their formatting is not guaranteed.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700435func CompactText(w io.Writer, pb interface{}) { marshalText(w, pb, true) }
436
437// CompactTextString is the same as CompactText, but returns the string directly.
438func CompactTextString(pb interface{}) string {
439 buf := new(bytes.Buffer)
440 marshalText(buf, pb, true)
441 return buf.String()
442}