text_parser.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. package proto
  32. // Functions for parsing the Text protocol buffer format.
  33. // TODO: message sets.
  34. import (
  35. "encoding"
  36. "errors"
  37. "fmt"
  38. "reflect"
  39. "strconv"
  40. "strings"
  41. "unicode/utf8"
  42. )
  43. type ParseError struct {
  44. Message string
  45. Line int // 1-based line number
  46. Offset int // 0-based byte offset from start of input
  47. }
  48. func (p *ParseError) Error() string {
  49. if p.Line == 1 {
  50. // show offset only for first line
  51. return fmt.Sprintf("line 1.%d: %v", p.Offset, p.Message)
  52. }
  53. return fmt.Sprintf("line %d: %v", p.Line, p.Message)
  54. }
  55. type token struct {
  56. value string
  57. err *ParseError
  58. line int // line number
  59. offset int // byte number from start of input, not start of line
  60. unquoted string // the unquoted version of value, if it was a quoted string
  61. }
  62. func (t *token) String() string {
  63. if t.err == nil {
  64. return fmt.Sprintf("%q (line=%d, offset=%d)", t.value, t.line, t.offset)
  65. }
  66. return fmt.Sprintf("parse error: %v", t.err)
  67. }
  68. type textParser struct {
  69. s string // remaining input
  70. done bool // whether the parsing is finished (success or error)
  71. backed bool // whether back() was called
  72. offset, line int
  73. cur token
  74. }
  75. func newTextParser(s string) *textParser {
  76. p := new(textParser)
  77. p.s = s
  78. p.line = 1
  79. p.cur.line = 1
  80. return p
  81. }
  82. func (p *textParser) errorf(format string, a ...interface{}) *ParseError {
  83. pe := &ParseError{fmt.Sprintf(format, a...), p.cur.line, p.cur.offset}
  84. p.cur.err = pe
  85. p.done = true
  86. return pe
  87. }
  88. // Numbers and identifiers are matched by [-+._A-Za-z0-9]
  89. func isIdentOrNumberChar(c byte) bool {
  90. switch {
  91. case 'A' <= c && c <= 'Z', 'a' <= c && c <= 'z':
  92. return true
  93. case '0' <= c && c <= '9':
  94. return true
  95. }
  96. switch c {
  97. case '-', '+', '.', '_':
  98. return true
  99. }
  100. return false
  101. }
  102. func isWhitespace(c byte) bool {
  103. switch c {
  104. case ' ', '\t', '\n', '\r':
  105. return true
  106. }
  107. return false
  108. }
  109. func (p *textParser) skipWhitespace() {
  110. i := 0
  111. for i < len(p.s) && (isWhitespace(p.s[i]) || p.s[i] == '#') {
  112. if p.s[i] == '#' {
  113. // comment; skip to end of line or input
  114. for i < len(p.s) && p.s[i] != '\n' {
  115. i++
  116. }
  117. if i == len(p.s) {
  118. break
  119. }
  120. }
  121. if p.s[i] == '\n' {
  122. p.line++
  123. }
  124. i++
  125. }
  126. p.offset += i
  127. p.s = p.s[i:len(p.s)]
  128. if len(p.s) == 0 {
  129. p.done = true
  130. }
  131. }
  132. func (p *textParser) advance() {
  133. // Skip whitespace
  134. p.skipWhitespace()
  135. if p.done {
  136. return
  137. }
  138. // Start of non-whitespace
  139. p.cur.err = nil
  140. p.cur.offset, p.cur.line = p.offset, p.line
  141. p.cur.unquoted = ""
  142. switch p.s[0] {
  143. case '<', '>', '{', '}', ':', '[', ']', ';', ',':
  144. // Single symbol
  145. p.cur.value, p.s = p.s[0:1], p.s[1:len(p.s)]
  146. case '"', '\'':
  147. // Quoted string
  148. i := 1
  149. for i < len(p.s) && p.s[i] != p.s[0] && p.s[i] != '\n' {
  150. if p.s[i] == '\\' && i+1 < len(p.s) {
  151. // skip escaped char
  152. i++
  153. }
  154. i++
  155. }
  156. if i >= len(p.s) || p.s[i] != p.s[0] {
  157. p.errorf("unmatched quote")
  158. return
  159. }
  160. unq, err := unquoteC(p.s[1:i], rune(p.s[0]))
  161. if err != nil {
  162. p.errorf("invalid quoted string %v", p.s[0:i+1])
  163. return
  164. }
  165. p.cur.value, p.s = p.s[0:i+1], p.s[i+1:len(p.s)]
  166. p.cur.unquoted = unq
  167. default:
  168. i := 0
  169. for i < len(p.s) && isIdentOrNumberChar(p.s[i]) {
  170. i++
  171. }
  172. if i == 0 {
  173. p.errorf("unexpected byte %#x", p.s[0])
  174. return
  175. }
  176. p.cur.value, p.s = p.s[0:i], p.s[i:len(p.s)]
  177. }
  178. p.offset += len(p.cur.value)
  179. }
  180. var (
  181. errBadUTF8 = errors.New("proto: bad UTF-8")
  182. errBadHex = errors.New("proto: bad hexadecimal")
  183. )
  184. func unquoteC(s string, quote rune) (string, error) {
  185. // This is based on C++'s tokenizer.cc.
  186. // Despite its name, this is *not* parsing C syntax.
  187. // For instance, "\0" is an invalid quoted string.
  188. // Avoid allocation in trivial cases.
  189. simple := true
  190. for _, r := range s {
  191. if r == '\\' || r == quote {
  192. simple = false
  193. break
  194. }
  195. }
  196. if simple {
  197. return s, nil
  198. }
  199. buf := make([]byte, 0, 3*len(s)/2)
  200. for len(s) > 0 {
  201. r, n := utf8.DecodeRuneInString(s)
  202. if r == utf8.RuneError && n == 1 {
  203. return "", errBadUTF8
  204. }
  205. s = s[n:]
  206. if r != '\\' {
  207. if r < utf8.RuneSelf {
  208. buf = append(buf, byte(r))
  209. } else {
  210. buf = append(buf, string(r)...)
  211. }
  212. continue
  213. }
  214. ch, tail, err := unescape(s)
  215. if err != nil {
  216. return "", err
  217. }
  218. buf = append(buf, ch...)
  219. s = tail
  220. }
  221. return string(buf), nil
  222. }
  223. func unescape(s string) (ch string, tail string, err error) {
  224. r, n := utf8.DecodeRuneInString(s)
  225. if r == utf8.RuneError && n == 1 {
  226. return "", "", errBadUTF8
  227. }
  228. s = s[n:]
  229. switch r {
  230. case 'a':
  231. return "\a", s, nil
  232. case 'b':
  233. return "\b", s, nil
  234. case 'f':
  235. return "\f", s, nil
  236. case 'n':
  237. return "\n", s, nil
  238. case 'r':
  239. return "\r", s, nil
  240. case 't':
  241. return "\t", s, nil
  242. case 'v':
  243. return "\v", s, nil
  244. case '?':
  245. return "?", s, nil // trigraph workaround
  246. case '\'', '"', '\\':
  247. return string(r), s, nil
  248. case '0', '1', '2', '3', '4', '5', '6', '7', 'x', 'X':
  249. if len(s) < 2 {
  250. return "", "", fmt.Errorf(`\%c requires 2 following digits`, r)
  251. }
  252. base := 8
  253. ss := s[:2]
  254. s = s[2:]
  255. if r == 'x' || r == 'X' {
  256. base = 16
  257. } else {
  258. ss = string(r) + ss
  259. }
  260. i, err := strconv.ParseUint(ss, base, 8)
  261. if err != nil {
  262. return "", "", err
  263. }
  264. return string([]byte{byte(i)}), s, nil
  265. case 'u', 'U':
  266. n := 4
  267. if r == 'U' {
  268. n = 8
  269. }
  270. if len(s) < n {
  271. return "", "", fmt.Errorf(`\%c requires %d digits`, r, n)
  272. }
  273. bs := make([]byte, n/2)
  274. for i := 0; i < n; i += 2 {
  275. a, ok1 := unhex(s[i])
  276. b, ok2 := unhex(s[i+1])
  277. if !ok1 || !ok2 {
  278. return "", "", errBadHex
  279. }
  280. bs[i/2] = a<<4 | b
  281. }
  282. s = s[n:]
  283. return string(bs), s, nil
  284. }
  285. return "", "", fmt.Errorf(`unknown escape \%c`, r)
  286. }
  287. // Adapted from src/pkg/strconv/quote.go.
  288. func unhex(b byte) (v byte, ok bool) {
  289. switch {
  290. case '0' <= b && b <= '9':
  291. return b - '0', true
  292. case 'a' <= b && b <= 'f':
  293. return b - 'a' + 10, true
  294. case 'A' <= b && b <= 'F':
  295. return b - 'A' + 10, true
  296. }
  297. return 0, false
  298. }
  299. // Back off the parser by one token. Can only be done between calls to next().
  300. // It makes the next advance() a no-op.
  301. func (p *textParser) back() { p.backed = true }
  302. // Advances the parser and returns the new current token.
  303. func (p *textParser) next() *token {
  304. if p.backed || p.done {
  305. p.backed = false
  306. return &p.cur
  307. }
  308. p.advance()
  309. if p.done {
  310. p.cur.value = ""
  311. } else if len(p.cur.value) > 0 && p.cur.value[0] == '"' {
  312. // Look for multiple quoted strings separated by whitespace,
  313. // and concatenate them.
  314. cat := p.cur
  315. for {
  316. p.skipWhitespace()
  317. if p.done || p.s[0] != '"' {
  318. break
  319. }
  320. p.advance()
  321. if p.cur.err != nil {
  322. return &p.cur
  323. }
  324. cat.value += " " + p.cur.value
  325. cat.unquoted += p.cur.unquoted
  326. }
  327. p.done = false // parser may have seen EOF, but we want to return cat
  328. p.cur = cat
  329. }
  330. return &p.cur
  331. }
  332. func (p *textParser) consumeToken(s string) error {
  333. tok := p.next()
  334. if tok.err != nil {
  335. return tok.err
  336. }
  337. if tok.value != s {
  338. p.back()
  339. return p.errorf("expected %q, found %q", s, tok.value)
  340. }
  341. return nil
  342. }
  343. // Return a RequiredNotSetError indicating which required field was not set.
  344. func (p *textParser) missingRequiredFieldError(sv reflect.Value) *RequiredNotSetError {
  345. st := sv.Type()
  346. sprops := GetProperties(st)
  347. for i := 0; i < st.NumField(); i++ {
  348. if !isNil(sv.Field(i)) {
  349. continue
  350. }
  351. props := sprops.Prop[i]
  352. if props.Required {
  353. return &RequiredNotSetError{fmt.Sprintf("%v.%v", st, props.OrigName)}
  354. }
  355. }
  356. return &RequiredNotSetError{fmt.Sprintf("%v.<unknown field name>", st)} // should not happen
  357. }
  358. // Returns the index in the struct for the named field, as well as the parsed tag properties.
  359. func structFieldByName(st reflect.Type, name string) (int, *Properties, bool) {
  360. sprops := GetProperties(st)
  361. i, ok := sprops.decoderOrigNames[name]
  362. if ok {
  363. return i, sprops.Prop[i], true
  364. }
  365. return -1, nil, false
  366. }
  367. // Consume a ':' from the input stream (if the next token is a colon),
  368. // returning an error if a colon is needed but not present.
  369. func (p *textParser) checkForColon(props *Properties, typ reflect.Type) *ParseError {
  370. tok := p.next()
  371. if tok.err != nil {
  372. return tok.err
  373. }
  374. if tok.value != ":" {
  375. // Colon is optional when the field is a group or message.
  376. needColon := true
  377. switch props.Wire {
  378. case "group":
  379. needColon = false
  380. case "bytes":
  381. // A "bytes" field is either a message, a string, or a repeated field;
  382. // those three become *T, *string and []T respectively, so we can check for
  383. // this field being a pointer to a non-string.
  384. if typ.Kind() == reflect.Ptr {
  385. // *T or *string
  386. if typ.Elem().Kind() == reflect.String {
  387. break
  388. }
  389. } else if typ.Kind() == reflect.Slice {
  390. // []T or []*T
  391. if typ.Elem().Kind() != reflect.Ptr {
  392. break
  393. }
  394. } else if typ.Kind() == reflect.String {
  395. // The proto3 exception is for a string field,
  396. // which requires a colon.
  397. break
  398. }
  399. needColon = false
  400. }
  401. if needColon {
  402. return p.errorf("expected ':', found %q", tok.value)
  403. }
  404. p.back()
  405. }
  406. return nil
  407. }
  408. func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
  409. st := sv.Type()
  410. reqCount := GetProperties(st).reqCount
  411. var reqFieldErr error
  412. fieldSet := make(map[string]bool)
  413. // A struct is a sequence of "name: value", terminated by one of
  414. // '>' or '}', or the end of the input. A name may also be
  415. // "[extension]".
  416. for {
  417. tok := p.next()
  418. if tok.err != nil {
  419. return tok.err
  420. }
  421. if tok.value == terminator {
  422. break
  423. }
  424. if tok.value == "[" {
  425. // Looks like an extension.
  426. //
  427. // TODO: Check whether we need to handle
  428. // namespace rooted names (e.g. ".something.Foo").
  429. tok = p.next()
  430. if tok.err != nil {
  431. return tok.err
  432. }
  433. var desc *ExtensionDesc
  434. // This could be faster, but it's functional.
  435. // TODO: Do something smarter than a linear scan.
  436. for _, d := range RegisteredExtensions(reflect.New(st).Interface().(Message)) {
  437. if d.Name == tok.value {
  438. desc = d
  439. break
  440. }
  441. }
  442. if desc == nil {
  443. return p.errorf("unrecognized extension %q", tok.value)
  444. }
  445. // Check the extension terminator.
  446. tok = p.next()
  447. if tok.err != nil {
  448. return tok.err
  449. }
  450. if tok.value != "]" {
  451. return p.errorf("unrecognized extension terminator %q", tok.value)
  452. }
  453. props := &Properties{}
  454. props.Parse(desc.Tag)
  455. typ := reflect.TypeOf(desc.ExtensionType)
  456. if err := p.checkForColon(props, typ); err != nil {
  457. return err
  458. }
  459. rep := desc.repeated()
  460. // Read the extension structure, and set it in
  461. // the value we're constructing.
  462. var ext reflect.Value
  463. if !rep {
  464. ext = reflect.New(typ).Elem()
  465. } else {
  466. ext = reflect.New(typ.Elem()).Elem()
  467. }
  468. if err := p.readAny(ext, props); err != nil {
  469. if _, ok := err.(*RequiredNotSetError); !ok {
  470. return err
  471. }
  472. reqFieldErr = err
  473. }
  474. ep := sv.Addr().Interface().(extendableProto)
  475. if !rep {
  476. SetExtension(ep, desc, ext.Interface())
  477. } else {
  478. old, err := GetExtension(ep, desc)
  479. var sl reflect.Value
  480. if err == nil {
  481. sl = reflect.ValueOf(old) // existing slice
  482. } else {
  483. sl = reflect.MakeSlice(typ, 0, 1)
  484. }
  485. sl = reflect.Append(sl, ext)
  486. SetExtension(ep, desc, sl.Interface())
  487. }
  488. } else {
  489. // This is a normal, non-extension field.
  490. name := tok.value
  491. fi, props, ok := structFieldByName(st, name)
  492. if !ok {
  493. return p.errorf("unknown field name %q in %v", name, st)
  494. }
  495. dst := sv.Field(fi)
  496. if dst.Kind() == reflect.Map {
  497. // Consume any colon.
  498. if err := p.checkForColon(props, dst.Type()); err != nil {
  499. return err
  500. }
  501. // Construct the map if it doesn't already exist.
  502. if dst.IsNil() {
  503. dst.Set(reflect.MakeMap(dst.Type()))
  504. }
  505. key := reflect.New(dst.Type().Key()).Elem()
  506. val := reflect.New(dst.Type().Elem()).Elem()
  507. // The map entry should be this sequence of tokens:
  508. // < key : KEY value : VALUE >
  509. // Technically the "key" and "value" could come in any order,
  510. // but in practice they won't.
  511. tok := p.next()
  512. var terminator string
  513. switch tok.value {
  514. case "<":
  515. terminator = ">"
  516. case "{":
  517. terminator = "}"
  518. default:
  519. return p.errorf("expected '{' or '<', found %q", tok.value)
  520. }
  521. if err := p.consumeToken("key"); err != nil {
  522. return err
  523. }
  524. if err := p.consumeToken(":"); err != nil {
  525. return err
  526. }
  527. if err := p.readAny(key, props.mkeyprop); err != nil {
  528. return err
  529. }
  530. if err := p.consumeToken("value"); err != nil {
  531. return err
  532. }
  533. if err := p.checkForColon(props.mvalprop, dst.Type().Elem()); err != nil {
  534. return err
  535. }
  536. if err := p.readAny(val, props.mvalprop); err != nil {
  537. return err
  538. }
  539. if err := p.consumeToken(terminator); err != nil {
  540. return err
  541. }
  542. dst.SetMapIndex(key, val)
  543. continue
  544. }
  545. // Check that it's not already set if it's not a repeated field.
  546. if !props.Repeated && fieldSet[name] {
  547. return p.errorf("non-repeated field %q was repeated", name)
  548. }
  549. if err := p.checkForColon(props, st.Field(fi).Type); err != nil {
  550. return err
  551. }
  552. // Parse into the field.
  553. fieldSet[name] = true
  554. if err := p.readAny(dst, props); err != nil {
  555. if _, ok := err.(*RequiredNotSetError); !ok {
  556. return err
  557. }
  558. reqFieldErr = err
  559. } else if props.Required {
  560. reqCount--
  561. }
  562. }
  563. // For backward compatibility, permit a semicolon or comma after a field.
  564. tok = p.next()
  565. if tok.err != nil {
  566. return tok.err
  567. }
  568. if tok.value != ";" && tok.value != "," {
  569. p.back()
  570. }
  571. }
  572. if reqCount > 0 {
  573. return p.missingRequiredFieldError(sv)
  574. }
  575. return reqFieldErr
  576. }
  577. func (p *textParser) readAny(v reflect.Value, props *Properties) error {
  578. tok := p.next()
  579. if tok.err != nil {
  580. return tok.err
  581. }
  582. if tok.value == "" {
  583. return p.errorf("unexpected EOF")
  584. }
  585. switch fv := v; fv.Kind() {
  586. case reflect.Slice:
  587. at := v.Type()
  588. if at.Elem().Kind() == reflect.Uint8 {
  589. // Special case for []byte
  590. if tok.value[0] != '"' && tok.value[0] != '\'' {
  591. // Deliberately written out here, as the error after
  592. // this switch statement would write "invalid []byte: ...",
  593. // which is not as user-friendly.
  594. return p.errorf("invalid string: %v", tok.value)
  595. }
  596. bytes := []byte(tok.unquoted)
  597. fv.Set(reflect.ValueOf(bytes))
  598. return nil
  599. }
  600. // Repeated field. May already exist.
  601. flen := fv.Len()
  602. if flen == fv.Cap() {
  603. nav := reflect.MakeSlice(at, flen, 2*flen+1)
  604. reflect.Copy(nav, fv)
  605. fv.Set(nav)
  606. }
  607. fv.SetLen(flen + 1)
  608. // Read one.
  609. p.back()
  610. return p.readAny(fv.Index(flen), props)
  611. case reflect.Bool:
  612. // Either "true", "false", 1 or 0.
  613. switch tok.value {
  614. case "true", "1":
  615. fv.SetBool(true)
  616. return nil
  617. case "false", "0":
  618. fv.SetBool(false)
  619. return nil
  620. }
  621. case reflect.Float32, reflect.Float64:
  622. v := tok.value
  623. // Ignore 'f' for compatibility with output generated by C++, but don't
  624. // remove 'f' when the value is "-inf" or "inf".
  625. if strings.HasSuffix(v, "f") && tok.value != "-inf" && tok.value != "inf" {
  626. v = v[:len(v)-1]
  627. }
  628. if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {
  629. fv.SetFloat(f)
  630. return nil
  631. }
  632. case reflect.Int32:
  633. if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
  634. fv.SetInt(x)
  635. return nil
  636. }
  637. if len(props.Enum) == 0 {
  638. break
  639. }
  640. m, ok := enumValueMaps[props.Enum]
  641. if !ok {
  642. break
  643. }
  644. x, ok := m[tok.value]
  645. if !ok {
  646. break
  647. }
  648. fv.SetInt(int64(x))
  649. return nil
  650. case reflect.Int64:
  651. if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
  652. fv.SetInt(x)
  653. return nil
  654. }
  655. case reflect.Ptr:
  656. // A basic field (indirected through pointer), or a repeated message/group
  657. p.back()
  658. fv.Set(reflect.New(fv.Type().Elem()))
  659. return p.readAny(fv.Elem(), props)
  660. case reflect.String:
  661. if tok.value[0] == '"' || tok.value[0] == '\'' {
  662. fv.SetString(tok.unquoted)
  663. return nil
  664. }
  665. case reflect.Struct:
  666. var terminator string
  667. switch tok.value {
  668. case "{":
  669. terminator = "}"
  670. case "<":
  671. terminator = ">"
  672. default:
  673. return p.errorf("expected '{' or '<', found %q", tok.value)
  674. }
  675. // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
  676. return p.readStruct(fv, terminator)
  677. case reflect.Uint32:
  678. if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
  679. fv.SetUint(uint64(x))
  680. return nil
  681. }
  682. case reflect.Uint64:
  683. if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
  684. fv.SetUint(x)
  685. return nil
  686. }
  687. }
  688. return p.errorf("invalid %v: %v", v.Type(), tok.value)
  689. }
  690. // UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb
  691. // before starting to unmarshal, so any existing data in pb is always removed.
  692. // If a required field is not set and no other error occurs,
  693. // UnmarshalText returns *RequiredNotSetError.
  694. func UnmarshalText(s string, pb Message) error {
  695. if um, ok := pb.(encoding.TextUnmarshaler); ok {
  696. err := um.UnmarshalText([]byte(s))
  697. return err
  698. }
  699. pb.Reset()
  700. v := reflect.ValueOf(pb)
  701. if pe := newTextParser(s).readStruct(v.Elem(), ""); pe != nil {
  702. return pe
  703. }
  704. return nil
  705. }