From 0fe7faa0e424bd2bba5a8d8e05f8e88c1d88c705 Mon Sep 17 00:00:00 2001 From: "abitha.s" Date: Thu, 1 Oct 2015 18:29:56 +0530 Subject: [PATCH] Changes for JsonSchema and Raml Parser. 1.Added reading Double from Json Schema 2.Adding prevent and klockwork fixes for Raml and JsonSchema parser. 3.Minor fixes. Change-Id: I2459bbdee23b8491a290f21fdfa13c7979efda97 Signed-off-by: abitha.s Reviewed-on: https://gerrit.iotivity.org/gerrit/3375 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../simulator/ramlparser/example/raml_parser.cpp | 170 +++++++++++++++++---- .../ramlparser/raml/jsonSchemaParser/Definitions.h | 2 +- .../ramlparser/raml/jsonSchemaParser/Items.h | 2 +- .../raml/jsonSchemaParser/JsonSchema.cpp | 50 +++++- .../ramlparser/raml/jsonSchemaParser/JsonSchema.h | 3 +- .../ramlparser/raml/jsonSchemaParser/Properties.h | 62 +++++--- .../simulator_resource_creator.cpp | 3 - 7 files changed, 227 insertions(+), 65 deletions(-) mode change 100644 => 100755 service/simulator/src/service-provider/simulator_resource_creator.cpp diff --git a/service/simulator/ramlparser/example/raml_parser.cpp b/service/simulator/ramlparser/example/raml_parser.cpp index 2ebc47f..068e643 100755 --- a/service/simulator/ramlparser/example/raml_parser.cpp +++ b/service/simulator/ramlparser/example/raml_parser.cpp @@ -19,6 +19,7 @@ ******************************************************************/ #include "RamlParser.h" +#include "Helpers.h" #include #include #include @@ -73,20 +74,80 @@ void printRequestResponseBody(const RequestResponseBodyPtr &body) for ( auto pro : body->getSchema()->getProperties()->getProperties() ) { + std::cout << "-----------------------------" << std::endl; std::cout << "Name : " << pro.second->getName() << std::endl; - std::cout << "default : " << pro.second->getValue() << std::endl; - std::cout << "update_frequency : " << pro.second->getUpdateFrequencyTime() << std::endl; - int min = 0, max = 0, multipleOf = 0; - pro.second->getRange(min, max, multipleOf); - - std::cout << "range_min : " << min << std::endl; - std::cout << "range_max : " << max << std::endl; - std::cout << "allowed values : " << std::endl; - for (auto v : pro.second->getAllowedValues()) - std::cout << v << " "; - std::cout << std::endl; + std::cout << "-----------------------------" << std::endl; + try + { + switch (pro.second->getVariantType()) + { + case VariantType::INT : // Integer + std::cout << "Defaut: " << pro.second->getValueInt() << std::endl; + for (auto tt : pro.second->getAllowedValuesInt()) + { + std::cout << "enum value : " << tt << std::endl; + } + { + int min = 0, max = 0, mul = 0; + pro.second->getRange(min, max, mul); + std::cout << "Minimum: " << min << std::endl; + std::cout << "Maximum: " << max << std::endl; + } + break; + + case VariantType::DOUBLE : // Double + std::cout << "Defaut: " << pro.second->getValueDouble() << std::endl; + for (auto tt : pro.second->getAllowedValuesDouble()) + { + std::cout << "enum value : " << tt << std::endl; + } + { + double min = 0, max = 0; + int mul = 0; + pro.second->getRangeDouble(min, max, mul); + std::cout << "MinimumDouble: " << min << std::endl; + std::cout << "MaximumDouble: " << max << std::endl; + } + break; + + case VariantType::BOOL : // Boolean + std::cout << "Defaut: " << std::boolalpha << pro.second->getValueBool() << std::noboolalpha << + std::endl; + for (auto tt : pro.second->getAllowedValuesBool()) + { + std::cout << "enum value : " << tt << std::endl; + } + break; + + case VariantType::STRING : // String + std::cout << "Defaut: " << pro.second->getValueString() << std::endl; + for (auto tt : pro.second->getAllowedValuesString()) + { + std::cout << "enum value : " << tt << std::endl; + } + { + int min = 0, max = 0, mul = 0; + pro.second->getRange(min, max, mul); + std::cout << "MinimumLength: " << min << std::endl; + std::cout << "MaximumLength: " << max << std::endl; + } + break; + default: + break; + + } + } + catch (const boost::bad_lexical_cast &e) + { + std::cout << e.what() << std::endl; + } + catch ( ... ) + { + std::cout << "Unknown exception caught!" << std::endl; + } } + std::cout << "-----------------------------" << std::endl; std::cout << "Body : example : " << body->getExample() << std::endl; #ifdef PRINT_REQUEST_RESPONSE_BODY_PARAMS @@ -238,23 +299,74 @@ void printProperties(const PropertiesPtr &prop) std::cout << "-------------------------------" << std::endl; std::cout << "Type: " << prop->getType() << std::endl; std::cout << "Description: " << prop->getDescription() << std::endl; - int type = prop->getValueType(); - if (type) + try { - std::cout << "Defaut: " << prop->getValueString() << std::endl; - for (auto tt : prop->getAllowedValuesString()) + switch (prop->getVariantType()) { - std::cout << "enum value : " << tt << std::endl; + case VariantType::INT : // Integer + std::cout << "Defaut: " << prop->getValueInt() << std::endl; + for (auto tt : prop->getAllowedValuesInt()) + { + std::cout << "enum value : " << tt << std::endl; + } + { + int min = 0, max = 0, mul = 0; + prop->getRange(min, max, mul); + std::cout << "Minimum: " << min << std::endl; + std::cout << "Maximum: " << max << std::endl; + } + break; + + case VariantType::DOUBLE : // Double + std::cout << "Defaut: " << prop->getValueDouble() << std::endl; + for (auto tt : prop->getAllowedValuesDouble()) + { + std::cout << "enum value : " << tt << std::endl; + } + { + double min = 0, max = 0; + int mul = 0; + prop->getRangeDouble(min, max, mul); + std::cout << "MinimumDouble: " << min << std::endl; + std::cout << "MaximumDouble: " << max << std::endl; + } + break; + + case VariantType::BOOL : // Boolean + std::cout << "Defaut: " << std::boolalpha << prop->getValueBool() << std::noboolalpha << std::endl; + for (auto tt : prop->getAllowedValuesBool()) + { + std::cout << std::boolalpha << "enum value : " << tt << std::noboolalpha << std::endl; + } + break; + + case VariantType::STRING : // String + std::cout << "Defaut: " << prop->getValueString() << std::endl; + for (auto tt : prop->getAllowedValuesString()) + { + std::cout << "enum value : " << tt << std::endl; + } + { + int min = 0, max = 0, mul = 0; + prop->getRange(min, max, mul); + std::cout << "MinimumLength: " << min << std::endl; + std::cout << "MaximumLength: " << max << std::endl; + } + break; + default: + break; + } } - else + catch (const boost::bad_lexical_cast &e) { - std::cout << "Defaut: " << prop->getValueInt() << std::endl; - for (auto tt : prop->getAllowedValuesString()) - { - std::cout << "enum value : " << tt << std::endl; - } + std::cout << e.what() << std::endl; } + catch ( ... ) + { + std::cout << "Unknown exception caught!" << std::endl; + } + if (prop->getType() == "array") { for (auto it : prop->getItems()) @@ -265,13 +377,17 @@ void printProperties(const PropertiesPtr &prop) { std::cout << "enum value : " << tt << std::endl; } + for (auto tt : it->getProperties()) + { + printProperties(tt.second); + } + std::cout << "Item Required Values : " << std::endl; + for (auto tt : it->getRequiredValues()) + { + std::cout << tt << std::endl; + } } } - int min = 0, max = 0, mul = 0; - prop->getRange(min, max, mul); - std::cout << "Minimum: " << min << std::endl; - std::cout << "Maximum: " << max << std::endl; - std::cout << "MultipleOf: " << mul << std::endl; #endif } diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h index 401e448..d2eec8f 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h @@ -118,7 +118,7 @@ namespace RAML if (*it == reqValue) break; } - if (m_required.end() != it) + if (m_required.end() == it) { m_required.push_back(reqValue); } diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h index b0a0e50..d2463b2 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h @@ -124,7 +124,7 @@ namespace RAML if (*it == reqValue) break; } - if (m_required.end() != it) + if (m_required.end() == it) { m_required.push_back(reqValue); } diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp index 9425b0b..67968e2 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp @@ -176,11 +176,6 @@ namespace RAML { PropertiesPtr property = std::make_shared(attName); - cJSON *propertyUpdateFrequency = cJSON_GetObjectItem(childProperties, "update_frequency"); - if (propertyUpdateFrequency) - { - property->setUpdateFrequencyTime(propertyUpdateFrequency->valueint); - } cJSON *propertyDescription = cJSON_GetObjectItem(childProperties, "description"); if (propertyDescription) { @@ -295,7 +290,7 @@ namespace RAML } else if (attType == "integer") { - readNumber(childProperties, property); + readInteger(childProperties, property); } else if (attType == "array") { @@ -303,7 +298,7 @@ namespace RAML } else if (attType == "number") { - readNumber(childProperties, property); + readDouble(childProperties, property); } } @@ -418,7 +413,7 @@ namespace RAML } } - void JsonSchema::readNumber(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readInteger(cJSON *childProperties, PropertiesPtr property) { cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); if (Max) @@ -455,6 +450,45 @@ namespace RAML } } + + void JsonSchema::readDouble(cJSON *childProperties, PropertiesPtr property) + { + cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); + if (Max) + { + cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); + if (exclusiveMax) + { + if (exclusiveMax->type == cJSON_True) + property->setMaxDouble( --(Max->valuedouble)); + else + property->setMaxDouble(Max->valuedouble); + } + else + property->setMaxDouble(Max->valuedouble); + } + cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum"); + if (Min) + { + cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum"); + if (exclusiveMin) + { + if (exclusiveMin->type == cJSON_True) + property->setMinDouble( ++(Min->valuedouble)); + else + property->setMinDouble(Min->valuedouble); + } + else + property->setMinDouble(Min->valuedouble); + } + cJSON *multipleOf = cJSON_GetObjectItem(childProperties, "multipleOf"); + if (multipleOf) + { + property->setMultipleOf(multipleOf->valueint); + } + + } + DefinitionsPtr JsonSchema::readRef(std::string m_ref) { std::string delimiter1 = "#"; diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h index fb300ba..c52a145 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h @@ -280,7 +280,8 @@ namespace RAML const std::string &attType); void readString( cJSON *childProperties, PropertiesPtr property); void readArray( cJSON *childProperties, PropertiesPtr property); - void readNumber( cJSON *childProperties, PropertiesPtr property); + void readInteger( cJSON *childProperties, PropertiesPtr property); + void readDouble( cJSON *childProperties, PropertiesPtr property); DefinitionsPtr readRef(std::string m_ref); diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h index 61daf5e..7291635 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h @@ -50,8 +50,8 @@ namespace RAML /** * Constructor of Properties. */ - Properties(): m_min(INT_MAX), m_max(INT_MAX), m_multipleOf(INT_MAX), m_updateInterval(0), - m_unique(false), m_additionalItems(false) {} + Properties(): m_min(INT_MAX), m_max(INT_MAX), m_doubleMin(INT_MAX), m_doubleMax(INT_MAX), + m_multipleOf(INT_MAX), m_unique(false), m_additionalItems(false) {} /** * Constructor of Properties. @@ -59,7 +59,8 @@ namespace RAML * @param name - Properties name as string. */ Properties(const std::string &name) : m_name(name), m_min(INT_MAX), m_max(INT_MAX), - m_multipleOf(INT_MAX), m_updateInterval(0), m_unique(false), m_additionalItems(false) {} + m_doubleMin(INT_MAX), m_doubleMax(INT_MAX), m_multipleOf(INT_MAX), + m_unique(false), m_additionalItems(false) {} /** * This method is for getting Name from Properties. @@ -195,6 +196,19 @@ namespace RAML } /** + * This method is for getting Range from Properties. + * + * @param min - reference to hold Minimum value of Properties. + * @param max - reference to hold Maximum value of Properties. + * @param multipleOf - reference to hold multipleOf value of Properties. + */ + inline void getRangeDouble(double &min, double &max, int &multipleOf) const + { + min = m_doubleMin; + max = m_doubleMax; + multipleOf = m_multipleOf; + } + /** * This method is for setting Minimum to Properties * * @param min - Minimum value of Properties. @@ -215,6 +229,25 @@ namespace RAML } /** + * This method is for setting Minimum to Properties + * + * @param min - Minimum value of Properties. + */ + inline void setMinDouble(const double &min) + { + m_doubleMin = min; + } + + /** + * This method is for setting Maximum to Properties + * + * @param max - Maximum value of Properties. + */ + inline void setMaxDouble(const double &max) + { + m_doubleMax = max; + } + /** * This method is for setting multipleOf to Properties * * @param multipleOf - multipleOf value of Properties. @@ -323,26 +356,6 @@ namespace RAML } /** - * This method is for getting UpdateFrequency from Properties. - * - * @return UpdateFrequency as int - */ - inline int getUpdateFrequencyTime() - { - return m_updateInterval; - } - - /** - * This method is for setting UpdateFrequency to Properties - * - * @param interval - UpdateFrequency as int. - */ - inline void setUpdateFrequencyTime(int interval) - { - m_updateInterval = interval; - } - - /** * This method is for setting Type to Properties * * @param type - Type as string. @@ -469,9 +482,10 @@ namespace RAML ValueVariant m_value; int m_min; int m_max; + double m_doubleMin; + double m_doubleMax; int m_multipleOf; AllowedValues m_allowedValues; - int m_updateInterval; std::string m_type; std::string m_pattern; std::string m_format; diff --git a/service/simulator/src/service-provider/simulator_resource_creator.cpp b/service/simulator/src/service-provider/simulator_resource_creator.cpp old mode 100644 new mode 100755 index 6959f82..6f77b74 --- a/service/simulator/src/service-provider/simulator_resource_creator.cpp +++ b/service/simulator/src/service-provider/simulator_resource_creator.cpp @@ -118,9 +118,6 @@ SimulatorResourceServerImplSP SimulatorResourceCreator::createResource( break; } - // Set attriute update frequency interval - attribute.setUpdateFrequencyTime(propertyElement.second->getUpdateFrequencyTime()); - // Set range/supported values set int min = 0, max = 0, multipleof = 0; propertyElement.second->getRange(min, max, multipleof); -- 2.7.4