decode.go 18 KB

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