| 31 | } |
| 32 | |
| 33 | func decode(d *Decoder, v reflect.Value) { |
| 34 | t := v.Type() |
| 35 | |
| 36 | if t.Implements(tyDecodable) { |
| 37 | v.Interface().(Decodable).Decode(d) |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | switch t.Kind() { |
| 42 | case reflect.Float32: |
| 43 | v.SetFloat(float64(d.F32())) |
| 44 | case reflect.Float64: |
| 45 | v.SetFloat(d.F64()) |
| 46 | case reflect.Int8: |
| 47 | v.SetInt(int64(d.I8())) |
| 48 | case reflect.Int16: |
| 49 | v.SetInt(int64(d.I16())) |
| 50 | case reflect.Int32: |
| 51 | v.SetInt(int64(d.I32())) |
| 52 | case reflect.Int64: |
| 53 | if t.Implements(tyIntTy) { |
| 54 | v.SetInt(int64(d.Int())) |
| 55 | } else { |
| 56 | v.SetInt(d.I64()) |
| 57 | } |
| 58 | case reflect.Uint8: |
| 59 | if t.Implements(tyCharTy) { |
| 60 | v.SetUint(uint64(d.Char())) |
| 61 | } else { |
| 62 | v.SetUint(uint64(d.U8())) |
| 63 | } |
| 64 | case reflect.Uint16: |
| 65 | v.SetUint(uint64(d.U16())) |
| 66 | case reflect.Uint32: |
| 67 | v.SetUint(uint64(d.U32())) |
| 68 | case reflect.Uint64: |
| 69 | switch { |
| 70 | case t.Implements(tyPointer): |
| 71 | v.SetUint(uint64(d.Pointer())) |
| 72 | case t.Implements(tySizeTy): |
| 73 | v.SetUint(uint64(d.Size())) |
| 74 | case t.Implements(tyUintTy): |
| 75 | v.SetUint(uint64(d.Uint())) |
| 76 | default: |
| 77 | v.SetUint(d.U64()) |
| 78 | } |
| 79 | case reflect.Int: |
| 80 | v.SetInt(int64(d.Int())) |
| 81 | case reflect.Uint: |
| 82 | v.SetUint(uint64(d.Uint())) |
| 83 | case reflect.Array: |
| 84 | for i, c := 0, v.Len(); i < c; i++ { |
| 85 | decode(d, v.Index(i)) |
| 86 | } |
| 87 | case reflect.Slice: |
| 88 | if t.Elem() == tyUint8Ty { |
| 89 | d.Data(v.Interface().([]uint8)) |
| 90 | } else { |