Skip to content

Commit

Permalink
added method to get byte array from event param (#157)
Browse files Browse the repository at this point in the history
* added method to get byte array from event param

* fixed error
  • Loading branch information
bjartek authored Feb 26, 2024
1 parent 35af93d commit 50606ff
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ func (o OverflowResult) GetIdFromEvent(eventName string, fieldName string) (uint
return 0, err
}

// Get a byteArray field with the given fieldname from an event with a given suffix
func (o OverflowResult) GetByteArrayFromEvent(eventName string, fieldName string) ([]byte, error) {
for name, event := range o.Events {
if strings.HasSuffix(name, eventName) {
return getByteArray(event[0].Fields[fieldName])
}
}
err := fmt.Errorf("could not find field %s in event with suffix %s", fieldName, eventName)
if o.StopOnError {
panic(err)
}
return nil, err
}

func (o OverflowResult) GetIdsFromEvent(eventName string, fieldName string) []uint64 {
var ids []uint64
for name, events := range o.Events {
Expand Down Expand Up @@ -358,3 +372,19 @@ func (o OverflowResult) AssertDebugLog(t *testing.T, message ...string) Overflow
}
return o
}

func getByteArray(data interface{}) ([]byte, error) {
slice, ok := data.([]interface{})
if !ok {
return nil, fmt.Errorf("expected a slice of interfaces")
}
byteSlice := make([]byte, len(slice))
for i, val := range slice {
b, ok := val.(uint8)
if !ok {
return nil, fmt.Errorf("unexpected type at index %d", i)
}
byteSlice[i] = byte(b)
}
return byteSlice, nil
}

0 comments on commit 50606ff

Please sign in to comment.