C API: nix_get_string now accepts a callback to return the value

This commit is contained in:
José Luis Lafuente 2024-04-15 21:05:52 +02:00
parent 74e4bc9b1d
commit c75b143b6c
No known key found for this signature in database
GPG Key ID: 8A3455EBE455489A
6 changed files with 65 additions and 35 deletions

View File

@ -40,10 +40,19 @@ Nix expression `builtins.nixVersion`.
#include <nix_api_expr.h> #include <nix_api_expr.h>
#include <nix_api_value.h> #include <nix_api_value.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
// NOTE: This example lacks all error handling. Production code must check for // NOTE: This example lacks all error handling. Production code must check for
// errors, as some return values will be undefined. // errors, as some return values will be undefined.
int main() {
void my_get_string_cb(const char * start, unsigned int n, char ** user_data)
{
*user_data = strdup(start);
}
int main()
{
nix_libexpr_init(NULL); nix_libexpr_init(NULL);
Store * store = nix_store_open(NULL, "dummy://", NULL); Store * store = nix_store_open(NULL, "dummy://", NULL);
@ -52,8 +61,12 @@ int main() {
nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value);
nix_value_force(NULL, state, value); nix_value_force(NULL, state, value);
printf("Nix version: %s\n", nix_get_string(NULL, value));
char * version;
nix_get_string(NULL, value, my_get_string_cb, version);
printf("Nix version: %s\n", version);
free(version);
nix_gc_decref(NULL, value); nix_gc_decref(NULL, value);
nix_state_free(state); nix_state_free(state);
nix_store_free(store); nix_store_free(store);

View File

@ -166,16 +166,16 @@ bool nix_get_bool(nix_c_context * context, const Value * value)
NIXC_CATCH_ERRS_RES(false); NIXC_CATCH_ERRS_RES(false);
} }
const char * nix_get_string(nix_c_context * context, const Value * value) nix_err nix_get_string(nix_c_context * context, const Value * value, nix_get_string_callback callback, void * user_data)
{ {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
auto & v = check_value_not_null(value); auto & v = check_value_not_null(value);
assert(v.type() == nix::nString); assert(v.type() == nix::nString);
return v.c_str(); call_nix_get_string_callback(v.c_str(), callback, user_data);
} }
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS
} }
const char * nix_get_path_string(nix_c_context * context, const Value * value) const char * nix_get_path_string(nix_c_context * context, const Value * value)

View File

@ -178,10 +178,13 @@ bool nix_get_bool(nix_c_context * context, const Value * value);
* *
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect * @param[in] value Nix value to inspect
* @param[in] callback Called with the string value.
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called.
* @return string * @return string
* @return NULL in case of error. * @return error code, NIX_OK on success.
*/ */
const char * nix_get_string(nix_c_context * context, const Value * value); nix_err
nix_get_string(nix_c_context * context, const Value * value, nix_get_string_callback callback, void * user_data);
/** @brief Get path as string /** @brief Get path as string
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
@ -482,7 +485,6 @@ const StorePath * nix_realised_string_get_store_path(nix_realised_string * reali
*/ */
void nix_realised_string_free(nix_realised_string * realised_string); void nix_realised_string_free(nix_realised_string * realised_string);
// cffi end // cffi end
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -17,9 +17,10 @@ TEST_F(nix_api_expr_test, nix_expr_eval_from_string)
{ {
nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value); nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value);
nix_value_force(nullptr, state, value); nix_value_force(nullptr, state, value);
auto result = nix_get_string(nullptr, value); std::string result;
nix_get_string(nullptr, value, OBSERVE_STRING(result));
ASSERT_STREQ(PACKAGE_VERSION, result); ASSERT_STREQ(PACKAGE_VERSION, result.c_str());
} }
TEST_F(nix_api_expr_test, nix_expr_eval_add_numbers) TEST_F(nix_api_expr_test, nix_expr_eval_add_numbers)
@ -47,7 +48,8 @@ TEST_F(nix_api_expr_test, nix_expr_eval_drv)
nix_value_call(ctx, stateResult, valueFn, value, valueResult); nix_value_call(ctx, stateResult, valueFn, value, valueResult);
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, valueResult)); ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, valueResult));
std::string p = nix_get_string(nullptr, valueResult); std::string p;
nix_get_string(nullptr, valueResult, OBSERVE_STRING(p));
std::string pEnd = "-myname"; std::string pEnd = "-myname";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size())); ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
@ -69,7 +71,8 @@ TEST_F(nix_api_expr_test, nix_build_drv)
nix_expr_eval_from_string(nullptr, state, expr, ".", value); nix_expr_eval_from_string(nullptr, state, expr, ".", value);
Value * drvPathValue = nix_get_attr_byname(nullptr, value, state, "drvPath"); Value * drvPathValue = nix_get_attr_byname(nullptr, value, state, "drvPath");
const char * drvPath = nix_get_string(nullptr, drvPathValue); std::string drvPath;
nix_get_string(nullptr, drvPathValue, OBSERVE_STRING(drvPath));
std::string p = drvPath; std::string p = drvPath;
std::string pEnd = "-myname.drv"; std::string pEnd = "-myname.drv";
@ -78,18 +81,19 @@ TEST_F(nix_api_expr_test, nix_build_drv)
// NOTE: .drvPath should be usually be ignored. Output paths are more versatile. // NOTE: .drvPath should be usually be ignored. Output paths are more versatile.
// See https://github.com/NixOS/nix/issues/6507 // See https://github.com/NixOS/nix/issues/6507
// Use e.g. nix_string_realise to realise the output. // Use e.g. nix_string_realise to realise the output.
StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath); StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath.c_str());
ASSERT_EQ(true, nix_store_is_valid_path(ctx, store, drvStorePath)); ASSERT_EQ(true, nix_store_is_valid_path(ctx, store, drvStorePath));
Value * outPathValue = nix_get_attr_byname(ctx, value, state, "outPath"); Value * outPathValue = nix_get_attr_byname(ctx, value, state, "outPath");
const char * outPath = nix_get_string(ctx, outPathValue); std::string outPath;
nix_get_string(ctx, outPathValue, OBSERVE_STRING(outPath));
p = outPath; p = outPath;
pEnd = "-myname"; pEnd = "-myname";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size())); ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
ASSERT_EQ(true, drvStorePath->path.isDerivation()); ASSERT_EQ(true, drvStorePath->path.isDerivation());
StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath); StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath.c_str());
ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, outStorePath)); ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, outStorePath));
nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr); nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr);

View File

@ -6,7 +6,9 @@
#include "nix_api_expr_internal.h" #include "nix_api_expr_internal.h"
#include "nix_api_value.h" #include "nix_api_value.h"
#include "nix_api_external.h" #include "nix_api_external.h"
#include "tests/nix_api_expr.hh" #include "tests/nix_api_expr.hh"
#include "tests/string_callback.hh"
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -58,6 +60,9 @@ TEST_F(nix_api_expr_test, nix_expr_eval_external)
nix_value_call(ctx, state, valueFn, value, valueResult); nix_value_call(ctx, state, valueFn, value, valueResult);
ASSERT_STREQ("nix-external<MyExternalValueDesc( 42 )>", nix_get_string(nullptr, valueResult)); std::string string_value;
nix_get_string(nullptr, valueResult, OBSERVE_STRING(string_value));
ASSERT_STREQ("nix-external<MyExternalValueDesc( 42 )>", string_value.c_str());
} }
} }

View File

@ -6,6 +6,7 @@
#include "nix_api_value.h" #include "nix_api_value.h"
#include "tests/nix_api_expr.hh" #include "tests/nix_api_expr.hh"
#include "tests/string_callback.hh"
#include <cstdlib> #include <cstdlib>
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -53,13 +54,15 @@ TEST_F(nix_api_expr_test, nix_value_set_get_bool)
TEST_F(nix_api_expr_test, nix_value_set_get_string) TEST_F(nix_api_expr_test, nix_value_set_get_string)
{ {
ASSERT_EQ(nullptr, nix_get_string(ctx, nullptr)); std::string string_value;
ASSERT_DEATH(nix_get_string(ctx, value), ""); ASSERT_EQ(NIX_ERR_UNKNOWN, nix_get_string(ctx, nullptr, OBSERVE_STRING(string_value)));
ASSERT_DEATH(nix_get_string(ctx, value, OBSERVE_STRING(string_value)), "");
const char * myString = "some string"; const char * myString = "some string";
nix_init_string(ctx, value, myString); nix_init_string(ctx, value, myString);
ASSERT_STREQ(myString, nix_get_string(ctx, value)); nix_get_string(ctx, value, OBSERVE_STRING(string_value));
ASSERT_STREQ(myString, string_value.c_str());
ASSERT_STREQ("a string", nix_get_typename(ctx, value)); ASSERT_STREQ("a string", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(ctx, value)); ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(ctx, value));
} }
@ -162,11 +165,14 @@ TEST_F(nix_api_expr_test, nix_build_and_init_attr)
ASSERT_EQ(false, nix_has_attr_byname(ctx, value, state, "no-value")); ASSERT_EQ(false, nix_has_attr_byname(ctx, value, state, "no-value"));
out_value = nix_get_attr_byname(ctx, value, state, "b"); out_value = nix_get_attr_byname(ctx, value, state, "b");
ASSERT_STREQ("foo", nix_get_string(ctx, out_value)); std::string string_value;
nix_get_string(ctx, out_value, OBSERVE_STRING(string_value));
ASSERT_STREQ("foo", string_value.c_str());
nix_gc_decref(nullptr, out_value); nix_gc_decref(nullptr, out_value);
out_value = nix_get_attr_byidx(ctx, value, state, 1, out_name); out_value = nix_get_attr_byidx(ctx, value, state, 1, out_name);
ASSERT_STREQ("foo", nix_get_string(ctx, out_value)); nix_get_string(ctx, out_value, OBSERVE_STRING(string_value));
ASSERT_STREQ("foo", string_value.c_str());
ASSERT_STREQ("b", *out_name); ASSERT_STREQ("b", *out_name);
nix_gc_decref(nullptr, out_value); nix_gc_decref(nullptr, out_value);