diff --git a/CHANGELOG.md b/CHANGELOG.md index ba5eda662..1e8a369c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +**Features**: + +- Add `sentry_value_get_key(value, index)` function. ([#1142](https://github.com/getsentry/sentry-native/pull/1142)) + ## 0.7.20 **Features**: diff --git a/include/sentry.h b/include/sentry.h index 39f4b2cc7..29eec6922 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -290,6 +290,12 @@ SENTRY_API sentry_value_t sentry_value_get_by_key_owned( SENTRY_API sentry_value_t sentry_value_get_by_key_owned_n( sentry_value_t value, const char *k, size_t k_len); +/** + * Returns a key in a map by index. If missing or value is not a map, + * an empty string is returned. + */ +SENTRY_API const char *sentry_value_get_key(sentry_value_t value, size_t index); + /** * Looks up a value in a list by index. If missing a null value is returned. * The returned value is borrowed. diff --git a/src/sentry_value.c b/src/sentry_value.c index 1258ee818..f15a5d4b5 100644 --- a/src/sentry_value.c +++ b/src/sentry_value.c @@ -778,6 +778,19 @@ sentry_value_get_by_key_owned(sentry_value_t value, const char *k) return rv; } +const char * +sentry_value_get_key(sentry_value_t value, size_t index) +{ + const thing_t *thing = value_as_thing(value); + if (thing && thing_get_type(thing) == THING_TYPE_OBJECT) { + obj_t *o = thing->payload._ptr; + if (index < o->len) { + return o->pairs[index].k; + } + } + return ""; +} + sentry_value_t sentry_value_get_by_index(sentry_value_t value, size_t index) { diff --git a/tests/unit/test_value.c b/tests/unit/test_value.c index 8e738bc1d..2351b4c51 100644 --- a/tests/unit/test_value.c +++ b/tests/unit/test_value.c @@ -252,8 +252,10 @@ SENTRY_TEST(value_object) sentry_value_t child = sentry_value_get_by_key(val, key); if (i < 10) { TEST_CHECK(sentry_value_as_int32(child) == (int32_t)i); + TEST_CHECK(strcmp(sentry_value_get_key(val, i), key) == 0); } else { TEST_CHECK(sentry_value_is_null(child)); + TEST_CHECK(strlen(sentry_value_get_key(val, i)) == 0); } }