text_parser.go 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. errBadHex = errors.New("proto: bad hexadecimal")
  198. )
  199. func unquoteC(s string, quote rune) (string, error) {
  200. // This is based on C++'s tokenizer.cc.
  201. // Despite its name, this is *not* parsing C syntax.
  202. // For instance, "\0" is an invalid quoted string.
  203. // Avoid allocation in trivial cases.
  204. simple := true
  205. for _, r := range s {
  206. if r == '\\' || r == quote {
  207. simple = false
  208. break
  209. }
  210. }
  211. if simple {
  212. return s, nil
  213. }
  214. buf := make([]byte, 0, 3*len(s)/2)
  215. for len(s) > 0 {
  216. r, n := utf8.DecodeRuneInString(s)
  217. if r == utf8.RuneError && n == 1 {
  218. return "", errBadUTF8
  219. }
  220. s = s[n:]
  221. if r != '\\' {
  222. if r < utf8.RuneSelf {
  223. buf = append(buf, byte(r))
  224. } else {
  225. buf = append(buf, string(r)...)
  226. }
  227. continue
  228. }
  229. ch, tail, err := unescape(s)
  230. if err != nil {
  231. return "", err
  232. }
  233. buf = append(buf, ch...)
  234. s = tail
  235. }
  236. return string(buf), nil
  237. }
  238. func unescape(s string) (ch string, tail string, err error) {
  239. r, n := utf8.DecodeRuneInString(s)
  240. if r == utf8.RuneError && n == 1 {
  241. return "", "", errBadUTF8
  242. }
  243. s = s[n:]
  244. switch r {
  245. case 'a':
  246. return "\a", s, nil
  247. case 'b':
  248. return "\b", s, nil
  249. case 'f':
  250. return "\f", s, nil
  251. case 'n':
  252. return "\n", s, nil
  253. case 'r':
  254. return "\r", s, nil
  255. case 't':
  256. return "\t", s, nil
  257. case 'v':
  258. return "\v", s, nil
  259. case '?':
  260. return "?", s, nil // trigraph workaround
  261. case '\'', '"', '\\':
  262. return string(r), s, nil
  263. case '0', '1', '2', '3', '4', '5', '6', '7', 'x', 'X':
  264. if len(s) < 2 {
  265. return "", "", fmt.Errorf(`\%c requires 2 following digits`, r)
  266. }
  267. base := 8
  268. ss := s[:2]
  269. s = s[2:]
  270. if r == 'x' || r == 'X' {
  271. base = 16
  272. } else {
  273. ss = string(r) + ss
  274. }
  275. i, err := strconv.ParseUint(ss, base, 8)
  276. if err != nil {
  277. return "", "", err
  278. }
  279. return string([]byte{byte(i)}), s, nil
  280. case 'u', 'U':
  281. n := 4
  282. if r == 'U' {
  283. n = 8
  284. }
  285. if len(s) < n {
  286. return "", "", fmt.Errorf(`\%c requires %d digits`, r, n)
  287. }
  288. bs := make([]byte, n/2)
  289. for i := 0; i < n; i += 2 {
  290. a, ok1 := unhex(s[i])
  291. b, ok2 := unhex(s[i+1])
  292. if !ok1 || !ok2 {
  293. return "", "", errBadHex
  294. }
  295. bs[i/2] = a<<4 | b
  296. }
  297. s = s[n:]
  298. return string(bs), s, nil
  299. }
  300. return "", "", fmt.Errorf(`unknown escape \%c`, r)
  301. }
  302. // Adapted from src/pkg/strconv/quote.go.
  303. func unhex(b byte) (v byte, ok bool) {
  304. switch {
  305. case '0' <= b && b <= '9':
  306. return b - '0', true
  307. case 'a' <= b && b <= 'f':
  308. return b - 'a' + 10, true
  309. case 'A' <= b && b <= 'F':
  310. return b - 'A' + 10, true
  311. }
  312. return 0, false
  313. }
  314. // Back off the parser by one token. Can only be done between calls to next().
  315. // It makes the next advance() a no-op.
  316. func (p *textParser) back() { p.backed = true }
  317. // Advances the parser and returns the new current token.
  318. func (p *textParser) next() *token {
  319. if p.backed || p.done {
  320. p.backed = false
  321. return &p.cur
  322. }
  323. p.advance()
  324. if p.done {
  325. p.cur.value = ""
  326. } else if len(p.cur.value) > 0 && isQuote(p.cur.value[0]) {
  327. // Look for multiple quoted strings separated by whitespace,
  328. // and concatenate them.
  329. cat := p.cur
  330. for {
  331. p.skipWhitespace()
  332. if p.done || !isQuote(p.s[0]) {
  333. break
  334. }
  335. p.advance()
  336. if p.cur.err != nil {
  337. return &p.cur
  338. }
  339. cat.value += " " + p.cur.value
  340. cat.unquoted += p.cur.unquoted
  341. }
  342. p.done = false // parser may have seen EOF, but we want to return cat
  343. p.cur = cat
  344. }
  345. return &p.cur
  346. }
  347. func (p *textParser) consumeToken(s string) error {
  348. tok := p.next()
  349. if tok.err != nil {
  350. return tok.err
  351. }
  352. if tok.value != s {
  353. p.back()
  354. return p.errorf("expected %q, found %q", s, tok.value)
  355. }
  356. return nil
  357. }
  358. // Return a RequiredNotSetError indicating which required field was not set.
  359. func (p *textParser) missingRequiredFieldError(sv reflect.Value) *RequiredNotSetError {
  360. st := sv.Type()
  361. sprops := GetProperties(st)
  362. for i := 0; i < st.NumField(); i++ {
  363. if !isNil(sv.Field(i)) {
  364. continue
  365. }
  366. props := sprops.Prop[i]
  367. if props.Required {
  368. return &RequiredNotSetError{fmt.Sprintf("%v.%v", st, props.OrigName)}
  369. }
  370. }
  371. return &RequiredNotSetError{fmt.Sprintf("%v.<unknown field name>", st)} // should not happen
  372. }
  373. // Returns the index in the struct for the named field, as well as the parsed tag properties.
  374. func structFieldByName(sprops *StructProperties, name string) (int, *Properties, bool) {
  375. i, ok := sprops.decoderOrigNames[name]
  376. if ok {
  377. return i, sprops.Prop[i], true
  378. }
  379. return -1, nil, false
  380. }
  381. // Consume a ':' from the input stream (if the next token is a colon),
  382. // returning an error if a colon is needed but not present.
  383. func (p *textParser) checkForColon(props *Properties, typ reflect.Type) *ParseError {
  384. tok := p.next()
  385. if tok.err != nil {
  386. return tok.err
  387. }
  388. if tok.value != ":" {
  389. // Colon is optional when the field is a group or message.
  390. needColon := true
  391. switch props.Wire {
  392. case "group":
  393. needColon = false
  394. case "bytes":
  395. // A "bytes" field is either a message, a string, or a repeated field;
  396. // those three become *T, *string and []T respectively, so we can check for
  397. // this field being a pointer to a non-string.
  398. if typ.Kind() == reflect.Ptr {
  399. // *T or *string
  400. if typ.Elem().Kind() == reflect.String {
  401. break
  402. }
  403. } else if typ.Kind() == reflect.Slice {
  404. // []T or []*T
  405. if typ.Elem().Kind() != reflect.Ptr {
  406. break
  407. }
  408. } else if typ.Kind() == reflect.String {
  409. // The proto3 exception is for a string field,
  410. // which requires a colon.
  411. break
  412. }
  413. needColon = false
  414. }
  415. if needColon {
  416. return p.errorf("expected ':', found %q", tok.value)
  417. }
  418. p.back()
  419. }
  420. return nil
  421. }
  422. func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
  423. st := sv.Type()
  424. sprops := GetProperties(st)
  425. reqCount := sprops.reqCount
  426. var reqFieldErr error
  427. fieldSet := make(map[string]bool)
  428. // A struct is a sequence of "name: value", terminated by one of
  429. // '>' or '}', or the end of the input. A name may also be
  430. // "[extension]" or "[type/url]".
  431. //
  432. // The whole struct can also be an expanded Any message, like:
  433. // [type/url] < ... struct contents ... >
  434. for {
  435. tok := p.next()
  436. if tok.err != nil {
  437. return tok.err
  438. }
  439. if tok.value == terminator {
  440. break
  441. }
  442. if tok.value == "[" {
  443. // Looks like an extension or an Any.
  444. //
  445. // TODO: Check whether we need to handle
  446. // namespace rooted names (e.g. ".something.Foo").
  447. extName, err := p.consumeExtName()
  448. if err != nil {
  449. return err
  450. }
  451. if s := strings.LastIndex(extName, "/"); s >= 0 {
  452. // If it contains a slash, it's an Any type URL.
  453. messageName := extName[s+1:]
  454. mt := MessageType(messageName)
  455. if mt == nil {
  456. return p.errorf("unrecognized message %q in google.protobuf.Any", messageName)
  457. }
  458. tok = p.next()
  459. if tok.err != nil {
  460. return tok.err
  461. }
  462. // consume an optional colon
  463. if tok.value == ":" {
  464. tok = p.next()
  465. if tok.err != nil {
  466. return tok.err
  467. }
  468. }
  469. var terminator string
  470. switch tok.value {
  471. case "<":
  472. terminator = ">"
  473. case "{":
  474. terminator = "}"
  475. default:
  476. return p.errorf("expected '{' or '<', found %q", tok.value)
  477. }
  478. v := reflect.New(mt.Elem())
  479. if pe := p.readStruct(v.Elem(), terminator); pe != nil {
  480. return pe
  481. }
  482. b, err := Marshal(v.Interface().(Message))
  483. if err != nil {
  484. return p.errorf("failed to marshal message of type %q: %v", messageName, err)
  485. }
  486. if fieldSet["type_url"] {
  487. return p.errorf(anyRepeatedlyUnpacked, "type_url")
  488. }
  489. if fieldSet["value"] {
  490. return p.errorf(anyRepeatedlyUnpacked, "value")
  491. }
  492. sv.FieldByName("TypeUrl").SetString(extName)
  493. sv.FieldByName("Value").SetBytes(b)
  494. fieldSet["type_url"] = true
  495. fieldSet["value"] = true
  496. continue
  497. }
  498. var desc *ExtensionDesc
  499. // This could be faster, but it's functional.
  500. // TODO: Do something smarter than a linear scan.
  501. for _, d := range RegisteredExtensions(reflect.New(st).Interface().(Message)) {
  502. if d.Name == extName {
  503. desc = d
  504. break
  505. }
  506. }
  507. if desc == nil {
  508. return p.errorf("unrecognized extension %q", extName)
  509. }
  510. props := &Properties{}
  511. props.Parse(desc.Tag)
  512. typ := reflect.TypeOf(desc.ExtensionType)
  513. if err := p.checkForColon(props, typ); err != nil {
  514. return err
  515. }
  516. rep := desc.repeated()
  517. // Read the extension structure, and set it in
  518. // the value we're constructing.
  519. var ext reflect.Value
  520. if !rep {
  521. ext = reflect.New(typ).Elem()
  522. } else {
  523. ext = reflect.New(typ.Elem()).Elem()
  524. }
  525. if err := p.readAny(ext, props); err != nil {
  526. if _, ok := err.(*RequiredNotSetError); !ok {
  527. return err
  528. }
  529. reqFieldErr = err
  530. }
  531. ep := sv.Addr().Interface().(Message)
  532. if !rep {
  533. SetExtension(ep, desc, ext.Interface())
  534. } else {
  535. old, err := GetExtension(ep, desc)
  536. var sl reflect.Value
  537. if err == nil {
  538. sl = reflect.ValueOf(old) // existing slice
  539. } else {
  540. sl = reflect.MakeSlice(typ, 0, 1)
  541. }
  542. sl = reflect.Append(sl, ext)
  543. SetExtension(ep, desc, sl.Interface())
  544. }
  545. if err := p.consumeOptionalSeparator(); err != nil {
  546. return err
  547. }
  548. continue
  549. }
  550. // This is a normal, non-extension field.
  551. name := tok.value
  552. var dst reflect.Value
  553. fi, props, ok := structFieldByName(sprops, name)
  554. if ok {
  555. dst = sv.Field(fi)
  556. } else if oop, ok := sprops.OneofTypes[name]; ok {
  557. // It is a oneof.
  558. props = oop.Prop
  559. nv := reflect.New(oop.Type.Elem())
  560. dst = nv.Elem().Field(0)
  561. field := sv.Field(oop.Field)
  562. if !field.IsNil() {
  563. return p.errorf("field '%s' would overwrite already parsed oneof '%s'", name, sv.Type().Field(oop.Field).Name)
  564. }
  565. field.Set(nv)
  566. }
  567. if !dst.IsValid() {
  568. return p.errorf("unknown field name %q in %v", name, st)
  569. }
  570. if dst.Kind() == reflect.Map {
  571. // Consume any colon.
  572. if err := p.checkForColon(props, dst.Type()); err != nil {
  573. return err
  574. }
  575. // Construct the map if it doesn't already exist.
  576. if dst.IsNil() {
  577. dst.Set(reflect.MakeMap(dst.Type()))
  578. }
  579. key := reflect.New(dst.Type().Key()).Elem()
  580. val := reflect.New(dst.Type().Elem()).Elem()
  581. // The map entry should be this sequence of tokens:
  582. // < key : KEY value : VALUE >
  583. // However, implementations may omit key or value, and technically
  584. // we should support them in any order. See b/28924776 for a time
  585. // this went wrong.
  586. tok := p.next()
  587. var terminator string
  588. switch tok.value {
  589. case "<":
  590. terminator = ">"
  591. case "{":
  592. terminator = "}"
  593. default:
  594. return p.errorf("expected '{' or '<', found %q", tok.value)
  595. }
  596. for {
  597. tok := p.next()
  598. if tok.err != nil {
  599. return tok.err
  600. }
  601. if tok.value == terminator {
  602. break
  603. }
  604. switch tok.value {
  605. case "key":
  606. if err := p.consumeToken(":"); err != nil {
  607. return err
  608. }
  609. if err := p.readAny(key, props.mkeyprop); err != nil {
  610. return err
  611. }
  612. if err := p.consumeOptionalSeparator(); err != nil {
  613. return err
  614. }
  615. case "value":
  616. if err := p.checkForColon(props.mvalprop, dst.Type().Elem()); err != nil {
  617. return err
  618. }
  619. if err := p.readAny(val, props.mvalprop); err != nil {
  620. return err
  621. }
  622. if err := p.consumeOptionalSeparator(); err != nil {
  623. return err
  624. }
  625. default:
  626. p.back()
  627. return p.errorf(`expected "key", "value", or %q, found %q`, terminator, tok.value)
  628. }
  629. }
  630. dst.SetMapIndex(key, val)
  631. continue
  632. }
  633. // Check that it's not already set if it's not a repeated field.
  634. if !props.Repeated && fieldSet[name] {
  635. return p.errorf("non-repeated field %q was repeated", name)
  636. }
  637. if err := p.checkForColon(props, dst.Type()); err != nil {
  638. return err
  639. }
  640. // Parse into the field.
  641. fieldSet[name] = true
  642. if err := p.readAny(dst, props); err != nil {
  643. if _, ok := err.(*RequiredNotSetError); !ok {
  644. return err
  645. }
  646. reqFieldErr = err
  647. }
  648. if props.Required {
  649. reqCount--
  650. }
  651. if err := p.consumeOptionalSeparator(); err != nil {
  652. return err
  653. }
  654. }
  655. if reqCount > 0 {
  656. return p.missingRequiredFieldError(sv)
  657. }
  658. return reqFieldErr
  659. }
  660. // consumeExtName consumes extension name or expanded Any type URL and the
  661. // following ']'. It returns the name or URL consumed.
  662. func (p *textParser) consumeExtName() (string, error) {
  663. tok := p.next()
  664. if tok.err != nil {
  665. return "", tok.err
  666. }
  667. // If extension name or type url is quoted, it's a single token.
  668. if len(tok.value) > 2 && isQuote(tok.value[0]) && tok.value[len(tok.value)-1] == tok.value[0] {
  669. name, err := unquoteC(tok.value[1:len(tok.value)-1], rune(tok.value[0]))
  670. if err != nil {
  671. return "", err
  672. }
  673. return name, p.consumeToken("]")
  674. }
  675. // Consume everything up to "]"
  676. var parts []string
  677. for tok.value != "]" {
  678. parts = append(parts, tok.value)
  679. tok = p.next()
  680. if tok.err != nil {
  681. return "", p.errorf("unrecognized type_url or extension name: %s", tok.err)
  682. }
  683. }
  684. return strings.Join(parts, ""), nil
  685. }
  686. // consumeOptionalSeparator consumes an optional semicolon or comma.
  687. // It is used in readStruct to provide backward compatibility.
  688. func (p *textParser) consumeOptionalSeparator() error {
  689. tok := p.next()
  690. if tok.err != nil {
  691. return tok.err
  692. }
  693. if tok.value != ";" && tok.value != "," {
  694. p.back()
  695. }
  696. return nil
  697. }
  698. func (p *textParser) readAny(v reflect.Value, props *Properties) error {
  699. tok := p.next()
  700. if tok.err != nil {
  701. return tok.err
  702. }
  703. if tok.value == "" {
  704. return p.errorf("unexpected EOF")
  705. }
  706. if len(props.CustomType) > 0 {
  707. if props.Repeated {
  708. t := reflect.TypeOf(v.Interface())
  709. if t.Kind() == reflect.Slice {
  710. tc := reflect.TypeOf(new(Marshaler))
  711. ok := t.Elem().Implements(tc.Elem())
  712. if ok {
  713. fv := v
  714. flen := fv.Len()
  715. if flen == fv.Cap() {
  716. nav := reflect.MakeSlice(v.Type(), flen, 2*flen+1)
  717. reflect.Copy(nav, fv)
  718. fv.Set(nav)
  719. }
  720. fv.SetLen(flen + 1)
  721. // Read one.
  722. p.back()
  723. return p.readAny(fv.Index(flen), props)
  724. }
  725. }
  726. }
  727. if reflect.TypeOf(v.Interface()).Kind() == reflect.Ptr {
  728. custom := reflect.New(props.ctype.Elem()).Interface().(Unmarshaler)
  729. err := custom.Unmarshal([]byte(tok.unquoted))
  730. if err != nil {
  731. return p.errorf("%v %v: %v", err, v.Type(), tok.value)
  732. }
  733. v.Set(reflect.ValueOf(custom))
  734. } else {
  735. custom := reflect.New(reflect.TypeOf(v.Interface())).Interface().(Unmarshaler)
  736. err := custom.Unmarshal([]byte(tok.unquoted))
  737. if err != nil {
  738. return p.errorf("%v %v: %v", err, v.Type(), tok.value)
  739. }
  740. v.Set(reflect.Indirect(reflect.ValueOf(custom)))
  741. }
  742. return nil
  743. }
  744. if props.StdTime {
  745. fv := v
  746. p.back()
  747. props.StdTime = false
  748. tproto := &timestamp{}
  749. err := p.readAny(reflect.ValueOf(tproto).Elem(), props)
  750. props.StdTime = true
  751. if err != nil {
  752. return err
  753. }
  754. tim, err := timestampFromProto(tproto)
  755. if err != nil {
  756. return err
  757. }
  758. if props.Repeated {
  759. t := reflect.TypeOf(v.Interface())
  760. if t.Kind() == reflect.Slice {
  761. if t.Elem().Kind() == reflect.Ptr {
  762. ts := fv.Interface().([]*time.Time)
  763. ts = append(ts, &tim)
  764. fv.Set(reflect.ValueOf(ts))
  765. return nil
  766. } else {
  767. ts := fv.Interface().([]time.Time)
  768. ts = append(ts, tim)
  769. fv.Set(reflect.ValueOf(ts))
  770. return nil
  771. }
  772. }
  773. }
  774. if reflect.TypeOf(v.Interface()).Kind() == reflect.Ptr {
  775. v.Set(reflect.ValueOf(&tim))
  776. } else {
  777. v.Set(reflect.Indirect(reflect.ValueOf(&tim)))
  778. }
  779. return nil
  780. }
  781. if props.StdDuration {
  782. fv := v
  783. p.back()
  784. props.StdDuration = false
  785. dproto := &duration{}
  786. err := p.readAny(reflect.ValueOf(dproto).Elem(), props)
  787. props.StdDuration = true
  788. if err != nil {
  789. return err
  790. }
  791. dur, err := durationFromProto(dproto)
  792. if err != nil {
  793. return err
  794. }
  795. if props.Repeated {
  796. t := reflect.TypeOf(v.Interface())
  797. if t.Kind() == reflect.Slice {
  798. if t.Elem().Kind() == reflect.Ptr {
  799. ds := fv.Interface().([]*time.Duration)
  800. ds = append(ds, &dur)
  801. fv.Set(reflect.ValueOf(ds))
  802. return nil
  803. } else {
  804. ds := fv.Interface().([]time.Duration)
  805. ds = append(ds, dur)
  806. fv.Set(reflect.ValueOf(ds))
  807. return nil
  808. }
  809. }
  810. }
  811. if reflect.TypeOf(v.Interface()).Kind() == reflect.Ptr {
  812. v.Set(reflect.ValueOf(&dur))
  813. } else {
  814. v.Set(reflect.Indirect(reflect.ValueOf(&dur)))
  815. }
  816. return nil
  817. }
  818. switch fv := v; fv.Kind() {
  819. case reflect.Slice:
  820. at := v.Type()
  821. if at.Elem().Kind() == reflect.Uint8 {
  822. // Special case for []byte
  823. if tok.value[0] != '"' && tok.value[0] != '\'' {
  824. // Deliberately written out here, as the error after
  825. // this switch statement would write "invalid []byte: ...",
  826. // which is not as user-friendly.
  827. return p.errorf("invalid string: %v", tok.value)
  828. }
  829. bytes := []byte(tok.unquoted)
  830. fv.Set(reflect.ValueOf(bytes))
  831. return nil
  832. }
  833. // Repeated field.
  834. if tok.value == "[" {
  835. // Repeated field with list notation, like [1,2,3].
  836. for {
  837. fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
  838. err := p.readAny(fv.Index(fv.Len()-1), props)
  839. if err != nil {
  840. return err
  841. }
  842. ntok := p.next()
  843. if ntok.err != nil {
  844. return ntok.err
  845. }
  846. if ntok.value == "]" {
  847. break
  848. }
  849. if ntok.value != "," {
  850. return p.errorf("Expected ']' or ',' found %q", ntok.value)
  851. }
  852. }
  853. return nil
  854. }
  855. // One value of the repeated field.
  856. p.back()
  857. fv.Set(reflect.Append(fv, reflect.New(at.Elem()).Elem()))
  858. return p.readAny(fv.Index(fv.Len()-1), props)
  859. case reflect.Bool:
  860. // true/1/t/True or false/f/0/False.
  861. switch tok.value {
  862. case "true", "1", "t", "True":
  863. fv.SetBool(true)
  864. return nil
  865. case "false", "0", "f", "False":
  866. fv.SetBool(false)
  867. return nil
  868. }
  869. case reflect.Float32, reflect.Float64:
  870. v := tok.value
  871. // Ignore 'f' for compatibility with output generated by C++, but don't
  872. // remove 'f' when the value is "-inf" or "inf".
  873. if strings.HasSuffix(v, "f") && tok.value != "-inf" && tok.value != "inf" {
  874. v = v[:len(v)-1]
  875. }
  876. if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {
  877. fv.SetFloat(f)
  878. return nil
  879. }
  880. case reflect.Int32:
  881. if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
  882. fv.SetInt(x)
  883. return nil
  884. }
  885. if len(props.Enum) == 0 {
  886. break
  887. }
  888. m, ok := enumValueMaps[props.Enum]
  889. if !ok {
  890. break
  891. }
  892. x, ok := m[tok.value]
  893. if !ok {
  894. break
  895. }
  896. fv.SetInt(int64(x))
  897. return nil
  898. case reflect.Int64:
  899. if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
  900. fv.SetInt(x)
  901. return nil
  902. }
  903. case reflect.Ptr:
  904. // A basic field (indirected through pointer), or a repeated message/group
  905. p.back()
  906. fv.Set(reflect.New(fv.Type().Elem()))
  907. return p.readAny(fv.Elem(), props)
  908. case reflect.String:
  909. if tok.value[0] == '"' || tok.value[0] == '\'' {
  910. fv.SetString(tok.unquoted)
  911. return nil
  912. }
  913. case reflect.Struct:
  914. var terminator string
  915. switch tok.value {
  916. case "{":
  917. terminator = "}"
  918. case "<":
  919. terminator = ">"
  920. default:
  921. return p.errorf("expected '{' or '<', found %q", tok.value)
  922. }
  923. // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
  924. return p.readStruct(fv, terminator)
  925. case reflect.Uint32:
  926. if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
  927. fv.SetUint(x)
  928. return nil
  929. }
  930. case reflect.Uint64:
  931. if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
  932. fv.SetUint(x)
  933. return nil
  934. }
  935. }
  936. return p.errorf("invalid %v: %v", v.Type(), tok.value)
  937. }
  938. // UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb
  939. // before starting to unmarshal, so any existing data in pb is always removed.
  940. // If a required field is not set and no other error occurs,
  941. // UnmarshalText returns *RequiredNotSetError.
  942. func UnmarshalText(s string, pb Message) error {
  943. if um, ok := pb.(encoding.TextUnmarshaler); ok {
  944. err := um.UnmarshalText([]byte(s))
  945. return err
  946. }
  947. pb.Reset()
  948. v := reflect.ValueOf(pb)
  949. if pe := newTextParser(s).readStruct(v.Elem(), ""); pe != nil {
  950. return pe
  951. }
  952. return nil
  953. }