diff --git a/libs/common/include/launchdarkly/bindings/c/context.h b/libs/common/include/launchdarkly/bindings/c/context.h index 7f8bbf587..ff8080daf 100644 --- a/libs/common/include/launchdarkly/bindings/c/context.h +++ b/libs/common/include/launchdarkly/bindings/c/context.h @@ -17,6 +17,14 @@ typedef struct _LDContext* LDContext; typedef struct _LDContext_PrivateAttributesIter* LDContext_PrivateAttributesIter; +/** + * Get the canonical key for the context. + * @param context The context. Must not be NULL. + * @return Canonical key. Only valid for the lifetime of this context. + */ +LD_EXPORT(char const*) +LDContext_CanonicalKey(LDContext context); + /** * Check if the context is valid. * diff --git a/libs/common/src/bindings/c/context.cpp b/libs/common/src/bindings/c/context.cpp index cb9c690af..bf4b6bbe6 100644 --- a/libs/common/src/bindings/c/context.cpp +++ b/libs/common/src/bindings/c/context.cpp @@ -26,6 +26,13 @@ LD_EXPORT(bool) LDContext_Valid(LDContext context) { return AS_CONTEXT(context)->Valid(); } +LD_EXPORT(char const*) +LDContext_CanonicalKey(LDContext context) { + LD_ASSERT_NOT_NULL(context); + + return AS_CONTEXT(context)->CanonicalKey().c_str(); +} + LD_EXPORT(LDValue) LDContext_Get(LDContext context, char const* kind, char const* ref) { LD_ASSERT_NOT_NULL(context); diff --git a/libs/common/tests/bindings/context_test.cpp b/libs/common/tests/bindings/context_test.cpp index ebb6cd4b0..a45ecdeec 100644 --- a/libs/common/tests/bindings/context_test.cpp +++ b/libs/common/tests/bindings/context_test.cpp @@ -12,6 +12,7 @@ TEST(ContextCBindingTests, CanBuildBasicContext) { LDValue_GetString(LDContext_Get(context, "user", "key"))); EXPECT_TRUE(LDContext_Valid(context)); + EXPECT_STREQ(LDContext_CanonicalKey(context), "user-key"); LDContext_Free(context); } @@ -88,6 +89,8 @@ TEST(ContextCBindingTests, CanMakeMultiKindContext) { LDContext context = LDContextBuilder_Build(builder); + EXPECT_STREQ(LDContext_CanonicalKey(context), "org:org-key:user:user-key"); + EXPECT_EQ(std::string("user-key"), LDValue_GetString(LDContext_Get(context, "user", "key"))); @@ -114,6 +117,7 @@ TEST(ContextCBindingTests, CanCreateInvalidContext) { LDContext context = LDContextBuilder_Build(builder); EXPECT_FALSE(LDContext_Valid(context)); + EXPECT_STREQ(LDContext_CanonicalKey(context), ""); EXPECT_EQ(std::string("#)(#$@*(#^@&*: \"Kind contained invalid characters. " "A kind may contain ASCII letters or numbers, as "