text_parser.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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 %s: %v", p.s[0:i+1], err)
  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(sprops *StructProperties, name string) (int, *Properties, bool) {
  360. i, ok := sprops.decoderOrigNames[name]
  361. if ok {
  362. return i, sprops.Prop[i], true
  363. }
  364. return -1, nil, false
  365. }
  366. // Consume a ':' from the input stream (if the next token is a colon),
  367. // returning an error if a colon is needed but not present.
  368. func (p *textParser) checkForColon(props *Properties, typ reflect.Type) *ParseError {
  369. tok := p.next()
  370. if tok.err != nil {
  371. return tok.err
  372. }
  373. if tok.value != ":" {
  374. // Colon is optional when the field is a group or message.
  375. needColon := true
  376. switch props.Wire {
  377. case "group":
  378. needColon = false
  379. case "bytes":
  380. // A "bytes" field is either a message, a string, or a repeated field;
  381. // those three become *T, *string and []T respectively, so we can check for
  382. // this field being a pointer to a non-string.
  383. if typ.Kind() == reflect.Ptr {
  384. // *T or *string
  385. if typ.Elem().Kind() == reflect.String {
  386. break
  387. }
  388. } else if typ.Kind() == reflect.Slice {
  389. // []T or []*T
  390. if typ.Elem().Kind() != reflect.Ptr {
  391. break
  392. }
  393. } else if typ.Kind() == reflect.String {
  394. // The proto3 exception is for a string field,
  395. // which requires a colon.
  396. break
  397. }
  398. needColon = false
  399. }
  400. if needColon {
  401. return p.errorf("expected ':', found %q", tok.value)
  402. }
  403. p.back()
  404. }
  405. return nil
  406. }
  407. func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
  408. st := sv.Type()
  409. sprops := GetProperties(st)
  410. reqCount := sprops.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. if err := p.consumeOptionalSeparator(); err != nil {
  489. return err
  490. }
  491. continue
  492. }
  493. // This is a normal, non-extension field.
  494. name := tok.value
  495. var dst reflect.Value
  496. fi, props, ok := structFieldByName(sprops, name)
  497. if ok {
  498. dst = sv.Field(fi)
  499. } else if oop, ok := sprops.OneofTypes[name]; ok {
  500. // It is a oneof.
  501. props = oop.Prop
  502. nv := reflect.New(oop.Type.Elem())
  503. dst = nv.Elem().Field(0)
  504. sv.Field(oop.Field).Set(nv)
  505. }
  506. if !dst.IsValid() {
  507. return p.errorf("unknown field name %q in %v", name, st)
  508. }
  509. if dst.Kind() == reflect.Map {
  510. // Consume any colon.
  511. if err := p.checkForColon(props, dst.Type()); err != nil {
  512. return err
  513. }
  514. // Construct the map if it doesn't already exist.
  515. if dst.IsNil() {
  516. dst.Set(reflect.MakeMap(dst.Type()))
  517. }
  518. key := reflect.New(dst.Type().Key()).Elem()
  519. val := reflect.New(dst.Type().Elem()).Elem()
  520. // The map entry should be this sequence of tokens:
  521. // < key : KEY value : VALUE >
  522. // Technically the "key" and "value" could come in any order,
  523. // but in practice they won't.
  524. tok := p.next()
  525. var terminator string
  526. switch tok.value {
  527. case "<":
  528. terminator = ">"
  529. case "{":
  530. terminator = "}"
  531. default:
  532. return p.errorf("expected '{' or '<', found %q", tok.value)
  533. }
  534. if err := p.consumeToken("key"); err != nil {
  535. return err
  536. }
  537. if err := p.consumeToken(":"); err != nil {
  538. return err
  539. }
  540. if err := p.readAny(key, props.mkeyprop); err != nil {
  541. return err
  542. }
  543. if err := p.consumeOptionalSeparator(); err != nil {
  544. return err
  545. }
  546. if err := p.consumeToken("value"); err != nil {
  547. return err
  548. }
  549. if err := p.checkForColon(props.mvalprop, dst.Type().Elem()); err != nil {
  550. return err
  551. }
  552. if err := p.readAny(val, props.mvalprop); err != nil {
  553. return err
  554. }
  555. if err := p.consumeOptionalSeparator(); err != nil {
  556. return err
  557. }
  558. if err := p.consumeToken(terminator); err != nil {
  559. return err
  560. }
  561. dst.SetMapIndex(key, val)
  562. continue
  563. }
  564. // Check that it's not already set if it's not a repeated field.
  565. if !props.Repeated && fieldSet[name] {
  566. return p.errorf("non-repeated field %q was repeated", name)
  567. }
  568. if err := p.checkForColon(props, dst.Type()); err != nil {
  569. return err
  570. }
  571. // Parse into the field.
  572. fieldSet[name] = true
  573. if err := p.readAny(dst, props); err != nil {
  574. if _, ok := err.(*RequiredNotSetError); !ok {
  575. return err
  576. }
  577. reqFieldErr = err
  578. } else if props.Required {
  579. reqCount--
  580. }
  581. if err := p.consumeOptionalSeparator(); err != nil {
  582. return err
  583. }
  584. }
  585. if reqCount > 0 {
  586. return p.missingRequiredFieldError(sv)
  587. }
  588. return reqFieldErr
  589. }
  590. // consumeOptionalSeparator consumes an optional semicolon or comma.
  591. // It is used in readStruct to provide backward compatibility.
  592. func (p *textParser) consumeOptionalSeparator() error {
  593. tok := p.next()
  594. if tok.err != nil {
  595. return tok.err
  596. }
  597. if tok.value != ";" && tok.value != "," {
  598. p.back()
  599. }
  600. return nil
  601. }
  602. func (p *textParser) readAny(v reflect.Value, props *Properties) error {
  603. tok := p.next()
  604. if tok.err != nil {
  605. return tok.err
  606. }
  607. if tok.value == "" {
  608. return p.errorf("unexpected EOF")
  609. }
  610. switch fv := v; fv.Kind() {
  611. case reflect.Slice:
  612. at := v.Type()
  613. if at.Elem().Kind() == reflect.Uint8 {
  614. // Special case for []byte
  615. if tok.value[0] != '"' && tok.value[0] != '\'' {
  616. // Deliberately written out here, as the error after
  617. // this switch statement would write "invalid []byte: ...",
  618. // which is not as user-friendly.
  619. return p.errorf("invalid string: %v", tok.value)
  620. }
  621. bytes := []byte(tok.unquoted)
  622. fv.Set(reflect.ValueOf(bytes))
  623. return nil
  624. }
  625. // Repeated field.
  626. if tok.value == "[" {
  627. // Repeated field with list notation, like [1,2,3].
  628. for {
  629. fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
  630. err := p.readAny(fv.Index(fv.Len()-1), props)
  631. if err != nil {
  632. return err
  633. }
  634. tok := p.next()
  635. if tok.err != nil {
  636. return tok.err
  637. }
  638. if tok.value == "]" {
  639. break
  640. }
  641. if tok.value != "," {
  642. return p.errorf("Expected ']' or ',' found %q", tok.value)
  643. }
  644. }
  645. return nil
  646. }
  647. // One value of the repeated field.
  648. p.back()
  649. fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
  650. return p.readAny(fv.Index(fv.Len()-1), props)
  651. case reflect.Bool:
  652. // Either "true", "false", 1 or 0.
  653. switch tok.value {
  654. case "true", "1":
  655. fv.SetBool(true)
  656. return nil
  657. case "false", "0":
  658. fv.SetBool(false)
  659. return nil
  660. }
  661. case reflect.Float32, reflect.Float64:
  662. v := tok.value
  663. // Ignore 'f' for compatibility with output generated by C++, but don't
  664. // remove 'f' when the value is "-inf" or "inf".
  665. if strings.HasSuffix(v, "f") && tok.value != "-inf" && tok.value != "inf" {
  666. v = v[:len(v)-1]
  667. }
  668. if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {
  669. fv.SetFloat(f)
  670. return nil
  671. }
  672. case reflect.Int32:
  673. if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
  674. fv.SetInt(x)
  675. return nil
  676. }
  677. if len(props.Enum) == 0 {
  678. break
  679. }
  680. m, ok := enumValueMaps[props.Enum]
  681. if !ok {
  682. break
  683. }
  684. x, ok := m[tok.value]
  685. if !ok {
  686. break
  687. }
  688. fv.SetInt(int64(x))
  689. return nil
  690. case reflect.Int64:
  691. if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
  692. fv.SetInt(x)
  693. return nil
  694. }
  695. case reflect.Ptr:
  696. // A basic field (indirected through pointer), or a repeated message/group
  697. p.back()
  698. fv.Set(reflect.New(fv.Type().Elem()))
  699. return p.readAny(fv.Elem(), props)
  700. case reflect.String:
  701. if tok.value[0] == '"' || tok.value[0] == '\'' {
  702. fv.SetString(tok.unquoted)
  703. return nil
  704. }
  705. case reflect.Struct:
  706. var terminator string
  707. switch tok.value {
  708. case "{":
  709. terminator = "}"
  710. case "<":
  711. terminator = ">"
  712. default:
  713. return p.errorf("expected '{' or '<', found %q", tok.value)
  714. }
  715. // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
  716. return p.readStruct(fv, terminator)
  717. case reflect.Uint32:
  718. if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
  719. fv.SetUint(uint64(x))
  720. return nil
  721. }
  722. case reflect.Uint64:
  723. if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
  724. fv.SetUint(x)
  725. return nil
  726. }
  727. }
  728. return p.errorf("invalid %v: %v", v.Type(), tok.value)
  729. }
  730. // UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb
  731. // before starting to unmarshal, so any existing data in pb is always removed.
  732. // If a required field is not set and no other error occurs,
  733. // UnmarshalText returns *RequiredNotSetError.
  734. func UnmarshalText(s string, pb Message) error {
  735. if um, ok := pb.(encoding.TextUnmarshaler); ok {
  736. err := um.UnmarshalText([]byte(s))
  737. return err
  738. }
  739. pb.Reset()
  740. v := reflect.ValueOf(pb)
  741. if pe := newTextParser(s).readStruct(v.Elem(), ""); pe != nil {
  742. return pe
  743. }
  744. return nil
  745. }