You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

956 lines
24 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package binary
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "reflect"
  8. "sync"
  9. "time"
  10. . "github.com/tendermint/tendermint/common"
  11. )
  12. const (
  13. ReflectSliceChunk = 1024
  14. )
  15. type TypeInfo struct {
  16. Type reflect.Type // The type
  17. // If Type is kind reflect.Interface, is registered
  18. IsRegisteredInterface bool
  19. ByteToType map[byte]reflect.Type
  20. TypeToByte map[reflect.Type]byte
  21. // If Type is concrete
  22. Byte byte
  23. // If Type is kind reflect.Struct
  24. Fields []StructFieldInfo
  25. }
  26. type Options struct {
  27. JSONName string // (JSON) Corresponding JSON field name. (override with `json=""`)
  28. Varint bool // (Binary) Use length-prefixed encoding for (u)int*
  29. }
  30. func getOptionsFromField(field reflect.StructField) (skip bool, opts Options) {
  31. jsonName := field.Tag.Get("json")
  32. if jsonName == "-" {
  33. skip = true
  34. return
  35. } else if jsonName == "" {
  36. jsonName = field.Name
  37. }
  38. varint := false
  39. binTag := field.Tag.Get("binary")
  40. if binTag == "varint" { // TODO: extend
  41. varint = true
  42. }
  43. opts = Options{
  44. JSONName: jsonName,
  45. Varint: varint,
  46. }
  47. return
  48. }
  49. type StructFieldInfo struct {
  50. Index int // Struct field index
  51. Type reflect.Type // Struct field type
  52. Options // Encoding options
  53. }
  54. func (info StructFieldInfo) unpack() (int, reflect.Type, Options) {
  55. return info.Index, info.Type, info.Options
  56. }
  57. // e.g. If o is struct{Foo}{}, return is the Foo reflection type.
  58. func GetTypeFromStructDeclaration(o interface{}) reflect.Type {
  59. rt := reflect.TypeOf(o)
  60. if rt.NumField() != 1 {
  61. // SANITY CHECK
  62. panic("Unexpected number of fields in struct-wrapped declaration of type")
  63. }
  64. return rt.Field(0).Type
  65. }
  66. func SetByteForType(typeByte byte, rt reflect.Type) {
  67. typeInfo := GetTypeInfo(rt)
  68. if typeInfo.Byte != 0x00 && typeInfo.Byte != typeByte {
  69. // SANITY CHECK
  70. panic(Fmt("Type %v already registered with type byte %X", rt, typeByte))
  71. }
  72. typeInfo.Byte = typeByte
  73. // If pointer, we need to set it for the concrete type as well.
  74. if rt.Kind() == reflect.Ptr {
  75. SetByteForType(typeByte, rt.Elem())
  76. }
  77. }
  78. // Predeclaration of common types
  79. var (
  80. timeType = GetTypeFromStructDeclaration(struct{ time.Time }{})
  81. )
  82. const (
  83. rfc2822 = "Mon Jan 02 15:04:05 -0700 2006"
  84. )
  85. // NOTE: do not access typeInfos directly, but call GetTypeInfo()
  86. var typeInfosMtx sync.Mutex
  87. var typeInfos = map[reflect.Type]*TypeInfo{}
  88. func GetTypeInfo(rt reflect.Type) *TypeInfo {
  89. typeInfosMtx.Lock()
  90. defer typeInfosMtx.Unlock()
  91. info := typeInfos[rt]
  92. if info == nil {
  93. info = MakeTypeInfo(rt)
  94. typeInfos[rt] = info
  95. }
  96. return info
  97. }
  98. // For use with the RegisterInterface declaration
  99. type ConcreteType struct {
  100. O interface{}
  101. Byte byte
  102. }
  103. // Must use this to register an interface to properly decode the
  104. // underlying concrete type.
  105. func RegisterInterface(o interface{}, ctypes ...ConcreteType) *TypeInfo {
  106. it := GetTypeFromStructDeclaration(o)
  107. if it.Kind() != reflect.Interface {
  108. // SANITY CHECK
  109. panic("RegisterInterface expects an interface")
  110. }
  111. toType := make(map[byte]reflect.Type, 0)
  112. toByte := make(map[reflect.Type]byte, 0)
  113. for _, ctype := range ctypes {
  114. crt := reflect.TypeOf(ctype.O)
  115. typeByte := ctype.Byte
  116. SetByteForType(typeByte, crt)
  117. if typeByte == 0x00 {
  118. // SANITY CHECK
  119. panic(Fmt("Byte of 0x00 is reserved for nil (%v)", ctype))
  120. }
  121. if toType[typeByte] != nil {
  122. // SANITY CHECK
  123. panic(Fmt("Duplicate Byte for type %v and %v", ctype, toType[typeByte]))
  124. }
  125. toType[typeByte] = crt
  126. toByte[crt] = typeByte
  127. }
  128. typeInfo := &TypeInfo{
  129. Type: it,
  130. IsRegisteredInterface: true,
  131. ByteToType: toType,
  132. TypeToByte: toByte,
  133. }
  134. typeInfos[it] = typeInfo
  135. return typeInfo
  136. }
  137. func MakeTypeInfo(rt reflect.Type) *TypeInfo {
  138. info := &TypeInfo{Type: rt}
  139. // If struct, register field name options
  140. if rt.Kind() == reflect.Struct {
  141. numFields := rt.NumField()
  142. structFields := []StructFieldInfo{}
  143. for i := 0; i < numFields; i++ {
  144. field := rt.Field(i)
  145. if field.PkgPath != "" {
  146. continue
  147. }
  148. skip, opts := getOptionsFromField(field)
  149. if skip {
  150. continue
  151. }
  152. structFields = append(structFields, StructFieldInfo{
  153. Index: i,
  154. Type: field.Type,
  155. Options: opts,
  156. })
  157. }
  158. info.Fields = structFields
  159. }
  160. return info
  161. }
  162. // Contract: Caller must ensure that rt is supported
  163. // (e.g. is recursively composed of supported native types, and structs and slices.)
  164. func readReflectBinary(rv reflect.Value, rt reflect.Type, opts Options, r io.Reader, n *int64, err *error) {
  165. // Get typeInfo
  166. typeInfo := GetTypeInfo(rt)
  167. if rt.Kind() == reflect.Interface {
  168. if !typeInfo.IsRegisteredInterface {
  169. // There's no way we can read such a thing.
  170. *err = errors.New(Fmt("Cannot read unregistered interface type %v", rt))
  171. return
  172. }
  173. typeByte := ReadByte(r, n, err)
  174. if *err != nil {
  175. return
  176. }
  177. if typeByte == 0x00 {
  178. return // nil
  179. }
  180. crt, ok := typeInfo.ByteToType[typeByte]
  181. if !ok {
  182. *err = errors.New(Fmt("Unexpected type byte %X for type %v", typeByte, rt))
  183. return
  184. }
  185. crv := reflect.New(crt).Elem()
  186. r = NewPrefixedReader([]byte{typeByte}, r)
  187. readReflectBinary(crv, crt, opts, r, n, err)
  188. rv.Set(crv) // NOTE: orig rv is ignored.
  189. return
  190. }
  191. if rt.Kind() == reflect.Ptr {
  192. typeByte := ReadByte(r, n, err)
  193. if *err != nil {
  194. return
  195. }
  196. if typeByte == 0x00 {
  197. return // nil
  198. }
  199. // Create new if rv is nil.
  200. if rv.IsNil() {
  201. newRv := reflect.New(rt.Elem())
  202. rv.Set(newRv)
  203. rv = newRv
  204. }
  205. // Dereference pointer
  206. rv, rt = rv.Elem(), rt.Elem()
  207. typeInfo = GetTypeInfo(rt)
  208. if typeInfo.Byte != 0x00 {
  209. r = NewPrefixedReader([]byte{typeByte}, r)
  210. } else if typeByte != 0x01 {
  211. *err = errors.New(Fmt("Unexpected type byte %X for ptr of untyped thing", typeByte))
  212. return
  213. }
  214. // continue...
  215. }
  216. // Read Byte prefix
  217. if typeInfo.Byte != 0x00 {
  218. typeByte := ReadByte(r, n, err)
  219. if typeByte != typeInfo.Byte {
  220. *err = errors.New(Fmt("Expected Byte of %X but got %X", typeInfo.Byte, typeByte))
  221. return
  222. }
  223. }
  224. switch rt.Kind() {
  225. case reflect.Array:
  226. elemRt := rt.Elem()
  227. length := rt.Len()
  228. if elemRt.Kind() == reflect.Uint8 {
  229. // Special case: Bytearrays
  230. buf := make([]byte, length)
  231. ReadFull(buf, r, n, err)
  232. if *err != nil {
  233. return
  234. }
  235. log.Info("Read bytearray", "bytes", buf)
  236. reflect.Copy(rv, reflect.ValueOf(buf))
  237. } else {
  238. for i := 0; i < length; i++ {
  239. elemRv := rv.Index(i)
  240. readReflectBinary(elemRv, elemRt, opts, r, n, err)
  241. if *err != nil {
  242. return
  243. }
  244. if MaxBinaryReadSize < *n {
  245. *err = ErrBinaryReadSizeOverflow
  246. return
  247. }
  248. }
  249. log.Info(Fmt("Read %v-array", elemRt), "length", length)
  250. }
  251. case reflect.Slice:
  252. elemRt := rt.Elem()
  253. if elemRt.Kind() == reflect.Uint8 {
  254. // Special case: Byteslices
  255. byteslice := ReadByteSlice(r, n, err)
  256. log.Info("Read byteslice", "bytes", byteslice)
  257. rv.Set(reflect.ValueOf(byteslice))
  258. } else {
  259. var sliceRv reflect.Value
  260. // Read length
  261. length := ReadVarint(r, n, err)
  262. log.Info(Fmt("Read length: %v", length))
  263. sliceRv = reflect.MakeSlice(rt, 0, 0)
  264. // read one ReflectSliceChunk at a time and append
  265. for i := 0; i*ReflectSliceChunk < length; i++ {
  266. l := MinInt(ReflectSliceChunk, length-i*ReflectSliceChunk)
  267. tmpSliceRv := reflect.MakeSlice(rt, l, l)
  268. for j := 0; j < l; j++ {
  269. elemRv := tmpSliceRv.Index(j)
  270. readReflectBinary(elemRv, elemRt, opts, r, n, err)
  271. if *err != nil {
  272. return
  273. }
  274. if MaxBinaryReadSize < *n {
  275. *err = ErrBinaryReadSizeOverflow
  276. return
  277. }
  278. }
  279. sliceRv = reflect.AppendSlice(sliceRv, tmpSliceRv)
  280. }
  281. rv.Set(sliceRv)
  282. }
  283. case reflect.Struct:
  284. if rt == timeType {
  285. // Special case: time.Time
  286. t := ReadTime(r, n, err)
  287. log.Info(Fmt("Read time: %v", t))
  288. rv.Set(reflect.ValueOf(t))
  289. } else {
  290. for _, fieldInfo := range typeInfo.Fields {
  291. i, fieldType, opts := fieldInfo.unpack()
  292. fieldRv := rv.Field(i)
  293. readReflectBinary(fieldRv, fieldType, opts, r, n, err)
  294. }
  295. }
  296. case reflect.String:
  297. str := ReadString(r, n, err)
  298. log.Info(Fmt("Read string: %v", str))
  299. rv.SetString(str)
  300. case reflect.Int64:
  301. if opts.Varint {
  302. num := ReadVarint(r, n, err)
  303. log.Info(Fmt("Read num: %v", num))
  304. rv.SetInt(int64(num))
  305. } else {
  306. num := ReadInt64(r, n, err)
  307. log.Info(Fmt("Read num: %v", num))
  308. rv.SetInt(int64(num))
  309. }
  310. case reflect.Int32:
  311. num := ReadUint32(r, n, err)
  312. log.Info(Fmt("Read num: %v", num))
  313. rv.SetInt(int64(num))
  314. case reflect.Int16:
  315. num := ReadUint16(r, n, err)
  316. log.Info(Fmt("Read num: %v", num))
  317. rv.SetInt(int64(num))
  318. case reflect.Int8:
  319. num := ReadUint8(r, n, err)
  320. log.Info(Fmt("Read num: %v", num))
  321. rv.SetInt(int64(num))
  322. case reflect.Int:
  323. num := ReadVarint(r, n, err)
  324. log.Info(Fmt("Read num: %v", num))
  325. rv.SetInt(int64(num))
  326. case reflect.Uint64:
  327. if opts.Varint {
  328. num := ReadVarint(r, n, err)
  329. log.Info(Fmt("Read num: %v", num))
  330. rv.SetUint(uint64(num))
  331. } else {
  332. num := ReadUint64(r, n, err)
  333. log.Info(Fmt("Read num: %v", num))
  334. rv.SetUint(uint64(num))
  335. }
  336. case reflect.Uint32:
  337. num := ReadUint32(r, n, err)
  338. log.Info(Fmt("Read num: %v", num))
  339. rv.SetUint(uint64(num))
  340. case reflect.Uint16:
  341. num := ReadUint16(r, n, err)
  342. log.Info(Fmt("Read num: %v", num))
  343. rv.SetUint(uint64(num))
  344. case reflect.Uint8:
  345. num := ReadUint8(r, n, err)
  346. log.Info(Fmt("Read num: %v", num))
  347. rv.SetUint(uint64(num))
  348. case reflect.Uint:
  349. num := ReadVarint(r, n, err)
  350. log.Info(Fmt("Read num: %v", num))
  351. rv.SetUint(uint64(num))
  352. case reflect.Bool:
  353. num := ReadUint8(r, n, err)
  354. log.Info(Fmt("Read bool: %v", num))
  355. rv.SetBool(num > 0)
  356. default:
  357. // SANITY CHECK
  358. panic(Fmt("Unknown field type %v", rt.Kind()))
  359. }
  360. }
  361. // rv: the reflection value of the thing to write
  362. // rt: the type of rv as declared in the container, not necessarily rv.Type().
  363. func writeReflectBinary(rv reflect.Value, rt reflect.Type, opts Options, w io.Writer, n *int64, err *error) {
  364. // Get typeInfo
  365. typeInfo := GetTypeInfo(rt)
  366. if rt.Kind() == reflect.Interface {
  367. if rv.IsNil() {
  368. // XXX ensure that typeByte 0 is reserved.
  369. WriteByte(0x00, w, n, err)
  370. return
  371. }
  372. crv := rv.Elem() // concrete reflection value
  373. crt := crv.Type() // concrete reflection type
  374. if typeInfo.IsRegisteredInterface {
  375. // See if the crt is registered.
  376. // If so, we're more restrictive.
  377. _, ok := typeInfo.TypeToByte[crt]
  378. if !ok {
  379. switch crt.Kind() {
  380. case reflect.Ptr:
  381. *err = errors.New(Fmt("Unexpected pointer type %v. Was it registered as a value receiver rather than as a pointer receiver?", crt))
  382. case reflect.Struct:
  383. *err = errors.New(Fmt("Unexpected struct type %v. Was it registered as a pointer receiver rather than as a value receiver?", crt))
  384. default:
  385. *err = errors.New(Fmt("Unexpected type %v.", crt))
  386. }
  387. return
  388. }
  389. } else {
  390. // We support writing unsafely for convenience.
  391. }
  392. // We don't have to write the typeByte here,
  393. // the writeReflectBinary() call below will write it.
  394. writeReflectBinary(crv, crt, opts, w, n, err)
  395. return
  396. }
  397. if rt.Kind() == reflect.Ptr {
  398. // Dereference pointer
  399. rv, rt = rv.Elem(), rt.Elem()
  400. typeInfo = GetTypeInfo(rt)
  401. if !rv.IsValid() {
  402. // For better compatibility with other languages,
  403. // as far as tendermint/binary is concerned,
  404. // pointers to nil values are the same as nil.
  405. WriteByte(0x00, w, n, err)
  406. return
  407. }
  408. if typeInfo.Byte == 0x00 {
  409. WriteByte(0x01, w, n, err)
  410. // continue...
  411. } else {
  412. // continue...
  413. }
  414. }
  415. // Write type byte
  416. if typeInfo.Byte != 0x00 {
  417. WriteByte(typeInfo.Byte, w, n, err)
  418. }
  419. // All other types
  420. switch rt.Kind() {
  421. case reflect.Array:
  422. elemRt := rt.Elem()
  423. length := rt.Len()
  424. if elemRt.Kind() == reflect.Uint8 {
  425. // Special case: Bytearrays
  426. if rv.CanAddr() {
  427. byteslice := rv.Slice(0, length).Bytes()
  428. WriteTo(byteslice, w, n, err)
  429. } else {
  430. buf := make([]byte, length)
  431. reflect.Copy(reflect.ValueOf(buf), rv)
  432. WriteTo(buf, w, n, err)
  433. }
  434. } else {
  435. // Write elems
  436. for i := 0; i < length; i++ {
  437. elemRv := rv.Index(i)
  438. writeReflectBinary(elemRv, elemRt, opts, w, n, err)
  439. }
  440. }
  441. case reflect.Slice:
  442. elemRt := rt.Elem()
  443. if elemRt.Kind() == reflect.Uint8 {
  444. // Special case: Byteslices
  445. byteslice := rv.Bytes()
  446. WriteByteSlice(byteslice, w, n, err)
  447. } else {
  448. // Write length
  449. length := rv.Len()
  450. WriteVarint(length, w, n, err)
  451. // Write elems
  452. for i := 0; i < length; i++ {
  453. elemRv := rv.Index(i)
  454. writeReflectBinary(elemRv, elemRt, opts, w, n, err)
  455. }
  456. }
  457. case reflect.Struct:
  458. if rt == timeType {
  459. // Special case: time.Time
  460. WriteTime(rv.Interface().(time.Time), w, n, err)
  461. } else {
  462. for _, fieldInfo := range typeInfo.Fields {
  463. i, fieldType, opts := fieldInfo.unpack()
  464. fieldRv := rv.Field(i)
  465. writeReflectBinary(fieldRv, fieldType, opts, w, n, err)
  466. }
  467. }
  468. case reflect.String:
  469. WriteString(rv.String(), w, n, err)
  470. case reflect.Int64:
  471. if opts.Varint {
  472. WriteVarint(int(rv.Int()), w, n, err)
  473. } else {
  474. WriteInt64(rv.Int(), w, n, err)
  475. }
  476. case reflect.Int32:
  477. WriteInt32(int32(rv.Int()), w, n, err)
  478. case reflect.Int16:
  479. WriteInt16(int16(rv.Int()), w, n, err)
  480. case reflect.Int8:
  481. WriteInt8(int8(rv.Int()), w, n, err)
  482. case reflect.Int:
  483. WriteVarint(int(rv.Int()), w, n, err)
  484. case reflect.Uint64:
  485. if opts.Varint {
  486. WriteUvarint(uint(rv.Uint()), w, n, err)
  487. } else {
  488. WriteUint64(rv.Uint(), w, n, err)
  489. }
  490. case reflect.Uint32:
  491. WriteUint32(uint32(rv.Uint()), w, n, err)
  492. case reflect.Uint16:
  493. WriteUint16(uint16(rv.Uint()), w, n, err)
  494. case reflect.Uint8:
  495. WriteUint8(uint8(rv.Uint()), w, n, err)
  496. case reflect.Uint:
  497. WriteUvarint(uint(rv.Uint()), w, n, err)
  498. case reflect.Bool:
  499. if rv.Bool() {
  500. WriteUint8(uint8(1), w, n, err)
  501. } else {
  502. WriteUint8(uint8(0), w, n, err)
  503. }
  504. default:
  505. // SANITY CHECK
  506. panic(Fmt("Unknown field type %v", rt.Kind()))
  507. }
  508. }
  509. //-----------------------------------------------------------------------------
  510. func readByteJSON(o interface{}) (typeByte byte, rest interface{}, err error) {
  511. oSlice, ok := o.([]interface{})
  512. if !ok {
  513. err = errors.New(Fmt("Expected type [Byte,?] but got type %v", reflect.TypeOf(o)))
  514. return
  515. }
  516. if len(oSlice) != 2 {
  517. err = errors.New(Fmt("Expected [Byte,?] len 2 but got len %v", len(oSlice)))
  518. return
  519. }
  520. typeByte_, ok := oSlice[0].(float64)
  521. typeByte = byte(typeByte_)
  522. rest = oSlice[1]
  523. return
  524. }
  525. // Contract: Caller must ensure that rt is supported
  526. // (e.g. is recursively composed of supported native types, and structs and slices.)
  527. func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *error) {
  528. // Get typeInfo
  529. typeInfo := GetTypeInfo(rt)
  530. if rt.Kind() == reflect.Interface {
  531. if !typeInfo.IsRegisteredInterface {
  532. // There's no way we can read such a thing.
  533. *err = errors.New(Fmt("Cannot read unregistered interface type %v", rt))
  534. return
  535. }
  536. if o == nil {
  537. return // nil
  538. }
  539. typeByte, _, err_ := readByteJSON(o)
  540. if err_ != nil {
  541. *err = err_
  542. return
  543. }
  544. crt, ok := typeInfo.ByteToType[typeByte]
  545. if !ok {
  546. *err = errors.New(Fmt("Byte %X not registered for interface %v", typeByte, rt))
  547. return
  548. }
  549. crv := reflect.New(crt).Elem()
  550. readReflectJSON(crv, crt, o, err)
  551. rv.Set(crv) // NOTE: orig rv is ignored.
  552. return
  553. }
  554. if rt.Kind() == reflect.Ptr {
  555. if o == nil {
  556. return // nil
  557. }
  558. // Create new struct if rv is nil.
  559. if rv.IsNil() {
  560. newRv := reflect.New(rt.Elem())
  561. rv.Set(newRv)
  562. rv = newRv
  563. }
  564. // Dereference pointer
  565. rv, rt = rv.Elem(), rt.Elem()
  566. typeInfo = GetTypeInfo(rt)
  567. // continue...
  568. }
  569. // Read Byte prefix
  570. if typeInfo.Byte != 0x00 {
  571. typeByte, rest, err_ := readByteJSON(o)
  572. if err_ != nil {
  573. *err = err_
  574. return
  575. }
  576. if typeByte != typeInfo.Byte {
  577. *err = errors.New(Fmt("Expected Byte of %X but got %X", typeInfo.Byte, byte(typeByte)))
  578. return
  579. }
  580. o = rest
  581. }
  582. switch rt.Kind() {
  583. case reflect.Array:
  584. elemRt := rt.Elem()
  585. length := rt.Len()
  586. if elemRt.Kind() == reflect.Uint8 {
  587. // Special case: Bytearrays
  588. oString, ok := o.(string)
  589. if !ok {
  590. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  591. return
  592. }
  593. buf, err_ := hex.DecodeString(oString)
  594. if err_ != nil {
  595. *err = err_
  596. return
  597. }
  598. if len(buf) != length {
  599. *err = errors.New(Fmt("Expected bytearray of length %v but got %v", length, len(buf)))
  600. return
  601. }
  602. log.Info("Read bytearray", "bytes", buf)
  603. reflect.Copy(rv, reflect.ValueOf(buf))
  604. } else {
  605. oSlice, ok := o.([]interface{})
  606. if !ok {
  607. *err = errors.New(Fmt("Expected array of %v but got type %v", rt, reflect.TypeOf(o)))
  608. return
  609. }
  610. if len(oSlice) != length {
  611. *err = errors.New(Fmt("Expected array of length %v but got %v", length, len(oSlice)))
  612. return
  613. }
  614. for i := 0; i < length; i++ {
  615. elemRv := rv.Index(i)
  616. readReflectJSON(elemRv, elemRt, oSlice[i], err)
  617. }
  618. log.Info(Fmt("Read %v-array", elemRt), "length", length)
  619. }
  620. case reflect.Slice:
  621. elemRt := rt.Elem()
  622. if elemRt.Kind() == reflect.Uint8 {
  623. // Special case: Byteslices
  624. oString, ok := o.(string)
  625. if !ok {
  626. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  627. return
  628. }
  629. byteslice, err_ := hex.DecodeString(oString)
  630. if err_ != nil {
  631. *err = err_
  632. return
  633. }
  634. log.Info("Read byteslice", "bytes", byteslice)
  635. rv.Set(reflect.ValueOf(byteslice))
  636. } else {
  637. // Read length
  638. oSlice, ok := o.([]interface{})
  639. if !ok {
  640. *err = errors.New(Fmt("Expected array of %v but got type %v", rt, reflect.TypeOf(o)))
  641. return
  642. }
  643. length := len(oSlice)
  644. log.Info(Fmt("Read length: %v", length))
  645. sliceRv := reflect.MakeSlice(rt, length, length)
  646. // Read elems
  647. for i := 0; i < length; i++ {
  648. elemRv := sliceRv.Index(i)
  649. readReflectJSON(elemRv, elemRt, oSlice[i], err)
  650. }
  651. rv.Set(sliceRv)
  652. }
  653. case reflect.Struct:
  654. if rt == timeType {
  655. // Special case: time.Time
  656. str, ok := o.(string)
  657. if !ok {
  658. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  659. return
  660. }
  661. log.Info(Fmt("Read time: %v", str))
  662. t, err_ := time.Parse(rfc2822, str)
  663. if err_ != nil {
  664. *err = err_
  665. return
  666. }
  667. rv.Set(reflect.ValueOf(t))
  668. } else {
  669. oMap, ok := o.(map[string]interface{})
  670. if !ok {
  671. *err = errors.New(Fmt("Expected map but got type %v", reflect.TypeOf(o)))
  672. return
  673. }
  674. // TODO: ensure that all fields are set?
  675. // TODO: disallow unknown oMap fields?
  676. for _, fieldInfo := range typeInfo.Fields {
  677. i, fieldType, opts := fieldInfo.unpack()
  678. value, ok := oMap[opts.JSONName]
  679. if !ok {
  680. continue // Skip missing fields.
  681. }
  682. fieldRv := rv.Field(i)
  683. readReflectJSON(fieldRv, fieldType, value, err)
  684. }
  685. }
  686. case reflect.String:
  687. str, ok := o.(string)
  688. if !ok {
  689. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  690. return
  691. }
  692. log.Info(Fmt("Read string: %v", str))
  693. rv.SetString(str)
  694. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  695. num, ok := o.(float64)
  696. if !ok {
  697. *err = errors.New(Fmt("Expected numeric but got type %v", reflect.TypeOf(o)))
  698. return
  699. }
  700. log.Info(Fmt("Read num: %v", num))
  701. rv.SetInt(int64(num))
  702. case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
  703. num, ok := o.(float64)
  704. if !ok {
  705. *err = errors.New(Fmt("Expected numeric but got type %v", reflect.TypeOf(o)))
  706. return
  707. }
  708. if num < 0 {
  709. *err = errors.New(Fmt("Expected unsigned numeric but got %v", num))
  710. return
  711. }
  712. log.Info(Fmt("Read num: %v", num))
  713. rv.SetUint(uint64(num))
  714. case reflect.Bool:
  715. bl, ok := o.(bool)
  716. if !ok {
  717. *err = errors.New(Fmt("Expected boolean but got type %v", reflect.TypeOf(o)))
  718. return
  719. }
  720. log.Info(Fmt("Read boolean: %v", bl))
  721. rv.SetBool(bl)
  722. default:
  723. // SANITY CHECK
  724. panic(Fmt("Unknown field type %v", rt.Kind()))
  725. }
  726. }
  727. func writeReflectJSON(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64, err *error) {
  728. log.Info(Fmt("writeReflectJSON(%v, %v, %v, %v, %v)", rv, rt, w, n, err))
  729. // Get typeInfo
  730. typeInfo := GetTypeInfo(rt)
  731. if rt.Kind() == reflect.Interface {
  732. if rv.IsNil() {
  733. // XXX ensure that typeByte 0 is reserved.
  734. WriteTo([]byte("null"), w, n, err)
  735. return
  736. }
  737. crv := rv.Elem() // concrete reflection value
  738. crt := crv.Type() // concrete reflection type
  739. if typeInfo.IsRegisteredInterface {
  740. // See if the crt is registered.
  741. // If so, we're more restrictive.
  742. _, ok := typeInfo.TypeToByte[crt]
  743. if !ok {
  744. switch crt.Kind() {
  745. case reflect.Ptr:
  746. *err = errors.New(Fmt("Unexpected pointer type %v. Was it registered as a value receiver rather than as a pointer receiver?", crt))
  747. case reflect.Struct:
  748. *err = errors.New(Fmt("Unexpected struct type %v. Was it registered as a pointer receiver rather than as a value receiver?", crt))
  749. default:
  750. *err = errors.New(Fmt("Unexpected type %v.", crt))
  751. }
  752. return
  753. }
  754. } else {
  755. // We support writing unsafely for convenience.
  756. }
  757. // We don't have to write the typeByte here,
  758. // the writeReflectJSON() call below will write it.
  759. writeReflectJSON(crv, crt, w, n, err)
  760. return
  761. }
  762. if rt.Kind() == reflect.Ptr {
  763. // Dereference pointer
  764. rv, rt = rv.Elem(), rt.Elem()
  765. typeInfo = GetTypeInfo(rt)
  766. if !rv.IsValid() {
  767. // For better compatibility with other languages,
  768. // as far as tendermint/binary is concerned,
  769. // pointers to nil values are the same as nil.
  770. WriteTo([]byte("null"), w, n, err)
  771. return
  772. }
  773. // continue...
  774. }
  775. // Write Byte
  776. if typeInfo.Byte != 0x00 {
  777. WriteTo([]byte(Fmt("[%v,", typeInfo.Byte)), w, n, err)
  778. defer WriteTo([]byte("]"), w, n, err)
  779. }
  780. // All other types
  781. switch rt.Kind() {
  782. case reflect.Array:
  783. elemRt := rt.Elem()
  784. length := rt.Len()
  785. if elemRt.Kind() == reflect.Uint8 {
  786. // Special case: Bytearray
  787. bytearray := reflect.ValueOf(make([]byte, length))
  788. reflect.Copy(bytearray, rv)
  789. WriteTo([]byte(Fmt("\"%X\"", bytearray.Interface())), w, n, err)
  790. } else {
  791. WriteTo([]byte("["), w, n, err)
  792. // Write elems
  793. for i := 0; i < length; i++ {
  794. elemRv := rv.Index(i)
  795. writeReflectJSON(elemRv, elemRt, w, n, err)
  796. if i < length-1 {
  797. WriteTo([]byte(","), w, n, err)
  798. }
  799. }
  800. WriteTo([]byte("]"), w, n, err)
  801. }
  802. case reflect.Slice:
  803. elemRt := rt.Elem()
  804. if elemRt.Kind() == reflect.Uint8 {
  805. // Special case: Byteslices
  806. byteslice := rv.Bytes()
  807. WriteTo([]byte(Fmt("\"%X\"", byteslice)), w, n, err)
  808. } else {
  809. WriteTo([]byte("["), w, n, err)
  810. // Write elems
  811. length := rv.Len()
  812. for i := 0; i < length; i++ {
  813. elemRv := rv.Index(i)
  814. writeReflectJSON(elemRv, elemRt, w, n, err)
  815. if i < length-1 {
  816. WriteTo([]byte(","), w, n, err)
  817. }
  818. }
  819. WriteTo([]byte("]"), w, n, err)
  820. }
  821. case reflect.Struct:
  822. if rt == timeType {
  823. // Special case: time.Time
  824. t := rv.Interface().(time.Time)
  825. str := t.Format(rfc2822)
  826. jsonBytes, err_ := json.Marshal(str)
  827. if err_ != nil {
  828. *err = err_
  829. return
  830. }
  831. WriteTo(jsonBytes, w, n, err)
  832. } else {
  833. WriteTo([]byte("{"), w, n, err)
  834. wroteField := false
  835. for _, fieldInfo := range typeInfo.Fields {
  836. i, fieldType, opts := fieldInfo.unpack()
  837. fieldRv := rv.Field(i)
  838. if wroteField {
  839. WriteTo([]byte(","), w, n, err)
  840. } else {
  841. wroteField = true
  842. }
  843. WriteTo([]byte(Fmt("\"%v\":", opts.JSONName)), w, n, err)
  844. writeReflectJSON(fieldRv, fieldType, w, n, err)
  845. }
  846. WriteTo([]byte("}"), w, n, err)
  847. }
  848. case reflect.String:
  849. fallthrough
  850. case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
  851. fallthrough
  852. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  853. fallthrough
  854. case reflect.Bool:
  855. jsonBytes, err_ := json.Marshal(rv.Interface())
  856. if err_ != nil {
  857. *err = err_
  858. return
  859. }
  860. WriteTo(jsonBytes, w, n, err)
  861. default:
  862. // SANITY CHECK
  863. panic(Fmt("Unknown field type %v", rt.Kind()))
  864. }
  865. }