decode.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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); !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) 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); !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) 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. // Get the FieldDescriptor.
  158. var fd pref.FieldDescriptor
  159. if strings.HasPrefix(name, "[") && strings.HasSuffix(name, "]") {
  160. // Only extension names are in [name] format.
  161. xtName := pref.FullName(name[1 : len(name)-1])
  162. xt := xtTypes.ByName(xtName)
  163. if xt == nil {
  164. xt, err = o.findExtension(xtName)
  165. if err != nil && err != protoregistry.NotFound {
  166. return errors.New("unable to resolve [%v]: %v", xtName, err)
  167. }
  168. if xt != nil {
  169. xtTypes.Register(xt)
  170. }
  171. }
  172. fd = xt
  173. } else {
  174. // The name can either be the JSON name or the proto field name.
  175. fd = fieldDescs.ByJSONName(name)
  176. if fd == nil {
  177. fd = fieldDescs.ByName(pref.Name(name))
  178. }
  179. }
  180. if fd == nil {
  181. // Field is unknown.
  182. // TODO: Provide option to ignore unknown message fields.
  183. return newError("%v contains unknown field %s", msgType.FullName(), jval)
  184. }
  185. // Do not allow duplicate fields.
  186. num := uint64(fd.Number())
  187. if seenNums.Has(num) {
  188. return newError("%v contains repeated field %s", msgType.FullName(), jval)
  189. }
  190. seenNums.Set(num)
  191. // No need to set values for JSON null unless the field type is
  192. // google.protobuf.Value.
  193. if o.decoder.Peek() == json.Null && !isKnownValue(fd) {
  194. o.decoder.Read()
  195. continue
  196. }
  197. if cardinality := fd.Cardinality(); cardinality == pref.Repeated {
  198. // Map or list fields have cardinality of repeated.
  199. if err := o.unmarshalRepeated(knownFields, fd); !nerr.Merge(err) {
  200. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  201. }
  202. } else {
  203. // Required or optional fields.
  204. if err := o.unmarshalSingular(knownFields, fd); !nerr.Merge(err) {
  205. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  206. }
  207. if !o.AllowPartial && cardinality == pref.Required {
  208. reqNums.Set(num)
  209. }
  210. }
  211. }
  212. if !o.AllowPartial {
  213. // Check for any missing required fields.
  214. allReqNums := msgType.RequiredNumbers()
  215. if reqNums.Len() != allReqNums.Len() {
  216. for i := 0; i < allReqNums.Len(); i++ {
  217. if num := allReqNums.Get(i); !reqNums.Has(uint64(num)) {
  218. nerr.AppendRequiredNotSet(string(fieldDescs.ByNumber(num).FullName()))
  219. }
  220. }
  221. }
  222. }
  223. return nerr.E
  224. }
  225. // findExtension returns protoreflect.ExtensionType from the resolver if found.
  226. func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
  227. xt, err := o.Resolver.FindExtensionByName(xtName)
  228. if err == nil {
  229. return xt, nil
  230. }
  231. // Check if this is a MessageSet extension field.
  232. xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
  233. if err == nil && isMessageSetExtension(xt) {
  234. return xt, nil
  235. }
  236. return nil, protoregistry.NotFound
  237. }
  238. // unmarshalSingular unmarshals to the non-repeated field specified by the given
  239. // FieldDescriptor.
  240. func (o UnmarshalOptions) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
  241. var val pref.Value
  242. var err error
  243. num := fd.Number()
  244. switch fd.Kind() {
  245. case pref.MessageKind, pref.GroupKind:
  246. m := knownFields.NewMessage(num)
  247. err = o.unmarshalMessage(m)
  248. val = pref.ValueOf(m)
  249. default:
  250. val, err = o.unmarshalScalar(fd)
  251. }
  252. var nerr errors.NonFatal
  253. if !nerr.Merge(err) {
  254. return err
  255. }
  256. knownFields.Set(num, val)
  257. return nerr.E
  258. }
  259. // unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by
  260. // the given FieldDescriptor.
  261. func (o UnmarshalOptions) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) {
  262. const b32 int = 32
  263. const b64 int = 64
  264. var nerr errors.NonFatal
  265. jval, err := o.decoder.Read()
  266. if !nerr.Merge(err) {
  267. return pref.Value{}, err
  268. }
  269. kind := fd.Kind()
  270. switch kind {
  271. case pref.BoolKind:
  272. return unmarshalBool(jval)
  273. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  274. return unmarshalInt(jval, b32)
  275. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  276. return unmarshalInt(jval, b64)
  277. case pref.Uint32Kind, pref.Fixed32Kind:
  278. return unmarshalUint(jval, b32)
  279. case pref.Uint64Kind, pref.Fixed64Kind:
  280. return unmarshalUint(jval, b64)
  281. case pref.FloatKind:
  282. return unmarshalFloat(jval, b32)
  283. case pref.DoubleKind:
  284. return unmarshalFloat(jval, b64)
  285. case pref.StringKind:
  286. pval, err := unmarshalString(jval)
  287. if !nerr.Merge(err) {
  288. return pval, err
  289. }
  290. return pval, nerr.E
  291. case pref.BytesKind:
  292. return unmarshalBytes(jval)
  293. case pref.EnumKind:
  294. return unmarshalEnum(jval, fd)
  295. }
  296. panic(fmt.Sprintf("invalid scalar kind %v", kind))
  297. }
  298. func unmarshalBool(jval json.Value) (pref.Value, error) {
  299. if jval.Type() != json.Bool {
  300. return pref.Value{}, unexpectedJSONError{jval}
  301. }
  302. b, err := jval.Bool()
  303. return pref.ValueOf(b), err
  304. }
  305. func unmarshalInt(jval json.Value, bitSize int) (pref.Value, error) {
  306. switch jval.Type() {
  307. case json.Number:
  308. return getInt(jval, bitSize)
  309. case json.String:
  310. // Decode number from string.
  311. dec := json.NewDecoder([]byte(jval.String()))
  312. var nerr errors.NonFatal
  313. jval, err := dec.Read()
  314. if !nerr.Merge(err) {
  315. return pref.Value{}, err
  316. }
  317. return getInt(jval, bitSize)
  318. }
  319. return pref.Value{}, unexpectedJSONError{jval}
  320. }
  321. func getInt(jval json.Value, bitSize int) (pref.Value, error) {
  322. n, err := jval.Int(bitSize)
  323. if err != nil {
  324. return pref.Value{}, err
  325. }
  326. if bitSize == 32 {
  327. return pref.ValueOf(int32(n)), nil
  328. }
  329. return pref.ValueOf(n), nil
  330. }
  331. func unmarshalUint(jval json.Value, bitSize int) (pref.Value, error) {
  332. switch jval.Type() {
  333. case json.Number:
  334. return getUint(jval, bitSize)
  335. case json.String:
  336. // Decode number from string.
  337. dec := json.NewDecoder([]byte(jval.String()))
  338. var nerr errors.NonFatal
  339. jval, err := dec.Read()
  340. if !nerr.Merge(err) {
  341. return pref.Value{}, err
  342. }
  343. return getUint(jval, bitSize)
  344. }
  345. return pref.Value{}, unexpectedJSONError{jval}
  346. }
  347. func getUint(jval json.Value, bitSize int) (pref.Value, error) {
  348. n, err := jval.Uint(bitSize)
  349. if err != nil {
  350. return pref.Value{}, err
  351. }
  352. if bitSize == 32 {
  353. return pref.ValueOf(uint32(n)), nil
  354. }
  355. return pref.ValueOf(n), nil
  356. }
  357. func unmarshalFloat(jval json.Value, bitSize int) (pref.Value, error) {
  358. switch jval.Type() {
  359. case json.Number:
  360. return getFloat(jval, bitSize)
  361. case json.String:
  362. s := jval.String()
  363. switch s {
  364. case "NaN":
  365. if bitSize == 32 {
  366. return pref.ValueOf(float32(math.NaN())), nil
  367. }
  368. return pref.ValueOf(math.NaN()), nil
  369. case "Infinity":
  370. if bitSize == 32 {
  371. return pref.ValueOf(float32(math.Inf(+1))), nil
  372. }
  373. return pref.ValueOf(math.Inf(+1)), nil
  374. case "-Infinity":
  375. if bitSize == 32 {
  376. return pref.ValueOf(float32(math.Inf(-1))), nil
  377. }
  378. return pref.ValueOf(math.Inf(-1)), nil
  379. }
  380. // Decode number from string.
  381. dec := json.NewDecoder([]byte(s))
  382. var nerr errors.NonFatal
  383. jval, err := dec.Read()
  384. if !nerr.Merge(err) {
  385. return pref.Value{}, err
  386. }
  387. return getFloat(jval, bitSize)
  388. }
  389. return pref.Value{}, unexpectedJSONError{jval}
  390. }
  391. func getFloat(jval json.Value, bitSize int) (pref.Value, error) {
  392. n, err := jval.Float(bitSize)
  393. if err != nil {
  394. return pref.Value{}, err
  395. }
  396. if bitSize == 32 {
  397. return pref.ValueOf(float32(n)), nil
  398. }
  399. return pref.ValueOf(n), nil
  400. }
  401. func unmarshalString(jval json.Value) (pref.Value, error) {
  402. if jval.Type() != json.String {
  403. return pref.Value{}, unexpectedJSONError{jval}
  404. }
  405. return pref.ValueOf(jval.String()), nil
  406. }
  407. func unmarshalBytes(jval json.Value) (pref.Value, error) {
  408. if jval.Type() != json.String {
  409. return pref.Value{}, unexpectedJSONError{jval}
  410. }
  411. s := jval.String()
  412. enc := base64.StdEncoding
  413. if strings.ContainsAny(s, "-_") {
  414. enc = base64.URLEncoding
  415. }
  416. if len(s)%4 != 0 {
  417. enc = enc.WithPadding(base64.NoPadding)
  418. }
  419. b, err := enc.DecodeString(s)
  420. if err != nil {
  421. return pref.Value{}, err
  422. }
  423. return pref.ValueOf(b), nil
  424. }
  425. func unmarshalEnum(jval json.Value, fd pref.FieldDescriptor) (pref.Value, error) {
  426. switch jval.Type() {
  427. case json.String:
  428. // Lookup EnumNumber based on name.
  429. s := jval.String()
  430. if enumVal := fd.EnumType().Values().ByName(pref.Name(s)); enumVal != nil {
  431. return pref.ValueOf(enumVal.Number()), nil
  432. }
  433. return pref.Value{}, newError("invalid enum value %q", jval)
  434. case json.Number:
  435. n, err := jval.Int(32)
  436. if err != nil {
  437. return pref.Value{}, err
  438. }
  439. return pref.ValueOf(pref.EnumNumber(n)), nil
  440. }
  441. return pref.Value{}, unexpectedJSONError{jval}
  442. }
  443. // unmarshalRepeated unmarshals into a repeated field.
  444. func (o UnmarshalOptions) unmarshalRepeated(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
  445. var nerr errors.NonFatal
  446. num := fd.Number()
  447. val := knownFields.Get(num)
  448. if !fd.IsMap() {
  449. if err := o.unmarshalList(val.List(), fd); !nerr.Merge(err) {
  450. return err
  451. }
  452. } else {
  453. if err := o.unmarshalMap(val.Map(), fd); !nerr.Merge(err) {
  454. return err
  455. }
  456. }
  457. return nerr.E
  458. }
  459. // unmarshalList unmarshals into given protoreflect.List.
  460. func (o UnmarshalOptions) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
  461. var nerr errors.NonFatal
  462. jval, err := o.decoder.Read()
  463. if !nerr.Merge(err) {
  464. return err
  465. }
  466. if jval.Type() != json.StartArray {
  467. return unexpectedJSONError{jval}
  468. }
  469. switch fd.Kind() {
  470. case pref.MessageKind, pref.GroupKind:
  471. for {
  472. m := list.NewMessage()
  473. err := o.unmarshalMessage(m)
  474. if !nerr.Merge(err) {
  475. if e, ok := err.(unexpectedJSONError); ok {
  476. if e.value.Type() == json.EndArray {
  477. // Done with list.
  478. return nerr.E
  479. }
  480. }
  481. return err
  482. }
  483. list.Append(pref.ValueOf(m))
  484. }
  485. default:
  486. for {
  487. val, err := o.unmarshalScalar(fd)
  488. if !nerr.Merge(err) {
  489. if e, ok := err.(unexpectedJSONError); ok {
  490. if e.value.Type() == json.EndArray {
  491. // Done with list.
  492. return nerr.E
  493. }
  494. }
  495. return err
  496. }
  497. list.Append(val)
  498. }
  499. }
  500. return nerr.E
  501. }
  502. // unmarshalMap unmarshals into given protoreflect.Map.
  503. func (o UnmarshalOptions) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
  504. var nerr errors.NonFatal
  505. jval, err := o.decoder.Read()
  506. if !nerr.Merge(err) {
  507. return err
  508. }
  509. if jval.Type() != json.StartObject {
  510. return unexpectedJSONError{jval}
  511. }
  512. fields := fd.MessageType().Fields()
  513. keyDesc := fields.ByNumber(1)
  514. valDesc := fields.ByNumber(2)
  515. // Determine ahead whether map entry is a scalar type or a message type in
  516. // order to call the appropriate unmarshalMapValue func inside the for loop
  517. // below.
  518. unmarshalMapValue := func() (pref.Value, error) {
  519. return o.unmarshalScalar(valDesc)
  520. }
  521. switch valDesc.Kind() {
  522. case pref.MessageKind, pref.GroupKind:
  523. unmarshalMapValue = func() (pref.Value, error) {
  524. var nerr errors.NonFatal
  525. m := mmap.NewMessage()
  526. if err := o.unmarshalMessage(m); !nerr.Merge(err) {
  527. return pref.Value{}, err
  528. }
  529. return pref.ValueOf(m), nerr.E
  530. }
  531. }
  532. Loop:
  533. for {
  534. // Read field name.
  535. jval, err := o.decoder.Read()
  536. if !nerr.Merge(err) {
  537. return err
  538. }
  539. switch jval.Type() {
  540. default:
  541. return unexpectedJSONError{jval}
  542. case json.EndObject:
  543. break Loop
  544. case json.Name:
  545. // Continue.
  546. }
  547. name, err := jval.Name()
  548. if !nerr.Merge(err) {
  549. return err
  550. }
  551. // Unmarshal field name.
  552. pkey, err := unmarshalMapKey(name, keyDesc)
  553. if !nerr.Merge(err) {
  554. return err
  555. }
  556. // Check for duplicate field name.
  557. if mmap.Has(pkey) {
  558. return newError("duplicate map key %q", jval)
  559. }
  560. // Read and unmarshal field value.
  561. pval, err := unmarshalMapValue()
  562. if !nerr.Merge(err) {
  563. return err
  564. }
  565. mmap.Set(pkey, pval)
  566. }
  567. return nerr.E
  568. }
  569. // unmarshalMapKey converts given string into a protoreflect.MapKey. A map key type is any
  570. // integral or string type.
  571. func unmarshalMapKey(name string, fd pref.FieldDescriptor) (pref.MapKey, error) {
  572. const b32 = 32
  573. const b64 = 64
  574. const base10 = 10
  575. kind := fd.Kind()
  576. switch kind {
  577. case pref.StringKind:
  578. return pref.ValueOf(name).MapKey(), nil
  579. case pref.BoolKind:
  580. switch name {
  581. case "true":
  582. return pref.ValueOf(true).MapKey(), nil
  583. case "false":
  584. return pref.ValueOf(false).MapKey(), nil
  585. }
  586. return pref.MapKey{}, errors.New("invalid value for boolean key %q", name)
  587. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  588. n, err := strconv.ParseInt(name, base10, b32)
  589. if err != nil {
  590. return pref.MapKey{}, err
  591. }
  592. return pref.ValueOf(int32(n)).MapKey(), nil
  593. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  594. n, err := strconv.ParseInt(name, base10, b64)
  595. if err != nil {
  596. return pref.MapKey{}, err
  597. }
  598. return pref.ValueOf(int64(n)).MapKey(), nil
  599. case pref.Uint32Kind, pref.Fixed32Kind:
  600. n, err := strconv.ParseUint(name, base10, b32)
  601. if err != nil {
  602. return pref.MapKey{}, err
  603. }
  604. return pref.ValueOf(uint32(n)).MapKey(), nil
  605. case pref.Uint64Kind, pref.Fixed64Kind:
  606. n, err := strconv.ParseUint(name, base10, b64)
  607. if err != nil {
  608. return pref.MapKey{}, err
  609. }
  610. return pref.ValueOf(uint64(n)).MapKey(), nil
  611. }
  612. panic(fmt.Sprintf("%s: invalid kind %s for map key", fd.FullName(), kind))
  613. }