Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions include/pistache/description.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,18 @@ template <typename T> std::unique_ptr<DataType> makeDataType() {
return std::unique_ptr<DataType>(new DataTypeT<T>());
}

#define PARAMETER_LOCATIONS \
LOC(Path, "path") \
LOC(Query, "query")

enum class Location {
#define LOC(name, _) name,
PARAMETER_LOCATIONS
#undef LOC
};

struct Parameter {
Parameter(std::string name, std::string description);
Parameter(std::string name, std::string description, Location location, bool required);

template <typename T, typename... Args>
static Parameter create(Args &&... args) {
Expand All @@ -184,6 +194,7 @@ struct Parameter {

std::string name;
std::string description;
Location location;
bool required;
std::shared_ptr<DataType> type;
};
Expand Down Expand Up @@ -298,10 +309,17 @@ struct PathBuilder {
template <typename T>
PathBuilder &parameter(std::string name, std::string description) {
path_->parameters.push_back(
Parameter::create<T>(std::move(name), std::move(description)));
Parameter::create<T>(std::move(name), std::move(description), Location::Path, true));
return *this;
}

template <typename T>
PathBuilder &query(std::string name, std::string description, bool required) {
path_->parameters.push_back(
Parameter::create<T>(std::move(name), std::move(description), Location::Query, required));
return *this;
}

PathBuilder &response(Http::Code statusCode, std::string description) {
path_->responses.push_back(Response(statusCode, std::move(description)));
return *this;
Expand Down Expand Up @@ -363,8 +381,14 @@ struct SubPath {

template <typename T>
void parameter(std::string name, std::string description) {
parameters.push_back(
Parameter::create<T>(std::move(name), std::move(description)));
parameters.push_back(
Parameter::create<T>(std::move(name), std::move(description), Location::Path, true));
}

template <typename T>
void query(std::string name, std::string description, bool required) {
parameters.push_back(
Parameter::create<T>(std::move(name), std::move(description), Location::Query, required));
}

std::string prefix;
Expand Down
7 changes: 6 additions & 1 deletion include/pistache/thirdparty/serializer/rapidjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ void serializePC(Writer &writer, const Schema::ProduceConsume &pc) {

template <typename Writer>
void serializeParameter(Writer &writer, const Schema::Parameter &parameter) {
#define LOC(_, name) name,
static char* locations[] = {
PARAMETER_LOCATIONS
};
#undef LOC
writer.StartObject();
{
writer.String("name");
writer.String(parameter.name.c_str());
writer.String("in");
// @Feature: support other types of parameters
writer.String("path");
writer.String(locations[static_cast<size_t>(parameter.location)]);
writer.String("description");
writer.String(parameter.description.c_str());
writer.String("required");
Expand Down
6 changes: 3 additions & 3 deletions src/common/description.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ SubPath SubPath::path(const std::string &prefix) {
return SubPath(this->prefix + prefix, paths);
}

Parameter::Parameter(std::string name, std::string description)
: name(std::move(name)), description(std::move(description)),
required(true), type() {}
Parameter::Parameter(std::string name, std::string description, Location location, bool required)
: name(std::move(name)), description(std::move(description)), location(location),
required(required), type() {}

Response::Response(Http::Code statusCode, std::string description)
: statusCode(statusCode), description(std::move(description)) {}
Expand Down