Skip to content

Commit afb943c

Browse files
feat: Implement C-binding for Value type. (#33)
Co-authored-by: Casey Waldren <[email protected]>
1 parent 4de5eaa commit afb943c

19 files changed

+1474
-11
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ set(CMAKE_CXX_STANDARD 17)
2424

2525
option(BUILD_TESTING "Enable C++ unit tests." ON)
2626

27+
# If you want to run the unit tests with valgrind, then TESTING_SANITIZERS must of OFF.
28+
option(TESTING_SANITIZERS "Enable sanitizers for unit tests." ON)
29+
2730
if (BUILD_TESTING)
31+
if (TESTING_SANITIZERS)
32+
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
33+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=undefined -fsanitize=leak")
34+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL " GNU")
35+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=leak")
36+
endif ()
37+
endif ()
2838
include(FetchContent)
2939
FetchContent_Declare(
3040
googletest

libs/common/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
2121
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
2222
endif ()
2323

24-
# Needed to fetch external dependencies.
24+
# Needed to fetch external dependencies.
2525
include(FetchContent)
2626

2727
set(OPENSSL_USE_STATIC_LIBS OFF)

libs/common/include/attributes_builder.hpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,7 @@ class AttributesBuilder final {
126126
* In this example, firstName is marked as private, but lastName is not:
127127
*
128128
* ```
129-
* const context = {
130-
* kind: 'org',
131-
* key: 'my-key',
132-
* firstName: 'Pierre',
133-
* lastName: 'Menard',
134-
* _meta: {
135-
* privateAttributes: ['firstName'],
136-
* }
137-
* };
129+
* [TODO]
138130
* ```
139131
*
140132
* This is a metadata property, rather than an attribute that can be
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// NOLINTBEGIN modernize-use-using
2+
3+
#pragma once
4+
5+
#include "./export.h"
6+
#include "./value.h"
7+
8+
#ifdef __cplusplus
9+
extern "C" { // only need to export C interface if
10+
// used by C++ source code
11+
#endif
12+
13+
typedef struct _LDArrayBuilder* LDArrayBuilder;
14+
15+
/**
16+
* Construct a new array builder.
17+
* @return The new array builder.
18+
*
19+
*/
20+
LD_EXPORT(LDArrayBuilder) LDArrayBuilder_New();
21+
22+
/**
23+
* Free an array builder. This should only be done for a builder which
24+
* has not been built. Calling LDArrayBuilder_Build on an array builder
25+
* transfers consumes the array builder.
26+
*
27+
* @param array_builder The builder to free.
28+
*/
29+
LD_EXPORT(void) LDArrayBuilder_Free(LDArrayBuilder array_builder);
30+
31+
/**
32+
* Add a value to an array builder.
33+
*
34+
* After calling this method the provider LDValue is consumed. It should not
35+
* be accessed, and the caller doesn't need to call LDValue_Free.
36+
*
37+
* @param array_builder The array builder to add the value to.
38+
* @param val The value to add.
39+
*/
40+
LD_EXPORT(void) LDArrayBuilder_Add(LDArrayBuilder array_builder, LDValue val);
41+
42+
/**
43+
* Construct an LDValue from an array builder.
44+
*
45+
* After calling this method the array builder is consumed. It should not be
46+
* used and the caller does not need to call LDArrayBuilder_Free.
47+
*
48+
* @param array_builder The array builder to build an LDValue from.
49+
* @return The built LDValue.
50+
*/
51+
LD_EXPORT(LDValue) LDArrayBuilder_Build(LDArrayBuilder array_builder);
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
57+
// NOLINTEND modernize-use-using
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// NOLINTBEGIN modernize-use-using
2+
3+
#pragma once
4+
5+
#include "./export.h"
6+
#include "./value.h"
7+
8+
#include <stdbool.h>
9+
10+
#ifdef __cplusplus
11+
extern "C" { // only need to export C interface if
12+
// used by C++ source code
13+
#endif
14+
15+
typedef struct _LDContext* LDContext;
16+
typedef struct _LDContext_PrivateAttributesIter*
17+
LDContext_PrivateAttributesIter;
18+
19+
/**
20+
* Check if the context is valid.
21+
*
22+
* @param context The context to check.
23+
* @return True if the context is valid.
24+
*/
25+
LD_EXPORT(bool) LDContext_Valid(LDContext context);
26+
27+
/**
28+
* Free the context.
29+
*
30+
* @param context The context to free.
31+
*/
32+
LD_EXPORT(void) LDContext_Free(LDContext context);
33+
34+
/**
35+
* Get an attribute value by kind and attribute reference. If the kind is
36+
* not present, or the attribute not present in the kind, then
37+
* a null pointer will be returned.
38+
*
39+
* The lifetime of the LDValue is tied to the LDContext it was accessed from.
40+
* Do not access a returned LDValue from a context after the context has been
41+
* freed.
42+
*
43+
* @param context The context to get an attribute from
44+
* @param kind The kind within the context to get the attribute from.
45+
* @param ref An attribute reference representing the attribute to get.
46+
* @return The attribute value or a null pointer.
47+
*/
48+
LD_EXPORT(LDValue)
49+
LDContext_Get(LDContext context, char const* kind, char const* ref);
50+
51+
/**
52+
* If the context is not valid, then get a string containing the reason the
53+
* context is not valid.
54+
*
55+
* The lifetime of the returned string is tied to the LDContext.
56+
*
57+
* @param context The context to check for validity.
58+
* @return A string explaining why the context is not valid.
59+
*/
60+
LD_EXPORT(char const*) LDContext_Errors(LDContext context);
61+
62+
/**
63+
* Create an iterator which iterates over the private attributes for the
64+
* context kind. If there is no such context kind, then a null pointer will
65+
* be returned.
66+
*
67+
* The iterator must be destroyed with LDContext_DestroyPrivateAttributesIter.
68+
*
69+
* An iterator must not be used after the associated Context has been
70+
* freed.
71+
*
72+
* @param context The context containing the kind.
73+
* @param kind The kind to iterate private attributes for.
74+
* @return A private attributes iterator.
75+
*/
76+
LD_EXPORT(LDContext_PrivateAttributesIter)
77+
LDContext_CreatePrivateAttributesIter(LDContext context, char const* kind);
78+
79+
/**
80+
* Destroy the iterator.
81+
*
82+
* @param iter The iterator to destroy.
83+
*/
84+
LD_EXPORT(void)
85+
LDContext_DestroyPrivateAttributesIter(LDContext_PrivateAttributesIter iter);
86+
87+
/**
88+
* Move the iterator to the next item.
89+
*
90+
* @param iter The iterator to increment.
91+
*/
92+
LD_EXPORT(void)
93+
LDContext_PrivateAttributesIter_Next(LDContext_PrivateAttributesIter iter);
94+
95+
/**
96+
* Check if the iterator is at the end.
97+
*
98+
* @param iter The iterator to check.
99+
* @return True if the iterator is at the end.
100+
*/
101+
LD_EXPORT(bool)
102+
LDContext_PrivateAttributesIter_End(LDContext_PrivateAttributesIter iter);
103+
104+
/**
105+
* Get the value pointed to by the iterator.
106+
*
107+
* The lifetime of the returned value is the same as the Context.
108+
*
109+
* @param iter The iterator to get a value for.
110+
* @return The attribute reference as a string.
111+
*/
112+
LD_EXPORT(char const*)
113+
LDContext_PrivateAttributesIter_Value(LDContext_PrivateAttributesIter iter);
114+
115+
#ifdef __cplusplus
116+
}
117+
#endif
118+
119+
// NOLINTEND modernize-use-using

0 commit comments

Comments
 (0)