text_parser.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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.MapKeyProp); 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.MapValProp, dst.Type().Elem()); err != nil {
  605. return err
  606. }
  607. if err := p.readAny(val, props.MapValProp); 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.Int8:
  872. if x, err := strconv.ParseInt(tok.value, 0, 8); err == nil {
  873. fv.SetInt(x)
  874. return nil
  875. }
  876. case reflect.Int16:
  877. if x, err := strconv.ParseInt(tok.value, 0, 16); err == nil {
  878. fv.SetInt(x)
  879. return nil
  880. }
  881. case reflect.Int32:
  882. if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
  883. fv.SetInt(x)
  884. return nil
  885. }
  886. if len(props.Enum) == 0 {
  887. break
  888. }
  889. m, ok := enumValueMaps[props.Enum]
  890. if !ok {
  891. break
  892. }
  893. x, ok := m[tok.value]
  894. if !ok {
  895. break
  896. }
  897. fv.SetInt(int64(x))
  898. return nil
  899. case reflect.Int64:
  900. if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
  901. fv.SetInt(x)
  902. return nil
  903. }
  904. case reflect.Ptr:
  905. // A basic field (indirected through pointer), or a repeated message/group
  906. p.back()
  907. fv.Set(reflect.New(fv.Type().Elem()))
  908. return p.readAny(fv.Elem(), props)
  909. case reflect.String:
  910. if tok.value[0] == '"' || tok.value[0] == '\'' {
  911. fv.SetString(tok.unquoted)
  912. return nil
  913. }
  914. case reflect.Struct:
  915. var terminator string
  916. switch tok.value {
  917. case "{":
  918. terminator = "}"
  919. case "<":
  920. terminator = ">"
  921. default:
  922. return p.errorf("expected '{' or '<', found %q", tok.value)
  923. }
  924. // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
  925. return p.readStruct(fv, terminator)
  926. case reflect.Uint8:
  927. if x, err := strconv.ParseUint(tok.value, 0, 8); err == nil {
  928. fv.SetUint(x)
  929. return nil
  930. }
  931. case reflect.Uint16:
  932. if x, err := strconv.ParseUint(tok.value, 0, 16); err == nil {
  933. fv.SetUint(x)
  934. return nil
  935. }
  936. case reflect.Uint32:
  937. if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
  938. fv.SetUint(uint64(x))
  939. return nil
  940. }
  941. case reflect.Uint64:
  942. if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
  943. fv.SetUint(x)
  944. return nil
  945. }
  946. }
  947. return p.errorf("invalid %v: %v", v.Type(), tok.value)
  948. }
  949. // UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb
  950. // before starting to unmarshal, so any existing data in pb is always removed.
  951. // If a required field is not set and no other error occurs,
  952. // UnmarshalText returns *RequiredNotSetError.
  953. func UnmarshalText(s string, pb Message) error {
  954. if um, ok := pb.(encoding.TextUnmarshaler); ok {
  955. return um.UnmarshalText([]byte(s))
  956. }
  957. pb.Reset()
  958. v := reflect.ValueOf(pb)
  959. return newTextParser(s).readStruct(v.Elem(), "")
  960. }