decode.go 17 KB

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