Skip to content

Commit

Permalink
try fix go case
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 8, 2024
1 parent 70cef32 commit 0492f88
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ struct opendal_capability opendal_operator_info_get_native_capability(const stru
/**
* \brief Frees the heap memory used by the opendal_bytes
*/
void opendal_bytes_free(struct opendal_bytes *bs);
void opendal_bytes_free(struct opendal_bytes *ptr);

/**
* \brief Construct a heap-allocated opendal_operator_options
Expand Down
6 changes: 3 additions & 3 deletions bindings/c/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ impl opendal_bytes {

/// \brief Frees the heap memory used by the opendal_bytes
#[no_mangle]
pub unsafe extern "C" fn opendal_bytes_free(bs: *mut opendal_bytes) {
if !bs.is_null() {
let bs = &mut *bs;
pub unsafe extern "C" fn opendal_bytes_free(ptr: *mut opendal_bytes) {
if !ptr.is_null() {
let bs = &mut *ptr;
if !bs.data.is_null() {
drop(Vec::from_raw_parts(bs.data, bs.len, bs.capacity));
bs.data = std::ptr::null_mut();
Expand Down
2 changes: 1 addition & 1 deletion bindings/go/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func parseError(ctx context.Context, err *opendalError) error {
defer free(err)
return &Error{
code: ErrorCode(err.code),
message: string(parseBytes(&err.message)),
message: string(parseBytes(err.message)),
}
}

Expand Down
9 changes: 4 additions & 5 deletions bindings/go/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func (op *Operator) Read(path string) ([]byte, error) {
data := parseBytes(bytes)
if len(data) > 0 {
free := getFFI[bytesFree](op.ctx, symBytesFree)
free(bytes)

free(&bytes)
}
return data, nil
}
Expand Down Expand Up @@ -203,17 +202,17 @@ func (r *Reader) Close() error {

const symOperatorRead = "opendal_operator_read"

type operatorRead func(op *opendalOperator, path string) (*opendalBytes, error)
type operatorRead func(op *opendalOperator, path string) (opendalBytes, error)

var withOperatorRead = withFFI(ffiOpts{
sym: symOperatorRead,
rType: &typeResultRead,
aTypes: []*ffi.Type{&ffi.TypePointer, &ffi.TypePointer},
}, func(ctx context.Context, ffiCall func(rValue unsafe.Pointer, aValues ...unsafe.Pointer)) operatorRead {
return func(op *opendalOperator, path string) (*opendalBytes, error) {
return func(op *opendalOperator, path string) (opendalBytes, error) {
bytePath, err := unix.BytePtrFromString(path)
if err != nil {
return nil, err
return opendalBytes{}, err
}
var result resultRead
ffiCall(
Expand Down
8 changes: 4 additions & 4 deletions bindings/go/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
typeResultRead = ffi.Type{
Type: ffi.Struct,
Elements: &[]*ffi.Type{
&ffi.TypePointer,
&typeBytes,
&ffi.TypePointer,
nil,
}[0],
Expand Down Expand Up @@ -217,7 +217,7 @@ type resultOperatorNew struct {
type opendalOperator struct{}

type resultRead struct {
data *opendalBytes
data opendalBytes
error *opendalError
}

Expand Down Expand Up @@ -300,8 +300,8 @@ func toOpendalBytes(data []byte) opendalBytes {
}
}

func parseBytes(b *opendalBytes) (data []byte) {
if b == nil || b.len == 0 {
func parseBytes(b opendalBytes) (data []byte) {
if b.len == 0 {
return nil
}
data = make([]byte, b.len)
Expand Down
8 changes: 5 additions & 3 deletions bindings/zig/test/bdd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ test "Opendal BDD test" {
var testkit = OpendalBDDTest.init();
defer testkit.deinit();

const allocator = std.heap.page_allocator;
const dupe_content = try allocator.dupeZ(u8, std.mem.span(testkit.content));
// When Blocking write path "test" with content "Hello, World!"
const data: opendal.c.opendal_bytes = .{
.data = @constCast(testkit.content),
.len = std.mem.len(testkit.content),
.capacity = std.mem.len(testkit.content),
.data = dupe_content.ptr,
.len = dupe_content.len,
.capacity = dupe_content.len,
};
const result = opendal.c.opendal_operator_write(testkit.p, testkit.path, &data);
try testing.expectEqual(result, null);
Expand Down

0 comments on commit 0492f88

Please sign in to comment.