Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for optional position params #328

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions src/jsonrpccpp/common/procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ bool Procedure::ValdiateParameters(const Json::Value &parameters) const {
}
if (parameters.isArray() && this->paramDeclaration == PARAMS_BY_POSITION) {
return this->ValidatePositionalParameters(parameters);
} else if (parameters.isArray() && this->paramDeclaration == PARAMS_BY_POSITION_WITH_OPTIONAL) {
return this->ValidateOptionalPositionalParameters(parameters);
} else if (parameters.isObject() && this->paramDeclaration == PARAMS_BY_NAME) {
return this->ValidateNamedParameters(parameters);
} else {
Expand Down Expand Up @@ -101,6 +103,19 @@ bool Procedure::ValidatePositionalParameters(const Json::Value &parameters) cons
}
return ok;
}

bool Procedure::ValidateOptionalPositionalParameters(const Json::Value &parameters) const {
bool ok = true;

if (parameters.size() > this->parametersPosition.size()) {
return false;
}
for (unsigned int i = 0; ok && i < parameters.size(); i++) {
ok = this->ValidateSingleParameter(this->parametersPosition.at(i), parameters[i]);
}
return ok;
}

bool Procedure::ValidateSingleParameter(jsontype_t expectedType, const Json::Value &value) const {
bool ok = true;
switch (expectedType) {
Expand Down
3 changes: 2 additions & 1 deletion src/jsonrpccpp/common/procedure.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace jsonrpc {
typedef std::map<std::string, jsontype_t> parameterNameList_t;
typedef std::vector<jsontype_t> parameterPositionList_t;

typedef enum { PARAMS_BY_NAME, PARAMS_BY_POSITION } parameterDeclaration_t;
typedef enum { PARAMS_BY_NAME, PARAMS_BY_POSITION, PARAMS_BY_POSITION_WITH_OPTIONAL } parameterDeclaration_t;

class Procedure {
public:
Expand Down Expand Up @@ -73,6 +73,7 @@ namespace jsonrpc {

bool ValidateNamedParameters(const Json::Value &parameters) const;
bool ValidatePositionalParameters(const Json::Value &parameters) const;
bool ValidateOptionalPositionalParameters(const Json::Value &parameters) const;

private:
/**
Expand Down