Skip to content

Commit

Permalink
feat: add C binding for Context::CanonicalKey() (#349)
Browse files Browse the repository at this point in the history
Adds `LDContext_CanonicalKey`.
  • Loading branch information
cwaldren-ld authored Dec 22, 2023
1 parent 03b7de1 commit 319a3ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/common/include/launchdarkly/bindings/c/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions libs/common/src/bindings/c/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions libs/common/tests/bindings/context_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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")));

Expand All @@ -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 "
Expand Down

0 comments on commit 319a3ee

Please sign in to comment.