Skip to content

Commit

Permalink
Merge pull request #1 from andreaTP/all-struct-data-types
Browse files Browse the repository at this point in the history
All struct data types
  • Loading branch information
andreaTP authored Jan 17, 2025
2 parents c5aaaaa + 101f60f commit aedb9fb
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ For efficiency reasons the full content of the record is not transferred to the
debezium.GetString(debezium.Get(proxyPtr, "value.op"))
```

where `debezium.Get` is used to access the required field with a familiar dot(`.`) syntax, and `debezium.GetString` (or `debezium.IsNull`, `debezium.GetInt`) materialize the value.
where `debezium.Get` is used to access the required field with a familiar dot(`.`) syntax, and `debezium.GetString` (or `debezium.IsNull`, `debezium.GetInt32`, etc.) materialize the value.

Similarly, returning a value to Debezium is performed using the PDK functionalities:

```go
return debezium.SetString("foobar")
```

the value returned by the `Set` function (or `SetNull`, `SetBool`, `SetInt` ...) should be returned as the result of the `process` function.
the value returned by the `Set` function (or `SetNull`, `SetBool`, `SetString` ...) should be returned as the result of the `process` function.
90 changes: 71 additions & 19 deletions debezium.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package debezium

import (
"strconv"
"unsafe"

"github.com/debezium/debezium-smt-go-pdk/internal"
Expand All @@ -24,9 +23,47 @@ func GetString(proxyPtr uint32) string {
return result
}

// materialize the Numeric content referenced
func GetInt(proxyPtr uint32) uint32 {
return envGetInt(proxyPtr)
// materialize the Boolean content referenced
func GetBool(proxyPtr uint32) bool {
return envGetBool(proxyPtr) > 0
}

// materialize the Bytes content referenced
func GetBytes(proxyPtr uint32) []byte {
var resultPtr = envGetBytes(proxyPtr)
var result = internal.ReadCString(resultPtr)
internal.Free(unsafe.Pointer(uintptr(resultPtr)))
return []byte(result)
}

// materialize the Float32 content referenced
func GetFloat32(proxyPtr uint32) float32 {
return envGetFloat32(proxyPtr)
}

// materialize the Float64 content referenced
func GetFloat64(proxyPtr uint32) float64 {
return envGetFloat64(proxyPtr)
}

// materialize the Int16 content referenced
func GetInt16(proxyPtr uint32) int16 {
return int16(envGetInt16(proxyPtr))
}

// materialize the Int32 content referenced
func GetInt32(proxyPtr uint32) int32 {
return envGetInt32(proxyPtr)
}

// materialize the Int64 content referenced
func GetInt64(proxyPtr uint32) int64 {
return envGetInt64(proxyPtr)
}

// materialize the Int8 content referenced
func GetInt8(proxyPtr uint32) int8 {
return int8(envGetInt8(proxyPtr))
}

// check whenever the referenced content is Null
Expand Down Expand Up @@ -60,15 +97,6 @@ func SetString(value string) uint32 {
return envSetString(uint32(uintptr(valuePtr)))
}

// set a Numeric content for the Debezium Host
func SetInt(value uint32) uint32 {
bs := []byte(strconv.Itoa(int(value)))
var valuePtr = internal.Malloc(uintptr(len(bs) + 1))
internal.WriteCString(uintptr(valuePtr), string(bs))

return envSetInt(uint32(uintptr(valuePtr)))
}

//go:wasm-module env
//export get_string
func envGetString(proxyPtr uint32) uint32
Expand All @@ -85,10 +113,6 @@ func envIsNull(valuePtr uint32) uint32
//export set_string
func envSetString(valuePtr uint32) uint32

//go:wasm-module env
//export set_int
func envSetInt(valuePtr uint32) uint32

//go:wasm-module env
//export set_bool
func envSetBool(valuePtr uint32) uint32
Expand All @@ -98,5 +122,33 @@ func envSetBool(valuePtr uint32) uint32
func envGet(proxyPtr, fieldNamePtr uint32) uint32

//go:wasm-module env
//export get_int
func envGetInt(proxyPtr uint32) uint32
//export get_bool
func envGetBool(proxyPtr uint32) uint32

//go:wasm-module env
//export get_bytes
func envGetBytes(proxyPtr uint32) uint32

//go:wasm-module env
//export get_float32
func envGetFloat32(proxyPtr uint32) float32

//go:wasm-module env
//export get_float64
func envGetFloat64(proxyPtr uint32) float64

//go:wasm-module env
//export get_int16
func envGetInt16(proxyPtr uint32) uint32

//go:wasm-module env
//export get_int32
func envGetInt32(proxyPtr uint32) int32

//go:wasm-module env
//export get_int64
func envGetInt64(proxyPtr uint32) int64

//go:wasm-module env
//export get_int8
func envGetInt8(proxyPtr uint32) uint32
87 changes: 50 additions & 37 deletions it/debezium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
_ "embed"
"log"
"strconv"

"github.com/stretchr/testify/assert"

Expand All @@ -15,7 +14,6 @@ import (

var get_string_ptr uint32
var set_string_ptr uint32
var set_int uint32

// wazero module builder
func wazeroStub(ctx context.Context) wazero.Runtime {
Expand All @@ -32,37 +30,66 @@ func wazeroStub(ctx context.Context) wazero.Runtime {
WithFunc(func(v uint32) uint32 {
return 2
}).
Export("get_int").
Export("get_bool").
NewFunctionBuilder().
WithFunc(func(v1, v2 uint32) uint32 {
WithFunc(func(v uint32) uint32 {
return 3
}).
Export("get").
Export("get_bytes").
NewFunctionBuilder().
WithFunc(func(v uint32) uint32 {
return 4
}).
Export("set_bool").
Export("get_float32").
NewFunctionBuilder().
WithFunc(func(v uint32) uint32 {
set_int = v
WithFunc(func(v uint32) uint64 {
return 5
}).
Export("set_int").
Export("get_float64").
NewFunctionBuilder().
WithFunc(func(v uint32) uint32 {
set_string_ptr = v
return 6
}).
Export("get_int16").
NewFunctionBuilder().
WithFunc(func(v uint32) uint32 {
return 7
}).
Export("get_int32").
NewFunctionBuilder().
WithFunc(func(v1 uint32) uint32 {
return 8
}).
Export("get_int64").
NewFunctionBuilder().
WithFunc(func(v uint32) int64 {
return 9
}).
Export("get_int8").
NewFunctionBuilder().
WithFunc(func(v1, v2 uint32) uint32 {
return 10
}).
Export("get").
NewFunctionBuilder().
WithFunc(func(v uint32) uint32 {
return 11
}).
Export("set_bool").
NewFunctionBuilder().
WithFunc(func(v uint32) uint32 {
set_string_ptr = v
return 12
}).
Export("set_string").
NewFunctionBuilder().
WithFunc(func() uint32 {
return 7
return 13
}).
Export("set_null").
NewFunctionBuilder().
WithFunc(func(v uint32) uint32 {
return 8
return 14
}).
Export("is_null").
Instantiate(ctx)
Expand All @@ -82,10 +109,16 @@ func TestGuestNull(t *testing.T) {
var r = wazeroStub(ctx)
defer r.Close(ctx)

mod, _ := r.Instantiate(ctx, test1Wasm)
res, _ := mod.ExportedFunction("process").Call(ctx, 0)
mod, err := r.Instantiate(ctx, test1Wasm)
if err != nil {
log.Panicln(err)
}
res, err := mod.ExportedFunction("process").Call(ctx, 0)
if err != nil {
log.Panicln(err)
}

assert.Equal(t, res[0], uint64(7))
assert.Equal(t, uint64(13), res[0])
}

//go:embed testdata/test2.wasm
Expand All @@ -99,7 +132,7 @@ func TestGuestString(t *testing.T) {
mod, _ := r.Instantiate(ctx, test2Wasm)
res, _ := mod.ExportedFunction("process").Call(ctx, 0)

assert.Equal(t, res[0], uint64(6))
assert.Equal(t, uint64(12), res[0])

buf, _ := mod.Memory().Read(set_string_ptr, 3)
assert.Equal(t, "foo", string(buf))
Expand All @@ -122,28 +155,8 @@ func TestHostString(t *testing.T) {

res, _ := mod.ExportedFunction("process").Call(ctx, namePtr)

assert.Equal(t, res[0], uint64(6))
assert.Equal(t, uint64(12), res[0])

buf, _ := mod.Memory().Read(set_string_ptr, 3)
assert.Equal(t, "baz", string(buf))
}

//go:embed testdata/test4.wasm
var test4Wasm []byte

func TestGuestNumbers(t *testing.T) {
var ctx = context.Background()
var r = wazeroStub(ctx)
defer r.Close(ctx)

mod, _ := r.Instantiate(ctx, test4Wasm)

res, _ := mod.ExportedFunction("process").Call(ctx, 123)

assert.Equal(t, res[0], uint64(5))

buf, _ := mod.Memory().Read(set_int, 3)
myInt, _ := strconv.Atoi(string(buf))

assert.Equal(t, myInt, 123)
}
Binary file removed it/testdata/test4.wasm
Binary file not shown.
12 changes: 0 additions & 12 deletions testdata-project/test4.go

This file was deleted.

0 comments on commit aedb9fb

Please sign in to comment.