Skip to content

Commit

Permalink
contract tests + validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Sep 3, 2024
1 parent 5ef05d3 commit 35b5035
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 73 deletions.
172 changes: 113 additions & 59 deletions contract-tests/data-model/include/data_model/data_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,41 @@
#include <unordered_map>
#include "nlohmann/json.hpp"

Check failure on line 6 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:6:10 [clang-diagnostic-error]

'nlohmann/json.hpp' file not found

namespace nlohmann {

template <typename T>
struct adl_serializer<std::optional<T>> {
static void to_json(json& j, std::optional<T> const& opt) {
if (opt == std::nullopt) {
j = nullptr;
} else {
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
namespace nlohmann
{
template <typename T>
struct adl_serializer<std::optional<T>>
{
static void to_json(json& j, std::optional<T> const& opt)
{
if (opt == std::nullopt)
{
j = nullptr;
}
else
{
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
}
}
}

static void from_json(json const& j, std::optional<T>& opt) {
if (j.is_null()) {
opt = std::nullopt;
} else {
opt = j.get<T>(); // same as above, but with
// adl_serializer<T>::from_json

static void from_json(json const& j, std::optional<T>& opt)
{
if (j.is_null())
{
opt = std::nullopt;
}
else
{
opt = j.get<T>(); // same as above, but with
// adl_serializer<T>::from_json
}
}
}
};
} // namespace nlohmann
};
} // namespace nlohmann

struct ConfigTLSParams {
struct ConfigTLSParams
{
std::optional<bool> skipVerifyPeer;
std::optional<std::string> customCAFile;
};
Expand All @@ -38,66 +48,82 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigTLSParams,
skipVerifyPeer,
customCAFile);

struct ConfigStreamingParams {
struct ConfigStreamingParams
{
std::optional<std::string> baseUri;
std::optional<uint32_t> initialRetryDelayMs;
std::optional<std::string> filter;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigStreamingParams,
baseUri,
initialRetryDelayMs);
initialRetryDelayMs, filter);

struct ConfigPollingParams {
struct ConfigPollingParams
{
std::optional<std::string> baseUri;
std::optional<uint32_t> pollIntervalMs;
std::optional<std::string> filter;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigPollingParams,
baseUri,
pollIntervalMs);
pollIntervalMs, filter);

struct ConfigEventParams {
struct ConfigEventParams

Check warning on line 73 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:73:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: globalPrivateAttributes
{
std::optional<std::string> baseUri;
std::optional<uint32_t> capacity;
std::optional<bool> enableDiagnostics;
std::optional<bool> allAttributesPrivate;
std::vector<std::string> globalPrivateAttributes;
std::optional<int> flushIntervalMs;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigEventParams,
baseUri,
capacity,
enableDiagnostics,
allAttributesPrivate,
globalPrivateAttributes,
flushIntervalMs);
struct ConfigServiceEndpointsParams {

struct ConfigServiceEndpointsParams
{
std::optional<std::string> streaming;
std::optional<std::string> polling;
std::optional<std::string> events;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigServiceEndpointsParams,
streaming,
polling,
events);

struct ConfigClientSideParams {
struct ConfigClientSideParams

Check warning on line 103 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:103:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: initialContext
{
nlohmann::json initialContext;
std::optional<bool> evaluationReasons;
std::optional<bool> useReport;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigClientSideParams,
initialContext,
evaluationReasons,
useReport);

struct ConfigTags {
struct ConfigTags
{
std::optional<std::string> applicationId;
std::optional<std::string> applicationVersion;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigTags,
applicationId,
applicationVersion);

struct ConfigParams {
struct ConfigParams
{
std::string credential;
std::optional<uint32_t> startWaitTimeMs;
std::optional<bool> initCanFail;
Expand All @@ -109,6 +135,7 @@ struct ConfigParams {
std::optional<ConfigTags> tags;
std::optional<ConfigTLSParams> tls;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigParams,
credential,
startWaitTimeMs,
Expand All @@ -121,7 +148,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigParams,
tags,
tls);

struct ContextSingleParams {
struct ContextSingleParams

Check warning on line 151 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:151:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: custom
{
std::optional<std::string> kind;
std::string key;
std::optional<std::string> name;
Expand All @@ -133,16 +161,19 @@ struct ContextSingleParams {
// These are defined manually because of the 'private' field, which is a
// reserved keyword in C++.
inline void to_json(nlohmann::json& nlohmann_json_j,
ContextSingleParams const& nlohmann_json_t) {
ContextSingleParams const& nlohmann_json_t)
{
nlohmann_json_j["kind"] = nlohmann_json_t.kind;
nlohmann_json_j["key"] = nlohmann_json_t.key;
nlohmann_json_j["name"] = nlohmann_json_t.name;
nlohmann_json_j["anonymous"] = nlohmann_json_t.anonymous;
nlohmann_json_j["private"] = nlohmann_json_t._private;
nlohmann_json_j["custom"] = nlohmann_json_t.custom;
}

inline void from_json(nlohmann::json const& nlohmann_json_j,
ContextSingleParams& nlohmann_json_t) {
ContextSingleParams& nlohmann_json_t)
{
ContextSingleParams nlohmann_json_default_obj;
nlohmann_json_t.kind =
nlohmann_json_j.value("kind", nlohmann_json_default_obj.kind);
Expand All @@ -158,7 +189,8 @@ inline void from_json(nlohmann::json const& nlohmann_json_j,
nlohmann_json_j.value("custom", nlohmann_json_default_obj.custom);
}

struct ContextBuildParams {
struct ContextBuildParams
{
std::optional<ContextSingleParams> single;
std::optional<std::vector<ContextSingleParams>> multi;
};
Expand All @@ -167,98 +199,117 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ContextBuildParams,
single,
multi);

struct ContextConvertParams {
struct ContextConvertParams
{
std::string input;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ContextConvertParams, input);

struct ContextResponse {
struct ContextResponse
{
std::optional<std::string> output;
std::optional<std::string> error;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ContextResponse, output, error);

struct CreateInstanceParams {
struct CreateInstanceParams
{
ConfigParams configuration;
std::string tag;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(CreateInstanceParams,
configuration,
tag);

enum class ValueType { Bool = 1, Int, Double, String, Any, Unspecified };

NLOHMANN_JSON_SERIALIZE_ENUM(ValueType,
{{ValueType::Bool, "bool"},
{ValueType::Int, "int"},
{ValueType::Double, "double"},
{ValueType::String, "string"},
{ValueType::Any, "any"},
{ValueType::Unspecified, ""}})
{ValueType::Int, "int"},
{ValueType::Double, "double"},
{ValueType::String, "string"},
{ValueType::Any, "any"},
{ValueType::Unspecified, ""}})

struct EvaluateFlagParams {
struct EvaluateFlagParams
{
std::string flagKey;
std::optional<nlohmann::json> context;
ValueType valueType;
nlohmann::json defaultValue;
bool detail;
EvaluateFlagParams();
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(EvaluateFlagParams,
flagKey,
context,
valueType,
defaultValue,
detail);

struct EvaluateFlagResponse {
struct EvaluateFlagResponse

Check warning on line 254 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:254:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: value, reason
{
nlohmann::json value;
std::optional<uint32_t> variationIndex;
std::optional<nlohmann::json> reason;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(EvaluateFlagResponse,
value,
variationIndex,
reason);

struct EvaluateAllFlagParams {
struct EvaluateAllFlagParams

Check warning on line 266 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:266:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: context
{
std::optional<nlohmann::json> context;
std::optional<bool> withReasons;
std::optional<bool> clientSideOnly;
std::optional<bool> detailsOnlyForTrackedFlags;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(EvaluateAllFlagParams,
context,
withReasons,
clientSideOnly,
detailsOnlyForTrackedFlags);
struct EvaluateAllFlagsResponse {

struct EvaluateAllFlagsResponse

Check warning on line 280 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:280:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: state
{
nlohmann::json state;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(EvaluateAllFlagsResponse,
state);

struct CustomEventParams {
struct CustomEventParams
{
std::string eventKey;
std::optional<nlohmann::json> context;
std::optional<nlohmann::json> data;
std::optional<bool> omitNullData;
std::optional<double> metricValue;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(CustomEventParams,
eventKey,
context,
data,
omitNullData,
metricValue);

struct IdentifyEventParams {
struct IdentifyEventParams

Check warning on line 304 in contract-tests/data-model/include/data_model/data_model.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/contract-tests/data-model/include/data_model/data_model.hpp:304:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: context
{
nlohmann::json context;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(IdentifyEventParams, context);

enum class Command {
enum class Command
{
Unknown = -1,
EvaluateFlag,
EvaluateAllFlags,
Expand All @@ -268,17 +319,19 @@ enum class Command {
ContextBuild,
ContextConvert
};

NLOHMANN_JSON_SERIALIZE_ENUM(Command,
{{Command::Unknown, nullptr},
{Command::EvaluateFlag, "evaluate"},
{Command::EvaluateAllFlags, "evaluateAll"},
{Command::IdentifyEvent, "identifyEvent"},
{Command::CustomEvent, "customEvent"},
{Command::FlushEvents, "flushEvents"},
{Command::ContextBuild, "contextBuild"},
{Command::ContextConvert, "contextConvert"}});

struct CommandParams {
{Command::EvaluateFlag, "evaluate"},
{Command::EvaluateAllFlags, "evaluateAll"},
{Command::IdentifyEvent, "identifyEvent"},
{Command::CustomEvent, "customEvent"},
{Command::FlushEvents, "flushEvents"},
{Command::ContextBuild, "contextBuild"},
{Command::ContextConvert, "contextConvert"}});

struct CommandParams
{
Command command;
std::optional<EvaluateFlagParams> evaluate;
std::optional<EvaluateAllFlagParams> evaluateAll;
Expand All @@ -288,6 +341,7 @@ struct CommandParams {
std::optional<ContextConvertParams> contextConvert;
CommandParams();
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(CommandParams,
command,
evaluate,
Expand Down
Loading

0 comments on commit 35b5035

Please sign in to comment.