PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/proto/lib.go

https://code.google.com/p/goprotobuf/
Go | 777 lines | 469 code | 66 blank | 242 comment | 111 complexity | 8a275352ee41d905dc3a39e11eb4704f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 Google Inc. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. /*
  32. The proto package converts data structures to and from the
  33. wire format of protocol buffers. It works in concert with the
  34. Go source code generated for .proto files by the protocol compiler.
  35. A summary of the properties of the protocol buffer interface
  36. for a protocol buffer variable v:
  37. - Names are turned from camel_case to CamelCase for export.
  38. - There are no methods on v to set and get fields; just treat
  39. them as structure fields.
  40. - The zero value for a struct is its correct initialization state.
  41. All desired fields must be set before marshaling.
  42. - A Reset() method will restore a protobuf struct to its zero state.
  43. - Non-repeated fields are pointers to the values; nil means unset.
  44. That is, optional or required field int32 f becomes F *int32.
  45. - Repeated fields are slices.
  46. - Helper functions are available to simplify the getting and setting of fields:
  47. foo.String = proto.String("hello") // set field
  48. s := proto.GetString(foo.String) // get field
  49. - Constants are defined to hold the default values of all fields that
  50. have them. They have the form Default_StructName_FieldName.
  51. - Enums are given type names and maps between names to values,
  52. plus a helper function to create values. Enum values are prefixed
  53. with the enum's type name. Enum types have a String method.
  54. - Nested groups and enums have type names prefixed with the name of
  55. the surrounding message type.
  56. - Extensions are given descriptor names that start with E_,
  57. followed by an underscore-delimited list of the nested messages
  58. that contain it (if any) followed by the CamelCased name of the
  59. extension field itself. HasExtension, ClearExtension, GetExtension
  60. and SetExtension are functions for manipulating extensions.
  61. - Marshal and Unmarshal are functions to encode and decode the wire format.
  62. The simplest way to describe this is to see an example.
  63. Given file test.proto, containing
  64. package example;
  65. enum FOO { X = 17; };
  66. message Test {
  67. required string label = 1;
  68. optional int32 type = 2 [default=77];
  69. repeated int64 reps = 3;
  70. optional group OptionalGroup = 4 {
  71. required string RequiredField = 5;
  72. };
  73. }
  74. The resulting file, test.pb.go, is:
  75. package example
  76. import "code.google.com/p/goprotobuf/proto"
  77. type FOO int32
  78. const (
  79. FOO_X = 17
  80. )
  81. var FOO_name = map[int32] string {
  82. 17: "X",
  83. }
  84. var FOO_value = map[string] int32 {
  85. "X": 17,
  86. }
  87. func NewFOO(x int32) *FOO {
  88. e := FOO(x)
  89. return &e
  90. }
  91. func (x FOO) String() string {
  92. return proto.EnumName(FOO_name, int32(x))
  93. }
  94. type Test struct {
  95. Label *string `protobuf:"bytes,1,req,name=label"`
  96. Type *int32 `protobuf:"varint,2,opt,name=type,def=77"`
  97. Reps []int64 `protobuf:"varint,3,rep,name=reps"`
  98. Optionalgroup *Test_OptionalGroup `protobuf:"group,4,opt,name=optionalgroup"`
  99. XXX_unrecognized []byte
  100. }
  101. func (this *Test) Reset() {
  102. *this = Test{}
  103. }
  104. const Default_Test_Type int32 = 77
  105. type Test_OptionalGroup struct {
  106. RequiredField *string `protobuf:"bytes,5,req"`
  107. XXX_unrecognized []byte
  108. }
  109. func (this *Test_OptionalGroup) Reset() {
  110. *this = Test_OptionalGroup{}
  111. }
  112. func init() {
  113. proto.RegisterEnum("example.FOO", FOO_name, FOO_value)
  114. }
  115. To create and play with a Test object:
  116. package main
  117. import (
  118. "log"
  119. "code.google.com/p/goprotobuf/proto"
  120. "./example.pb"
  121. )
  122. func main() {
  123. test := &example.Test{
  124. Label: proto.String("hello"),
  125. Type: proto.Int32(17),
  126. Optionalgroup: &example.Test_OptionalGroup{
  127. RequiredField: proto.String("good bye"),
  128. },
  129. }
  130. data, err := proto.Marshal(test)
  131. if err != nil {
  132. log.Fatal("marshaling error: ", err)
  133. }
  134. newTest := new(example.Test)
  135. err = proto.Unmarshal(data, newTest)
  136. if err != nil {
  137. log.Fatal("unmarshaling error: ", err)
  138. }
  139. // Now test and newTest contain the same data.
  140. if proto.GetString(test.Label) != proto.GetString(newTest.Label) {
  141. log.Fatalf("data mismatch %q != %q", proto.GetString(test.Label), proto.GetString(newTest.Label))
  142. }
  143. // etc.
  144. }
  145. */
  146. package proto
  147. import (
  148. "fmt"
  149. "log"
  150. "reflect"
  151. "strconv"
  152. "sync"
  153. )
  154. // Stats records allocation details about the protocol buffer encoders
  155. // and decoders. Useful for tuning the library itself.
  156. type Stats struct {
  157. Emalloc uint64 // mallocs in encode
  158. Dmalloc uint64 // mallocs in decode
  159. Encode uint64 // number of encodes
  160. Decode uint64 // number of decodes
  161. Chit uint64 // number of cache hits
  162. Cmiss uint64 // number of cache misses
  163. }
  164. var stats Stats
  165. // GetStats returns a copy of the global Stats structure.
  166. func GetStats() Stats { return stats }
  167. // A Buffer is a buffer manager for marshaling and unmarshaling
  168. // protocol buffers. It may be reused between invocations to
  169. // reduce memory usage. It is not necessary to use a Buffer;
  170. // the global functions Marshal and Unmarshal create a
  171. // temporary Buffer and are fine for most applications.
  172. type Buffer struct {
  173. buf []byte // encode/decode byte stream
  174. index int // write point
  175. freelist [10][]byte // list of available buffers
  176. nfreelist int // number of free buffers
  177. // pools of basic types to amortize allocation.
  178. bools []bool
  179. int32s []int32
  180. int64s []int64
  181. }
  182. // NewBuffer allocates a new Buffer and initializes its internal data to
  183. // the contents of the argument slice.
  184. func NewBuffer(e []byte) *Buffer {
  185. p := new(Buffer)
  186. if e == nil {
  187. e = p.bufalloc()
  188. }
  189. p.buf = e
  190. p.index = 0
  191. return p
  192. }
  193. // Reset resets the Buffer, ready for marshaling a new protocol buffer.
  194. func (p *Buffer) Reset() {
  195. if p.buf == nil {
  196. p.buf = p.bufalloc()
  197. }
  198. p.buf = p.buf[0:0] // for reading/writing
  199. p.index = 0 // for reading
  200. }
  201. // SetBuf replaces the internal buffer with the slice,
  202. // ready for unmarshaling the contents of the slice.
  203. func (p *Buffer) SetBuf(s []byte) {
  204. p.buf = s
  205. p.index = 0
  206. }
  207. // Bytes returns the contents of the Buffer.
  208. func (p *Buffer) Bytes() []byte { return p.buf }
  209. // Allocate a buffer for the Buffer.
  210. func (p *Buffer) bufalloc() []byte {
  211. if p.nfreelist > 0 {
  212. // reuse an old one
  213. p.nfreelist--
  214. s := p.freelist[p.nfreelist]
  215. return s[0:0]
  216. }
  217. // make a new one
  218. s := make([]byte, 0, 16)
  219. return s
  220. }
  221. // Free (and remember in freelist) a byte buffer for the Buffer.
  222. func (p *Buffer) buffree(s []byte) {
  223. if p.nfreelist < len(p.freelist) {
  224. // Take next slot.
  225. p.freelist[p.nfreelist] = s
  226. p.nfreelist++
  227. return
  228. }
  229. // Find the smallest.
  230. besti := -1
  231. bestl := len(s)
  232. for i, b := range p.freelist {
  233. if len(b) < bestl {
  234. besti = i
  235. bestl = len(b)
  236. }
  237. }
  238. // Overwrite the smallest.
  239. if besti >= 0 {
  240. p.freelist[besti] = s
  241. }
  242. }
  243. /*
  244. * Helper routines for simplifying the creation of optional fields of basic type.
  245. */
  246. // Bool is a helper routine that allocates a new bool value
  247. // to store v and returns a pointer to it.
  248. func Bool(v bool) *bool {
  249. p := new(bool)
  250. *p = v
  251. return p
  252. }
  253. // Int32 is a helper routine that allocates a new int32 value
  254. // to store v and returns a pointer to it.
  255. func Int32(v int32) *int32 {
  256. p := new(int32)
  257. *p = v
  258. return p
  259. }
  260. // Int is a helper routine that allocates a new int32 value
  261. // to store v and returns a pointer to it, but unlike Int32
  262. // its argument value is an int.
  263. func Int(v int) *int32 {
  264. p := new(int32)
  265. *p = int32(v)
  266. return p
  267. }
  268. // Int64 is a helper routine that allocates a new int64 value
  269. // to store v and returns a pointer to it.
  270. func Int64(v int64) *int64 {
  271. p := new(int64)
  272. *p = v
  273. return p
  274. }
  275. // Float32 is a helper routine that allocates a new float32 value
  276. // to store v and returns a pointer to it.
  277. func Float32(v float32) *float32 {
  278. p := new(float32)
  279. *p = v
  280. return p
  281. }
  282. // Float64 is a helper routine that allocates a new float64 value
  283. // to store v and returns a pointer to it.
  284. func Float64(v float64) *float64 {
  285. p := new(float64)
  286. *p = v
  287. return p
  288. }
  289. // Uint32 is a helper routine that allocates a new uint32 value
  290. // to store v and returns a pointer to it.
  291. func Uint32(v uint32) *uint32 {
  292. p := new(uint32)
  293. *p = v
  294. return p
  295. }
  296. // Uint64 is a helper routine that allocates a new uint64 value
  297. // to store v and returns a pointer to it.
  298. func Uint64(v uint64) *uint64 {
  299. p := new(uint64)
  300. *p = v
  301. return p
  302. }
  303. // String is a helper routine that allocates a new string value
  304. // to store v and returns a pointer to it.
  305. func String(v string) *string {
  306. p := new(string)
  307. *p = v
  308. return p
  309. }
  310. /*
  311. * Helper routines for simplifying the fetching of optional fields of basic type.
  312. * If the field is missing, they return the zero for the type.
  313. */
  314. // GetBool is a helper routine that returns an optional bool value.
  315. func GetBool(p *bool) bool {
  316. if p == nil {
  317. return false
  318. }
  319. return *p
  320. }
  321. // GetInt32 is a helper routine that returns an optional int32 value.
  322. func GetInt32(p *int32) int32 {
  323. if p == nil {
  324. return 0
  325. }
  326. return *p
  327. }
  328. // GetInt64 is a helper routine that returns an optional int64 value.
  329. func GetInt64(p *int64) int64 {
  330. if p == nil {
  331. return 0
  332. }
  333. return *p
  334. }
  335. // GetFloat32 is a helper routine that returns an optional float32 value.
  336. func GetFloat32(p *float32) float32 {
  337. if p == nil {
  338. return 0
  339. }
  340. return *p
  341. }
  342. // GetFloat64 is a helper routine that returns an optional float64 value.
  343. func GetFloat64(p *float64) float64 {
  344. if p == nil {
  345. return 0
  346. }
  347. return *p
  348. }
  349. // GetUint32 is a helper routine that returns an optional uint32 value.
  350. func GetUint32(p *uint32) uint32 {
  351. if p == nil {
  352. return 0
  353. }
  354. return *p
  355. }
  356. // GetUint64 is a helper routine that returns an optional uint64 value.
  357. func GetUint64(p *uint64) uint64 {
  358. if p == nil {
  359. return 0
  360. }
  361. return *p
  362. }
  363. // GetString is a helper routine that returns an optional string value.
  364. func GetString(p *string) string {
  365. if p == nil {
  366. return ""
  367. }
  368. return *p
  369. }
  370. // EnumName is a helper function to simplify printing protocol buffer enums
  371. // by name. Given an enum map and a value, it returns a useful string.
  372. func EnumName(m map[int32]string, v int32) string {
  373. s, ok := m[v]
  374. if ok {
  375. return s
  376. }
  377. return "unknown_enum_" + strconv.Itoa(int(v))
  378. }
  379. // DebugPrint dumps the encoded data in b in a debugging format with a header
  380. // including the string s. Used in testing but made available for general debugging.
  381. func (o *Buffer) DebugPrint(s string, b []byte) {
  382. var u uint64
  383. obuf := o.buf
  384. index := o.index
  385. o.buf = b
  386. o.index = 0
  387. depth := 0
  388. fmt.Printf("\n--- %s ---\n", s)
  389. out:
  390. for {
  391. for i := 0; i < depth; i++ {
  392. fmt.Print(" ")
  393. }
  394. index := o.index
  395. if index == len(o.buf) {
  396. break
  397. }
  398. op, err := o.DecodeVarint()
  399. if err != nil {
  400. fmt.Printf("%3d: fetching op err %v\n", index, err)
  401. break out
  402. }
  403. tag := op >> 3
  404. wire := op & 7
  405. switch wire {
  406. default:
  407. fmt.Printf("%3d: t=%3d unknown wire=%d\n",
  408. index, tag, wire)
  409. break out
  410. case WireBytes:
  411. var r []byte
  412. r, err = o.DecodeRawBytes(false)
  413. if err != nil {
  414. break out
  415. }
  416. fmt.Printf("%3d: t=%3d bytes [%d]", index, tag, len(r))
  417. if len(r) <= 6 {
  418. for i := 0; i < len(r); i++ {
  419. fmt.Printf(" %.2x", r[i])
  420. }
  421. } else {
  422. for i := 0; i < 3; i++ {
  423. fmt.Printf(" %.2x", r[i])
  424. }
  425. fmt.Printf(" ..")
  426. for i := len(r) - 3; i < len(r); i++ {
  427. fmt.Printf(" %.2x", r[i])
  428. }
  429. }
  430. fmt.Printf("\n")
  431. case WireFixed32:
  432. u, err = o.DecodeFixed32()
  433. if err != nil {
  434. fmt.Printf("%3d: t=%3d fix32 err %v\n", index, tag, err)
  435. break out
  436. }
  437. fmt.Printf("%3d: t=%3d fix32 %d\n", index, tag, u)
  438. case WireFixed64:
  439. u, err = o.DecodeFixed64()
  440. if err != nil {
  441. fmt.Printf("%3d: t=%3d fix64 err %v\n", index, tag, err)
  442. break out
  443. }
  444. fmt.Printf("%3d: t=%3d fix64 %d\n", index, tag, u)
  445. break
  446. case WireVarint:
  447. u, err = o.DecodeVarint()
  448. if err != nil {
  449. fmt.Printf("%3d: t=%3d varint err %v\n", index, tag, err)
  450. break out
  451. }
  452. fmt.Printf("%3d: t=%3d varint %d\n", index, tag, u)
  453. case WireStartGroup:
  454. if err != nil {
  455. fmt.Printf("%3d: t=%3d start err %v\n", index, tag, err)
  456. break out
  457. }
  458. fmt.Printf("%3d: t=%3d start\n", index, tag)
  459. depth++
  460. case WireEndGroup:
  461. depth--
  462. if err != nil {
  463. fmt.Printf("%3d: t=%3d end err %v\n", index, tag, err)
  464. break out
  465. }
  466. fmt.Printf("%3d: t=%3d end\n", index, tag)
  467. }
  468. }
  469. if depth != 0 {
  470. fmt.Printf("%3d: start-end not balanced %d\n", o.index, depth)
  471. }
  472. fmt.Printf("\n")
  473. o.buf = obuf
  474. o.index = index
  475. }
  476. // SetDefaults sets unset protocol buffer fields to their default values.
  477. // It only modifies fields that are both unset and have defined defaults.
  478. // It recursively sets default values in any non-nil sub-messages.
  479. func SetDefaults(pb interface{}) {
  480. v := reflect.ValueOf(pb)
  481. if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
  482. log.Printf("proto: hit non-pointer-to-struct %v", v)
  483. }
  484. setDefaults(v, true, false)
  485. }
  486. // v is a pointer to a struct.
  487. func setDefaults(v reflect.Value, recur, zeros bool) {
  488. v = v.Elem()
  489. defaultMu.RLock()
  490. dm, ok := defaults[v.Type()]
  491. defaultMu.RUnlock()
  492. if !ok {
  493. dm = buildDefaultMessage(v.Type())
  494. defaultMu.Lock()
  495. defaults[v.Type()] = dm
  496. defaultMu.Unlock()
  497. }
  498. for _, sf := range dm.scalars {
  499. f := v.Field(sf.index)
  500. if !f.IsNil() {
  501. // field already set
  502. continue
  503. }
  504. dv := sf.value
  505. if dv == nil && !zeros {
  506. // no explicit default, and don't want to set zeros
  507. continue
  508. }
  509. fptr := f.Addr().Interface() // **T
  510. // TODO: Consider batching the allocations we do here.
  511. switch sf.kind {
  512. case reflect.Bool:
  513. b := new(bool)
  514. if dv != nil {
  515. *b = dv.(bool)
  516. }
  517. *(fptr.(**bool)) = b
  518. case reflect.Float32:
  519. f := new(float32)
  520. if dv != nil {
  521. *f = dv.(float32)
  522. }
  523. *(fptr.(**float32)) = f
  524. case reflect.Float64:
  525. f := new(float64)
  526. if dv != nil {
  527. *f = dv.(float64)
  528. }
  529. *(fptr.(**float64)) = f
  530. case reflect.Int32:
  531. // might be an enum
  532. if ft := f.Type(); ft != int32PtrType {
  533. // enum
  534. f.Set(reflect.New(ft.Elem()))
  535. if dv != nil {
  536. f.Elem().SetInt(int64(dv.(int32)))
  537. }
  538. } else {
  539. // int32 field
  540. i := new(int32)
  541. if dv != nil {
  542. *i = dv.(int32)
  543. }
  544. *(fptr.(**int32)) = i
  545. }
  546. case reflect.Int64:
  547. i := new(int64)
  548. if dv != nil {
  549. *i = dv.(int64)
  550. }
  551. *(fptr.(**int64)) = i
  552. case reflect.String:
  553. s := new(string)
  554. if dv != nil {
  555. *s = dv.(string)
  556. }
  557. *(fptr.(**string)) = s
  558. case reflect.Uint8:
  559. // exceptional case: []byte
  560. var b []byte
  561. if dv != nil {
  562. db := dv.([]byte)
  563. b = make([]byte, len(db))
  564. copy(b, db)
  565. } else {
  566. b = []byte{}
  567. }
  568. *(fptr.(*[]byte)) = b
  569. case reflect.Uint32:
  570. u := new(uint32)
  571. if dv != nil {
  572. *u = dv.(uint32)
  573. }
  574. *(fptr.(**uint32)) = u
  575. case reflect.Uint64:
  576. u := new(uint64)
  577. if dv != nil {
  578. *u = dv.(uint64)
  579. }
  580. *(fptr.(**uint64)) = u
  581. default:
  582. log.Printf("proto: can't set default for field %v (sf.kind=%v)", f, sf.kind)
  583. }
  584. }
  585. for _, ni := range dm.nested {
  586. f := v.Field(ni)
  587. if f.IsNil() {
  588. continue
  589. }
  590. setDefaults(f, recur, zeros)
  591. }
  592. }
  593. var (
  594. // defaults maps a protocol buffer struct type to a slice of the fields,
  595. // with its scalar fields set to their proto-declared non-zero default values.
  596. defaultMu sync.RWMutex
  597. defaults = make(map[reflect.Type]defaultMessage)
  598. int32PtrType = reflect.TypeOf((*int32)(nil))
  599. )
  600. // defaultMessage represents information about the default values of a message.
  601. type defaultMessage struct {
  602. scalars []scalarField
  603. nested []int // struct field index of nested messages
  604. }
  605. type scalarField struct {
  606. index int // struct field index
  607. kind reflect.Kind // element type (the T in *T or []T)
  608. value interface{} // the proto-declared default value, or nil
  609. }
  610. // t is a struct type.
  611. func buildDefaultMessage(t reflect.Type) (dm defaultMessage) {
  612. sprop := GetProperties(t)
  613. for _, prop := range sprop.Prop {
  614. fi := sprop.tags[prop.Tag]
  615. ft := t.Field(fi).Type
  616. // nested messages
  617. if ft.Kind() == reflect.Ptr && ft.Elem().Kind() == reflect.Struct {
  618. dm.nested = append(dm.nested, fi)
  619. continue
  620. }
  621. sf := scalarField{
  622. index: fi,
  623. kind: ft.Elem().Kind(),
  624. }
  625. // scalar fields without defaults
  626. if prop.Default == "" {
  627. dm.scalars = append(dm.scalars, sf)
  628. continue
  629. }
  630. // a scalar field: either *T or []byte
  631. switch ft.Elem().Kind() {
  632. case reflect.Bool:
  633. x, err := strconv.ParseBool(prop.Default)
  634. if err != nil {
  635. log.Printf("proto: bad default bool %q: %v", prop.Default, err)
  636. continue
  637. }
  638. sf.value = x
  639. case reflect.Float32:
  640. x, err := strconv.ParseFloat(prop.Default, 32)
  641. if err != nil {
  642. log.Printf("proto: bad default float32 %q: %v", prop.Default, err)
  643. continue
  644. }
  645. sf.value = float32(x)
  646. case reflect.Float64:
  647. x, err := strconv.ParseFloat(prop.Default, 64)
  648. if err != nil {
  649. log.Printf("proto: bad default float64 %q: %v", prop.Default, err)
  650. continue
  651. }
  652. sf.value = x
  653. case reflect.Int32:
  654. x, err := strconv.ParseInt(prop.Default, 10, 32)
  655. if err != nil {
  656. log.Printf("proto: bad default int32 %q: %v", prop.Default, err)
  657. continue
  658. }
  659. sf.value = int32(x)
  660. case reflect.Int64:
  661. x, err := strconv.ParseInt(prop.Default, 10, 64)
  662. if err != nil {
  663. log.Printf("proto: bad default int64 %q: %v", prop.Default, err)
  664. continue
  665. }
  666. sf.value = x
  667. case reflect.String:
  668. sf.value = prop.Default
  669. case reflect.Uint8:
  670. // []byte (not *uint8)
  671. sf.value = []byte(prop.Default)
  672. case reflect.Uint32:
  673. x, err := strconv.ParseUint(prop.Default, 10, 32)
  674. if err != nil {
  675. log.Printf("proto: bad default uint32 %q: %v", prop.Default, err)
  676. continue
  677. }
  678. sf.value = uint32(x)
  679. case reflect.Uint64:
  680. x, err := strconv.ParseUint(prop.Default, 10, 64)
  681. if err != nil {
  682. log.Printf("proto: bad default uint64 %q: %v", prop.Default, err)
  683. continue
  684. }
  685. sf.value = x
  686. default:
  687. log.Printf("proto: unhandled def kind %v", ft.Elem().Kind())
  688. continue
  689. }
  690. dm.scalars = append(dm.scalars, sf)
  691. }
  692. return dm
  693. }