You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "kassane (via GitHub)" <gi...@apache.org> on 2023/05/31 13:15:23 UTC

[GitHub] [incubator-opendal] kassane opened a new pull request, #2381: refactor(bindings/zig): add errors handler and module test

kassane opened a new pull request, #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381

   cc: @tisonkun @Xuanwo
   
   * Add Zig Error Handler
   * Split tests - BDD test with opendal module (sample use)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211737606


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   zig `translate-c` don't read `go to` and macro functions



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] tisonkun commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211719168


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,

Review Comment:
   IIRC we map all unknown error to `Unexpected` at least for Java bindings. Maybe here is a full conversion - cc @Xuanwo @Ji-Xinyou this can be first handled in C bindings.



##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   Why do we need this trick?



##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {

Review Comment:
   ```suggestion
   pub fn errorToCode(err: OpendalError) c_int {
   ```
   
   It seems no exception can happen?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211736386


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   In `opendal.zig` replace to `testing.refAllDeclsRecursive(@This());`
   
   Get `cimport.zig`error:
   ```bash
   incubator-opendal/bindings/zig/zig-cache/o/a0e8d4131e89949c3a09815aac390bdd/cimport.zig:145:33: error: unable to translate macro: undefined identifier `L`
   pub const __INTMAX_C_SUFFIX__ = @compileError("unable to translate macro: undefined identifier `L`"); // (no file):80:9
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   referenced by:
       refAllDeclsRecursive__anon_4324: /home/kassane/zig/0.11.0-dev.3329+1ab008d89/files/lib/std/testing.zig:1129:25
       refAllDeclsRecursive__anon_1871: /home/kassane/zig/0.11.0-dev.3329+1ab008d89/files/lib/std/testing.zig:1131:56
       remaining reference traces hidden; use '-freference-trace' to see all reference traces
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] Ji-Xinyou commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "Ji-Xinyou (via GitHub)" <gi...@apache.org>.
Ji-Xinyou commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211732248


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,

Review Comment:
   In C binding, the unknown error leads to panic, you can see it [here](https://github.com/apache/incubator-opendal/blob/main/bindings/c/src/error.rs) since the `opendal::Error` represents all the errors that could happen. Any ideas? @Xuanwo 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211737606


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   zig `translate-c` don't read `goto` and macro functions



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211773822


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,

Review Comment:
   It's possible:
   
   **Method 1** (unwrap style)
   ```diff
   - else => c.OPENDAL_ERROR,
   + else => unreachable,
   ```
   **Method 2** 
   ```diff
   - else => c.OPENDAL_ERROR,
   + else => @panic(str),
   ```
   **Method 3**
   ```diff
   - else => c.OPENDAL_ERROR,
   + else => @compileError(str),
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] tisonkun commented on pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#issuecomment-1570323379

   Merging...
   
   As an under development binding I'd prefer unblock our contributors. Thank you @kassane!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211773822


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,

Review Comment:
   It's possible:
   
   **Method 1** (unwrap style)
   ```diff
   - else => c.OPENDAL_ERROR,
   + else => unreachable,
   ```
   **Method 2** 
   ```diff
   - else => c.OPENDAL_ERROR,
   + else => @panic(str),
   ```
   **Method 3**
   ```diff
   - else => c.OPENDAL_ERROR,
   + else => @compilerError(str),
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211740857


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {

Review Comment:
   https://github.com/apache/incubator-opendal/pull/2381/commits/abd282adac2f44e5bf72f1d27353acc6c61546b0



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211731220


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   read all contents file. like internal lint.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211733439


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {

Review Comment:
   Ok. However, it will affect this lines:
   https://github.com/apache/incubator-opendal/blob/7202c420513ed22ffbb0f216c7cfd408831b720c/bindings/zig/src/opendal.zig#L95-L106
   
   Need remove `try` on errorToCode



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] kassane commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "kassane (via GitHub)" <gi...@apache.org>.
kassane commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211737606


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   zig tranlate-c don't read `go to` and macro functions



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] tisonkun commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211789255


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   Perhaps it can be some zig toolchain improvements to avoid such tricks - it seems a little bit boilerplate code.



##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,
     };
-
-    var testkit = OpendalBDDTest.init();
-    defer testkit.deinit();
-
-    // When Blocking write path "test" with content "Hello, World!"
-    const data: opendal.opendal_bytes = .{
-        .data = testkit.content,
-        // c_str does not have len field (.* is ptr)
-        .len = std.mem.len(testkit.content),
+}
+pub fn errorToCode(err: OpendalError) OpendalError!c_int {
+    return switch (err) {
+        error.Unexpected => c.OPENDAL_UNEXPECTED,
+        error.Unsupported => c.OPENDAL_UNSUPPORTED,
+        error.ConfigInvalid => c.OPENDAL_CONFIG_INVALID,
+        error.NotFound => c.OPENDAL_NOT_FOUND,
+        error.PermissionDenied => c.OPENDAL_PERMISSION_DENIED,
+        error.IsDirectory => c.OPENDAL_IS_A_DIRECTORY,
+        error.IsNotDirectory => c.OPENDAL_NOT_A_DIRECTORY,
+        error.AlreadyExists => c.OPENDAL_ALREADY_EXISTS,
+        error.RateLimited => c.OPENDAL_RATE_LIMITED,
+        error.IsSameFile => c.OPENDAL_IS_SAME_FILE,
     };
-    const code = opendal.opendal_operator_blocking_write(testkit.p, testkit.path, data);
-    try testing.expectEqual(code, opendal.OPENDAL_OK);
-
-    // The blocking file "test" should exist
-    var e: opendal.opendal_result_is_exist = opendal.opendal_operator_is_exist(testkit.p, testkit.path);
-    try testing.expectEqual(e.code, opendal.OPENDAL_OK);
-    try testing.expect(e.is_exist);
-
-    // The blocking file "test" entry mode must be file
-    var s: opendal.opendal_result_stat = opendal.opendal_operator_stat(testkit.p, testkit.path);
-    try testing.expectEqual(s.code, opendal.OPENDAL_OK);
-    var meta: opendal.opendal_metadata = s.meta;
-    try testing.expect(opendal.opendal_metadata_is_file(&meta));
+}
 
-    // The blocking file "test" content length must be 13
-    try testing.expectEqual(opendal.opendal_metadata_content_length(&meta), 13);
-    defer opendal.opendal_metadata_free(&meta);
+const std = @import("std");
+const testing = std.testing;
 
-    // The blocking file "test" must have content "Hello, World!"
-    var r: opendal.opendal_result_read = opendal.opendal_operator_blocking_read(testkit.p, testkit.path);
-    defer opendal.opendal_bytes_free(r.data);
-    try testing.expect(r.code == opendal.OPENDAL_OK);
-    try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
+test "Error Tests" {
+    // C code to Zig error
+    try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED));
+    try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED));
+    try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID));
+    try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND));
+    try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED));
+    try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY));
+    try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY));
+    try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS));
+    try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED));
+    try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE));
+
+    // Zig error to C code
+    try testing.expectEqual(c.OPENDAL_UNEXPECTED, try errorToCode(error.Unexpected));
+    try testing.expectEqual(c.OPENDAL_UNSUPPORTED, try errorToCode(error.Unsupported));
+    try testing.expectEqual(c.OPENDAL_CONFIG_INVALID, try errorToCode(error.ConfigInvalid));
+    try testing.expectEqual(c.OPENDAL_NOT_FOUND, try errorToCode(error.NotFound));
+    try testing.expectEqual(c.OPENDAL_PERMISSION_DENIED, try errorToCode(error.PermissionDenied));
+    try testing.expectEqual(c.OPENDAL_IS_A_DIRECTORY, try errorToCode(error.IsDirectory));
+    try testing.expectEqual(c.OPENDAL_NOT_A_DIRECTORY, try errorToCode(error.IsNotDirectory));
+    try testing.expectEqual(c.OPENDAL_ALREADY_EXISTS, try errorToCode(error.AlreadyExists));
+    try testing.expectEqual(c.OPENDAL_RATE_LIMITED, try errorToCode(error.RateLimited));
+    try testing.expectEqual(c.OPENDAL_IS_SAME_FILE, try errorToCode(error.IsSameFile));
+}
 
-    var count: usize = 0;
-    while (count < r.data.*.len) : (count += 1) {
-        try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
-    }
+test "Semantic Analyzer" {

Review Comment:
   Thanks for your explanation!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] Ji-Xinyou commented on a diff in pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "Ji-Xinyou (via GitHub)" <gi...@apache.org>.
Ji-Xinyou commented on code in PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381#discussion_r1211778788


##########
bindings/zig/src/opendal.zig:
##########
@@ -15,79 +15,96 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub const opendal = @cImport(@cInclude("opendal.h"));
-
-const std = @import("std");
-const testing = std.testing;
-
-test "Opendal BDD test" {
-    const c_str = [*:0]const u8; // define a type for 'const char*' in C
-
-    const OpendalBDDTest = struct {
-        p: opendal.opendal_operator_ptr,
-        scheme: c_str,
-        path: c_str,
-        content: c_str,
-
-        pub fn init() Self {
-            var self: Self = undefined;
-            self.scheme = "memory";
-            self.path = "test";
-            self.content = "Hello, World!";
-
-            var options: opendal.opendal_operator_options = opendal.opendal_operator_options_new();
-            defer opendal.opendal_operator_options_free(&options);
-            opendal.opendal_operator_options_set(&options, "root", "/myroot");
-
-            // Given A new OpenDAL Blocking Operator
-            self.p = opendal.opendal_operator_new(self.scheme, &options);
-            std.debug.assert(self.p.ptr != null);
-
-            return self;
-        }
-
-        pub fn deinit(self: *Self) void {
-            opendal.opendal_operator_free(&self.p);
-        }
-
-        const Self = @This();
+pub const c = @cImport(@cInclude("opendal.h"));
+
+// Zig code get values C code
+pub const Code = enum(c.opendal_code) {
+    OK = c.OPENDAL_OK,
+    ERROR = c.OPENDAL_ERROR,
+    UNEXPECTED = c.OPENDAL_UNEXPECTED,
+    UNSUPPORTED = c.OPENDAL_UNSUPPORTED,
+    CONFIG_INVALID = c.OPENDAL_CONFIG_INVALID,
+    NOT_FOUND = c.OPENDAL_NOT_FOUND,
+    PERMISSION_DENIED = c.OPENDAL_PERMISSION_DENIED,
+    IS_A_DIRECTORY = c.OPENDAL_IS_A_DIRECTORY,
+    NOT_A_DIRECTORY = c.OPENDAL_NOT_A_DIRECTORY,
+    ALREADY_EXISTS = c.OPENDAL_ALREADY_EXISTS,
+    RATE_LIMITED = c.OPENDAL_RATE_LIMITED,
+    IS_SAME_FILE = c.OPENDAL_IS_SAME_FILE,
+};
+
+pub const OpendalError = error{
+    Unexpected,
+    Unsupported,
+    ConfigInvalid,
+    NotFound,
+    PermissionDenied,
+    IsDirectory,
+    IsNotDirectory,
+    AlreadyExists,
+    RateLimited,
+    IsSameFile,
+};
+
+pub fn codeToError(code: c.opendal_code) OpendalError!c.opendal_code {
+    return switch (code) {
+        c.OPENDAL_UNEXPECTED => error.Unexpected,
+        c.OPENDAL_UNSUPPORTED => error.Unsupported,
+        c.OPENDAL_NOT_FOUND => error.NotFound,
+        c.OPENDAL_CONFIG_INVALID => error.ConfigInvalid,
+        c.OPENDAL_PERMISSION_DENIED => error.PermissionDenied,
+        c.OPENDAL_IS_A_DIRECTORY => error.IsDirectory,
+        c.OPENDAL_NOT_A_DIRECTORY => error.IsNotDirectory,
+        c.OPENDAL_ALREADY_EXISTS => error.AlreadyExists,
+        c.OPENDAL_RATE_LIMITED => error.RateLimited,
+        c.OPENDAL_IS_SAME_FILE => error.IsSameFile,
+        else => c.OPENDAL_ERROR,

Review Comment:
   > It's possible:
   > 
   > **Method 1** (unwrap style)
   > 
   > ```diff
   > - else => c.OPENDAL_ERROR,
   > + else => unreachable,
   > ```
   > 
   > **Method 2**
   > 
   > ```diff
   > - else => c.OPENDAL_ERROR,
   > + else => @panic(str),
   > ```
   > 
   > **Method 3**
   > 
   > ```diff
   > - else => c.OPENDAL_ERROR,
   > + else => @compilerError(str),
   > ```
   
   I personally prefer compilerError one. I was trying to do that in Rust, but rustc does not support that 🤣 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-opendal] tisonkun merged pull request #2381: refactor(bindings/zig): add errors handler and module test

Posted by "tisonkun (via GitHub)" <gi...@apache.org>.
tisonkun merged PR #2381:
URL: https://github.com/apache/incubator-opendal/pull/2381


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org