Skip to content

Commit 319a3ee

Browse files
authored
feat: add C binding for Context::CanonicalKey() (#349)
Adds `LDContext_CanonicalKey`.
1 parent 03b7de1 commit 319a3ee

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

libs/common/include/launchdarkly/bindings/c/context.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ typedef struct _LDContext* LDContext;
1717
typedef struct _LDContext_PrivateAttributesIter*
1818
LDContext_PrivateAttributesIter;
1919

20+
/**
21+
* Get the canonical key for the context.
22+
* @param context The context. Must not be NULL.
23+
* @return Canonical key. Only valid for the lifetime of this context.
24+
*/
25+
LD_EXPORT(char const*)
26+
LDContext_CanonicalKey(LDContext context);
27+
2028
/**
2129
* Check if the context is valid.
2230
*

libs/common/src/bindings/c/context.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ LD_EXPORT(bool) LDContext_Valid(LDContext context) {
2626
return AS_CONTEXT(context)->Valid();
2727
}
2828

29+
LD_EXPORT(char const*)
30+
LDContext_CanonicalKey(LDContext context) {
31+
LD_ASSERT_NOT_NULL(context);
32+
33+
return AS_CONTEXT(context)->CanonicalKey().c_str();
34+
}
35+
2936
LD_EXPORT(LDValue)
3037
LDContext_Get(LDContext context, char const* kind, char const* ref) {
3138
LD_ASSERT_NOT_NULL(context);

libs/common/tests/bindings/context_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TEST(ContextCBindingTests, CanBuildBasicContext) {
1212
LDValue_GetString(LDContext_Get(context, "user", "key")));
1313

1414
EXPECT_TRUE(LDContext_Valid(context));
15+
EXPECT_STREQ(LDContext_CanonicalKey(context), "user-key");
1516

1617
LDContext_Free(context);
1718
}
@@ -88,6 +89,8 @@ TEST(ContextCBindingTests, CanMakeMultiKindContext) {
8889

8990
LDContext context = LDContextBuilder_Build(builder);
9091

92+
EXPECT_STREQ(LDContext_CanonicalKey(context), "org:org-key:user:user-key");
93+
9194
EXPECT_EQ(std::string("user-key"),
9295
LDValue_GetString(LDContext_Get(context, "user", "key")));
9396

@@ -114,6 +117,7 @@ TEST(ContextCBindingTests, CanCreateInvalidContext) {
114117
LDContext context = LDContextBuilder_Build(builder);
115118

116119
EXPECT_FALSE(LDContext_Valid(context));
120+
EXPECT_STREQ(LDContext_CanonicalKey(context), "");
117121

118122
EXPECT_EQ(std::string("#)(#$@*(#^@&*: \"Kind contained invalid characters. "
119123
"A kind may contain ASCII letters or numbers, as "

0 commit comments

Comments
 (0)