-
Notifications
You must be signed in to change notification settings - Fork 8
/
istate-general-utils.go
152 lines (138 loc) · 3.81 KB
/
istate-general-utils.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright 2020 Motoreq Infotech Pvt Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package istate
import (
"encoding/json"
"github.com/hyperledger/fabric/core/chaincode/shim"
"reflect"
"strings"
)
// Note: It returns reflect.Value , not interface{}
func (iState *iState) unmarshalToStruct(valBytes []byte) (uObj reflect.Value, iStateErr Error) {
singleElem := reflect.New(reflect.TypeOf(iState.structRef)).Interface()
err := json.Unmarshal(valBytes, &singleElem)
if err != nil {
iStateErr = newError(err, 4004)
return
}
uObj = reflect.ValueOf(singleElem).Elem()
return
}
func (iState *iState) getQIndexMap(key string, valBytes []byte) (encodedKV map[string][]byte, iStateErr Error) {
var tempVar map[string]interface{}
err := json.Unmarshal(valBytes, &tempVar)
if err != nil {
iStateErr = newError(err, 4005)
return
}
encodedKV, _, _, iStateErr = iState.encodeState(tempVar, key, "", 1) // keyRefSeperatedIndex = 1, query = false
if iStateErr != nil {
return
}
return
}
//
func convertObjToMap(obj interface{}) (uObj map[string]interface{}, iStateErr Error) {
mo, err := json.Marshal(obj)
if err != nil {
iStateErr = newError(err, 4001)
return
}
err = json.Unmarshal(mo, &uObj)
if err != nil {
iStateErr = newError(err, 4002)
return
}
return
}
//
func getKeyByRange(stub shim.ChaincodeStubInterface, startKey, endKey string, limit ...int) (fetchedKVMap map[string][]byte, iStateErr Error) {
if len(limit) == 0 {
limit = []int{int32Biggest}
}
fetchedKVMap = make(map[string][]byte)
iterator, err := stub.GetStateByRange(startKey, endKey)
if err != nil {
iStateErr = newError(err, 3006)
return
}
defer iterator.Close()
for i := 0; iterator.HasNext(); i++ {
iteratorResult, err := iterator.Next()
if err != nil {
iStateErr = newError(err, 4003)
return
}
key := iteratorResult.GetKey()
val := iteratorResult.GetValue()
fetchedKVMap[key] = val
if i >= limit[0] {
break
}
}
return
}
func getKeyByRangeWithPagination(stub shim.ChaincodeStubInterface, startKey, endKey string, pagesize int32, bookmark string) (fetchedKVMap map[string][]byte, iStateErr Error) {
fetchedKVMap = make(map[string][]byte)
iterator, _, err := stub.GetStateByRangeWithPagination(startKey, endKey, pagesize, bookmark)
if err != nil {
iStateErr = newError(err, 3006)
return
}
defer iterator.Close()
for i := 0; iterator.HasNext(); i++ {
iteratorResult, err := iterator.Next()
if err != nil {
iStateErr = newError(err, 4003)
return
}
key := iteratorResult.GetKey()
val := iteratorResult.GetValue()
fetchedKVMap[key] = val
}
return
}
func getKeyFromIndex(indexkey string) (keyRef string) {
splitPosition := strings.LastIndex(indexkey, null)
if splitPosition != -1 {
keyRef = indexkey[splitPosition+1:]
}
return
}
func splitIndexAndKey(index string) (partindex, keyRef string) {
partindex = index
splitPosition := strings.LastIndex(index, null)
if splitPosition != -1 {
partindex = index[:splitPosition]
keyRef = index[splitPosition+1:]
}
return
}
func incLastChar(val string) (incVal string) {
if len(val) == 0 {
return
}
lastChar := val[len(val)-1]
val = val[:len(val)-1]
incVal = val + string(lastChar+1)
return
}
func decLastChar(val string) (decVal string) {
if len(val) == 0 {
return
}
lastChar := val[len(val)-1]
val = val[:len(val)-1]
decVal = val + string(lastChar-1)
return
}