From f7c1872a6f6c503f06552193d123af11e6ede243 Mon Sep 17 00:00:00 2001
From: Matus Kysel <matuskysel1@gmail.com>
Date: Fri, 22 Dec 2023 08:41:54 +0100
Subject: [PATCH] feat: add support for optional position params

---
 src/jsonrpccpp/common/procedure.cpp | 15 +++++++++++++++
 src/jsonrpccpp/common/procedure.h   |  3 ++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/jsonrpccpp/common/procedure.cpp b/src/jsonrpccpp/common/procedure.cpp
index 02069853..bfb6866a 100644
--- a/src/jsonrpccpp/common/procedure.cpp
+++ b/src/jsonrpccpp/common/procedure.cpp
@@ -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 {
@@ -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) {
diff --git a/src/jsonrpccpp/common/procedure.h b/src/jsonrpccpp/common/procedure.h
index 079c7bd5..eeb47a52 100644
--- a/src/jsonrpccpp/common/procedure.h
+++ b/src/jsonrpccpp/common/procedure.h
@@ -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:
@@ -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:
     /**