diff --git a/Readme.md b/Readme.md index 3f56a94..ee022c6 100644 --- a/Readme.md +++ b/Readme.md @@ -49,7 +49,7 @@ 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: @@ -57,4 +57,4 @@ Similarly, returning a value to Debezium is performed using the PDK functionalit 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. diff --git a/debezium.go b/debezium.go index 4355714..cd913d6 100644 --- a/debezium.go +++ b/debezium.go @@ -1,7 +1,6 @@ package debezium import ( - "strconv" "unsafe" "github.com/debezium/debezium-smt-go-pdk/internal" @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/it/debezium_test.go b/it/debezium_test.go index 2928b18..6f8de38 100644 --- a/it/debezium_test.go +++ b/it/debezium_test.go @@ -6,7 +6,6 @@ import ( "context" _ "embed" "log" - "strconv" "github.com/stretchr/testify/assert" @@ -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 { @@ -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) @@ -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 @@ -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)) @@ -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) -} diff --git a/it/testdata/test4.wasm b/it/testdata/test4.wasm deleted file mode 100644 index c07a277..0000000 Binary files a/it/testdata/test4.wasm and /dev/null differ diff --git a/testdata-project/test4.go b/testdata-project/test4.go deleted file mode 100644 index bd0ff5a..0000000 --- a/testdata-project/test4.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import ( - "github.com/debezium/debezium-smt-go-pdk" -) - -//export process -func process(proxyPtr uint32) uint32 { - return debezium.SetInt(proxyPtr) -} - -func main() {}