nix_api_value: add nix_get_attr_name_byidx get attr names without forcing

This commit is contained in:
Yorick van Pelt 2023-08-07 15:10:04 +02:00 committed by José Luis Lafuente
parent 9cccb8bae0
commit e891aac2e4
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
2 changed files with 28 additions and 0 deletions

View file

@ -266,6 +266,18 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value,
NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_NULL
} }
const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int i) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
const nix::Attr &a = (*v.attrs)[i];
return ((const std::string &)(state->state.symbols[a.name])).c_str();
}
NIXC_CATCH_ERRS_NULL
}
nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) { nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;

View file

@ -207,6 +207,8 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value,
State *state, const char *name); State *state, const char *name);
/** @brief Get an attribute by index in the sorted bindings /** @brief Get an attribute by index in the sorted bindings
*
* Also gives you the name.
* *
* Owned by the GC. Use nix_gc_decref when you're done with the pointer * Owned by the GC. Use nix_gc_decref when you're done with the pointer
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
@ -218,6 +220,20 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value,
*/ */
Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, Value *nix_get_attr_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int i, const char **name); State *state, unsigned int i, const char **name);
/** @brief Get an attribute name by index in the sorted bindings
*
* Useful when you want the name but want to avoid evaluation.
*
* Owned by the nix State
* @param[out] context Optional, stores error information
* @param[in] value Nix value to inspect
* @param[in] state nix evaluator state
* @param[in] i attribute index
* @return name, NULL in case of errors
*/
const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int i);
/**@}*/ /**@}*/
/** @name Setters /** @name Setters
*/ */