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

commit 267d62b243cd331345e31ec0684a99a48db0d18a
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 | 171 ++++++++++++++++++-------------
 2 files changed, 100 insertions(+), 71 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..4ade2815f 100644
--- a/bindings/c/tests/test_suites_presign.cpp
+++ b/bindings/c/tests/test_suites_presign.cpp
@@ -300,6 +300,86 @@ 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;
+}
+
+#define PRESIGN_ASSERT_PREPARE_OK(curl_handle, chunk_handle, prepare_result, 
prepare_error) \
+    do { \
+        if ((prepare_result) != PRESIGN_PREPARE_OK) { \
+            presign_cleanup_curl((curl_handle), (chunk_handle)); \
+            if ((prepare_result) == PRESIGN_PREPARE_BUILD_HEADERS_FAIL) { \
+                OPENDAL_ASSERT(0, "Building CURL headers should succeed"); \
+            } else if ((prepare_result) == 
PRESIGN_PREPARE_STANDARD_OPTIONS_FAIL) { \
+                OPENDAL_ASSERT_EQ(CURLE_OK, (prepare_error), "Setting standard 
CURL options should succeed"); \
+            } else { \
+                OPENDAL_ASSERT_EQ(CURLE_OK, (prepare_error), "Setting CURL 
headers should succeed"); \
+            } \
+        } \
+    } while (0)
+
 // Test: Presign read operation
 void test_presign_read(opendal_test_context* ctx)
 {
@@ -332,25 +412,12 @@ 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) {
-        presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT(build_ok, "Building CURL headers should succeed");
-    }
+    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);
+    PRESIGN_ASSERT_PREPARE_OK(curl, chunk, prepare_res, setup_error);
 
-    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");
-        }
-    }
+    CURLcode opt_res = CURLE_OK;
 
     presign_header_context header_ctx;
     header_ctx.content_length = 0;
@@ -431,24 +498,12 @@ 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) {
-        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");
-    }
+    CURLcode setup_error = CURLE_OK;
+    presign_prepare_result prepare_res = presign_prepare_curl(curl, url, 
method,
+        headers, headers_len, content_len, &chunk, &setup_error);
+    PRESIGN_ASSERT_PREPARE_OK(curl, chunk, prepare_res, setup_error);
 
-    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");
-        }
-    }
+    CURLcode opt_res = CURLE_OK;
 
     presign_upload_context upload_ctx;
     upload_ctx.data = content;
@@ -549,24 +604,12 @@ 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) {
-        presign_cleanup_curl(curl, chunk);
-        OPENDAL_ASSERT_EQ(CURLE_OK, opt_res, "Setting standard CURL options 
should succeed");
-    }
+    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);
+    PRESIGN_ASSERT_PREPARE_OK(curl, chunk, prepare_res, setup_error);
 
-    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");
-        }
-    }
+    CURLcode opt_res = CURLE_OK;
 
     presign_header_context header_ctx;
     header_ctx.content_length = 0;
@@ -649,26 +692,12 @@ 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) {
-        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");
-        }
-    }
+    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);
+    PRESIGN_ASSERT_PREPARE_OK(curl, chunk, prepare_res, setup_error);
 
-    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