| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame^] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 | |
| 32 | package proto |
| 33 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 34 | // Functions for writing the text protocol buffer format. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 35 | |
| 36 | import ( |
| 37 | "bytes" |
| 38 | "fmt" |
| 39 | "io" |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 40 | "log" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 41 | "os" |
| 42 | "reflect" |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 43 | "sort" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 44 | "strings" |
| 45 | ) |
| 46 | |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 47 | var ( |
| 48 | newline = []byte("\n") |
| 49 | spaces = []byte(" ") |
| 50 | gtNewline = []byte(">\n") |
| 51 | endBraceNewline = []byte("}\n") |
| 52 | backslashN = []byte{'\\', 'n'} |
| 53 | backslashR = []byte{'\\', 'r'} |
| 54 | backslashT = []byte{'\\', 't'} |
| 55 | backslashDQ = []byte{'\\', '"'} |
| 56 | backslashBS = []byte{'\\', '\\'} |
| 57 | ) |
| 58 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 59 | // textWriter is an io.Writer that tracks its indentation level. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 60 | type textWriter struct { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 61 | ind int |
| 62 | complete bool // if the current position is a complete line |
| 63 | compact bool // whether to write out as a one-liner |
| 64 | writer io.Writer |
| 65 | writeByte func(byte) error // write a single byte to writer |
| 66 | } |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 67 | |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 68 | func (w *textWriter) WriteString(s string) (n int, err error) { |
| 69 | if !strings.Contains(s, "\n") { |
| 70 | if !w.compact && w.complete { |
| 71 | w.writeIndent() |
| 72 | } |
| 73 | w.complete = false |
| 74 | return io.WriteString(w.writer, s) |
| 75 | } |
| 76 | // WriteString is typically called without newlines, so this |
| 77 | // codepath and its copy are rare. We copy to avoid |
| 78 | // duplicating all of Write's logic here. |
| 79 | return w.Write([]byte(s)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 82 | func (w *textWriter) Write(p []byte) (n int, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 83 | n, err = len(p), nil |
| 84 | |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 85 | newlines := bytes.Count(p, newline) |
| 86 | if newlines == 0 { |
| 87 | if !w.compact && w.complete { |
| 88 | w.writeIndent() |
| 89 | } |
| 90 | w.writer.Write(p) |
| 91 | w.complete = false |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | frags := bytes.SplitN(p, newline, newlines+1) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 96 | if w.compact { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 97 | for i, frag := range frags { |
| 98 | if i > 0 { |
| 99 | w.writeByte(' ') |
| 100 | } |
| 101 | w.writer.Write(frag) |
| 102 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 103 | return |
| 104 | } |
| 105 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 106 | for i, frag := range frags { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 107 | if w.complete { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 108 | w.writeIndent() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 109 | } |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 110 | w.writer.Write(frag) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 111 | if i+1 < len(frags) { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 112 | w.writeByte('\n') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | w.complete = len(frags[len(frags)-1]) == 0 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 116 | return |
| 117 | } |
| 118 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 119 | func (w *textWriter) WriteByte(c byte) error { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 120 | if w.compact && c == '\n' { |
| 121 | c = ' ' |
| 122 | } |
| 123 | if !w.compact && w.complete { |
| 124 | w.writeIndent() |
| 125 | } |
| 126 | err := w.writeByte(c) |
| 127 | w.complete = c == '\n' |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 128 | return err |
| 129 | } |
| 130 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 131 | func (w *textWriter) indent() { w.ind++ } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 132 | |
| 133 | func (w *textWriter) unindent() { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 134 | if w.ind == 0 { |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 135 | log.Printf("proto: textWriter unindented too far") |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 136 | return |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 137 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 138 | w.ind-- |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 141 | func writeName(w *textWriter, props *Properties) { |
| 142 | io.WriteString(w, props.OrigName) |
| 143 | if props.Wire != "group" { |
| 144 | w.WriteByte(':') |
| 145 | } |
| 146 | } |
| 147 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 148 | var ( |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 149 | messageSetType = reflect.TypeOf((*MessageSet)(nil)).Elem() |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 150 | ) |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 151 | |
| David Symonds | e9e7aaf | 2012-03-23 13:12:33 +1100 | [diff] [blame] | 152 | // raw is the interface satisfied by RawMessage. |
| 153 | type raw interface { |
| 154 | Bytes() []byte |
| 155 | } |
| 156 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 157 | func writeStruct(w *textWriter, sv reflect.Value) { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 158 | if sv.Type() == messageSetType { |
| 159 | writeMessageSet(w, sv.Addr().Interface().(*MessageSet)) |
| 160 | return |
| 161 | } |
| 162 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 163 | st := sv.Type() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 164 | sprops := GetProperties(st) |
| 165 | for i := 0; i < sv.NumField(); i++ { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 166 | fv := sv.Field(i) |
| 167 | if name := st.Field(i).Name; strings.HasPrefix(name, "XXX_") { |
| 168 | // There's only two XXX_ fields: |
| 169 | // XXX_unrecognized []byte |
| 170 | // XXX_extensions map[int32]proto.Extension |
| 171 | // The first is handled here; |
| 172 | // the second is handled at the bottom of this function. |
| 173 | if name == "XXX_unrecognized" && !fv.IsNil() { |
| 174 | writeUnknownStruct(w, fv.Interface().([]byte)) |
| 175 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 176 | continue |
| 177 | } |
| 178 | props := sprops.Prop[i] |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 179 | if fv.Kind() == reflect.Ptr && fv.IsNil() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 180 | // Field not filled in. This could be an optional field or |
| 181 | // a required field that wasn't filled in. Either way, there |
| 182 | // isn't anything we can show for it. |
| 183 | continue |
| 184 | } |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 185 | if fv.Kind() == reflect.Slice && fv.IsNil() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 186 | // Repeated field that is empty, or a bytes field that is unused. |
| 187 | continue |
| 188 | } |
| 189 | |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 190 | if props.Repeated && fv.Kind() == reflect.Slice { |
| 191 | // Repeated field. |
| 192 | for j := 0; j < fv.Len(); j++ { |
| 193 | writeName(w, props) |
| 194 | if !w.compact { |
| 195 | w.WriteByte(' ') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 196 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 197 | writeAny(w, fv.Index(j), props) |
| 198 | w.WriteByte('\n') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 199 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 200 | continue |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 203 | writeName(w, props) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 204 | if !w.compact { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 205 | w.WriteByte(' ') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 206 | } |
| David Symonds | e9e7aaf | 2012-03-23 13:12:33 +1100 | [diff] [blame] | 207 | if b, ok := fv.Interface().(raw); ok { |
| 208 | writeRaw(w, b.Bytes()) |
| 209 | continue |
| 210 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 211 | if props.Enum != "" && tryWriteEnum(w, props.Enum, fv) { |
| 212 | // Enum written. |
| 213 | } else { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 214 | writeAny(w, fv, props) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 215 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 216 | w.WriteByte('\n') |
| 217 | } |
| 218 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 219 | // Extensions (the XXX_extensions field). |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 220 | pv := sv.Addr() |
| 221 | if pv.Type().Implements(extendableProtoType) { |
| 222 | writeExtensions(w, pv) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| David Symonds | e9e7aaf | 2012-03-23 13:12:33 +1100 | [diff] [blame] | 226 | // writeRaw writes an uninterpreted raw message. |
| 227 | func writeRaw(w *textWriter, b []byte) { |
| 228 | w.WriteByte('<') |
| 229 | if !w.compact { |
| 230 | w.WriteByte('\n') |
| 231 | } |
| 232 | w.indent() |
| 233 | writeUnknownStruct(w, b) |
| 234 | w.unindent() |
| 235 | w.WriteByte('>') |
| 236 | } |
| 237 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 238 | // tryWriteEnum attempts to write an enum value as a symbolic constant. |
| 239 | // If the enum is unregistered, nothing is written and false is returned. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 240 | func tryWriteEnum(w *textWriter, enum string, v reflect.Value) bool { |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 241 | v = reflect.Indirect(v) |
| 242 | if v.Type().Kind() != reflect.Int32 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 243 | return false |
| 244 | } |
| 245 | m, ok := enumNameMaps[enum] |
| 246 | if !ok { |
| 247 | return false |
| 248 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 249 | str, ok := m[int32(v.Int())] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 250 | if !ok { |
| 251 | return false |
| 252 | } |
| 253 | fmt.Fprintf(w, str) |
| 254 | return true |
| 255 | } |
| 256 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 257 | // writeAny writes an arbitrary field. |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 258 | func writeAny(w *textWriter, v reflect.Value, props *Properties) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 259 | v = reflect.Indirect(v) |
| 260 | |
| 261 | // We don't attempt to serialise every possible value type; only those |
| 262 | // that can occur in protocol buffers, plus a few extra that were easy. |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 263 | switch v.Kind() { |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 264 | case reflect.Slice: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 265 | // Should only be a []byte; repeated fields are handled in writeStruct. |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 266 | writeString(w, string(v.Interface().([]byte))) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 267 | case reflect.String: |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 268 | writeString(w, v.String()) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 269 | case reflect.Struct: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 270 | // Required/optional group/message. |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 271 | var bra, ket byte = '<', '>' |
| 272 | if props != nil && props.Wire == "group" { |
| 273 | bra, ket = '{', '}' |
| 274 | } |
| 275 | w.WriteByte(bra) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 276 | if !w.compact { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 277 | w.WriteByte('\n') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 278 | } |
| 279 | w.indent() |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 280 | writeStruct(w, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 281 | w.unindent() |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 282 | w.WriteByte(ket) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 283 | default: |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 284 | fmt.Fprint(w, v.Interface()) |
| 285 | } |
| 286 | } |
| 287 | |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 288 | // equivalent to C's isprint. |
| 289 | func isprint(c byte) bool { |
| 290 | return c >= 0x20 && c < 0x7f |
| 291 | } |
| 292 | |
| 293 | // writeString writes a string in the protocol buffer text format. |
| 294 | // It is similar to strconv.Quote except we don't use Go escape sequences, |
| 295 | // we treat the string as a byte sequence, and we use octal escapes. |
| 296 | // These differences are to maintain interoperability with the other |
| 297 | // languages' implementations of the text format. |
| 298 | func writeString(w *textWriter, s string) { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 299 | w.WriteByte('"') // use WriteByte here to get any needed indent |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 300 | // Loop over the bytes, not the runes. |
| 301 | for i := 0; i < len(s); i++ { |
| 302 | // Divergence from C++: we don't escape apostrophes. |
| 303 | // There's no need to escape them, and the C++ parser |
| 304 | // copes with a naked apostrophe. |
| 305 | switch c := s[i]; c { |
| 306 | case '\n': |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 307 | w.writer.Write(backslashN) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 308 | case '\r': |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 309 | w.writer.Write(backslashR) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 310 | case '\t': |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 311 | w.writer.Write(backslashT) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 312 | case '"': |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 313 | w.writer.Write(backslashDQ) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 314 | case '\\': |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 315 | w.writer.Write(backslashBS) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 316 | default: |
| 317 | if isprint(c) { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 318 | w.writeByte(c) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 319 | } else { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 320 | fmt.Fprintf(w.writer, "\\%03o", c) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | } |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 324 | w.WriteByte('"') |
| 325 | } |
| 326 | |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 327 | func writeMessageSet(w *textWriter, ms *MessageSet) { |
| 328 | for _, item := range ms.Item { |
| 329 | id := *item.TypeId |
| 330 | if msd, ok := messageSetMap[id]; ok { |
| 331 | // Known message set type. |
| 332 | fmt.Fprintf(w, "[%s]: <\n", msd.name) |
| 333 | w.indent() |
| 334 | |
| 335 | pb := reflect.New(msd.t.Elem()) |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 336 | if err := Unmarshal(item.Message, pb.Interface().(Message)); err != nil { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 337 | fmt.Fprintf(w, "/* bad message: %v */\n", err) |
| 338 | } else { |
| 339 | writeStruct(w, pb.Elem()) |
| 340 | } |
| 341 | } else { |
| 342 | // Unknown type. |
| 343 | fmt.Fprintf(w, "[%d]: <\n", id) |
| 344 | w.indent() |
| 345 | writeUnknownStruct(w, item.Message) |
| 346 | } |
| 347 | w.unindent() |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 348 | w.Write(gtNewline) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | func writeUnknownStruct(w *textWriter, data []byte) { |
| 353 | if !w.compact { |
| 354 | fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data)) |
| 355 | } |
| 356 | b := NewBuffer(data) |
| 357 | for b.index < len(b.buf) { |
| 358 | x, err := b.DecodeVarint() |
| 359 | if err != nil { |
| 360 | fmt.Fprintf(w, "/* %v */\n", err) |
| 361 | return |
| 362 | } |
| 363 | wire, tag := x&7, x>>3 |
| 364 | if wire == WireEndGroup { |
| 365 | w.unindent() |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 366 | w.Write(endBraceNewline) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 367 | continue |
| 368 | } |
| 369 | fmt.Fprintf(w, "tag%d", tag) |
| 370 | if wire != WireStartGroup { |
| 371 | w.WriteByte(':') |
| 372 | } |
| 373 | if !w.compact || wire == WireStartGroup { |
| 374 | w.WriteByte(' ') |
| 375 | } |
| 376 | switch wire { |
| 377 | case WireBytes: |
| 378 | buf, err := b.DecodeRawBytes(false) |
| 379 | if err == nil { |
| 380 | fmt.Fprintf(w, "%q", buf) |
| 381 | } else { |
| 382 | fmt.Fprintf(w, "/* %v */", err) |
| 383 | } |
| 384 | case WireFixed32: |
| 385 | x, err := b.DecodeFixed32() |
| 386 | writeUnknownInt(w, x, err) |
| 387 | case WireFixed64: |
| 388 | x, err := b.DecodeFixed64() |
| 389 | writeUnknownInt(w, x, err) |
| 390 | case WireStartGroup: |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 391 | w.WriteByte('{') |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 392 | w.indent() |
| 393 | case WireVarint: |
| 394 | x, err := b.DecodeVarint() |
| 395 | writeUnknownInt(w, x, err) |
| 396 | default: |
| 397 | fmt.Fprintf(w, "/* unknown wire type %d */", wire) |
| 398 | } |
| 399 | w.WriteByte('\n') |
| 400 | } |
| 401 | } |
| 402 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 403 | func writeUnknownInt(w *textWriter, x uint64, err error) { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 404 | if err == nil { |
| 405 | fmt.Fprint(w, x) |
| 406 | } else { |
| 407 | fmt.Fprintf(w, "/* %v */", err) |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | type int32Slice []int32 |
| 412 | |
| 413 | func (s int32Slice) Len() int { return len(s) } |
| 414 | func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] } |
| 415 | func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 416 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 417 | // writeExtensions writes all the extensions in pv. |
| 418 | // pv is assumed to be a pointer to a protocol message struct that is extendable. |
| 419 | func writeExtensions(w *textWriter, pv reflect.Value) { |
| 420 | emap := extensionMaps[pv.Type().Elem()] |
| 421 | ep := pv.Interface().(extendableProto) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 422 | |
| 423 | // Order the extensions by ID. |
| 424 | // This isn't strictly necessary, but it will give us |
| 425 | // canonical output, which will also make testing easier. |
| 426 | m := ep.ExtensionMap() |
| 427 | ids := make([]int32, 0, len(m)) |
| 428 | for id := range m { |
| 429 | ids = append(ids, id) |
| 430 | } |
| 431 | sort.Sort(int32Slice(ids)) |
| 432 | |
| 433 | for _, extNum := range ids { |
| 434 | ext := m[extNum] |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 435 | var desc *ExtensionDesc |
| 436 | if emap != nil { |
| 437 | desc = emap[extNum] |
| 438 | } |
| 439 | if desc == nil { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 440 | // Unknown extension. |
| 441 | writeUnknownStruct(w, ext.enc) |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 442 | continue |
| 443 | } |
| 444 | |
| 445 | pb, err := GetExtension(ep, desc) |
| 446 | if err != nil { |
| 447 | fmt.Fprintln(os.Stderr, "proto: failed getting extension: ", err) |
| 448 | continue |
| 449 | } |
| 450 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 451 | // Repeated extensions will appear as a slice. |
| 452 | if !desc.repeated() { |
| 453 | writeExtension(w, desc.Name, pb) |
| 454 | } else { |
| 455 | v := reflect.ValueOf(pb) |
| 456 | for i := 0; i < v.Len(); i++ { |
| 457 | writeExtension(w, desc.Name, v.Index(i).Interface()) |
| 458 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 459 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 463 | func writeExtension(w *textWriter, name string, pb interface{}) { |
| 464 | fmt.Fprintf(w, "[%s]:", name) |
| 465 | if !w.compact { |
| 466 | w.WriteByte(' ') |
| 467 | } |
| 468 | writeAny(w, reflect.ValueOf(pb), nil) |
| 469 | w.WriteByte('\n') |
| 470 | } |
| 471 | |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 472 | func (w *textWriter) writeIndent() { |
| 473 | if !w.complete { |
| 474 | return |
| 475 | } |
| 476 | remain := w.ind * 2 |
| 477 | for remain > 0 { |
| 478 | n := remain |
| 479 | if n > len(spaces) { |
| 480 | n = len(spaces) |
| 481 | } |
| 482 | w.writer.Write(spaces[:n]) |
| 483 | remain -= n |
| 484 | } |
| 485 | w.complete = false |
| 486 | } |
| 487 | |
| 488 | type byteWriter interface { |
| 489 | WriteByte(byte) error |
| 490 | } |
| 491 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 492 | func marshalText(w io.Writer, pb Message, compact bool) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 493 | if pb == nil { |
| 494 | w.Write([]byte("<nil>")) |
| 495 | return |
| 496 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 497 | aw := new(textWriter) |
| 498 | aw.writer = w |
| 499 | aw.complete = true |
| 500 | aw.compact = compact |
| 501 | |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 502 | if bw, ok := w.(byteWriter); ok { |
| 503 | aw.writeByte = func(c byte) error { |
| 504 | return bw.WriteByte(c) |
| 505 | } |
| 506 | } else { |
| 507 | var scratch [1]byte |
| 508 | aw.writeByte = func(c byte) error { |
| 509 | scratch[0] = c |
| 510 | _, err := w.Write(scratch[:]) |
| 511 | return err |
| 512 | } |
| 513 | } |
| 514 | |
| David Symonds | 92dd6c1 | 2012-03-23 10:59:49 +1100 | [diff] [blame] | 515 | // Dereference the received pointer so we don't have outer < and >. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 516 | v := reflect.Indirect(reflect.ValueOf(pb)) |
| 517 | writeStruct(aw, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 520 | // MarshalText writes a given protocol buffer in text format. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 521 | func MarshalText(w io.Writer, pb Message) { marshalText(w, pb, false) } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 522 | |
| David Symonds | d2bff3c | 2012-03-14 10:45:25 +1100 | [diff] [blame] | 523 | // MarshalTextString is the same as MarshalText, but returns the string directly. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 524 | func MarshalTextString(pb Message) string { |
| David Symonds | d2bff3c | 2012-03-14 10:45:25 +1100 | [diff] [blame] | 525 | var buf bytes.Buffer |
| 526 | marshalText(&buf, pb, false) |
| 527 | return buf.String() |
| 528 | } |
| 529 | |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 530 | // CompactText writes a given protocol buffer in compact text format (one line). |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 531 | func CompactText(w io.Writer, pb Message) { marshalText(w, pb, true) } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 532 | |
| 533 | // CompactTextString is the same as CompactText, but returns the string directly. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 534 | func CompactTextString(pb Message) string { |
| David Symonds | 183124e | 2012-03-23 13:20:23 +1100 | [diff] [blame] | 535 | var buf bytes.Buffer |
| 536 | marshalText(&buf, pb, true) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 537 | return buf.String() |
| 538 | } |