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
/
state_list.go
83 lines (68 loc) · 1.87 KB
/
state_list.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
package state
import (
"fmt"
"reflect"
"github.com/hyperledger/fabric-chaincode-go/shim"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"github.com/s7techlab/cckit/state/schema"
)
type (
StateList struct {
itemTarget interface{}
listTarget interface{}
list []interface{}
}
)
func NewStateList(config ...interface{}) (sl *StateList, err error) {
var (
itemTarget, listTarget interface{}
)
if len(config) > 0 {
itemTarget = config[0]
}
if len(config) > 1 {
listTarget = config[1]
}
return &StateList{itemTarget: itemTarget, listTarget: listTarget}, nil
}
func (sl *StateList) Fill(iter shim.StateQueryIteratorInterface, fromBytes FromBytesTransformer) (list interface{}, err error) {
for iter.HasNext() {
kv, err := iter.Next()
if err != nil {
return nil, err
}
item, err := fromBytes(kv.Value, sl.itemTarget)
if err != nil {
return nil, fmt.Errorf(`transform list entry: %w`, err)
}
sl.list = append(sl.list, item)
}
return sl.Get()
}
func (sl *StateList) Get() (list interface{}, err error) {
// custom list proto.Message
if _, isListProto := sl.listTarget.(proto.Message); isListProto {
customList := proto.Clone(sl.listTarget.(proto.Message))
items := reflect.ValueOf(customList).Elem().FieldByName(`Items`)
for _, v := range sl.list {
items.Set(reflect.Append(items, reflect.ValueOf(v)))
}
return customList, nil
// default list proto.Message (with repeated Any)
} else if _, isItemProto := sl.itemTarget.(proto.Message); isItemProto {
defList := &schema.List{}
for _, item := range sl.list {
newAny, err := anypb.New(item.(proto.Message))
if err != nil {
return nil, err
}
defList.Items = append(defList.Items, newAny)
}
return defList, nil
}
return sl.list, nil
}
func (sl *StateList) AddElementToList(elem interface{}) {
sl.list = append(sl.list, elem)
}