-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.go
169 lines (138 loc) · 3.82 KB
/
schema.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ces
import (
"bytes"
"database/sql/driver"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/make-software/casper-go-sdk/v2/casper"
"github.com/make-software/casper-go-sdk/v2/types/clvalue"
"github.com/make-software/casper-go-sdk/v2/types/clvalue/cltype"
"github.com/make-software/ces-go-parser/v2/utils"
)
var ErrInvalidSchemaFormat = errors.New("invalid schema format")
type SchemaData struct {
ParamName string
ParamType cltype.CLType
}
type Schemas map[EventName][]SchemaData
func NewSchemasFromBytes(rawSchemas []byte) (Schemas, error) {
buf := bytes.NewBuffer(rawSchemas)
// For all the ces events schema for parsing CLType has next representation
cesEventCLTypeParsingSchema := cltype.Map{Key: cltype.String, Val: &cltype.List{ElementsType: &cltype.Tuple2{
Inner1: cltype.String,
Inner2: &cltype.Dynamic{},
}}}
data, err := clvalue.FromBufferByType(buf, &cesEventCLTypeParsingSchema)
if err != nil {
return nil, err
}
result := make(map[EventName][]SchemaData, data.Map.Len())
for name, schema := range data.Map.Map() {
var schemaList []SchemaData
for _, event := range schema.List.Elements {
schemaList = append(schemaList, SchemaData{
ParamName: event.Tuple2.Inner1.String(),
ParamType: event.Tuple2.Inner2.GetType(),
})
}
result[name] = schemaList
}
return result, nil
}
func (t *SchemaData) MarshalJSON() ([]byte, error) {
temp := struct {
Name string `json:"name"`
Bytes []byte `json:"bytes"`
}{
Name: t.ParamName,
Bytes: t.ParamType.Bytes(),
}
return json.Marshal(temp)
}
func (t *SchemaData) UnmarshalJSON(data []byte) error {
temp := struct {
ParamName string `json:"name"`
ParamType string `json:"bytes"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
decodedBytes, err := base64.StdEncoding.DecodeString(temp.ParamType)
if err != nil {
return err
}
resulType, err := cltype.FromBuffer(bytes.NewBuffer(decodedBytes))
if err != nil {
return err
}
t.ParamType = resulType
t.ParamName = temp.ParamName
return nil
}
type EventPayload map[string]interface{}
func (e EventPayload) StringParam(key string) (string, error) {
rawParam, ok := e[key]
if !ok {
return "", fmt.Errorf("failed to get '%s' param from ces event payload", key)
}
value, ok := rawParam.(string)
if !ok {
return "", fmt.Errorf("failed to assert '%s' param type to string from ces event payload: type: %T", key, rawParam)
}
return value, nil
}
func (e EventPayload) HashParam(key string) (casper.Hash, error) {
value, err := e.StringParam(key)
if err != nil {
return casper.Hash{}, err
}
return casper.NewHash(value)
}
func (e EventPayload) Map() map[string]interface{} {
return map[string]interface{}(e)
}
func (t Schemas) ParseEventPayload(event Event) (EventPayload, error) {
return t.ParseEventRawDataPayload(event.Name, event.RawData)
}
func (t Schemas) ParseEventRawDataPayload(name, rawData string) (EventPayload, error) {
rawEventData, err := hex.DecodeString(rawData)
if err != nil {
return nil, err
}
result, err := ParseEventDataFromSchemaBytes(t[name], bytes.NewBuffer(rawEventData))
if err != nil {
return nil, err
}
eventData := make(map[string]interface{}, len(result))
for key, res := range result {
eventData[key] = utils.CLValueToJSONValue(res)
}
return eventData, nil
}
func (t Schemas) Value() (driver.Value, error) {
marshaled, err := json.Marshal(t)
if err != nil {
return nil, err
}
return marshaled, nil
}
// Scan rewrite behaviour for selecting Hash from db
func (t *Schemas) Scan(value interface{}) error {
bv, err := driver.String.ConvertValue(value)
if err != nil {
return ErrInvalidSchemaFormat
}
v, ok := bv.([]byte)
if !ok {
return ErrInvalidSchemaFormat
}
var schemas Schemas
if err := json.Unmarshal(v, &schemas); err != nil {
return err
}
*t = schemas
return nil
}