text_parser.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  5. //
  6. // Go support for Protocol Buffers - Google's data interchange format
  7. //
  8. // Copyright 2010 The Go Authors. All rights reserved.
  9. // https://github.com/golang/protobuf
  10. //
  11. // Redistribution and use in source and binary forms, with or without
  12. // modification, are permitted provided that the following conditions are
  13. // met:
  14. //
  15. // * Redistributions of source code must retain the above copyright
  16. // notice, this list of conditions and the following disclaimer.
  17. // * Redistributions in binary form must reproduce the above
  18. // copyright notice, this list of conditions and the following disclaimer
  19. // in the documentation and/or other materials provided with the
  20. // distribution.
  21. // * Neither the name of Google Inc. nor the names of its
  22. // contributors may be used to endorse or promote products derived from
  23. // this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. package proto
  37. // Functions for parsing the Text protocol buffer format.
  38. // TODO: message sets.
  39. import (
  40. "encoding"
  41. "errors"
  42. "fmt"
  43. "reflect"
  44. "strconv"
  45. "strings"
  46. "time"
  47. "unicode/utf8"
  48. )
  49. // Error string emitted when deserializing Any and fields are already set
  50. const anyRepeatedlyUnpacked = "Any message unpacked multiple times, or %q already set"
  51. type ParseError struct {
  52. Message string
  53. Line int // 1-based line number
  54. Offset int // 0-based byte offset from start of input
  55. }
  56. func (p *ParseError) Error() string {
  57. if p.Line == 1 {
  58. // show offset only for first line
  59. return fmt.Sprintf("line 1.%d: %v", p.Offset, p.Message)
  60. }
  61. return fmt.Sprintf("line %d: %v", p.Line, p.Message)
  62. }
  63. type token struct {
  64. value string
  65. err *ParseError
  66. line int // line number
  67. offset int // byte number from start of input, not start of line
  68. unquoted string // the unquoted version of value, if it was a quoted string
  69. }
  70. func (t *token) String() string {
  71. if t.err == nil {
  72. return fmt.Sprintf("%q (line=%d, offset=%d)", t.value, t.line, t.offset)
  73. }
  74. return fmt.Sprintf("parse error: %v", t.err)
  75. }
  76. type textParser struct {
  77. s string // remaining input
  78. done bool // whether the parsing is finished (success or error)
  79. backed bool // whether back() was called
  80. offset, line int
  81. cur token
  82. }
  83. func newTextParser(s string) *textParser {
  84. p := new(textParser)
  85. p.s = s
  86. p.line = 1
  87. p.cur.line = 1
  88. return p
  89. }
  90. func (p *textParser) errorf(format string, a ...interface{}) *ParseError {
  91. pe := &ParseError{fmt.Sprintf(format, a...), p.cur.line, p.cur.offset}
  92. p.cur.err = pe
  93. p.done = true
  94. return pe
  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. func isWhitespace(c byte) bool {
  111. switch c {
  112. case ' ', '\t', '\n', '\r':
  113. return true
  114. }
  115. return false
  116. }
  117. func isQuote(c byte) bool {
  118. switch c {
  119. case '"', '\'':
  120. return true
  121. }
  122. return false
  123. }
  124. func (p *textParser) skipWhitespace() {
  125. i := 0
  126. for i < len(p.s) && (isWhitespace(p.s[i]) || p.s[i] == '#') {
  127. if p.s[i] == '#' {
  128. // comment; skip to end of line or input
  129. for i < len(p.s) && p.s[i] != '\n' {
  130. i++
  131. }
  132. if i == len(p.s) {
  133. break
  134. }
  135. }
  136. if p.s[i] == '\n' {
  137. p.line++
  138. }
  139. i++
  140. }
  141. p.offset += i
  142. p.s = p.s[i:len(p.s)]
  143. if len(p.s) == 0 {
  144. p.done = true
  145. }
  146. }
  147. func (p *textParser) advance() {
  148. // Skip whitespace
  149. p.skipWhitespace()
  150. if p.done {
  151. return
  152. }
  153. // Start of non-whitespace
  154. p.cur.err = nil
  155. p.cur.offset, p.cur.line = p.offset, p.line
  156. p.cur.unquoted = ""
  157. switch p.s[0] {
  158. case '<', '>', '{', '}', ':', '[', ']', ';', ',', '/':
  159. // Single symbol
  160. p.cur.value, p.s = p.s[0:1], p.s[1:len(p.s)]
  161. case '"', '\'':
  162. // Quoted string
  163. i := 1
  164. for i < len(p.s) && p.s[i] != p.s[0] && p.s[i] != '\n' {
  165. if p.s[i] == '\\' && i+1 < len(p.s) {
  166. // skip escaped char
  167. i++
  168. }
  169. i++
  170. }
  171. if i >= len(p.s) || p.s[i] != p.s[0] {
  172. p.errorf("unmatched quote")
  173. return
  174. }
  175. unq, err := unquoteC(p.s[1:i], rune(p.s[0]))
  176. if err != nil {
  177. p.errorf("invalid quoted string %s: %v", p.s[0:i+1], err)
  178. return
  179. }
  180. p.cur.value, p.s = p.s[0:i+1], p.s[i+1:len(p.s)]
  181. p.cur.unquoted = unq
  182. default:
  183. i := 0
  184. for i < len(p.s) && isIdentOrNumberChar(p.s[i]) {
  185. i++
  186. }
  187. if i == 0 {
  188. p.errorf("unexpected byte %#x", p.s[0])
  189. return
  190. }
  191. p.cur.value, p.s = p.s[0:i], p.s[i:len(p.s)]
  192. }
  193. p.offset += len(p.cur.value)
  194. }
  195. var (
  196. errBadUTF8 = errors.New("proto: bad UTF-8")
  197. )
  198. func unquoteC(s string, quote rune) (string, error) {
  199. // This is based on C++'s tokenizer.cc.
  200. // Despite its name, this is *not* parsing C syntax.
  201. // For instance, "\0" is an invalid quoted string.
  202. // Avoid allocation in trivial cases.
  203. simple := true
  204. for _, r := range s {
  205. if r == '\\' || r == quote {
  206. simple = false
  207. break
  208. }
  209. }
  210. if simple {
  211. return s, nil
  212. }
  213. buf := make([]byte, 0, 3*len(s)/2)
  214. for len(s) > 0 {
  215. r, n := utf8.DecodeRuneInString(s)
  216. if r == utf8.RuneError && n == 1 {
  217. return "", errBadUTF8
  218. }
  219. s = s[n:]
  220. if r != '\\' {
  221. if r < utf8.RuneSelf {
  222. buf = append(buf, byte(r))
  223. } else {
  224. buf = append(buf, string(r)...)
  225. }
  226. continue
  227. }
  228. ch, tail, err := unescape(s)
  229. if err != nil {
  230. return "", err
  231. }
  232. buf = append(buf, ch...)
  233. s = tail
  234. }
  235. return string(buf), nil
  236. }
  237. func unescape(s string) (ch string, tail string, err error) {
  238. r, n := utf8.DecodeRuneInString(s)
  239. if r == utf8.RuneError && n == 1 {
  240. return "", "", errBadUTF8
  241. }
  242. s = s[n:]
  243. switch r {
  244. case 'a':
  245. return "\a", s, nil
  246. case 'b':
  247. return "\b", s, nil
  248. case 'f':
  249. return "\f", s, nil
  250. case 'n':
  251. return "\n", s, nil
  252. case 'r':
  253. return "\r", s, nil
  254. case 't':
  255. return "\t", s, nil
  256. case 'v':
  257. return "\v", s, nil
  258. case '?':
  259. return "?", s, nil // trigraph workaround
  260. case '\'', '"', '\\':
  261. return string(r), s, nil
  262. case '0', '1', '2', '3', '4', '5', '6', '7':
  263. if len(s) < 2 {
  264. return "", "", fmt.Errorf(`\%c requires 2 following digits`, r)
  265. }
  266. ss := string(r) + s[:2]
  267. s = s[2:]
  268. i, err := strconv.ParseUint(ss, 8, 8)
  269. if err != nil {
  270. return "", "", fmt.Errorf(`\%s contains non-octal digits`, ss)
  271. }
  272. return string([]byte{byte(i)}), s, nil
  273. case 'x', 'X', 'u', 'U':
  274. var n int
  275. switch r {
  276. case 'x', 'X':
  277. n = 2
  278. case 'u':
  279. n = 4
  280. case 'U':
  281. n = 8
  282. }
  283. if len(s) < n {
  284. return "", "", fmt.Errorf(`\%c requires %d following digits`, r, n)
  285. }
  286. ss := s[:n]
  287. s = s[n:]
  288. i, err := strconv.ParseUint(ss, 16, 64)
  289. if err != nil {
  290. return "", "", fmt.Errorf(`\%c%s contains non-hexadecimal digits`, r, ss)
  291. }
  292. if r == 'x' || r == 'X' {
  293. return string([]byte{byte(i)}), s, nil
  294. }
  295. if i > utf8.MaxRune {
  296. return "", "", fmt.Errorf(`\%c%s is not a valid Unicode code point`, r, ss)
  297. }
  298. return string(i), s, nil
  299. }
  300. return "", "", fmt.Errorf(`unknown escape \%c`, r)
  301. }
  302. // Back off the parser by one token. Can only be done between calls to next().
  303. // It makes the next advance() a no-op.
  304. func (p *textParser) back() { p.backed = true }
  305. // Advances the parser and returns the new current token.
  306. func (p *textParser) next() *token {
  307. if p.backed || p.done {
  308. p.backed = false
  309. return &p.cur
  310. }
  311. p.advance()
  312. if p.done {
  313. p.cur.value = ""
  314. } else if len(p.cur.value) > 0 && isQuote(p.cur.value[0]) {
  315. // Look for multiple quoted strings separated by whitespace,
  316. // and concatenate them.
  317. cat := p.cur
  318. for {
  319. p.skipWhitespace()
  320. if p.done || !isQuote(p.s[0]) {
  321. break
  322. }
  323. p.advance()
  324. if p.cur.err != nil {
  325. return &p.cur
  326. }
  327. cat.value += " " + p.cur.value
  328. cat.unquoted += p.cur.unquoted
  329. }
  330. p.done = false // parser may have seen EOF, but we want to return cat
  331. p.cur = cat
  332. }
  333. return &p.cur
  334. }
  335. func (p *textParser) consumeToken(s string) error {
  336. tok := p.next()
  337. if tok.err != nil {
  338. return tok.err
  339. }
  340. if tok.value != s {
  341. p.back()
  342. return p.errorf("expected %q, found %q", s, tok.value)
  343. }
  344. return nil
  345. }
  346. // Return a RequiredNotSetError indicating which required field was not set.
  347. func (p *textParser) missingRequiredFieldError(sv reflect.Value) *RequiredNotSetError {
  348. st := sv.Type()
  349. sprops := GetProperties(st)
  350. for i := 0; i < st.NumField(); i++ {
  351. if !isNil(sv.Field(i)) {
  352. continue
  353. }
  354. props := sprops.Prop[i]
  355. if props.Required {
  356. return &RequiredNotSetError{fmt.Sprintf("%v.%v", st, props.OrigName)}
  357. }
  358. }
  359. return &RequiredNotSetError{fmt.Sprintf("%v.<unknown field name>", st)} // should not happen
  360. }
  361. // Returns the index in the struct for the named field, as well as the parsed tag properties.
  362. func structFieldByName(sprops *StructProperties, name string) (int, *Properties, bool) {
  363. i, ok := sprops.decoderOrigNames[name]
  364. if ok {
  365. return i, sprops.Prop[i], true
  366. }
  367. return -1, nil, false
  368. }
  369. // Consume a ':' from the input stream (if the next token is a colon),
  370. // returning an error if a colon is needed but not present.
  371. func (p *textParser) checkForColon(props *Properties, typ reflect.Type) *ParseError {
  372. tok := p.next()
  373. if tok.err != nil {
  374. return tok.err
  375. }
  376. if tok.value != ":" {
  377. // Colon is optional when the field is a group or message.
  378. needColon := true
  379. switch props.Wire {
  380. case "group":
  381. needColon = false
  382. case "bytes":
  383. // A "bytes" field is either a message, a string, or a repeated field;
  384. // those three become *T, *string and []T respectively, so we can check for
  385. // this field being a pointer to a non-string.
  386. if typ.Kind() == reflect.Ptr {
  387. // *T or *string
  388. if typ.Elem().Kind() == reflect.String {
  389. break
  390. }
  391. } else if typ.Kind() == reflect.Slice {
  392. // []T or []*T
  393. if typ.Elem().Kind() != reflect.Ptr {
  394. break
  395. }
  396. } else if typ.Kind() == reflect.String {
  397. // The proto3 exception is for a string field,
  398. // which requires a colon.
  399. break
  400. }
  401. needColon = false
  402. }
  403. if needColon {
  404. return p.errorf("expected ':', found %q", tok.value)
  405. }
  406. p.back()
  407. }
  408. return nil
  409. }
  410. func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
  411. st := sv.Type()
  412. sprops := GetProperties(st)
  413. reqCount := sprops.reqCount
  414. var reqFieldErr error
  415. fieldSet := make(map[string]bool)
  416. // A struct is a sequence of "name: value", terminated by one of
  417. // '>' or '}', or the end of the input. A name may also be
  418. // "[extension]" or "[type/url]".
  419. //
  420. // The whole struct can also be an expanded Any message, like:
  421. // [type/url] < ... struct contents ... >
  422. for {
  423. tok := p.next()
  424. if tok.err != nil {
  425. return tok.err
  426. }
  427. if tok.value == terminator {
  428. break
  429. }
  430. if tok.value == "[" {
  431. // Looks like an extension or an Any.
  432. //
  433. // TODO: Check whether we need to handle
  434. // namespace rooted names (e.g. ".something.Foo").
  435. extName, err := p.consumeExtName()
  436. if err != nil {
  437. return err
  438. }
  439. if s := strings.LastIndex(extName, "/"); s >= 0 {
  440. // If it contains a slash, it's an Any type URL.
  441. messageName := extName[s+1:]
  442. mt := MessageType(messageName)
  443. if mt == nil {
  444. return p.errorf("unrecognized message %q in google.protobuf.Any", messageName)
  445. }
  446. tok = p.next()
  447. if tok.err != nil {
  448. return tok.err
  449. }
  450. // consume an optional colon
  451. if tok.value == ":" {
  452. tok = p.next()
  453. if tok.err != nil {
  454. return tok.err
  455. }
  456. }
  457. var terminator string
  458. switch tok.value {
  459. case "<":
  460. terminator = ">"
  461. case "{":
  462. terminator = "}"
  463. default:
  464. return p.errorf("expected '{' or '<', found %q", tok.value)
  465. }
  466. v := reflect.New(mt.Elem())
  467. if pe := p.readStruct(v.Elem(), terminator); pe != nil {
  468. return pe
  469. }
  470. b, err := Marshal(v.Interface().(Message))
  471. if err != nil {
  472. return p.errorf("failed to marshal message of type %q: %v", messageName, err)
  473. }
  474. if fieldSet["type_url"] {
  475. return p.errorf(anyRepeatedlyUnpacked, "type_url")
  476. }
  477. if fieldSet["value"] {
  478. return p.errorf(anyRepeatedlyUnpacked, "value")
  479. }
  480. sv.FieldByName("TypeUrl").SetString(extName)
  481. sv.FieldByName("Value").SetBytes(b)
  482. fieldSet["type_url"] = true
  483. fieldSet["value"] = true
  484. continue
  485. }
  486. var desc *ExtensionDesc
  487. // This could be faster, but it's functional.
  488. // TODO: Do something smarter than a linear scan.
  489. for _, d := range RegisteredExtensions(reflect.New(st).Interface().(Message)) {
  490. if d.Name == extName {
  491. desc = d
  492. break
  493. }
  494. }
  495. if desc == nil {
  496. return p.errorf("unrecognized extension %q", extName)
  497. }
  498. props := &Properties{}
  499. props.Parse(desc.Tag)
  500. typ := reflect.TypeOf(desc.ExtensionType)
  501. if err := p.checkForColon(props, typ); err != nil {
  502. return err
  503. }
  504. rep := desc.repeated()
  505. // Read the extension structure, and set it in
  506. // the value we're constructing.
  507. var ext reflect.Value
  508. if !rep {
  509. ext = reflect.New(typ).Elem()
  510. } else {
  511. ext = reflect.New(typ.Elem()).Elem()
  512. }
  513. if err := p.readAny(ext, props); err != nil {
  514. if _, ok := err.(*RequiredNotSetError); !ok {
  515. return err
  516. }
  517. reqFieldErr = err
  518. }
  519. ep := sv.Addr().Interface().(Message)
  520. if !rep {
  521. SetExtension(ep, desc, ext.Interface())
  522. } else {
  523. old, err := GetExtension(ep, desc)
  524. var sl reflect.Value
  525. if err == nil {
  526. sl = reflect.ValueOf(old) // existing slice
  527. } else {
  528. sl = reflect.MakeSlice(typ, 0, 1)
  529. }
  530. sl = reflect.Append(sl, ext)
  531. SetExtension(ep, desc, sl.Interface())
  532. }
  533. if err := p.consumeOptionalSeparator(); err != nil {
  534. return err
  535. }
  536. continue
  537. }
  538. // This is a normal, non-extension field.
  539. name := tok.value
  540. var dst reflect.Value
  541. fi, props, ok := structFieldByName(sprops, name)
  542. if ok {
  543. dst = sv.Field(fi)
  544. } else if oop, ok := sprops.OneofTypes[name]; ok {
  545. // It is a oneof.
  546. props = oop.Prop
  547. nv := reflect.New(oop.Type.Elem())
  548. dst = nv.Elem().Field(0)
  549. field := sv.Field(oop.Field)
  550. if !field.IsNil() {
  551. return p.errorf("field '%s' would overwrite already parsed oneof '%s'", name, sv.Type().Field(oop.Field).Name)
  552. }
  553. field.Set(nv)
  554. }
  555. if !dst.IsValid() {
  556. return p.errorf("unknown field name %q in %v", name, st)
  557. }
  558. if dst.Kind() == reflect.Map {
  559. // Consume any colon.
  560. if err := p.checkForColon(props, dst.Type()); err != nil {
  561. return err
  562. }
  563. // Construct the map if it doesn't already exist.
  564. if dst.IsNil() {
  565. dst.Set(reflect.MakeMap(dst.Type()))
  566. }
  567. key := reflect.New(dst.Type().Key()).Elem()
  568. val := reflect.New(dst.Type().Elem()).Elem()
  569. // The map entry should be this sequence of tokens:
  570. // < key : KEY value : VALUE >
  571. // However, implementations may omit key or value, and technically
  572. // we should support them in any order. See b/28924776 for a time
  573. // this went wrong.
  574. tok := p.next()
  575. var terminator string
  576. switch tok.value {
  577. case "<":
  578. terminator = ">"
  579. case "{":
  580. terminator = "}"
  581. default:
  582. return p.errorf("expected '{' or '<', found %q", tok.value)
  583. }
  584. for {
  585. tok := p.next()
  586. if tok.err != nil {
  587. return tok.err
  588. }
  589. if tok.value == terminator {
  590. break
  591. }
  592. switch tok.value {
  593. case "key":
  594. if err := p.consumeToken(":"); err != nil {
  595. return err
  596. }
  597. if err := p.readAny(key, props.mkeyprop); err != nil {
  598. return err
  599. }
  600. if err := p.consumeOptionalSeparator(); err != nil {
  601. return err
  602. }
  603. case "value":
  604. if err := p.checkForColon(props.mvalprop, dst.Type().Elem()); err != nil {
  605. return err
  606. }
  607. if err := p.readAny(val, props.mvalprop); err != nil {
  608. return err
  609. }
  610. if err := p.consumeOptionalSeparator(); err != nil {
  611. return err
  612. }
  613. default:
  614. p.back()
  615. return p.errorf(`expected "key", "value", or %q, found %q`, terminator, tok.value)
  616. }
  617. }
  618. dst.SetMapIndex(key, val)
  619. continue
  620. }
  621. // Check that it's not already set if it's not a repeated field.
  622. if !props.Repeated && fieldSet[name] {
  623. return p.errorf("non-repeated field %q was repeated", name)
  624. }
  625. if err := p.checkForColon(props, dst.Type()); err != nil {
  626. return err
  627. }
  628. // Parse into the field.
  629. fieldSet[name] = true
  630. if err := p.readAny(dst, props); err != nil {
  631. if _, ok := err.(*RequiredNotSetError); !ok {
  632. return err
  633. }
  634. reqFieldErr = err
  635. }
  636. if props.Required {
  637. reqCount--
  638. }
  639. if err := p.consumeOptionalSeparator(); err != nil {
  640. return err
  641. }
  642. }
  643. if reqCount > 0 {
  644. return p.missingRequiredFieldError(sv)
  645. }
  646. return reqFieldErr
  647. }
  648. // consumeExtName consumes extension name or expanded Any type URL and the
  649. // following ']'. It returns the name or URL consumed.
  650. func (p *textParser) consumeExtName() (string, error) {
  651. tok := p.next()
  652. if tok.err != nil {
  653. return "", tok.err
  654. }
  655. // If extension name or type url is quoted, it's a single token.
  656. if len(tok.value) > 2 && isQuote(tok.value[0]) && tok.value[len(tok.value)-1] == tok.value[0] {
  657. name, err := unquoteC(tok.value[1:len(tok.value)-1], rune(tok.value[0]))
  658. if err != nil {
  659. return "", err
  660. }
  661. return name, p.consumeToken("]")
  662. }
  663. // Consume everything up to "]"
  664. var parts []string
  665. for tok.value != "]" {
  666. parts = append(parts, tok.value)
  667. tok = p.next()
  668. if tok.err != nil {
  669. return "", p.errorf("unrecognized type_url or extension name: %s", tok.err)
  670. }
  671. if p.done && tok.value != "]" {
  672. return "", p.errorf("unclosed type_url or extension name")
  673. }
  674. }
  675. return strings.Join(parts, ""), nil
  676. }
  677. // consumeOptionalSeparator consumes an optional semicolon or comma.
  678. // It is used in readStruct to provide backward compatibility.
  679. func (p *textParser) consumeOptionalSeparator() error {
  680. tok := p.next()
  681. if tok.err != nil {
  682. return tok.err
  683. }
  684. if tok.value != ";" && tok.value != "," {
  685. p.back()
  686. }
  687. return nil
  688. }
  689. func (p *textParser) readAny(v reflect.Value, props *Properties) error {
  690. tok := p.next()
  691. if tok.err != nil {
  692. return tok.err
  693. }
  694. if tok.value == "" {
  695. return p.errorf("unexpected EOF")
  696. }
  697. if len(props.CustomType) > 0 {
  698. if props.Repeated {
  699. t := reflect.TypeOf(v.Interface())
  700. if t.Kind() == reflect.Slice {
  701. tc := reflect.TypeOf(new(Marshaler))
  702. ok := t.Elem().Implements(tc.Elem())
  703. if ok {
  704. fv := v
  705. flen := fv.Len()
  706. if flen == fv.Cap() {
  707. nav := reflect.MakeSlice(v.Type(), flen, 2*flen+1)
  708. reflect.Copy(nav, fv)
  709. fv.Set(nav)
  710. }
  711. fv.SetLen(flen + 1)
  712. // Read one.
  713. p.back()
  714. return p.readAny(fv.Index(flen), props)
  715. }
  716. }
  717. }
  718. if reflect.TypeOf(v.Interface()).Kind() == reflect.Ptr {
  719. custom := reflect.New(props.ctype.Elem()).Interface().(Unmarshaler)
  720. err := custom.Unmarshal([]byte(tok.unquoted))
  721. if err != nil {
  722. return p.errorf("%v %v: %v", err, v.Type(), tok.value)
  723. }
  724. v.Set(reflect.ValueOf(custom))
  725. } else {
  726. custom := reflect.New(reflect.TypeOf(v.Interface())).Interface().(Unmarshaler)
  727. err := custom.Unmarshal([]byte(tok.unquoted))
  728. if err != nil {
  729. return p.errorf("%v %v: %v", err, v.Type(), tok.value)
  730. }
  731. v.Set(reflect.Indirect(reflect.ValueOf(custom)))
  732. }
  733. return nil
  734. }
  735. if props.StdTime {
  736. fv := v
  737. p.back()
  738. props.StdTime = false
  739. tproto := &timestamp{}
  740. err := p.readAny(reflect.ValueOf(tproto).Elem(), props)
  741. props.StdTime = true
  742. if err != nil {
  743. return err
  744. }
  745. tim, err := timestampFromProto(tproto)
  746. if err != nil {
  747. return err
  748. }
  749. if props.Repeated {
  750. t := reflect.TypeOf(v.Interface())
  751. if t.Kind() == reflect.Slice {
  752. if t.Elem().Kind() == reflect.Ptr {
  753. ts := fv.Interface().([]*time.Time)
  754. ts = append(ts, &tim)
  755. fv.Set(reflect.ValueOf(ts))
  756. return nil
  757. } else {
  758. ts := fv.Interface().([]time.Time)
  759. ts = append(ts, tim)
  760. fv.Set(reflect.ValueOf(ts))
  761. return nil
  762. }
  763. }
  764. }
  765. if reflect.TypeOf(v.Interface()).Kind() == reflect.Ptr {
  766. v.Set(reflect.ValueOf(&tim))
  767. } else {
  768. v.Set(reflect.Indirect(reflect.ValueOf(&tim)))
  769. }
  770. return nil
  771. }
  772. if props.StdDuration {
  773. fv := v
  774. p.back()
  775. props.StdDuration = false
  776. dproto := &duration{}
  777. err := p.readAny(reflect.ValueOf(dproto).Elem(), props)
  778. props.StdDuration = true
  779. if err != nil {
  780. return err
  781. }
  782. dur, err := durationFromProto(dproto)
  783. if err != nil {
  784. return err
  785. }
  786. if props.Repeated {
  787. t := reflect.TypeOf(v.Interface())
  788. if t.Kind() == reflect.Slice {
  789. if t.Elem().Kind() == reflect.Ptr {
  790. ds := fv.Interface().([]*time.Duration)
  791. ds = append(ds, &dur)
  792. fv.Set(reflect.ValueOf(ds))
  793. return nil
  794. } else {
  795. ds := fv.Interface().([]time.Duration)
  796. ds = append(ds, dur)
  797. fv.Set(reflect.ValueOf(ds))
  798. return nil
  799. }
  800. }
  801. }
  802. if reflect.TypeOf(v.Interface()).Kind() == reflect.Ptr {
  803. v.Set(reflect.ValueOf(&dur))
  804. } else {
  805. v.Set(reflect.Indirect(reflect.ValueOf(&dur)))
  806. }
  807. return nil
  808. }
  809. switch fv := v; fv.Kind() {
  810. case reflect.Slice:
  811. at := v.Type()
  812. if at.Elem().Kind() == reflect.Uint8 {
  813. // Special case for []byte
  814. if tok.value[0] != '"' && tok.value[0] != '\'' {
  815. // Deliberately written out here, as the error after
  816. // this switch statement would write "invalid []byte: ...",
  817. // which is not as user-friendly.
  818. return p.errorf("invalid string: %v", tok.value)
  819. }
  820. bytes := []byte(tok.unquoted)
  821. fv.Set(reflect.ValueOf(bytes))
  822. return nil
  823. }
  824. // Repeated field.
  825. if tok.value == "[" {
  826. // Repeated field with list notation, like [1,2,3].
  827. for {
  828. fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
  829. err := p.readAny(fv.Index(fv.Len()-1), props)
  830. if err != nil {
  831. return err
  832. }
  833. ntok := p.next()
  834. if ntok.err != nil {
  835. return ntok.err
  836. }
  837. if ntok.value == "]" {
  838. break
  839. }
  840. if ntok.value != "," {
  841. return p.errorf("Expected ']' or ',' found %q", ntok.value)
  842. }
  843. }
  844. return nil
  845. }
  846. // One value of the repeated field.
  847. p.back()
  848. fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
  849. return p.readAny(fv.Index(fv.Len()-1), props)
  850. case reflect.Bool:
  851. // true/1/t/True or false/f/0/False.
  852. switch tok.value {
  853. case "true", "1", "t", "True":
  854. fv.SetBool(true)
  855. return nil
  856. case "false", "0", "f", "False":
  857. fv.SetBool(false)
  858. return nil
  859. }
  860. case reflect.Float32, reflect.Float64:
  861. v := tok.value
  862. // Ignore 'f' for compatibility with output generated by C++, but don't
  863. // remove 'f' when the value is "-inf" or "inf".
  864. if strings.HasSuffix(v, "f") && tok.value != "-inf" && tok.value != "inf" {
  865. v = v[:len(v)-1]
  866. }
  867. if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {
  868. fv.SetFloat(f)
  869. return nil
  870. }
  871. case reflect.Int32:
  872. if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
  873. fv.SetInt(x)
  874. return nil
  875. }
  876. if len(props.Enum) == 0 {
  877. break
  878. }
  879. m, ok := enumValueMaps[props.Enum]
  880. if !ok {
  881. break
  882. }
  883. x, ok := m[tok.value]
  884. if !ok {
  885. break
  886. }
  887. fv.SetInt(int64(x))
  888. return nil
  889. case reflect.Int64:
  890. if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
  891. fv.SetInt(x)
  892. return nil
  893. }
  894. case reflect.Ptr:
  895. // A basic field (indirected through pointer), or a repeated message/group
  896. p.back()
  897. fv.Set(reflect.New(fv.Type().Elem()))
  898. return p.readAny(fv.Elem(), props)
  899. case reflect.String:
  900. if tok.value[0] == '"' || tok.value[0] == '\'' {
  901. fv.SetString(tok.unquoted)
  902. return nil
  903. }
  904. case reflect.Struct:
  905. var terminator string
  906. switch tok.value {
  907. case "{":
  908. terminator = "}"
  909. case "<":
  910. terminator = ">"
  911. default:
  912. return p.errorf("expected '{' or '<', found %q", tok.value)
  913. }
  914. // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
  915. return p.readStruct(fv, terminator)
  916. case reflect.Uint32:
  917. if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
  918. fv.SetUint(uint64(x))
  919. return nil
  920. }
  921. case reflect.Uint64:
  922. if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
  923. fv.SetUint(x)
  924. return nil
  925. }
  926. }
  927. return p.errorf("invalid %v: %v", v.Type(), tok.value)
  928. }
  929. // UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb
  930. // before starting to unmarshal, so any existing data in pb is always removed.
  931. // If a required field is not set and no other error occurs,
  932. // UnmarshalText returns *RequiredNotSetError.
  933. func UnmarshalText(s string, pb Message) error {
  934. if um, ok := pb.(encoding.TextUnmarshaler); ok {
  935. return um.UnmarshalText([]byte(s))
  936. }
  937. pb.Reset()
  938. v := reflect.ValueOf(pb)
  939. return newTextParser(s).readStruct(v.Elem(), "")
  940. }