summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2016-02-16 07:22:01 -0500
committerimarom <imarom@cisco.com>2016-02-16 07:22:01 -0500
commit0bc8b0acfb2d2e158c7b41bc398aef3137087859 (patch)
tree89287366dbae72b45f5efc043ba5e05a692c2a58
parent8f6067d8738fa77a147955ee208ece8dea198111 (diff)
added support for default fields at the JSON RPC server
-rw-r--r--src/rpc-server/trex_rpc_cmd.cpp97
-rw-r--r--src/rpc-server/trex_rpc_cmd_api.h154
2 files changed, 134 insertions, 117 deletions
diff --git a/src/rpc-server/trex_rpc_cmd.cpp b/src/rpc-server/trex_rpc_cmd.cpp
index aea7980b..f8779860 100644
--- a/src/rpc-server/trex_rpc_cmd.cpp
+++ b/src/rpc-server/trex_rpc_cmd.cpp
@@ -145,102 +145,6 @@ TrexRpcCommand::json_type_to_name(const Json::Value &value) {
}
-uint8_t
-TrexRpcCommand::parse_byte(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_BYTE, result);
- return parent[name].asUInt();
-}
-
-uint8_t
-TrexRpcCommand::parse_byte(const Json::Value &parent, int index, Json::Value &result) {
- check_field_type(parent, index, FIELD_TYPE_BYTE, result);
- return parent[index].asUInt();
-}
-
-uint16_t
-TrexRpcCommand::parse_uint16(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_UINT16, result);
- return parent[name].asUInt();
-}
-
-uint16_t
-TrexRpcCommand::parse_uint16(const Json::Value &parent, int index, Json::Value &result) {
- check_field_type(parent, index, FIELD_TYPE_UINT16, result);
- return parent[index].asUInt();
-}
-
-uint32_t
-TrexRpcCommand::parse_uint32(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_UINT32, result);
- return parent[name].asUInt();
-}
-
-uint32_t
-TrexRpcCommand::parse_uint32(const Json::Value &parent, int index, Json::Value &result) {
- check_field_type(parent, index, FIELD_TYPE_UINT32, result);
- return parent[index].asUInt();
-}
-
-uint64_t
-TrexRpcCommand::parse_uint64(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_UINT64, result);
- return parent[name].asUInt64();
-}
-
-uint64_t
-TrexRpcCommand::parse_uint64(const Json::Value &parent, int index, Json::Value &result) {
- check_field_type(parent, index, FIELD_TYPE_UINT64, result);
- return parent[index].asUInt64();
-}
-
-int
-TrexRpcCommand::parse_int(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_INT, result);
- return parent[name].asInt();
-}
-
-bool
-TrexRpcCommand::parse_bool(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_BOOL, result);
- return parent[name].asBool();
-}
-
-double
-TrexRpcCommand::parse_double(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_DOUBLE, result);
- return parent[name].asDouble();
-}
-
-const std::string
-TrexRpcCommand::parse_string(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_STR, result);
- return parent[name].asString();
-}
-
-/**
- * object version
- */
-const Json::Value &
-TrexRpcCommand::parse_object(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_OBJ, result);
- return parent[name];
-}
-
-/**
- * index version
- */
-const Json::Value &
-TrexRpcCommand::parse_object(const Json::Value &parent, int index, Json::Value &result) {
- check_field_type(parent, index, FIELD_TYPE_OBJ, result);
- return parent[index];
-}
-
-const Json::Value &
-TrexRpcCommand::parse_array(const Json::Value &parent, const std::string &name, Json::Value &result) {
- check_field_type(parent, name, FIELD_TYPE_ARRAY, result);
- return parent[name];
-}
-
/**
* for index element (array)
*/
@@ -269,6 +173,7 @@ TrexRpcCommand::check_field_type(const Json::Value &parent, const std::string &n
const Json::Value &field = parent[name];
check_field_type_common(field, name, type, result);
}
+
void
TrexRpcCommand::check_field_type_common(const Json::Value &field, const std::string &name, field_type_e type, Json::Value &result) {
std::stringstream ss;
diff --git a/src/rpc-server/trex_rpc_cmd_api.h b/src/rpc-server/trex_rpc_cmd_api.h
index 675d2900..7e694768 100644
--- a/src/rpc-server/trex_rpc_cmd_api.h
+++ b/src/rpc-server/trex_rpc_cmd_api.h
@@ -146,27 +146,139 @@ protected:
* parse functions
*
*/
- uint8_t parse_byte(const Json::Value &parent, const std::string &name, Json::Value &result);
- uint16_t parse_uint16(const Json::Value &parent, const std::string &name, Json::Value &result);
- uint32_t parse_uint32(const Json::Value &parent, const std::string &name, Json::Value &result);
- uint64_t parse_uint64(const Json::Value &parent, const std::string &name, Json::Value &result);
- int parse_int(const Json::Value &parent, const std::string &name, Json::Value &result);
- double parse_double(const Json::Value &parent, const std::string &name, Json::Value &result);
- bool parse_bool(const Json::Value &parent, const std::string &name, Json::Value &result);
- const std::string parse_string(const Json::Value &parent, const std::string &name, Json::Value &result);
- const Json::Value & parse_object(const Json::Value &parent, const std::string &name, Json::Value &result);
- const Json::Value & parse_array(const Json::Value &parent, const std::string &name, Json::Value &result);
-
- uint8_t parse_byte(const Json::Value &parent, int index, Json::Value &result);
- uint16_t parse_uint16(const Json::Value &parent, int index, Json::Value &result);
- uint32_t parse_uint32(const Json::Value &parent, int index, Json::Value &result);
- uint64_t parse_uint64(const Json::Value &parent, int index, Json::Value &result);
- int parse_int(const Json::Value &parent, int index, Json::Value &result);
- double parse_double(const Json::Value &parent, int index, Json::Value &result);
- bool parse_bool(const Json::Value &parent, int index, Json::Value &result);
- const std::string parse_string(const Json::Value &parent, int index, Json::Value &result);
- const Json::Value & parse_object(const Json::Value &parent, int index, Json::Value &result);
- const Json::Value & parse_array(const Json::Value &parent, int index, Json::Value &result);
+ template<typename T> uint8_t parse_byte(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_BYTE, result);
+ return parent[param].asUInt();
+ }
+
+ template<typename T> uint16_t parse_uint16(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_UINT16, result);
+ return parent[param].asUInt();
+ }
+
+ template<typename T> uint32_t parse_uint32(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_UINT32, result);
+ return parent[param].asUInt();
+ }
+
+ template<typename T> uint64_t parse_uint64(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_UINT64, result);
+ return parent[param].asUInt64();
+ }
+
+ template<typename T> int parse_int(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_INT, result);
+ return parent[param].asInt();
+ }
+
+ template<typename T> double parse_double(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_DOUBLE, result);
+ return parent[param].asDouble();
+ }
+
+ template<typename T> bool parse_bool(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_BOOL, result);
+ return parent[param].asBool();
+ }
+
+ template<typename T> const std::string parse_string(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_STR, result);
+ return parent[param].asString();
+ }
+
+ template<typename T> const Json::Value & parse_object(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_OBJ, result);
+ return parent[param];
+ }
+
+ template<typename T> const Json::Value & parse_array(const Json::Value &parent, const T &param, Json::Value &result) {
+ check_field_type(parent, param, FIELD_TYPE_ARRAY, result);
+ return parent[param];
+ }
+
+
+ /**
+ * parse with defaults
+ */
+ template<typename T> uint8_t parse_byte(const Json::Value &parent, const T &param, Json::Value &result, uint8_t def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_byte(parent, param, result);
+ }
+
+ template<typename T> uint16_t parse_uint16(const Json::Value &parent, const T &param, Json::Value &result, uint16_t def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_uint16(parent, param, result);
+ }
+
+ template<typename T> uint32_t parse_uint32(const Json::Value &parent, const T &param, Json::Value &result, uint32_t def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_uint32(parent, param, result);
+ }
+
+ template<typename T> uint64_t parse_uint64(const Json::Value &parent, const T &param, Json::Value &result, uint64_t def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_uint64(parent, param, result);
+ }
+
+ template<typename T> int parse_int(const Json::Value &parent, const T &param, Json::Value &result, int def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_int(parent, param, result);
+ }
+
+ template<typename T> double parse_double(const Json::Value &parent, const T &param, Json::Value &result, double def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_double(parent, param, result);
+ }
+
+ template<typename T> bool parse_bool(const Json::Value &parent, const T &param, Json::Value &result, bool def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_bool(parent, param, result);
+ }
+
+ template<typename T> const std::string parse_string(const Json::Value &parent, const T &param, Json::Value &result, const std::string &def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_string(parent, param, result);
+ }
+
+ template<typename T> const Json::Value & parse_object(const Json::Value &parent, const T &param, Json::Value &result, const Json::Value &def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_object(parent, param, result);
+ }
+
+ template<typename T> const Json::Value & parse_array(const Json::Value &parent, const T &param, Json::Value &result, const Json::Value &def) {
+ /* if not exists - default */
+ if (parent[param] == Json::Value::null) {
+ return def;
+ }
+ return parse_array(parent, param, result);
+ }
/* shortcut for parsing port id */
uint8_t parse_port(const Json::Value &params, Json::Value &result);