From d0b5797577d7b07232e8289fbd6a66667c8da88b Mon Sep 17 00:00:00 2001 From: Volodymyr-K Date: Fri, 5 Jul 2024 13:55:11 +0200 Subject: [PATCH 1/2] Extend interface of Schema to usability (CMW-1277) --- schema.go | 56 ++++++++++++++++++++++++++++++++++++++ utils/casper.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 utils/casper.go diff --git a/schema.go b/schema.go index d9c6731..1d70d99 100644 --- a/schema.go +++ b/schema.go @@ -4,11 +4,15 @@ import ( "bytes" "database/sql/driver" "encoding/base64" + "encoding/hex" "encoding/json" "errors" + "fmt" + "github.com/make-software/casper-go-sdk/casper" "github.com/make-software/casper-go-sdk/types/clvalue" "github.com/make-software/casper-go-sdk/types/clvalue/cltype" + "github.com/make-software/ces-go-parser/utils" ) var ErrInvalidSchemaFormat = errors.New("invalid schema format") @@ -82,6 +86,58 @@ func (t *SchemaData) UnmarshalJSON(data []byte) error { 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 { diff --git a/utils/casper.go b/utils/casper.go new file mode 100644 index 0000000..1498d74 --- /dev/null +++ b/utils/casper.go @@ -0,0 +1,72 @@ +package utils + +import ( + "github.com/make-software/casper-go-sdk/types/clvalue" + "github.com/make-software/casper-go-sdk/types/clvalue/cltype" +) + +// TODO: Refactore and move to casper go sdk +func CLValueToJSONValue(value clvalue.CLValue) interface{} { + switch value.Type.GetTypeID() { + case cltype.TypeIDBool: + return value.Bool.Value() + case cltype.TypeIDI32: + return value.I32.Value() + case cltype.TypeIDI64: + return value.I64.Value() + case cltype.TypeIDU8: + return value.UI8.Value() + case cltype.TypeIDU32: + return value.UI32.Value() + case cltype.TypeIDU64: + return value.UI64.Value() + case cltype.TypeIDU128: + return value.UI128.Value().String() + case cltype.TypeIDU256: + return value.UI256.Value().String() + case cltype.TypeIDU512: + return value.UI512.Value().String() + case cltype.TypeIDUnit: + return value.Unit.String() + case cltype.TypeIDString: + return value.StringVal.String() + case cltype.TypeIDKey: + return value.Key.String() + case cltype.TypeIDURef: + return value.Uref.String() + case cltype.TypeIDOption: + if value.Option.IsEmpty() { + return nil + } + return CLValueToJSONValue(*value.Option.Inner) + case cltype.TypeIDList: + var data []interface{} + for _, one := range value.List.Elements { + data = append(data, CLValueToJSONValue(one)) + } + return data + case cltype.TypeIDByteArray: + return value.ByteArray.String() + case cltype.TypeIDResult: + if value.Result.IsSuccess { + return CLValueToJSONValue(value.Result.Inner) + } + return nil + case cltype.TypeIDMap: + res := make(map[string]interface{}, value.Map.Len()) + for key, value := range value.Map.Map() { + res[key] = CLValueToJSONValue(value) + } + return res + case cltype.TypeIDTuple1: + return CLValueToJSONValue(value.Tuple1.Value()) + case cltype.TypeIDTuple2: + return [2]interface{}{CLValueToJSONValue(value.Tuple2.Inner1), CLValueToJSONValue(value.Tuple2.Inner2)} + case cltype.TypeIDTuple3: + return [3]interface{}{CLValueToJSONValue(value.Tuple3.Inner1), CLValueToJSONValue(value.Tuple3.Inner2), CLValueToJSONValue(value.Tuple3.Inner3)} + case cltype.TypeIDPublicKey: + return value.PublicKey.String() + default: + return nil + } +} From c0663cfe9ea0db2c9a03a24d6b1496bde7589fd5 Mon Sep 17 00:00:00 2001 From: Artem Zhmaka Date: Tue, 13 Aug 2024 20:58:08 +0200 Subject: [PATCH 2/2] CMW-1277/extend-schema-functionality Actualize with master --- utils/casper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/casper.go b/utils/casper.go index 1498d74..8bfbc4c 100644 --- a/utils/casper.go +++ b/utils/casper.go @@ -1,8 +1,8 @@ package utils import ( - "github.com/make-software/casper-go-sdk/types/clvalue" - "github.com/make-software/casper-go-sdk/types/clvalue/cltype" + "github.com/make-software/casper-go-sdk/v2/types/clvalue" + "github.com/make-software/casper-go-sdk/v2/types/clvalue/cltype" ) // TODO: Refactore and move to casper go sdk