Skip to content

Commit

Permalink
added support for eventType and tested it (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjartek authored Oct 17, 2023
1 parent 759fcf0 commit 4215a58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
26 changes: 18 additions & 8 deletions cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func CadenceValueToJsonString(value cadence.Value) (string, error) {
return "", nil
}
j, err := json.MarshalIndent(result, "", " ")

if err != nil {
return "", err
}
Expand Down Expand Up @@ -90,7 +89,7 @@ func CadenceValueToInterface(field cadence.Value) interface{} {
case cadence.Optional:
return CadenceValueToInterface(field.Value)
case cadence.Dictionary:
//fmt.Println("is dict ", field.ToGoValue(), " ", field.String())
// fmt.Println("is dict ", field.ToGoValue(), " ", field.String())
result := map[string]interface{}{}
for _, item := range field.Pairs {
value := CadenceValueToInterface(item.Value)
Expand All @@ -105,7 +104,7 @@ func CadenceValueToInterface(field cadence.Value) interface{} {
}
return result
case cadence.Struct:
//fmt.Println("is struct ", field.ToGoValue(), " ", field.String())
// fmt.Println("is struct ", field.ToGoValue(), " ", field.String())
result := map[string]interface{}{}
subStructNames := field.StructType.Fields

Expand All @@ -123,7 +122,7 @@ func CadenceValueToInterface(field cadence.Value) interface{} {
}
return result
case cadence.Array:
//fmt.Println("is array ", field.ToGoValue(), " ", field.String())
// fmt.Println("is array ", field.ToGoValue(), " ", field.String())
var result []interface{}
for _, item := range field.Values {
value := CadenceValueToInterface(item)
Expand All @@ -142,27 +141,38 @@ func CadenceValueToInterface(field cadence.Value) interface{} {
case cadence.Address:
return field.String()
case cadence.TypeValue:
//fmt.Println("is type ", field.ToGoValue(), " ", field.String())
// fmt.Println("is type ", field.ToGoValue(), " ", field.String())
return field.StaticType.ID()
case cadence.String:
//fmt.Println("is string ", field.ToGoValue(), " ", field.String())
// fmt.Println("is string ", field.ToGoValue(), " ", field.String())
value := getAndUnquoteString(field)
if value == "" {
return nil
}
return value

case cadence.UFix64:
//fmt.Println("is ufix64 ", field.ToGoValue(), " ", field.String())
// fmt.Println("is ufix64 ", field.ToGoValue(), " ", field.String())

float, _ := strconv.ParseFloat(field.String(), 64)
return float
case cadence.Fix64:
float, _ := strconv.ParseFloat(field.String(), 64)
return float
case cadence.Event:
result := map[string]interface{}{}

for i, subField := range field.Fields {
value := CadenceValueToInterface(subField)
if value != nil {
result[field.EventType.Fields[i].Identifier] = value
}
}

return result

default:
//fmt.Println("is fallthrough ", field.ToGoValue(), " ", field.String())
// fmt.Println("is fallthrough ", field.ToGoValue(), " ", field.String())

goValue := field.ToGoValue()
if goValue != nil {
Expand Down
19 changes: 11 additions & 8 deletions cadence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type Cadencetest struct {
}

func TestCadenceValueToInterface(t *testing.T) {

foo := cadenceString("foo")
bar := cadenceString("bar")
emptyString := cadenceString("")
Expand Down Expand Up @@ -54,8 +53,17 @@ func TestCadenceValueToInterface(t *testing.T) {

cadenceAddress1 := cadence.BytesToAddress(address1.Bytes())

structTypeValue := cadence.NewTypeValue(&structType)
stringType := cadence.NewStringType()
cadenceEvent := cadence.NewEvent([]cadence.Value{foo}).WithType(&cadence.EventType{
QualifiedIdentifier: "TestEvent",
Fields: []cadence.Field{{
Type: cadence.StringType{},
Identifier: "foo",
}},
},
)

structTypeValue := cadence.NewTypeValue(&structType)
stringTypeValue := cadence.NewTypeValue(&stringType)
ufix, _ := cadence.NewUFix64("42.0")
path := cadence.Path{Domain: common.PathDomainStorage, Identifier: "foo"}
Expand Down Expand Up @@ -84,6 +92,7 @@ func TestCadenceValueToInterface(t *testing.T) {
{autogold.Want("Emoji", "😁"), emoji},
{autogold.Want("EmojiDict", map[string]interface{}{"😁": "😁"}), emojiDict},
{autogold.Want("StoragePath", "/storage/foo"), path},
{autogold.Want("Event", map[string]interface{}{"foo": "foo"}), cadenceEvent},
}

for _, tc := range testCases {
Expand All @@ -101,7 +110,6 @@ func TestCadenceValueToJson(t *testing.T) {
}

func TestParseInputValue(t *testing.T) {

foo := "foo"

var strPointer *string = nil
Expand Down Expand Up @@ -132,11 +140,9 @@ func TestParseInputValue(t *testing.T) {
assert.Equal(t, string(cvj), string(vj))
})
}

}

func TestMarshalCadenceStruct(t *testing.T) {

val, err := InputToCadence(Foo{Bar: "foo"}, func(string) (string, error) {
return "A.123.Foo.Bar", nil
})
Expand All @@ -145,11 +151,9 @@ func TestMarshalCadenceStruct(t *testing.T) {
jsonVal, err := CadenceValueToJsonString(val)
assert.NoError(t, err)
assert.JSONEq(t, `{ "bar": "foo" }`, jsonVal)

}

func TestMarshalCadenceStructWithStructTag(t *testing.T) {

val, err := InputToCadence(Foo{Bar: "foo"}, func(string) (string, error) {
return "A.123.Foo.Baz", nil
})
Expand All @@ -158,7 +162,6 @@ func TestMarshalCadenceStructWithStructTag(t *testing.T) {
jsonVal, err := CadenceValueToJsonString(val)
assert.NoError(t, err)
assert.JSONEq(t, `{ "bar": "foo" }`, jsonVal)

}

func TestPrimitiveInputToCadence(t *testing.T) {
Expand Down

0 comments on commit 4215a58

Please sign in to comment.