This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
from_bytes.go
103 lines (88 loc) · 2.64 KB
/
from_bytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package convert
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
)
// FromBytes converts []byte to target interface
func FromBytes(bb []byte, target interface{}) (result interface{}, err error) {
// create copy
switch t := target.(type) {
case string:
return string(bb), nil
case []byte:
return bb, nil
case int:
return strconv.Atoi(string(bb))
case bool:
return strconv.ParseBool(string(bb))
case []string:
arrInterface, err := JSONUnmarshalPtr(bb, &target)
if err != nil {
return nil, err
}
var arrString []string
for _, v := range arrInterface.([]interface{}) {
arrString = append(arrString, v.(string))
}
return arrString, nil
case FromByter:
return t.FromBytes(bb)
case proto.Message:
return ProtoUnmarshal(bb, t)
default:
return FromBytesToStruct(bb, target)
}
}
// FromBytesToStruct converts []byte to struct,array,slice depending on target type
func FromBytesToStruct(bb []byte, target interface{}) (result interface{}, err error) {
if bb == nil {
return nil, ErrUnableToConvertNilToStruct
}
targetType := reflect.TypeOf(target).Kind()
switch targetType {
case reflect.Struct:
fallthrough
case reflect.Array:
fallthrough
case reflect.Slice:
// will be map[string]interface{}
return JSONUnmarshalPtr(bb, &target)
case reflect.Ptr:
return JSONUnmarshalPtr(bb, target)
default:
return nil, fmt.Errorf(
`fromBytes converting supports ToByter interface,struct,array,slice and string, current type is %s`,
targetType)
}
}
// JSONUnmarshalPtr unmarshalls []byte as json to pointer, and returns value pointed to
func JSONUnmarshalPtr(bb []byte, to interface{}) (result interface{}, err error) {
targetPtr := reflect.New(reflect.ValueOf(to).Elem().Type()).Interface()
err = json.Unmarshal(bb, targetPtr)
if err != nil {
return nil, ErrUnableToConvertValueToStruct
}
return reflect.Indirect(reflect.ValueOf(targetPtr)).Interface(), nil
}
// ProtoUnmarshal r unmarshalls []byte as proto.Message to pointer, and returns value pointed to
func ProtoUnmarshal(bb []byte, messageType proto.Message) (message proto.Message, err error) {
msg := proto.Clone(messageType)
err = proto.Unmarshal(bb, msg)
if err != nil {
return nil, ErrUnableToConvertValueToStruct
}
return msg, nil
}
// FromResponse converts response.Payload to target
func FromResponse(response peer.Response, target interface{}) (result interface{}, err error) {
if response.Status == shim.ERROR {
return nil, errors.New(response.Message)
}
return FromBytes(response.Payload, target)
}