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.

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