This is an automated email from the ASF dual-hosted git repository.

yuchanns pushed a commit to branch feat/go-binding/presign
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/feat/go-binding/presign by 
this push:
     new fe97e631d feat(bindings/c): reduce code
fe97e631d is described below

commit fe97e631dd33ae483d9890a2b9bb6382a2448a27
Author: Hanchin Hsieh <[email protected]>
AuthorDate: Tue Jan 13 16:14:30 2026 +0800

    feat(bindings/c): reduce code
---
 bindings/c/tests/opendal_test_runner     | Bin 416312 -> 414816 bytes
 bindings/c/tests/test_suites_presign.cpp | 175 ++++++++++++++++++++-----------
 2 files changed, 113 insertions(+), 62 deletions(-)

diff --git a/bindings/c/tests/opendal_test_runner 
b/bindings/c/tests/opendal_test_runner
index 1ac1d1a2d..bd1d46393 100755
Binary files a/bindings/c/tests/opendal_test_runner and 
b/bindings/c/tests/opendal_test_runner differ
diff --git a/bindings/c/tests/test_suites_presign.cpp 
b/bindings/c/tests/test_suites_presign.cpp
index 3ce3354d6..c6a9e1da1 100644
--- a/bindings/c/tests/test_suites_presign.cpp
+++ b/bindings/c/tests/test_suites_presign.cpp
@@ -300,6 +300,72 @@ static CURLcode presign_set_standard_options(CURL* curl, 
const char* url, const
     return opt_res;
 }
 
+typedef enum presign_prepare_result {
+    PRESIGN_PREPARE_OK = 0,
+    PRESIGN_PREPARE_BUILD_HEADERS_FAIL,
+    PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL,
+    PRESIGN_PREPARE_SET_HEADERS_FAIL,
+} presign_prepare_result;
+
+static presign_prepare_result presign_prepare_curl(CURL* curl,
+    const char* url,
+    const char* method,
+    const opendal_http_header_pair* headers,
+    uintptr_t headers_len,
+    size_t override_content_len,
+    struct curl_slist** out_chunk,
+    CURLcode* out_error)
+{
+    if (out_chunk == NULL) {
+        if (out_error != NULL) {
+            *out_error = CURLE_FAILED_INIT;
+        }
+        return PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL;
+    }
+
+    *out_chunk = NULL;
+
+    if (!presign_build_header_list(headers, headers_len, override_content_len, 
out_chunk)) {
+        if (*out_chunk != NULL) {
+            curl_slist_free_all(*out_chunk);
+            *out_chunk = NULL;
+        }
+        if (out_error != NULL) {
+            *out_error = CURLE_OK;
+        }
+        return PRESIGN_PREPARE_BUILD_HEADERS_FAIL;
+    }
+
+    CURLcode opt_res = presign_set_standard_options(curl, url, method);
+    if (opt_res != CURLE_OK) {
+        if (*out_chunk != NULL) {
+            curl_slist_free_all(*out_chunk);
+            *out_chunk = NULL;
+        }
+        if (out_error != NULL) {
+            *out_error = opt_res;
+        }
+        return PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL;
+    }
+
+    if (*out_chunk != NULL) {
+        opt_res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, *out_chunk);
+        if (opt_res != CURLE_OK) {
+            curl_slist_free_all(*out_chunk);
+            *out_chunk = NULL;
+            if (out_error != NULL) {
+                *out_error = opt_res;
+            }
+            return PRESIGN_PREPARE_SET_HEADERS_FAIL;
+        }
+    }
+
+    if (out_error != NULL) {
+        *out_error = CURLE_OK;
+    }
+    return PRESIGN_PREPARE_OK;
+}
+
 // Test: Presign read operation
 void test_presign_read(opendal_test_context* ctx)
 {
@@ -332,26 +398,22 @@ void test_presign_read(opendal_test_context* ctx)
 
     size_t expected_len = strlen(content);
     struct curl_slist* chunk = NULL;
-    int build_ok = presign_build_header_list(headers, headers_len, 
PRESIGN_NO_OVERRIDE, &chunk);
-    if (!build_ok) {
+    CURLcode setup_error = CURLE_OK;
+    presign_prepare_result prepare_res = presign_prepare_curl(curl, url, 
method,
+        headers, headers_len, PRESIGN_NO_OVERRIDE, &chunk, &setup_error);
+    if (prepare_res != PRESIGN_PREPARE_OK) {
         presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT(build_ok, "Building CURL headers should succeed");
-    }
-
-    CURLcode opt_res = presign_set_standard_options(curl, url, method);
-    if (opt_res != CURLE_OK) {
-        presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting standard CURL options 
should succeed");
-    }
-
-    if (chunk != NULL) {
-        opt_res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-        if (opt_res != CURLE_OK) {
-            presign_cleanup_curl(curl, chunk);
-            OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting CURL headers should 
succeed");
+        if (prepare_res == PRESIGN_PREPARE_BUILD_HEADERS_FAIL) {
+            OPENDAL_ASSERT(0, "Building CURL headers should succeed");
+        } else if (prepare_res == PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL) {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting standard CURL 
options should succeed");
+        } else {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting CURL headers 
should succeed");
         }
     }
 
+    CURLcode opt_res = CURLE_OK;
+
     presign_header_context header_ctx;
     header_ctx.content_length = 0;
     header_ctx.content_length_found = 0;
@@ -431,25 +493,22 @@ void test_presign_write(opendal_test_context* ctx)
     OPENDAL_ASSERT_NOT_NULL(curl, "CURL initialization should succeed");
 
     struct curl_slist* chunk = NULL;
-    int build_ok = presign_build_header_list(headers, headers_len, 
content_len, &chunk);
-    if (!build_ok) {
+    CURLcode setup_error = CURLE_OK;
+    presign_prepare_result prepare_res = presign_prepare_curl(curl, url, 
method,
+        headers, headers_len, content_len, &chunk, &setup_error);
+    if (prepare_res != PRESIGN_PREPARE_OK) {
         presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT(build_ok, "Building CURL headers should succeed");
-    }
-    CURLcode opt_res = presign_set_standard_options(curl, url, method);
-    if (opt_res != CURLE_OK) {
-        presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting standard CURL options 
should succeed");
-    }
-
-    if (chunk != NULL) {
-        opt_res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-        if (opt_res != CURLE_OK) {
-            presign_cleanup_curl(curl, chunk);
-            OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting CURL headers should 
succeed");
+        if (prepare_res == PRESIGN_PREPARE_BUILD_HEADERS_FAIL) {
+            OPENDAL_ASSERT(0, "Building CURL headers should succeed");
+        } else if (prepare_res == PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL) {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting standard CURL 
options should succeed");
+        } else {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting CURL headers 
should succeed");
         }
     }
 
+    CURLcode opt_res = CURLE_OK;
+
     presign_upload_context upload_ctx;
     upload_ctx.data = content;
     upload_ctx.len = content_len;
@@ -549,25 +608,22 @@ void test_presign_stat(opendal_test_context* ctx)
     OPENDAL_ASSERT_NOT_NULL(curl, "CURL initialization should succeed");
 
     struct curl_slist* chunk = NULL;
-    int build_ok = presign_build_header_list(headers, headers_len, 
PRESIGN_NO_OVERRIDE, &chunk);
-    if (!build_ok) {
-        presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT(build_ok, "Building CURL headers should succeed");
-    }
-    CURLcode opt_res = presign_set_standard_options(curl, url, method);
-    if (opt_res != CURLE_OK) {
+    CURLcode setup_error = CURLE_OK;
+    presign_prepare_result prepare_res = presign_prepare_curl(curl, url, 
method,
+        headers, headers_len, PRESIGN_NO_OVERRIDE, &chunk, &setup_error);
+    if (prepare_res != PRESIGN_PREPARE_OK) {
         presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting standard CURL options 
should succeed");
-    }
-
-    if (chunk != NULL) {
-        opt_res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-        if (opt_res != CURLE_OK) {
-            presign_cleanup_curl(curl, chunk);
-            OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting CURL headers should 
succeed");
+        if (prepare_res == PRESIGN_PREPARE_BUILD_HEADERS_FAIL) {
+            OPENDAL_ASSERT(0, "Building CURL headers should succeed");
+        } else if (prepare_res == PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL) {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting standard CURL 
options should succeed");
+        } else {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting CURL headers 
should succeed");
         }
     }
 
+    CURLcode opt_res = CURLE_OK;
+
     presign_header_context header_ctx;
     header_ctx.content_length = 0;
     header_ctx.content_length_found = 0;
@@ -649,26 +705,21 @@ void test_presign_delete(opendal_test_context* ctx)
     OPENDAL_ASSERT_NOT_NULL(curl, "CURL initialization should succeed");
 
     struct curl_slist* chunk = NULL;
-    int build_ok = presign_build_header_list(headers, headers_len, 
PRESIGN_NO_OVERRIDE, &chunk);
-    if (!build_ok) {
-        presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT(build_ok, "Building CURL headers should succeed");
-    }
-    CURLcode opt_res = presign_set_standard_options(curl, url, method);
-    if (opt_res != CURLE_OK) {
+    CURLcode setup_error = CURLE_OK;
+    presign_prepare_result prepare_res = presign_prepare_curl(curl, url, 
method,
+        headers, headers_len, PRESIGN_NO_OVERRIDE, &chunk, &setup_error);
+    if (prepare_res != PRESIGN_PREPARE_OK) {
         presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting standard CURL options 
should succeed");
-    }
-
-    if (chunk != NULL) {
-        opt_res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-        if (opt_res != CURLE_OK) {
-            presign_cleanup_curl(curl, chunk);
-            OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting CURL headers should 
succeed");
+        if (prepare_res == PRESIGN_PREPARE_BUILD_HEADERS_FAIL) {
+            OPENDAL_ASSERT(0, "Building CURL headers should succeed");
+        } else if (prepare_res == PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL) {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting standard CURL 
options should succeed");
+        } else {
+            OPENDAL_ASSERT_EQ(CURLE_OK, setup_error, "Setting CURL headers 
should succeed");
         }
     }
 
-    opt_res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, 
presign_sink_callback);
+    CURLcode opt_res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, 
presign_sink_callback);
     if (opt_res != CURLE_OK) {
         presign_cleanup_curl(curl, chunk);
         OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting CURL sink callback 
should succeed");

Reply via email to