Skip to content

Commit

Permalink
implement int32, int64 and bytes extraction for Constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Alesfatalis committed May 5, 2024
1 parent 107926a commit a33734d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
58 changes: 58 additions & 0 deletions constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type Constant interface {
Type() (string, error)
// Value returns the Constant value as string
Value() (string, error)
// Int32 extracts int32 value and returns error if wrong Constant type
Int32() (int32, error)
// Int64 extracts int64 value and returns error if wrong Constant type
Int64() (int64, error)
// Bytes extracts byte array and returns error if wrong Constant type
Bytes() ([]byte, error)
bytesLength() (int, error)
pointer() C.ConstantPtr
}

Expand Down Expand Up @@ -143,6 +150,57 @@ func (c *constant) Value() (string, error) {
return strings.ReplaceAll(C.GoString(constantValueStr), " ", ""), nil
}

func (c *constant) Int32() (int32, error) {
res := C.ergo_lib_constant_to_i32(c.p)
err := newError(res.error)
if err.isError() {
return 0, err.error()
}
return int32(res.value), nil
}

func (c *constant) Int64() (int64, error) {
res := C.ergo_lib_constant_to_i64(c.p)
err := newError(res.error)
if err.isError() {
return 0, err.error()
}
return int64(res.value), nil
}

func (c *constant) bytesLength() (int, error) {
var returnNum C.ReturnNum_usize
returnNum = C.ergo_lib_constant_bytes_len(c.p)
err := newError(returnNum.error)

if err.isError() {
return 0, err.error()
}
size := C.ulong(returnNum.value)

return int(size), nil
}

func (c *constant) Bytes() ([]byte, error) {
bytesLength, bytesLengthErr := c.bytesLength()
if bytesLengthErr != nil {
return []byte{}, bytesLengthErr
}

output := C.malloc(C.uintptr_t(bytesLength))
defer C.free(unsafe.Pointer(output))

errPtr := C.ergo_lib_constant_to_bytes(c.p, (*C.uint8_t)(output))
err := newError(errPtr)

if err.isError() {
return []byte{}, err.error()
}

result := C.GoBytes(unsafe.Pointer(output), C.int(bytesLength))
return result, nil
}

func (c *constant) pointer() C.ConstantPtr {
return c.p
}
Expand Down
27 changes: 13 additions & 14 deletions constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,25 @@ func TestNewConstant_TupleExpression(t *testing.T) {
assert.Equal(t, "BoundedVec{inner:[102,99]}", constValue)
}

func TestNewConstantFromInt32(t *testing.T) {
c := NewConstantFromInt32(999999999)
encoded, _ := c.Base16()
decoded, _ := NewConstant(encoded)
assert.Equal(t, c, decoded)
func TestConstant_Int32(t *testing.T) {
testValue := int32(999999999)
c := NewConstantFromInt32(testValue)
res, _ := c.Int32()
assert.Equal(t, testValue, res)
}

func TestNewConstantFromInt64(t *testing.T) {
c := NewConstantFromInt64(9223372036854775807)
encoded, _ := c.Base16()
decoded, _ := NewConstant(encoded)
assert.Equal(t, c, decoded)
func TestConstant_Int64(t *testing.T) {
testValue := int64(9223372036854775807)
c := NewConstantFromInt64(testValue)
res, _ := c.Int64()
assert.Equal(t, testValue, res)
}

func TestNewConstantFromBytes(t *testing.T) {
func TestConstant_Bytes(t *testing.T) {
b := []byte{1, 1, 2, 255}
c, _ := NewConstantFromBytes(b)
encoded, _ := c.Base16()
decoded, _ := NewConstant(encoded)
assert.Equal(t, c, decoded)
res, _ := c.Bytes()
assert.Equal(t, b, res)
}

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

0 comments on commit a33734d

Please sign in to comment.