Skip to content

Commit

Permalink
Merge pull request #8 from make-software/CMW-1277/extend-schema-funct…
Browse files Browse the repository at this point in the history
…ionality

Extend interface of Schema to usability (CMW-1277)
  • Loading branch information
ZhmakAS authored Aug 13, 2024
2 parents e93080d + c0663cf commit e54da79
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
56 changes: 56 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ 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")
Expand Down Expand Up @@ -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 {
Expand Down
72 changes: 72 additions & 0 deletions utils/casper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package utils

import (
"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
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
}
}

0 comments on commit e54da79

Please sign in to comment.