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.

954 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
  1. package wire
  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. iso8601 = "2006-01-02T15:04:05.000Z" // forced microseconds
  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 for registered interface %v. "+
  376. "Was it registered as a value receiver rather than as a pointer receiver?", crt, rt.Name()))
  377. case reflect.Struct:
  378. *err = errors.New(Fmt("Unexpected struct type %v for registered interface %v. "+
  379. "Was it registered as a pointer receiver rather than as a value receiver?", crt, rt.Name()))
  380. default:
  381. *err = errors.New(Fmt("Unexpected type %v for registered interface %v. "+
  382. "If this is intentional, please register it.", crt, rt.Name()))
  383. }
  384. return
  385. }
  386. } else {
  387. // We support writing unsafely for convenience.
  388. }
  389. // We don't have to write the typeByte here,
  390. // the writeReflectBinary() call below will write it.
  391. writeReflectBinary(crv, crt, opts, w, n, err)
  392. return
  393. }
  394. if rt.Kind() == reflect.Ptr {
  395. // Dereference pointer
  396. rv, rt = rv.Elem(), rt.Elem()
  397. typeInfo = GetTypeInfo(rt)
  398. if !rv.IsValid() {
  399. // For better compatibility with other languages,
  400. // as far as tendermint/wire is concerned,
  401. // pointers to nil values are the same as nil.
  402. WriteByte(0x00, w, n, err)
  403. return
  404. }
  405. if typeInfo.Byte == 0x00 {
  406. WriteByte(0x01, w, n, err)
  407. // continue...
  408. } else {
  409. // continue...
  410. }
  411. }
  412. // Write type byte
  413. if typeInfo.Byte != 0x00 {
  414. WriteByte(typeInfo.Byte, w, n, err)
  415. }
  416. // All other types
  417. switch rt.Kind() {
  418. case reflect.Array:
  419. elemRt := rt.Elem()
  420. length := rt.Len()
  421. if elemRt.Kind() == reflect.Uint8 {
  422. // Special case: Bytearrays
  423. if rv.CanAddr() {
  424. byteslice := rv.Slice(0, length).Bytes()
  425. WriteTo(byteslice, w, n, err)
  426. } else {
  427. buf := make([]byte, length)
  428. reflect.Copy(reflect.ValueOf(buf), rv)
  429. WriteTo(buf, w, n, err)
  430. }
  431. } else {
  432. // Write elems
  433. for i := 0; i < length; i++ {
  434. elemRv := rv.Index(i)
  435. writeReflectBinary(elemRv, elemRt, opts, w, n, err)
  436. }
  437. }
  438. case reflect.Slice:
  439. elemRt := rt.Elem()
  440. if elemRt.Kind() == reflect.Uint8 {
  441. // Special case: Byteslices
  442. byteslice := rv.Bytes()
  443. WriteByteSlice(byteslice, w, n, err)
  444. } else {
  445. // Write length
  446. length := rv.Len()
  447. WriteVarint(length, w, n, err)
  448. // Write elems
  449. for i := 0; i < length; i++ {
  450. elemRv := rv.Index(i)
  451. writeReflectBinary(elemRv, elemRt, opts, w, n, err)
  452. }
  453. }
  454. case reflect.Struct:
  455. if rt == timeType {
  456. // Special case: time.Time
  457. WriteTime(rv.Interface().(time.Time), w, n, err)
  458. } else {
  459. for _, fieldInfo := range typeInfo.Fields {
  460. i, fieldType, opts := fieldInfo.unpack()
  461. fieldRv := rv.Field(i)
  462. writeReflectBinary(fieldRv, fieldType, opts, w, n, err)
  463. }
  464. }
  465. case reflect.String:
  466. WriteString(rv.String(), w, n, err)
  467. case reflect.Int64:
  468. if opts.Varint {
  469. WriteVarint(int(rv.Int()), w, n, err)
  470. } else {
  471. WriteInt64(rv.Int(), w, n, err)
  472. }
  473. case reflect.Int32:
  474. WriteInt32(int32(rv.Int()), w, n, err)
  475. case reflect.Int16:
  476. WriteInt16(int16(rv.Int()), w, n, err)
  477. case reflect.Int8:
  478. WriteInt8(int8(rv.Int()), w, n, err)
  479. case reflect.Int:
  480. WriteVarint(int(rv.Int()), w, n, err)
  481. case reflect.Uint64:
  482. if opts.Varint {
  483. WriteUvarint(uint(rv.Uint()), w, n, err)
  484. } else {
  485. WriteUint64(rv.Uint(), w, n, err)
  486. }
  487. case reflect.Uint32:
  488. WriteUint32(uint32(rv.Uint()), w, n, err)
  489. case reflect.Uint16:
  490. WriteUint16(uint16(rv.Uint()), w, n, err)
  491. case reflect.Uint8:
  492. WriteUint8(uint8(rv.Uint()), w, n, err)
  493. case reflect.Uint:
  494. WriteUvarint(uint(rv.Uint()), w, n, err)
  495. case reflect.Bool:
  496. if rv.Bool() {
  497. WriteUint8(uint8(1), w, n, err)
  498. } else {
  499. WriteUint8(uint8(0), w, n, err)
  500. }
  501. default:
  502. PanicSanity(Fmt("Unknown field type %v", rt.Kind()))
  503. }
  504. }
  505. //-----------------------------------------------------------------------------
  506. func readByteJSON(o interface{}) (typeByte byte, rest interface{}, err error) {
  507. oSlice, ok := o.([]interface{})
  508. if !ok {
  509. err = errors.New(Fmt("Expected type [Byte,?] but got type %v", reflect.TypeOf(o)))
  510. return
  511. }
  512. if len(oSlice) != 2 {
  513. err = errors.New(Fmt("Expected [Byte,?] len 2 but got len %v", len(oSlice)))
  514. return
  515. }
  516. typeByte_, ok := oSlice[0].(float64)
  517. typeByte = byte(typeByte_)
  518. rest = oSlice[1]
  519. return
  520. }
  521. // Contract: Caller must ensure that rt is supported
  522. // (e.g. is recursively composed of supported native types, and structs and slices.)
  523. // rv and rt refer to the object we're unmarhsaling into, whereas o is the result of naiive json unmarshal (map[string]interface{})
  524. func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *error) {
  525. // Get typeInfo
  526. typeInfo := GetTypeInfo(rt)
  527. if rt.Kind() == reflect.Interface {
  528. if !typeInfo.IsRegisteredInterface {
  529. // There's no way we can read such a thing.
  530. *err = errors.New(Fmt("Cannot read unregistered interface type %v", rt))
  531. return
  532. }
  533. if o == nil {
  534. return // nil
  535. }
  536. typeByte, _, err_ := readByteJSON(o)
  537. if err_ != nil {
  538. *err = err_
  539. return
  540. }
  541. crt, ok := typeInfo.ByteToType[typeByte]
  542. if !ok {
  543. *err = errors.New(Fmt("Byte %X not registered for interface %v", typeByte, rt))
  544. return
  545. }
  546. crv := reflect.New(crt).Elem()
  547. readReflectJSON(crv, crt, o, err)
  548. rv.Set(crv) // NOTE: orig rv is ignored.
  549. return
  550. }
  551. if rt.Kind() == reflect.Ptr {
  552. if o == nil {
  553. return // nil
  554. }
  555. // Create new struct if rv is nil.
  556. if rv.IsNil() {
  557. newRv := reflect.New(rt.Elem())
  558. rv.Set(newRv)
  559. rv = newRv
  560. }
  561. // Dereference pointer
  562. rv, rt = rv.Elem(), rt.Elem()
  563. typeInfo = GetTypeInfo(rt)
  564. // continue...
  565. }
  566. // Read Byte prefix
  567. if typeInfo.Byte != 0x00 {
  568. typeByte, rest, err_ := readByteJSON(o)
  569. if err_ != nil {
  570. *err = err_
  571. return
  572. }
  573. if typeByte != typeInfo.Byte {
  574. *err = errors.New(Fmt("Expected Byte of %X but got %X", typeInfo.Byte, byte(typeByte)))
  575. return
  576. }
  577. o = rest
  578. }
  579. switch rt.Kind() {
  580. case reflect.Array:
  581. elemRt := rt.Elem()
  582. length := rt.Len()
  583. if elemRt.Kind() == reflect.Uint8 {
  584. // Special case: Bytearrays
  585. oString, ok := o.(string)
  586. if !ok {
  587. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  588. return
  589. }
  590. buf, err_ := hex.DecodeString(oString)
  591. if err_ != nil {
  592. *err = err_
  593. return
  594. }
  595. if len(buf) != length {
  596. *err = errors.New(Fmt("Expected bytearray of length %v but got %v", length, len(buf)))
  597. return
  598. }
  599. log.Info("Read bytearray", "bytes", buf)
  600. reflect.Copy(rv, reflect.ValueOf(buf))
  601. } else {
  602. oSlice, ok := o.([]interface{})
  603. if !ok {
  604. *err = errors.New(Fmt("Expected array of %v but got type %v", rt, reflect.TypeOf(o)))
  605. return
  606. }
  607. if len(oSlice) != length {
  608. *err = errors.New(Fmt("Expected array of length %v but got %v", length, len(oSlice)))
  609. return
  610. }
  611. for i := 0; i < length; i++ {
  612. elemRv := rv.Index(i)
  613. readReflectJSON(elemRv, elemRt, oSlice[i], err)
  614. }
  615. log.Info(Fmt("Read %v-array", elemRt), "length", length)
  616. }
  617. case reflect.Slice:
  618. elemRt := rt.Elem()
  619. if elemRt.Kind() == reflect.Uint8 {
  620. // Special case: Byteslices
  621. oString, ok := o.(string)
  622. if !ok {
  623. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  624. return
  625. }
  626. byteslice, err_ := hex.DecodeString(oString)
  627. if err_ != nil {
  628. *err = err_
  629. return
  630. }
  631. log.Info("Read byteslice", "bytes", byteslice)
  632. rv.Set(reflect.ValueOf(byteslice))
  633. } else {
  634. // Read length
  635. oSlice, ok := o.([]interface{})
  636. if !ok {
  637. *err = errors.New(Fmt("Expected array of %v but got type %v", rt, reflect.TypeOf(o)))
  638. return
  639. }
  640. length := len(oSlice)
  641. log.Info(Fmt("Read length: %v", length))
  642. sliceRv := reflect.MakeSlice(rt, length, length)
  643. // Read elems
  644. for i := 0; i < length; i++ {
  645. elemRv := sliceRv.Index(i)
  646. readReflectJSON(elemRv, elemRt, oSlice[i], err)
  647. }
  648. rv.Set(sliceRv)
  649. }
  650. case reflect.Struct:
  651. if rt == timeType {
  652. // Special case: time.Time
  653. str, ok := o.(string)
  654. if !ok {
  655. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  656. return
  657. }
  658. log.Info(Fmt("Read time: %v", str))
  659. t, err_ := time.Parse(iso8601, str)
  660. if err_ != nil {
  661. *err = err_
  662. return
  663. }
  664. rv.Set(reflect.ValueOf(t))
  665. } else {
  666. oMap, ok := o.(map[string]interface{})
  667. if !ok {
  668. *err = errors.New(Fmt("Expected map but got type %v", reflect.TypeOf(o)))
  669. return
  670. }
  671. // TODO: ensure that all fields are set?
  672. // TODO: disallow unknown oMap fields?
  673. for _, fieldInfo := range typeInfo.Fields {
  674. i, fieldType, opts := fieldInfo.unpack()
  675. value, ok := oMap[opts.JSONName]
  676. if !ok {
  677. continue // Skip missing fields.
  678. }
  679. fieldRv := rv.Field(i)
  680. readReflectJSON(fieldRv, fieldType, value, err)
  681. }
  682. }
  683. case reflect.String:
  684. str, ok := o.(string)
  685. if !ok {
  686. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  687. return
  688. }
  689. log.Info(Fmt("Read string: %v", str))
  690. rv.SetString(str)
  691. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  692. num, ok := o.(float64)
  693. if !ok {
  694. *err = errors.New(Fmt("Expected numeric but got type %v", reflect.TypeOf(o)))
  695. return
  696. }
  697. log.Info(Fmt("Read num: %v", num))
  698. rv.SetInt(int64(num))
  699. case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
  700. num, ok := o.(float64)
  701. if !ok {
  702. *err = errors.New(Fmt("Expected numeric but got type %v", reflect.TypeOf(o)))
  703. return
  704. }
  705. if num < 0 {
  706. *err = errors.New(Fmt("Expected unsigned numeric but got %v", num))
  707. return
  708. }
  709. log.Info(Fmt("Read num: %v", num))
  710. rv.SetUint(uint64(num))
  711. case reflect.Bool:
  712. bl, ok := o.(bool)
  713. if !ok {
  714. *err = errors.New(Fmt("Expected boolean but got type %v", reflect.TypeOf(o)))
  715. return
  716. }
  717. log.Info(Fmt("Read boolean: %v", bl))
  718. rv.SetBool(bl)
  719. default:
  720. PanicSanity(Fmt("Unknown field type %v", rt.Kind()))
  721. }
  722. }
  723. func writeReflectJSON(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64, err *error) {
  724. log.Info(Fmt("writeReflectJSON(%v, %v, %v, %v, %v)", rv, rt, w, n, err))
  725. // Get typeInfo
  726. typeInfo := GetTypeInfo(rt)
  727. if rt.Kind() == reflect.Interface {
  728. if rv.IsNil() {
  729. // XXX ensure that typeByte 0 is reserved.
  730. WriteTo([]byte("null"), w, n, err)
  731. return
  732. }
  733. crv := rv.Elem() // concrete reflection value
  734. crt := crv.Type() // concrete reflection type
  735. if typeInfo.IsRegisteredInterface {
  736. // See if the crt is registered.
  737. // If so, we're more restrictive.
  738. _, ok := typeInfo.TypeToByte[crt]
  739. if !ok {
  740. switch crt.Kind() {
  741. case reflect.Ptr:
  742. *err = errors.New(Fmt("Unexpected pointer type %v for registered interface %v. "+
  743. "Was it registered as a value receiver rather than as a pointer receiver?", crt, rt.Name()))
  744. case reflect.Struct:
  745. *err = errors.New(Fmt("Unexpected struct type %v for registered interface %v. "+
  746. "Was it registered as a pointer receiver rather than as a value receiver?", crt, rt.Name()))
  747. default:
  748. *err = errors.New(Fmt("Unexpected type %v for registered interface %v. "+
  749. "If this is intentional, please register it.", crt, rt.Name()))
  750. }
  751. return
  752. }
  753. } else {
  754. // We support writing unsafely for convenience.
  755. }
  756. // We don't have to write the typeByte here,
  757. // the writeReflectJSON() call below will write it.
  758. writeReflectJSON(crv, crt, w, n, err)
  759. return
  760. }
  761. if rt.Kind() == reflect.Ptr {
  762. // Dereference pointer
  763. rv, rt = rv.Elem(), rt.Elem()
  764. typeInfo = GetTypeInfo(rt)
  765. if !rv.IsValid() {
  766. // For better compatibility with other languages,
  767. // as far as tendermint/wire is concerned,
  768. // pointers to nil values are the same as nil.
  769. WriteTo([]byte("null"), w, n, err)
  770. return
  771. }
  772. // continue...
  773. }
  774. // Write Byte
  775. if typeInfo.Byte != 0x00 {
  776. WriteTo([]byte(Fmt("[%v,", typeInfo.Byte)), w, n, err)
  777. defer WriteTo([]byte("]"), w, n, err)
  778. }
  779. // All other types
  780. switch rt.Kind() {
  781. case reflect.Array:
  782. elemRt := rt.Elem()
  783. length := rt.Len()
  784. if elemRt.Kind() == reflect.Uint8 {
  785. // Special case: Bytearray
  786. bytearray := reflect.ValueOf(make([]byte, length))
  787. reflect.Copy(bytearray, rv)
  788. WriteTo([]byte(Fmt("\"%X\"", bytearray.Interface())), w, n, err)
  789. } else {
  790. WriteTo([]byte("["), w, n, err)
  791. // Write elems
  792. for i := 0; i < length; i++ {
  793. elemRv := rv.Index(i)
  794. writeReflectJSON(elemRv, elemRt, w, n, err)
  795. if i < length-1 {
  796. WriteTo([]byte(","), w, n, err)
  797. }
  798. }
  799. WriteTo([]byte("]"), w, n, err)
  800. }
  801. case reflect.Slice:
  802. elemRt := rt.Elem()
  803. if elemRt.Kind() == reflect.Uint8 {
  804. // Special case: Byteslices
  805. byteslice := rv.Bytes()
  806. WriteTo([]byte(Fmt("\"%X\"", byteslice)), w, n, err)
  807. } else {
  808. WriteTo([]byte("["), w, n, err)
  809. // Write elems
  810. length := rv.Len()
  811. for i := 0; i < length; i++ {
  812. elemRv := rv.Index(i)
  813. writeReflectJSON(elemRv, elemRt, w, n, err)
  814. if i < length-1 {
  815. WriteTo([]byte(","), w, n, err)
  816. }
  817. }
  818. WriteTo([]byte("]"), w, n, err)
  819. }
  820. case reflect.Struct:
  821. if rt == timeType {
  822. // Special case: time.Time
  823. t := rv.Interface().(time.Time).UTC()
  824. str := t.Format(iso8601)
  825. jsonBytes, err_ := json.Marshal(str)
  826. if err_ != nil {
  827. *err = err_
  828. return
  829. }
  830. WriteTo(jsonBytes, w, n, err)
  831. } else {
  832. WriteTo([]byte("{"), w, n, err)
  833. wroteField := false
  834. for _, fieldInfo := range typeInfo.Fields {
  835. i, fieldType, opts := fieldInfo.unpack()
  836. fieldRv := rv.Field(i)
  837. if wroteField {
  838. WriteTo([]byte(","), w, n, err)
  839. } else {
  840. wroteField = true
  841. }
  842. WriteTo([]byte(Fmt("\"%v\":", opts.JSONName)), w, n, err)
  843. writeReflectJSON(fieldRv, fieldType, w, n, err)
  844. }
  845. WriteTo([]byte("}"), w, n, err)
  846. }
  847. case reflect.String:
  848. fallthrough
  849. case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
  850. fallthrough
  851. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  852. fallthrough
  853. case reflect.Bool:
  854. jsonBytes, err_ := json.Marshal(rv.Interface())
  855. if err_ != nil {
  856. *err = err_
  857. return
  858. }
  859. WriteTo(jsonBytes, w, n, err)
  860. default:
  861. PanicSanity(Fmt("Unknown field type %v", rt.Kind()))
  862. }
  863. }