decode.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package jsonpb
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "google.golang.org/protobuf/internal/encoding/json"
  12. "google.golang.org/protobuf/internal/errors"
  13. "google.golang.org/protobuf/internal/pragma"
  14. "google.golang.org/protobuf/internal/set"
  15. "google.golang.org/protobuf/proto"
  16. pref "google.golang.org/protobuf/reflect/protoreflect"
  17. "google.golang.org/protobuf/reflect/protoregistry"
  18. )
  19. // Unmarshal reads the given []byte into the given proto.Message.
  20. func Unmarshal(m proto.Message, b []byte) error {
  21. return UnmarshalOptions{}.Unmarshal(m, b)
  22. }
  23. // UnmarshalOptions is a configurable JSON format parser.
  24. type UnmarshalOptions struct {
  25. pragma.NoUnkeyedLiterals
  26. // If AllowPartial is set, input for messages that will result in missing
  27. // required fields will not return an error.
  28. AllowPartial bool
  29. // If DiscardUnknown is set, unknown fields are ignored.
  30. DiscardUnknown bool
  31. // Resolver is the registry used for type lookups when unmarshaling extensions
  32. // and processing Any. If Resolver is not set, unmarshaling will default to
  33. // using protoregistry.GlobalTypes.
  34. Resolver *protoregistry.Types
  35. decoder *json.Decoder
  36. }
  37. // Unmarshal reads the given []byte and populates the given proto.Message using
  38. // options in UnmarshalOptions object. It will clear the message first before
  39. // setting the fields. If it returns an error, the given message may be
  40. // partially set.
  41. func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
  42. mr := m.ProtoReflect()
  43. // TODO: Determine if we would like to have an option for merging or only
  44. // have merging behavior. We should at least be consistent with textproto
  45. // marshaling.
  46. resetMessage(mr)
  47. if o.Resolver == nil {
  48. o.Resolver = protoregistry.GlobalTypes
  49. }
  50. o.decoder = json.NewDecoder(b)
  51. var nerr errors.NonFatal
  52. if err := o.unmarshalMessage(mr, false); !nerr.Merge(err) {
  53. return err
  54. }
  55. // Check for EOF.
  56. val, err := o.decoder.Read()
  57. if err != nil {
  58. return err
  59. }
  60. if val.Type() != json.EOF {
  61. return unexpectedJSONError{val}
  62. }
  63. if !o.AllowPartial {
  64. nerr.Merge(proto.IsInitialized(m))
  65. }
  66. return nerr.E
  67. }
  68. // resetMessage clears all fields of given protoreflect.Message.
  69. func resetMessage(m pref.Message) {
  70. knownFields := m.KnownFields()
  71. knownFields.Range(func(num pref.FieldNumber, _ pref.Value) bool {
  72. knownFields.Clear(num)
  73. return true
  74. })
  75. unknownFields := m.UnknownFields()
  76. unknownFields.Range(func(num pref.FieldNumber, _ pref.RawFields) bool {
  77. unknownFields.Set(num, nil)
  78. return true
  79. })
  80. extTypes := knownFields.ExtensionTypes()
  81. extTypes.Range(func(xt pref.ExtensionType) bool {
  82. extTypes.Remove(xt)
  83. return true
  84. })
  85. }
  86. // unexpectedJSONError is an error that contains the unexpected json.Value. This
  87. // is returned by methods to provide callers the read json.Value that it did not
  88. // expect.
  89. // TODO: Consider moving this to internal/encoding/json for consistency with
  90. // errors that package returns.
  91. type unexpectedJSONError struct {
  92. value json.Value
  93. }
  94. func (e unexpectedJSONError) Error() string {
  95. return newError("unexpected value %s", e.value).Error()
  96. }
  97. // newError returns an error object. If one of the values passed in is of
  98. // json.Value type, it produces an error with position info.
  99. func newError(f string, x ...interface{}) error {
  100. var hasValue bool
  101. var line, column int
  102. for i := 0; i < len(x); i++ {
  103. if val, ok := x[i].(json.Value); ok {
  104. line, column = val.Position()
  105. hasValue = true
  106. break
  107. }
  108. }
  109. e := errors.New(f, x...)
  110. if hasValue {
  111. return errors.New("(line %d:%d): %v", line, column, e)
  112. }
  113. return e
  114. }
  115. // unmarshalMessage unmarshals a message into the given protoreflect.Message.
  116. func (o UnmarshalOptions) unmarshalMessage(m pref.Message, skipTypeURL bool) error {
  117. var nerr errors.NonFatal
  118. if isCustomType(m.Descriptor().FullName()) {
  119. return o.unmarshalCustomType(m)
  120. }
  121. jval, err := o.decoder.Read()
  122. if !nerr.Merge(err) {
  123. return err
  124. }
  125. if jval.Type() != json.StartObject {
  126. return unexpectedJSONError{jval}
  127. }
  128. if err := o.unmarshalFields(m, skipTypeURL); !nerr.Merge(err) {
  129. return err
  130. }
  131. return nerr.E
  132. }
  133. // unmarshalFields unmarshals the fields into the given protoreflect.Message.
  134. func (o UnmarshalOptions) unmarshalFields(m pref.Message, skipTypeURL bool) error {
  135. var nerr errors.NonFatal
  136. var seenNums set.Ints
  137. var seenOneofs set.Ints
  138. messageDesc := m.Descriptor()
  139. knownFields := m.KnownFields()
  140. fieldDescs := messageDesc.Fields()
  141. xtTypes := knownFields.ExtensionTypes()
  142. Loop:
  143. for {
  144. // Read field name.
  145. jval, err := o.decoder.Read()
  146. if !nerr.Merge(err) {
  147. return err
  148. }
  149. switch jval.Type() {
  150. default:
  151. return unexpectedJSONError{jval}
  152. case json.EndObject:
  153. break Loop
  154. case json.Name:
  155. // Continue below.
  156. }
  157. name, err := jval.Name()
  158. if !nerr.Merge(err) {
  159. return err
  160. }
  161. // Unmarshaling a non-custom embedded message in Any will contain the
  162. // JSON field "@type" which should be skipped because it is not a field
  163. // of the embedded message, but simply an artifact of the Any format.
  164. if skipTypeURL && name == "@type" {
  165. o.decoder.Read()
  166. continue
  167. }
  168. // Get the FieldDescriptor.
  169. var fd pref.FieldDescriptor
  170. if strings.HasPrefix(name, "[") && strings.HasSuffix(name, "]") {
  171. // Only extension names are in [name] format.
  172. xtName := pref.FullName(name[1 : len(name)-1])
  173. xt := xtTypes.ByName(xtName)
  174. if xt == nil {
  175. xt, err = o.findExtension(xtName)
  176. if err != nil && err != protoregistry.NotFound {
  177. return errors.New("unable to resolve [%v]: %v", xtName, err)
  178. }
  179. if xt != nil {
  180. xtTypes.Register(xt)
  181. }
  182. }
  183. if xt != nil {
  184. fd = xt.Descriptor()
  185. }
  186. } else {
  187. // The name can either be the JSON name or the proto field name.
  188. fd = fieldDescs.ByJSONName(name)
  189. if fd == nil {
  190. fd = fieldDescs.ByName(pref.Name(name))
  191. }
  192. }
  193. if fd == nil {
  194. // Field is unknown.
  195. if o.DiscardUnknown {
  196. if err := skipJSONValue(o.decoder); !nerr.Merge(err) {
  197. return err
  198. }
  199. continue
  200. }
  201. return newError("%v contains unknown field %s", messageDesc.FullName(), jval)
  202. }
  203. // Do not allow duplicate fields.
  204. num := uint64(fd.Number())
  205. if seenNums.Has(num) {
  206. return newError("%v contains repeated field %s", messageDesc.FullName(), jval)
  207. }
  208. seenNums.Set(num)
  209. // No need to set values for JSON null unless the field type is
  210. // google.protobuf.Value or google.protobuf.NullValue.
  211. if o.decoder.Peek() == json.Null && !isKnownValue(fd) && !isNullValue(fd) {
  212. o.decoder.Read()
  213. continue
  214. }
  215. switch {
  216. case fd.IsList():
  217. list := knownFields.Get(fd.Number()).List()
  218. if err := o.unmarshalList(list, fd); !nerr.Merge(err) {
  219. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  220. }
  221. case fd.IsMap():
  222. mmap := knownFields.Get(fd.Number()).Map()
  223. if err := o.unmarshalMap(mmap, fd); !nerr.Merge(err) {
  224. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  225. }
  226. default:
  227. // If field is a oneof, check if it has already been set.
  228. if od := fd.ContainingOneof(); od != nil {
  229. idx := uint64(od.Index())
  230. if seenOneofs.Has(idx) {
  231. return errors.New("%v: oneof is already set", od.FullName())
  232. }
  233. seenOneofs.Set(idx)
  234. }
  235. // Required or optional fields.
  236. if err := o.unmarshalSingular(knownFields, fd); !nerr.Merge(err) {
  237. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  238. }
  239. }
  240. }
  241. return nerr.E
  242. }
  243. // findExtension returns protoreflect.ExtensionType from the resolver if found.
  244. func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
  245. xt, err := o.Resolver.FindExtensionByName(xtName)
  246. if err == nil {
  247. return xt, nil
  248. }
  249. // Check if this is a MessageSet extension field.
  250. xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
  251. if err == nil && isMessageSetExtension(xt) {
  252. return xt, nil
  253. }
  254. return nil, protoregistry.NotFound
  255. }
  256. func isKnownValue(fd pref.FieldDescriptor) bool {
  257. md := fd.Message()
  258. return md != nil && md.FullName() == "google.protobuf.Value"
  259. }
  260. func isNullValue(fd pref.FieldDescriptor) bool {
  261. ed := fd.Enum()
  262. return ed != nil && ed.FullName() == "google.protobuf.NullValue"
  263. }
  264. // unmarshalSingular unmarshals to the non-repeated field specified by the given
  265. // FieldDescriptor.
  266. func (o UnmarshalOptions) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
  267. var val pref.Value
  268. var err error
  269. num := fd.Number()
  270. switch fd.Kind() {
  271. case pref.MessageKind, pref.GroupKind:
  272. m := knownFields.NewMessage(num)
  273. err = o.unmarshalMessage(m, false)
  274. val = pref.ValueOf(m)
  275. default:
  276. val, err = o.unmarshalScalar(fd)
  277. }
  278. var nerr errors.NonFatal
  279. if !nerr.Merge(err) {
  280. return err
  281. }
  282. knownFields.Set(num, val)
  283. return nerr.E
  284. }
  285. // unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by
  286. // the given FieldDescriptor.
  287. func (o UnmarshalOptions) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) {
  288. const b32 int = 32
  289. const b64 int = 64
  290. var nerr errors.NonFatal
  291. jval, err := o.decoder.Read()
  292. if !nerr.Merge(err) {
  293. return pref.Value{}, err
  294. }
  295. kind := fd.Kind()
  296. switch kind {
  297. case pref.BoolKind:
  298. return unmarshalBool(jval)
  299. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  300. return unmarshalInt(jval, b32)
  301. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  302. return unmarshalInt(jval, b64)
  303. case pref.Uint32Kind, pref.Fixed32Kind:
  304. return unmarshalUint(jval, b32)
  305. case pref.Uint64Kind, pref.Fixed64Kind:
  306. return unmarshalUint(jval, b64)
  307. case pref.FloatKind:
  308. return unmarshalFloat(jval, b32)
  309. case pref.DoubleKind:
  310. return unmarshalFloat(jval, b64)
  311. case pref.StringKind:
  312. pval, err := unmarshalString(jval)
  313. if !nerr.Merge(err) {
  314. return pval, err
  315. }
  316. return pval, nerr.E
  317. case pref.BytesKind:
  318. return unmarshalBytes(jval)
  319. case pref.EnumKind:
  320. return unmarshalEnum(jval, fd)
  321. }
  322. panic(fmt.Sprintf("invalid scalar kind %v", kind))
  323. }
  324. func unmarshalBool(jval json.Value) (pref.Value, error) {
  325. if jval.Type() != json.Bool {
  326. return pref.Value{}, unexpectedJSONError{jval}
  327. }
  328. b, err := jval.Bool()
  329. return pref.ValueOf(b), err
  330. }
  331. func unmarshalInt(jval json.Value, bitSize int) (pref.Value, error) {
  332. switch jval.Type() {
  333. case json.Number:
  334. return getInt(jval, bitSize)
  335. case json.String:
  336. // Decode number from string.
  337. s := strings.TrimSpace(jval.String())
  338. if len(s) != len(jval.String()) {
  339. return pref.Value{}, errors.New("invalid number %v", jval.Raw())
  340. }
  341. dec := json.NewDecoder([]byte(s))
  342. var nerr errors.NonFatal
  343. jval, err := dec.Read()
  344. if !nerr.Merge(err) {
  345. return pref.Value{}, err
  346. }
  347. return getInt(jval, bitSize)
  348. }
  349. return pref.Value{}, unexpectedJSONError{jval}
  350. }
  351. func getInt(jval json.Value, bitSize int) (pref.Value, error) {
  352. n, err := jval.Int(bitSize)
  353. if err != nil {
  354. return pref.Value{}, err
  355. }
  356. if bitSize == 32 {
  357. return pref.ValueOf(int32(n)), nil
  358. }
  359. return pref.ValueOf(n), nil
  360. }
  361. func unmarshalUint(jval json.Value, bitSize int) (pref.Value, error) {
  362. switch jval.Type() {
  363. case json.Number:
  364. return getUint(jval, bitSize)
  365. case json.String:
  366. // Decode number from string.
  367. s := strings.TrimSpace(jval.String())
  368. if len(s) != len(jval.String()) {
  369. return pref.Value{}, errors.New("invalid number %v", jval.Raw())
  370. }
  371. dec := json.NewDecoder([]byte(s))
  372. var nerr errors.NonFatal
  373. jval, err := dec.Read()
  374. if !nerr.Merge(err) {
  375. return pref.Value{}, err
  376. }
  377. return getUint(jval, bitSize)
  378. }
  379. return pref.Value{}, unexpectedJSONError{jval}
  380. }
  381. func getUint(jval json.Value, bitSize int) (pref.Value, error) {
  382. n, err := jval.Uint(bitSize)
  383. if err != nil {
  384. return pref.Value{}, err
  385. }
  386. if bitSize == 32 {
  387. return pref.ValueOf(uint32(n)), nil
  388. }
  389. return pref.ValueOf(n), nil
  390. }
  391. func unmarshalFloat(jval json.Value, bitSize int) (pref.Value, error) {
  392. switch jval.Type() {
  393. case json.Number:
  394. return getFloat(jval, bitSize)
  395. case json.String:
  396. s := jval.String()
  397. switch s {
  398. case "NaN":
  399. if bitSize == 32 {
  400. return pref.ValueOf(float32(math.NaN())), nil
  401. }
  402. return pref.ValueOf(math.NaN()), nil
  403. case "Infinity":
  404. if bitSize == 32 {
  405. return pref.ValueOf(float32(math.Inf(+1))), nil
  406. }
  407. return pref.ValueOf(math.Inf(+1)), nil
  408. case "-Infinity":
  409. if bitSize == 32 {
  410. return pref.ValueOf(float32(math.Inf(-1))), nil
  411. }
  412. return pref.ValueOf(math.Inf(-1)), nil
  413. }
  414. // Decode number from string.
  415. if len(s) != len(strings.TrimSpace(s)) {
  416. return pref.Value{}, errors.New("invalid number %v", jval.Raw())
  417. }
  418. dec := json.NewDecoder([]byte(s))
  419. var nerr errors.NonFatal
  420. jval, err := dec.Read()
  421. if !nerr.Merge(err) {
  422. return pref.Value{}, err
  423. }
  424. return getFloat(jval, bitSize)
  425. }
  426. return pref.Value{}, unexpectedJSONError{jval}
  427. }
  428. func getFloat(jval json.Value, bitSize int) (pref.Value, error) {
  429. n, err := jval.Float(bitSize)
  430. if err != nil {
  431. return pref.Value{}, err
  432. }
  433. if bitSize == 32 {
  434. return pref.ValueOf(float32(n)), nil
  435. }
  436. return pref.ValueOf(n), nil
  437. }
  438. func unmarshalString(jval json.Value) (pref.Value, error) {
  439. if jval.Type() != json.String {
  440. return pref.Value{}, unexpectedJSONError{jval}
  441. }
  442. return pref.ValueOf(jval.String()), nil
  443. }
  444. func unmarshalBytes(jval json.Value) (pref.Value, error) {
  445. if jval.Type() != json.String {
  446. return pref.Value{}, unexpectedJSONError{jval}
  447. }
  448. s := jval.String()
  449. enc := base64.StdEncoding
  450. if strings.ContainsAny(s, "-_") {
  451. enc = base64.URLEncoding
  452. }
  453. if len(s)%4 != 0 {
  454. enc = enc.WithPadding(base64.NoPadding)
  455. }
  456. b, err := enc.DecodeString(s)
  457. if err != nil {
  458. return pref.Value{}, err
  459. }
  460. return pref.ValueOf(b), nil
  461. }
  462. func unmarshalEnum(jval json.Value, fd pref.FieldDescriptor) (pref.Value, error) {
  463. switch jval.Type() {
  464. case json.String:
  465. // Lookup EnumNumber based on name.
  466. s := jval.String()
  467. if enumVal := fd.Enum().Values().ByName(pref.Name(s)); enumVal != nil {
  468. return pref.ValueOf(enumVal.Number()), nil
  469. }
  470. return pref.Value{}, newError("invalid enum value %q", jval)
  471. case json.Number:
  472. n, err := jval.Int(32)
  473. if err != nil {
  474. return pref.Value{}, err
  475. }
  476. return pref.ValueOf(pref.EnumNumber(n)), nil
  477. case json.Null:
  478. // This is only valid for google.protobuf.NullValue.
  479. if isNullValue(fd) {
  480. return pref.ValueOf(pref.EnumNumber(0)), nil
  481. }
  482. }
  483. return pref.Value{}, unexpectedJSONError{jval}
  484. }
  485. func (o UnmarshalOptions) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
  486. var nerr errors.NonFatal
  487. jval, err := o.decoder.Read()
  488. if !nerr.Merge(err) {
  489. return err
  490. }
  491. if jval.Type() != json.StartArray {
  492. return unexpectedJSONError{jval}
  493. }
  494. switch fd.Kind() {
  495. case pref.MessageKind, pref.GroupKind:
  496. for {
  497. m := list.NewMessage()
  498. err := o.unmarshalMessage(m, false)
  499. if !nerr.Merge(err) {
  500. if e, ok := err.(unexpectedJSONError); ok {
  501. if e.value.Type() == json.EndArray {
  502. // Done with list.
  503. return nerr.E
  504. }
  505. }
  506. return err
  507. }
  508. list.Append(pref.ValueOf(m))
  509. }
  510. default:
  511. for {
  512. val, err := o.unmarshalScalar(fd)
  513. if !nerr.Merge(err) {
  514. if e, ok := err.(unexpectedJSONError); ok {
  515. if e.value.Type() == json.EndArray {
  516. // Done with list.
  517. return nerr.E
  518. }
  519. }
  520. return err
  521. }
  522. list.Append(val)
  523. }
  524. }
  525. return nerr.E
  526. }
  527. func (o UnmarshalOptions) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
  528. var nerr errors.NonFatal
  529. jval, err := o.decoder.Read()
  530. if !nerr.Merge(err) {
  531. return err
  532. }
  533. if jval.Type() != json.StartObject {
  534. return unexpectedJSONError{jval}
  535. }
  536. // Determine ahead whether map entry is a scalar type or a message type in
  537. // order to call the appropriate unmarshalMapValue func inside the for loop
  538. // below.
  539. var unmarshalMapValue func() (pref.Value, error)
  540. switch fd.MapValue().Kind() {
  541. case pref.MessageKind, pref.GroupKind:
  542. unmarshalMapValue = func() (pref.Value, error) {
  543. var nerr errors.NonFatal
  544. m := mmap.NewMessage()
  545. if err := o.unmarshalMessage(m, false); !nerr.Merge(err) {
  546. return pref.Value{}, err
  547. }
  548. return pref.ValueOf(m), nerr.E
  549. }
  550. default:
  551. unmarshalMapValue = func() (pref.Value, error) {
  552. return o.unmarshalScalar(fd.MapValue())
  553. }
  554. }
  555. Loop:
  556. for {
  557. // Read field name.
  558. jval, err := o.decoder.Read()
  559. if !nerr.Merge(err) {
  560. return err
  561. }
  562. switch jval.Type() {
  563. default:
  564. return unexpectedJSONError{jval}
  565. case json.EndObject:
  566. break Loop
  567. case json.Name:
  568. // Continue.
  569. }
  570. name, err := jval.Name()
  571. if !nerr.Merge(err) {
  572. return err
  573. }
  574. // Unmarshal field name.
  575. pkey, err := unmarshalMapKey(name, fd.MapKey())
  576. if !nerr.Merge(err) {
  577. return err
  578. }
  579. // Check for duplicate field name.
  580. if mmap.Has(pkey) {
  581. return newError("duplicate map key %q", jval)
  582. }
  583. // Read and unmarshal field value.
  584. pval, err := unmarshalMapValue()
  585. if !nerr.Merge(err) {
  586. return err
  587. }
  588. mmap.Set(pkey, pval)
  589. }
  590. return nerr.E
  591. }
  592. // unmarshalMapKey converts given string into a protoreflect.MapKey. A map key type is any
  593. // integral or string type.
  594. func unmarshalMapKey(name string, fd pref.FieldDescriptor) (pref.MapKey, error) {
  595. const b32 = 32
  596. const b64 = 64
  597. const base10 = 10
  598. kind := fd.Kind()
  599. switch kind {
  600. case pref.StringKind:
  601. return pref.ValueOf(name).MapKey(), nil
  602. case pref.BoolKind:
  603. switch name {
  604. case "true":
  605. return pref.ValueOf(true).MapKey(), nil
  606. case "false":
  607. return pref.ValueOf(false).MapKey(), nil
  608. }
  609. return pref.MapKey{}, errors.New("invalid value for boolean key %q", name)
  610. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  611. n, err := strconv.ParseInt(name, base10, b32)
  612. if err != nil {
  613. return pref.MapKey{}, err
  614. }
  615. return pref.ValueOf(int32(n)).MapKey(), nil
  616. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  617. n, err := strconv.ParseInt(name, base10, b64)
  618. if err != nil {
  619. return pref.MapKey{}, err
  620. }
  621. return pref.ValueOf(int64(n)).MapKey(), nil
  622. case pref.Uint32Kind, pref.Fixed32Kind:
  623. n, err := strconv.ParseUint(name, base10, b32)
  624. if err != nil {
  625. return pref.MapKey{}, err
  626. }
  627. return pref.ValueOf(uint32(n)).MapKey(), nil
  628. case pref.Uint64Kind, pref.Fixed64Kind:
  629. n, err := strconv.ParseUint(name, base10, b64)
  630. if err != nil {
  631. return pref.MapKey{}, err
  632. }
  633. return pref.ValueOf(uint64(n)).MapKey(), nil
  634. }
  635. panic(fmt.Sprintf("%s: invalid kind %s for map key", fd.FullName(), kind))
  636. }