| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // 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 | |
| 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. |
| 35 | // TODO: message sets. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 36 | |
| 37 | import ( |
| 38 | "bytes" |
| 39 | "fmt" |
| 40 | "io" |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 41 | "log" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 42 | "os" |
| 43 | "reflect" |
| 44 | "strconv" |
| 45 | "strings" |
| 46 | ) |
| 47 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 48 | // textWriter is an io.Writer that tracks its indentation level. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 49 | type textWriter struct { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 50 | ind int |
| 51 | complete bool // if the current position is a complete line |
| 52 | compact bool // whether to write out as a one-liner |
| 53 | writer io.Writer |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 54 | |
| 55 | c [1]byte // scratch |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func (w *textWriter) Write(p []byte) (n int, err os.Error) { |
| 59 | n, err = len(p), nil |
| 60 | |
| Rob Pike | 5338544 | 2010-06-30 22:22:43 -0700 | [diff] [blame] | 61 | frags := strings.Split(string(p), "\n", -1) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 62 | if w.compact { |
| 63 | w.writer.Write([]byte(strings.Join(frags, " "))) |
| 64 | return |
| 65 | } |
| 66 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 67 | for i, frag := range frags { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 68 | if w.complete { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 69 | for j := 0; j < w.ind; j++ { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 70 | w.writer.Write([]byte{' ', ' '}) |
| 71 | } |
| 72 | w.complete = false |
| 73 | } |
| 74 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 75 | w.writer.Write([]byte(frag)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 76 | if i+1 < len(frags) { |
| 77 | w.writer.Write([]byte{'\n'}) |
| 78 | } |
| 79 | } |
| 80 | w.complete = len(frags[len(frags)-1]) == 0 |
| 81 | |
| 82 | return |
| 83 | } |
| 84 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 85 | func (w *textWriter) WriteByte(c byte) os.Error { |
| 86 | w.c[0] = c |
| 87 | _, err := w.Write(w.c[:]) |
| 88 | return err |
| 89 | } |
| 90 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 91 | func (w *textWriter) indent() { w.ind++ } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 92 | |
| 93 | func (w *textWriter) unindent() { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 94 | if w.ind == 0 { |
| 95 | log.Printf("proto: textWriter unindented too far!") |
| 96 | return |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 97 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 98 | w.ind-- |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 101 | func writeName(w *textWriter, props *Properties) { |
| 102 | io.WriteString(w, props.OrigName) |
| 103 | if props.Wire != "group" { |
| 104 | w.WriteByte(':') |
| 105 | } |
| 106 | } |
| 107 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 108 | var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem() |
| 109 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 110 | func writeStruct(w *textWriter, sv reflect.Value) { |
| 111 | st := sv.Type() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 112 | sprops := GetProperties(st) |
| 113 | for i := 0; i < sv.NumField(); i++ { |
| 114 | if strings.HasPrefix(st.Field(i).Name, "XXX_") { |
| 115 | continue |
| 116 | } |
| 117 | props := sprops.Prop[i] |
| 118 | fv := sv.Field(i) |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 119 | if fv.Kind() == reflect.Ptr && fv.IsNil() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 120 | // Field not filled in. This could be an optional field or |
| 121 | // a required field that wasn't filled in. Either way, there |
| 122 | // isn't anything we can show for it. |
| 123 | continue |
| 124 | } |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 125 | if fv.Kind() == reflect.Slice && fv.IsNil() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 126 | // Repeated field that is empty, or a bytes field that is unused. |
| 127 | continue |
| 128 | } |
| 129 | |
| 130 | if props.Repeated { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 131 | if fv.Kind() == reflect.Slice { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 132 | // Repeated field. |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 133 | for j := 0; j < fv.Len(); j++ { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 134 | writeName(w, props) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 135 | if !w.compact { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 136 | w.WriteByte(' ') |
| 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 | writeAny(w, fv.Index(j), props) |
| 139 | w.WriteByte('\n') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 140 | } |
| 141 | continue |
| 142 | } |
| 143 | } |
| 144 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 145 | writeName(w, props) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 146 | if !w.compact { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 147 | w.WriteByte(' ') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 148 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 149 | if props.Enum != "" && tryWriteEnum(w, props.Enum, fv) { |
| 150 | // Enum written. |
| 151 | } else { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 152 | writeAny(w, fv, props) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 153 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 154 | w.WriteByte('\n') |
| 155 | } |
| 156 | |
| 157 | // Extensions. |
| 158 | pv := sv.Addr() |
| 159 | if pv.Type().Implements(extendableProtoType) { |
| 160 | writeExtensions(w, pv) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 164 | // tryWriteEnum attempts to write an enum value as a symbolic constant. |
| 165 | // If the enum is unregistered, nothing is written and false is returned. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 166 | func tryWriteEnum(w *textWriter, enum string, v reflect.Value) bool { |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 167 | v = reflect.Indirect(v) |
| 168 | if v.Type().Kind() != reflect.Int32 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 169 | return false |
| 170 | } |
| 171 | m, ok := enumNameMaps[enum] |
| 172 | if !ok { |
| 173 | return false |
| 174 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 175 | str, ok := m[int32(v.Int())] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 176 | if !ok { |
| 177 | return false |
| 178 | } |
| 179 | fmt.Fprintf(w, str) |
| 180 | return true |
| 181 | } |
| 182 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 183 | // writeAny writes an arbitrary field. |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 184 | func writeAny(w *textWriter, v reflect.Value, props *Properties) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 185 | v = reflect.Indirect(v) |
| 186 | |
| 187 | // We don't attempt to serialise every possible value type; only those |
| 188 | // 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^] | 189 | switch v.Kind() { |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 190 | case reflect.Slice: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 191 | // Should only be a []byte; repeated fields are handled in writeStruct. |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 192 | // TODO: Should be strconv.QuoteToASCII, which should be released after 2011-06-20. |
| 193 | fmt.Fprint(w, strconv.Quote(string(v.Interface().([]byte)))) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 194 | case reflect.String: |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 195 | // TODO: Should be strconv.QuoteToASCII, which should be released after 2011-06-20. |
| 196 | fmt.Fprint(w, strconv.Quote(v.String())) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 197 | case reflect.Struct: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 198 | // Required/optional group/message. |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 199 | var bra, ket byte = '<', '>' |
| 200 | if props != nil && props.Wire == "group" { |
| 201 | bra, ket = '{', '}' |
| 202 | } |
| 203 | w.WriteByte(bra) |
| 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('\n') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 206 | } |
| 207 | w.indent() |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 208 | writeStruct(w, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 209 | w.unindent() |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 210 | w.WriteByte(ket) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 211 | default: |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 212 | fmt.Fprint(w, v.Interface()) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // writeExtensions writes all the extensions in pv. |
| 217 | // pv is assumed to be a pointer to a protocol message struct that is extendable. |
| 218 | func writeExtensions(w *textWriter, pv reflect.Value) { |
| 219 | emap := extensionMaps[pv.Type().Elem()] |
| 220 | ep := pv.Interface().(extendableProto) |
| 221 | for extNum := range ep.ExtensionMap() { |
| 222 | var desc *ExtensionDesc |
| 223 | if emap != nil { |
| 224 | desc = emap[extNum] |
| 225 | } |
| 226 | if desc == nil { |
| 227 | // TODO: Handle printing unknown extensions. |
| 228 | fmt.Fprintln(os.Stderr, "proto: unknown extension: ", extNum) |
| 229 | continue |
| 230 | } |
| 231 | |
| 232 | pb, err := GetExtension(ep, desc) |
| 233 | if err != nil { |
| 234 | fmt.Fprintln(os.Stderr, "proto: failed getting extension: ", err) |
| 235 | continue |
| 236 | } |
| 237 | |
| 238 | fmt.Fprintf(w, "[%s]:", desc.Name) |
| 239 | if !w.compact { |
| 240 | w.WriteByte(' ') |
| 241 | } |
| 242 | writeAny(w, reflect.ValueOf(pb), nil) |
| 243 | w.WriteByte('\n') |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | func marshalText(w io.Writer, pb interface{}, compact bool) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 248 | if pb == nil { |
| 249 | w.Write([]byte("<nil>")) |
| 250 | return |
| 251 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 252 | aw := new(textWriter) |
| 253 | aw.writer = w |
| 254 | aw.complete = true |
| 255 | aw.compact = compact |
| 256 | |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 257 | v := reflect.ValueOf(pb) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 258 | // We should normally be passed a struct, or a pointer to a struct, |
| 259 | // and we don't want the outer < and > in that case. |
| 260 | v = reflect.Indirect(v) |
| David Symonds | a9cda21 | 2011-04-15 01:23:17 -0700 | [diff] [blame] | 261 | if v.Kind() == reflect.Struct { |
| 262 | writeStruct(aw, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 263 | } else { |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 264 | writeAny(aw, v, nil) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 268 | // MarshalText writes a given protocol buffer in text format. |
| 269 | // Values that are not protocol buffers can also be written, but their formatting is not guaranteed. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 270 | func MarshalText(w io.Writer, pb interface{}) { marshalText(w, pb, false) } |
| 271 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame^] | 272 | // CompactText writes a given protocl buffer in compact text format (one line). |
| 273 | // Values that are not protocol buffers can also be written, but their formatting is not guaranteed. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 274 | func CompactText(w io.Writer, pb interface{}) { marshalText(w, pb, true) } |
| 275 | |
| 276 | // CompactTextString is the same as CompactText, but returns the string directly. |
| 277 | func CompactTextString(pb interface{}) string { |
| 278 | buf := new(bytes.Buffer) |
| 279 | marshalText(buf, pb, true) |
| 280 | return buf.String() |
| 281 | } |