| 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 | |
| 34 | // Functions for parsing the Text protocol buffer format. |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 35 | // TODO: message sets. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 36 | |
| 37 | import ( |
| 38 | "fmt" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 39 | "reflect" |
| 40 | "strconv" |
| David Symonds | 183124e | 2012-03-23 13:20:23 +1100 | [diff] [blame] | 41 | "strings" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 42 | ) |
| 43 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 44 | type ParseError struct { |
| 45 | Message string |
| 46 | Line int // 1-based line number |
| 47 | Offset int // 0-based byte offset from start of input |
| 48 | } |
| 49 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 50 | func (p *ParseError) Error() string { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 51 | if p.Line == 1 { |
| 52 | // show offset only for first line |
| 53 | return fmt.Sprintf("line 1.%d: %v", p.Offset, p.Message) |
| 54 | } |
| 55 | return fmt.Sprintf("line %d: %v", p.Line, p.Message) |
| 56 | } |
| 57 | |
| 58 | type token struct { |
| 59 | value string |
| 60 | err *ParseError |
| 61 | line int // line number |
| 62 | offset int // byte number from start of input, not start of line |
| 63 | unquoted string // the unquoted version of value, if it was a quoted string |
| 64 | } |
| 65 | |
| 66 | func (t *token) String() string { |
| 67 | if t.err == nil { |
| 68 | return fmt.Sprintf("%q (line=%d, offset=%d)", t.value, t.line, t.offset) |
| 69 | } |
| 70 | return fmt.Sprintf("parse error: %v", t.err) |
| 71 | } |
| 72 | |
| 73 | type textParser struct { |
| 74 | s string // remaining input |
| 75 | done bool // whether the parsing is finished (success or error) |
| 76 | backed bool // whether back() was called |
| 77 | offset, line int |
| 78 | cur token |
| 79 | } |
| 80 | |
| 81 | func newTextParser(s string) *textParser { |
| 82 | p := new(textParser) |
| 83 | p.s = s |
| 84 | p.line = 1 |
| 85 | p.cur.line = 1 |
| 86 | return p |
| 87 | } |
| 88 | |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 89 | func (p *textParser) errorf(format string, a ...interface{}) *ParseError { |
| Rob Pike | ad7cac7 | 2010-09-29 12:29:26 -0700 | [diff] [blame] | 90 | pe := &ParseError{fmt.Sprintf(format, a...), p.cur.line, p.cur.offset} |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 91 | p.cur.err = pe |
| 92 | p.done = true |
| 93 | return pe |
| 94 | } |
| 95 | |
| 96 | // Numbers and identifiers are matched by [-+._A-Za-z0-9] |
| 97 | func isIdentOrNumberChar(c byte) bool { |
| 98 | switch { |
| 99 | case 'A' <= c && c <= 'Z', 'a' <= c && c <= 'z': |
| 100 | return true |
| 101 | case '0' <= c && c <= '9': |
| 102 | return true |
| 103 | } |
| 104 | switch c { |
| 105 | case '-', '+', '.', '_': |
| 106 | return true |
| 107 | } |
| 108 | return false |
| 109 | } |
| 110 | |
| 111 | func isWhitespace(c byte) bool { |
| 112 | switch c { |
| 113 | case ' ', '\t', '\n', '\r': |
| 114 | return true |
| 115 | } |
| 116 | return false |
| 117 | } |
| 118 | |
| 119 | func (p *textParser) skipWhitespace() { |
| 120 | i := 0 |
| 121 | for i < len(p.s) && (isWhitespace(p.s[i]) || p.s[i] == '#') { |
| 122 | if p.s[i] == '#' { |
| 123 | // comment; skip to end of line or input |
| 124 | for i < len(p.s) && p.s[i] != '\n' { |
| 125 | i++ |
| 126 | } |
| 127 | if i == len(p.s) { |
| 128 | break |
| 129 | } |
| 130 | } |
| 131 | if p.s[i] == '\n' { |
| 132 | p.line++ |
| 133 | } |
| 134 | i++ |
| 135 | } |
| 136 | p.offset += i |
| 137 | p.s = p.s[i:len(p.s)] |
| 138 | if len(p.s) == 0 { |
| 139 | p.done = true |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func (p *textParser) advance() { |
| 144 | // Skip whitespace |
| 145 | p.skipWhitespace() |
| 146 | if p.done { |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | // Start of non-whitespace |
| 151 | p.cur.err = nil |
| 152 | p.cur.offset, p.cur.line = p.offset, p.line |
| 153 | p.cur.unquoted = "" |
| 154 | switch p.s[0] { |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 155 | case '<', '>', '{', '}', ':', '[', ']': |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 156 | // Single symbol |
| 157 | p.cur.value, p.s = p.s[0:1], p.s[1:len(p.s)] |
| David Symonds | 162d003 | 2012-06-28 09:44:46 -0700 | [diff] [blame] | 158 | case '"', '\'': |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 159 | // Quoted string |
| 160 | i := 1 |
| David Symonds | 162d003 | 2012-06-28 09:44:46 -0700 | [diff] [blame] | 161 | for i < len(p.s) && p.s[i] != p.s[0] && p.s[i] != '\n' { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 162 | if p.s[i] == '\\' && i+1 < len(p.s) { |
| 163 | // skip escaped char |
| 164 | i++ |
| 165 | } |
| 166 | i++ |
| 167 | } |
| David Symonds | 162d003 | 2012-06-28 09:44:46 -0700 | [diff] [blame] | 168 | if i >= len(p.s) || p.s[i] != p.s[0] { |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 169 | p.errorf("unmatched quote") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 170 | return |
| 171 | } |
| David Symonds | 183124e | 2012-03-23 13:20:23 +1100 | [diff] [blame] | 172 | unq, err := unquoteC(p.s[0 : i+1]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 173 | if err != nil { |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 174 | p.errorf("invalid quoted string %v", p.s[0:i+1]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 175 | return |
| 176 | } |
| 177 | p.cur.value, p.s = p.s[0:i+1], p.s[i+1:len(p.s)] |
| 178 | p.cur.unquoted = unq |
| 179 | default: |
| 180 | i := 0 |
| 181 | for i < len(p.s) && isIdentOrNumberChar(p.s[i]) { |
| 182 | i++ |
| 183 | } |
| 184 | if i == 0 { |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 185 | p.errorf("unexpected byte %#x", p.s[0]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 186 | return |
| 187 | } |
| 188 | p.cur.value, p.s = p.s[0:i], p.s[i:len(p.s)] |
| 189 | } |
| 190 | p.offset += len(p.cur.value) |
| 191 | } |
| 192 | |
| David Symonds | 162d003 | 2012-06-28 09:44:46 -0700 | [diff] [blame] | 193 | // quoteSwap returns a single quote for a double quote, and vice versa. |
| 194 | // It is intended to be used with strings.Map. |
| 195 | func quoteSwap(r rune) rune { |
| 196 | switch r { |
| 197 | case '\'': |
| 198 | return '"' |
| 199 | case '"': |
| 200 | return '\'' |
| 201 | } |
| 202 | return r |
| 203 | } |
| 204 | |
| David Symonds | 183124e | 2012-03-23 13:20:23 +1100 | [diff] [blame] | 205 | func unquoteC(s string) (string, error) { |
| David Symonds | 162d003 | 2012-06-28 09:44:46 -0700 | [diff] [blame] | 206 | // TODO: This is getting hacky. We should replace it work a self-contained parser. |
| 207 | |
| 208 | // strconv.Unquote is for Go strings, but text format strings may use |
| 209 | // single *or* double quotes. |
| 210 | if s[0] == '\'' { |
| 211 | s = strings.Map(quoteSwap, s) |
| 212 | s, err := unquoteC(s) |
| 213 | s = strings.Map(quoteSwap, s) |
| 214 | return s, err |
| 215 | } |
| 216 | |
| David Symonds | 183124e | 2012-03-23 13:20:23 +1100 | [diff] [blame] | 217 | // A notable divergence between quoted string literals in Go |
| 218 | // and what is acceptable for text format protocol buffers: |
| 219 | // the former considers \' invalid, but the latter considers it valid. |
| 220 | s = strings.Replace(s, `\'`, "'", -1) |
| 221 | return strconv.Unquote(s) |
| 222 | } |
| 223 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 224 | // Back off the parser by one token. Can only be done between calls to next(). |
| 225 | // It makes the next advance() a no-op. |
| 226 | func (p *textParser) back() { p.backed = true } |
| 227 | |
| 228 | // Advances the parser and returns the new current token. |
| 229 | func (p *textParser) next() *token { |
| 230 | if p.backed || p.done { |
| 231 | p.backed = false |
| 232 | return &p.cur |
| 233 | } |
| 234 | p.advance() |
| 235 | if p.done { |
| 236 | p.cur.value = "" |
| 237 | } else if len(p.cur.value) > 0 && p.cur.value[0] == '"' { |
| 238 | // Look for multiple quoted strings separated by whitespace, |
| 239 | // and concatenate them. |
| 240 | cat := p.cur |
| 241 | for { |
| 242 | p.skipWhitespace() |
| 243 | if p.done || p.s[0] != '"' { |
| 244 | break |
| 245 | } |
| 246 | p.advance() |
| 247 | if p.cur.err != nil { |
| 248 | return &p.cur |
| 249 | } |
| 250 | cat.value += " " + p.cur.value |
| 251 | cat.unquoted += p.cur.unquoted |
| 252 | } |
| 253 | p.done = false // parser may have seen EOF, but we want to return cat |
| 254 | p.cur = cat |
| 255 | } |
| 256 | return &p.cur |
| 257 | } |
| 258 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 259 | // Return an error indicating which required field was not set. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 260 | func (p *textParser) missingRequiredFieldError(sv reflect.Value) *ParseError { |
| 261 | st := sv.Type() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 262 | sprops := GetProperties(st) |
| 263 | for i := 0; i < st.NumField(); i++ { |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 264 | if !isNil(sv.Field(i)) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 265 | continue |
| 266 | } |
| 267 | |
| 268 | props := sprops.Prop[i] |
| 269 | if props.Required { |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 270 | return p.errorf("message %v missing required field %q", st, props.OrigName) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 273 | return p.errorf("message %v missing required field", st) // should not happen |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Returns the index in the struct for the named field, as well as the parsed tag properties. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 277 | func structFieldByName(st reflect.Type, name string) (int, *Properties, bool) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 278 | sprops := GetProperties(st) |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 279 | i, ok := sprops.origNames[name] |
| 280 | if ok { |
| 281 | return i, sprops.Prop[i], true |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 282 | } |
| 283 | return -1, nil, false |
| 284 | } |
| 285 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 286 | // Consume a ':' from the input stream (if the next token is a colon), |
| 287 | // returning an error if a colon is needed but not present. |
| 288 | func (p *textParser) checkForColon(props *Properties, typ reflect.Type) *ParseError { |
| 289 | tok := p.next() |
| 290 | if tok.err != nil { |
| 291 | return tok.err |
| 292 | } |
| 293 | if tok.value != ":" { |
| 294 | // Colon is optional when the field is a group or message. |
| 295 | needColon := true |
| 296 | switch props.Wire { |
| 297 | case "group": |
| 298 | needColon = false |
| 299 | case "bytes": |
| 300 | // A "bytes" field is either a message, a string, or a repeated field; |
| 301 | // those three become *T, *string and []T respectively, so we can check for |
| 302 | // this field being a pointer to a non-string. |
| 303 | if typ.Kind() == reflect.Ptr { |
| 304 | // *T or *string |
| 305 | if typ.Elem().Kind() == reflect.String { |
| 306 | break |
| 307 | } |
| 308 | } else if typ.Kind() == reflect.Slice { |
| 309 | // []T or []*T |
| 310 | if typ.Elem().Kind() != reflect.Ptr { |
| 311 | break |
| 312 | } |
| 313 | } |
| 314 | needColon = false |
| 315 | } |
| 316 | if needColon { |
| 317 | return p.errorf("expected ':', found %q", tok.value) |
| 318 | } |
| 319 | p.back() |
| 320 | } |
| 321 | return nil |
| 322 | } |
| 323 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 324 | func (p *textParser) readStruct(sv reflect.Value, terminator string) *ParseError { |
| 325 | st := sv.Type() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 326 | reqCount := GetProperties(st).reqCount |
| 327 | // A struct is a sequence of "name: value", terminated by one of |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 328 | // '>' or '}', or the end of the input. A name may also be |
| 329 | // "[extension]". |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 330 | for { |
| 331 | tok := p.next() |
| 332 | if tok.err != nil { |
| 333 | return tok.err |
| 334 | } |
| 335 | if tok.value == terminator { |
| 336 | break |
| 337 | } |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 338 | if tok.value == "[" { |
| 339 | // Looks like an extension. |
| 340 | // |
| 341 | // TODO: Check whether we need to handle |
| 342 | // namespace rooted names (e.g. ".something.Foo"). |
| 343 | tok = p.next() |
| 344 | if tok.err != nil { |
| 345 | return tok.err |
| 346 | } |
| 347 | var desc *ExtensionDesc |
| 348 | // This could be faster, but it's functional. |
| 349 | // TODO: Do something smarter than a linear scan. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 350 | for _, d := range RegisteredExtensions(reflect.New(st).Interface().(Message)) { |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 351 | if d.Name == tok.value { |
| 352 | desc = d |
| 353 | break |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 354 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 355 | } |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 356 | if desc == nil { |
| 357 | return p.errorf("unrecognized extension %q", tok.value) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 358 | } |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 359 | // Check the extension terminator. |
| 360 | tok = p.next() |
| 361 | if tok.err != nil { |
| 362 | return tok.err |
| 363 | } |
| 364 | if tok.value != "]" { |
| 365 | return p.errorf("unrecognized extension terminator %q", tok.value) |
| 366 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 367 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 368 | props := &Properties{} |
| 369 | props.Parse(desc.Tag) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 370 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 371 | typ := reflect.TypeOf(desc.ExtensionType) |
| 372 | if err := p.checkForColon(props, typ); err != nil { |
| 373 | return err |
| 374 | } |
| 375 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 376 | rep := desc.repeated() |
| 377 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 378 | // Read the extension structure, and set it in |
| 379 | // the value we're constructing. |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 380 | var ext reflect.Value |
| 381 | if !rep { |
| 382 | ext = reflect.New(typ).Elem() |
| 383 | } else { |
| 384 | ext = reflect.New(typ.Elem()).Elem() |
| 385 | } |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 386 | if err := p.readAny(ext, props); err != nil { |
| 387 | return err |
| 388 | } |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 389 | ep := sv.Addr().Interface().(extendableProto) |
| 390 | if !rep { |
| 391 | SetExtension(ep, desc, ext.Interface()) |
| 392 | } else { |
| 393 | old, err := GetExtension(ep, desc) |
| 394 | var sl reflect.Value |
| 395 | if err == nil { |
| 396 | sl = reflect.ValueOf(old) // existing slice |
| 397 | } else { |
| 398 | sl = reflect.MakeSlice(typ, 0, 1) |
| 399 | } |
| 400 | sl = reflect.Append(sl, ext) |
| 401 | SetExtension(ep, desc, sl.Interface()) |
| 402 | } |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 403 | } else { |
| 404 | // This is a normal, non-extension field. |
| 405 | fi, props, ok := structFieldByName(st, tok.value) |
| 406 | if !ok { |
| 407 | return p.errorf("unknown field name %q in %v", tok.value, st) |
| 408 | } |
| 409 | |
| 410 | // Check that it's not already set if it's not a repeated field. |
| 411 | if !props.Repeated && !isNil(sv.Field(fi)) { |
| 412 | return p.errorf("non-repeated field %q was repeated", tok.value) |
| 413 | } |
| 414 | |
| 415 | if err := p.checkForColon(props, st.Field(fi).Type); err != nil { |
| 416 | return err |
| 417 | } |
| 418 | |
| 419 | // Parse into the field. |
| 420 | if err := p.readAny(sv.Field(fi), props); err != nil { |
| 421 | return err |
| 422 | } |
| 423 | |
| 424 | if props.Required { |
| 425 | reqCount-- |
| 426 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | if reqCount > 0 { |
| 431 | return p.missingRequiredFieldError(sv) |
| 432 | } |
| 433 | return nil |
| 434 | } |
| 435 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 436 | func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError { |
| 437 | tok := p.next() |
| 438 | if tok.err != nil { |
| 439 | return tok.err |
| 440 | } |
| 441 | if tok.value == "" { |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 442 | return p.errorf("unexpected EOF") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 445 | switch fv := v; fv.Kind() { |
| 446 | case reflect.Slice: |
| 447 | at := v.Type() |
| Rob Pike | ab5b802 | 2010-06-21 17:47:58 -0700 | [diff] [blame] | 448 | if at.Elem().Kind() == reflect.Uint8 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 449 | // Special case for []byte |
| David Symonds | 162d003 | 2012-06-28 09:44:46 -0700 | [diff] [blame] | 450 | if tok.value[0] != '"' && tok.value[0] != '\'' { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 451 | // Deliberately written out here, as the error after |
| 452 | // this switch statement would write "invalid []byte: ...", |
| 453 | // which is not as user-friendly. |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 454 | return p.errorf("invalid string: %v", tok.value) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 455 | } |
| 456 | bytes := []byte(tok.unquoted) |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 457 | fv.Set(reflect.ValueOf(bytes)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 458 | return nil |
| 459 | } |
| 460 | // Repeated field. May already exist. |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 461 | flen := fv.Len() |
| 462 | if flen == fv.Cap() { |
| 463 | nav := reflect.MakeSlice(at, flen, 2*flen+1) |
| Rob Pike | 48fd4a4 | 2010-12-14 23:40:41 -0800 | [diff] [blame] | 464 | reflect.Copy(nav, fv) |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 465 | fv.Set(nav) |
| 466 | } |
| 467 | fv.SetLen(flen + 1) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 468 | |
| 469 | // Read one. |
| 470 | p.back() |
| David Symonds | ef8f0e8 | 2011-10-13 12:57:34 +1100 | [diff] [blame] | 471 | return p.readAny(fv.Index(flen), props) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 472 | case reflect.Bool: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 473 | // Either "true", "false", 1 or 0. |
| 474 | switch tok.value { |
| 475 | case "true", "1": |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 476 | fv.SetBool(true) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 477 | return nil |
| 478 | case "false", "0": |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 479 | fv.SetBool(false) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 480 | return nil |
| 481 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 482 | case reflect.Float32, reflect.Float64: |
| David Symonds | 6bd081e | 2012-06-28 10:46:25 -0700 | [diff] [blame] | 483 | v := tok.value |
| 484 | if strings.HasSuffix(v, "f") { |
| 485 | // Ignore 'f' for compatibility with output generated by C++. |
| 486 | v = v[:len(v)-1] |
| 487 | } |
| 488 | if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil { |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 489 | fv.SetFloat(f) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 490 | return nil |
| 491 | } |
| Rob Pike | 19b2dbb | 2011-04-11 16:49:15 -0700 | [diff] [blame] | 492 | case reflect.Int32: |
| David Symonds | 32612dd | 2012-06-15 07:59:05 -0700 | [diff] [blame] | 493 | if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil { |
| Rob Pike | 19b2dbb | 2011-04-11 16:49:15 -0700 | [diff] [blame] | 494 | fv.SetInt(x) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 495 | return nil |
| Rob Pike | 19b2dbb | 2011-04-11 16:49:15 -0700 | [diff] [blame] | 496 | } |
| 497 | if len(props.Enum) == 0 { |
| 498 | break |
| 499 | } |
| 500 | m, ok := enumValueMaps[props.Enum] |
| 501 | if !ok { |
| 502 | break |
| 503 | } |
| 504 | x, ok := m[tok.value] |
| 505 | if !ok { |
| 506 | break |
| 507 | } |
| 508 | fv.SetInt(int64(x)) |
| 509 | return nil |
| 510 | case reflect.Int64: |
| David Symonds | 32612dd | 2012-06-15 07:59:05 -0700 | [diff] [blame] | 511 | if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil { |
| Rob Pike | 19b2dbb | 2011-04-11 16:49:15 -0700 | [diff] [blame] | 512 | fv.SetInt(x) |
| 513 | return nil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 514 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 515 | case reflect.Ptr: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 516 | // A basic field (indirected through pointer), or a repeated message/group |
| 517 | p.back() |
| Rob Pike | ccd260c | 2011-04-18 13:13:04 -0700 | [diff] [blame] | 518 | fv.Set(reflect.New(fv.Type().Elem())) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 519 | return p.readAny(fv.Elem(), props) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 520 | case reflect.String: |
| David Symonds | 162d003 | 2012-06-28 09:44:46 -0700 | [diff] [blame] | 521 | if tok.value[0] == '"' || tok.value[0] == '\'' { |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 522 | fv.SetString(tok.unquoted) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 523 | return nil |
| 524 | } |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 525 | case reflect.Struct: |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 526 | var terminator string |
| 527 | switch tok.value { |
| 528 | case "{": |
| 529 | terminator = "}" |
| 530 | case "<": |
| 531 | terminator = ">" |
| 532 | default: |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 533 | return p.errorf("expected '{' or '<', found %q", tok.value) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 534 | } |
| 535 | return p.readStruct(fv, terminator) |
| Rob Pike | 19b2dbb | 2011-04-11 16:49:15 -0700 | [diff] [blame] | 536 | case reflect.Uint32: |
| David Symonds | 32612dd | 2012-06-15 07:59:05 -0700 | [diff] [blame] | 537 | if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil { |
| Rob Pike | 19b2dbb | 2011-04-11 16:49:15 -0700 | [diff] [blame] | 538 | fv.SetUint(uint64(x)) |
| 539 | return nil |
| 540 | } |
| 541 | case reflect.Uint64: |
| David Symonds | 32612dd | 2012-06-15 07:59:05 -0700 | [diff] [blame] | 542 | if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil { |
| Rob Pike | 19b2dbb | 2011-04-11 16:49:15 -0700 | [diff] [blame] | 543 | fv.SetUint(x) |
| 544 | return nil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| Rob Pike | d6420b8 | 2011-04-13 16:37:04 -0700 | [diff] [blame] | 547 | return p.errorf("invalid %v: %v", v.Type(), tok.value) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 550 | // UnmarshalText reads a protocol buffer in Text format. |
| 551 | func UnmarshalText(s string, pb Message) error { |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 552 | v := reflect.ValueOf(pb) |
| David Symonds | a9cda21 | 2011-04-15 01:23:17 -0700 | [diff] [blame] | 553 | if pe := newTextParser(s).readStruct(v.Elem(), ""); pe != nil { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 554 | return pe |
| 555 | } |
| 556 | return nil |
| 557 | } |