From 1fc385ff136489e0f104603f8ea1ad6f12df3027 Mon Sep 17 00:00:00 2001 From: Abitha Shankar Date: Wed, 6 Jan 2016 17:07:14 +0530 Subject: [PATCH] Design Changes to JsonSchema Parser 1. Changes to read Json Schema to read Simple Arrays and Array of Array of Properties required for Collection 2. Changes to read as Simple Attribute as vector of homogeneous values into Simulator Factory 3. Changes to Read the Properties from schema in a Unified Manner using ValueVariant to hold the values 4. Added Support to read Multiple ValueProperties for each property 5. Added support for Pattern, Format and Array ValueProperties. 6.Changes to read the referenced file in Json Schema from a single Implementation 7. Added Default values for attributes when not obtained from Schema Change-Id: Iccc3a1cb0881d19b511cc743975299e85fd27c2d Signed-off-by: Abitha Shankar Reviewed-on: https://gerrit.iotivity.org/gerrit/4755 Tested-by: jenkins-iotivity Reviewed-by: Radha Bhavani Reviewed-by: Uze Choi --- service/simulator/ramlparser/SConscript | 2 + .../raml/jsonSchemaParser/AllowedValues.h | 168 ---- .../raml/jsonSchemaParser/Definitions.cpp | 91 +++ .../ramlparser/raml/jsonSchemaParser/Definitions.h | 88 +-- .../ramlparser/raml/jsonSchemaParser/Helpers.h | 55 -- .../ramlparser/raml/jsonSchemaParser/Items.h | 205 ----- .../raml/jsonSchemaParser/JsonSchema.cpp | 851 ++++++++++----------- .../ramlparser/raml/jsonSchemaParser/JsonSchema.h | 97 +-- .../raml/jsonSchemaParser/Properties.cpp | 410 ++++++++++ .../ramlparser/raml/jsonSchemaParser/Properties.h | 691 +++++++++-------- .../simulator/src/common/request_model_builder.cpp | 226 ++++-- .../simulator/src/common/request_model_builder.h | 5 + .../src/server/simulator_resource_factory.cpp | 235 +++--- .../src/server/simulator_resource_factory.h | 7 +- 14 files changed, 1630 insertions(+), 1501 deletions(-) delete mode 100755 service/simulator/ramlparser/raml/jsonSchemaParser/AllowedValues.h create mode 100755 service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.cpp delete mode 100755 service/simulator/ramlparser/raml/jsonSchemaParser/Helpers.h delete mode 100755 service/simulator/ramlparser/raml/jsonSchemaParser/Items.h create mode 100755 service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp mode change 100644 => 100755 service/simulator/src/common/request_model_builder.h mode change 100644 => 100755 service/simulator/src/server/simulator_resource_factory.cpp diff --git a/service/simulator/ramlparser/SConscript b/service/simulator/ramlparser/SConscript index adb8803..7e1504c 100755 --- a/service/simulator/ramlparser/SConscript +++ b/service/simulator/ramlparser/SConscript @@ -32,6 +32,8 @@ raml_env.AppendUnique(CPPPATH = ['raml/model','raml/jsonSchemaParser','raml', '. raml_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-pthread']) raml_env.AppendUnique(CPPDEFINES = ['LINUX']) +raml_env.Append( RPATH = env.Literal('\\$$ORIGIN')) + raml_env.AppendUnique(CPPPATH = ['../../../extlibs/cjson/']) raml_env.PrependUnique(LIBS = ['octbstack', 'YamlParser']) raml_env.AppendUnique(LIBS = ['pthread']) diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/AllowedValues.h b/service/simulator/ramlparser/raml/jsonSchemaParser/AllowedValues.h deleted file mode 100755 index b0946ae..0000000 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/AllowedValues.h +++ /dev/null @@ -1,168 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ - -/** - * @file AllowedValues.h - * - * @brief This file provides data Model for Json Schema AllowedValues. - */ - -#ifndef ALLOWED_VALUES_H_ -#define ALLOWED_VALUES_H_ - -#include -#include -#include -#include -#include -#include "Helpers.h" - -namespace RAML -{ - /** - * @class AllowedValues - * @brief This class provides data Model for Json Schema AllowedValues. - */ - class AllowedValues - { - public: - /** - * This method is for setting AllowedValues - * - * @param value - Allowed Value to set. - */ - template - void addValue(const T &value) - { - ValueVariant temp = value; - m_values.push_back(temp); - } - - /** - * This method is for setting AllowedValues - * - * @param values - list of Allowed Values to set. - */ - template - void addValues(const std::vector &values) - { - for (auto value : values) - { - ValueVariant vValue = value; - m_values.push_back(vValue); - } - } - - /** - * This method is for getting AllowedValues - * - * @param index - Allowed Values at index to be fetched - */ - inline ValueVariant &at(int index) - { - return m_values.at(index); - } - - /** - * This method is for getting size of AllowedValues - * - * @return size of Allowed Values list - */ - inline int size() const - { - return m_values.size(); - } - - /** - * This method is for getting AllowedValues. - * - * @return list of AllowedValues - */ - inline std::vector getValues() - { - return m_values; - } - - /** - * This method is for getting AllowedValues as integer. - * - * @return list of AllowedValues as integer. - */ - inline std::vector getValuesInt() - { - std::vector values; - for (auto value : m_values) - { - values.push_back(boost::lexical_cast (value)); - } - return values; - } - - /** - * This method is for getting AllowedValues as string. - * - * @return list of AllowedValues as string. - */ - inline std::vector getValuesString() - { - std::vector values; - for (auto value : m_values) - { - values.push_back(boost::lexical_cast (value)); - } - return values; - } - - /** - * This method is for getting AllowedValues as Double. - * - * @return list of AllowedValues as Double. - */ - inline std::vector getValuesDouble() - { - std::vector values; - for (auto value : m_values) - { - values.push_back(boost::lexical_cast (value)); - } - return values; - } - - /** - * This method is for getting AllowedValues as Bool. - * - * @return list of AllowedValues as Bool. - */ - inline std::vector getValuesBool() - { - std::vector values; - for (auto value : m_values) - { - values.push_back(boost::lexical_cast (value)); - } - return values; - } - - private: - std::vector m_values; - }; - -} -#endif diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.cpp b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.cpp new file mode 100755 index 0000000..f662469 --- /dev/null +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.cpp @@ -0,0 +1,91 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** + * @file Definitions.cpp + * + * @brief This file provides data Model for Json Schema Definitions. + */ + +#include "Definitions.h" + +namespace RAML +{ + std::string Definitions::getName() const + { + return m_defName; + } + + void Definitions::setName(const std::string &name) + { + m_defName = name; + } + + std::string Definitions::getType() const + { + return m_type; + } + + void Definitions::setType(const std::string &type) + { + m_type = type; + } + + std::vector const &Definitions::getRequiredValues() const + { + return m_required; + } + + void Definitions::setRequiredValue(const std::string &reqValue) + { + auto it = m_required.begin(); + for (; it != m_required.end(); ++it) + { + if (*it == reqValue) + break; + } + if (m_required.end() == it) + { + m_required.push_back(reqValue); + } + } + + PropertiesPtr Definitions::getproperty(const std::string &propName ) + { + if (m_properties.end() != m_properties.find(propName)) + { + return m_properties[propName]; + } + return nullptr; + } + + std::map const &Definitions::getProperties() + { + return m_properties; + } + + void Definitions::addProperty(const std::string &propName, const PropertiesPtr &property) + { + if (m_properties.end() == m_properties.find(propName)) + { + m_properties[propName] = property; + } + } +} diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h index d2eec8f..b6afe8a 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Definitions.h @@ -47,6 +47,30 @@ namespace RAML * Constructor of Definitions. */ Definitions() = default; + /** + * Copy Constructor of Definitions. + * + * @param Definitions + */ + Definitions(const Definitions &) = default; + /** + * Assignment operator for Definitions. + * + * @param Definitions + */ + Definitions &operator=(const Definitions &) = default; + /** + * Copy Constructor of Definitions. + * + * @param Definitions + */ + Definitions(Definitions &&) = default; + /** + * Assignment operator for Definitions. + * + * @param Definitions + */ + Definitions &operator=(Definitions &&) = default; /** * Constructor of Definitions. @@ -60,69 +84,40 @@ namespace RAML * * @return Definitions name as string */ - inline std::string getName(void) const - { - return m_defName; - } - + std::string getName() const; /** * This method is for setting name to Definitions * * @param name - Definitions name as string. */ - inline void setName(const std::string &name) - { - m_defName = name; - } + void setName(const std::string &name); /** * This method is for getting Type from Definitions. * * @return Definitions Type as string */ - inline std::string getType(void) const - { - return m_type; - } + std::string getType() const; /** * This method is for setting Type to Definitions * * @param type - Definitions Type as string. */ - inline void setType(const std::string &type) - { - m_type = type; - } - + void setType(const std::string &type); /** * This method is for getting RequiredValue from Definitions. * * @return list of RequiredValue as string */ - std::vector const &getRequiredValues() const - { - return m_required; - } + std::vector const &getRequiredValues() const; /** * This method is for setting RequiredValue to Definitions * * @param reqValue - RequiredValue as string. */ - void setRequiredValue(const std::string &reqValue) - { - auto it = m_required.begin(); - for (; it != m_required.end(); ++it) - { - if (*it == reqValue) - break; - } - if (m_required.end() == it) - { - m_required.push_back(reqValue); - } - } + void setRequiredValue(const std::string &reqValue); /** * This method is for getting size of Properties from Definitions. @@ -138,24 +133,14 @@ namespace RAML * * @return pointer to Properties. */ - inline PropertiesPtr getproperty(const std::string &propName ) - { - if (m_properties.end() != m_properties.find(propName)) - { - return m_properties[propName]; - } - return nullptr; - } + PropertiesPtr getproperty(const std::string &propName ); /** * This method is for getting Properties from Definitions. * * @return map of Property name and pointer to Properties */ - inline std::map const &getProperties() - { - return m_properties; - } + std::map const &getProperties(); /** * This method is for setting Properties to Definitions @@ -163,13 +148,8 @@ namespace RAML * @param propName - Definitions Type as string. * @param property - pointer to Properties. */ - void addProperty(const std::string &propName, const PropertiesPtr &property) - { - if (m_properties.end() == m_properties.find(propName)) - { - m_properties[propName] = property; - } - } + void addProperty(const std::string &propName, const PropertiesPtr &property); + private: std::map m_properties; std::string m_defName; diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Helpers.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Helpers.h deleted file mode 100755 index d5ceb68..0000000 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Helpers.h +++ /dev/null @@ -1,55 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ - -/** - * @file Helpers.h - * - * @brief This file provides helper definitions for Json Schema parser. - */ - -#ifndef HELPERS_H_ -#define HELPERS_H_ - -#include -#include -#include -#include -#include - -namespace RAML -{ - - /** ValueVariant - Boost Variant to hold type of int, string, double and bool*/ - typedef boost::variant < - int, - double, - bool, - std::string - > ValueVariant; - - /** VariantType - enumeration for variant types*/ - enum class VariantType - { - INT, DOUBLE, BOOL, STRING - }; - - -} -#endif diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h deleted file mode 100755 index d2463b2..0000000 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Items.h +++ /dev/null @@ -1,205 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ - -/** - * @file Items.h - * - * @brief This file provides data Model for Json Schema Array Items. - */ - -#ifndef ITEMS_H_ -#define ITEMS_H_ - -#include -#include -#include -#include "Properties.h" -#include "Helpers.h" -#include "AllowedValues.h" -#include - -namespace RAML -{ - class Properties; - class AllowedValues; - /** - * @class Items - * @brief This class provides data Model for Json Schema Array Items. - */ - class Items - { - public: - /** - * Constructor of Items. - */ - Items() {} - - /** - * This method is for setting Properties to Items - * - * @param propName - Properties name as string. - * @param property - pointer to Properties. - */ - void addProperty(const std::string &propName, const std::shared_ptr &property) - { - if (m_properties.end() == m_properties.find(propName)) - { - m_properties[propName] = property; - } - } - - /** - * This method is for getting Properties from Items. - * - * @param propName - Properties name as string. - * - * @return pointer to Properties to put the value got - */ - std::shared_ptr getproperty(const std::string &propName) - { - if (m_properties.end() != m_properties.find(propName)) - { - return m_properties[propName]; - } - return nullptr; - } - - /** - * This method is for getting Properties from Items. - * - * @return map of Properties name as string and pointer to Properties - */ - std::map > const &getProperties() - { - return m_properties; - } - - /** - * This method is for setting Type to Items - * - * @param type - Type as string. - */ - void setType(const std::string &type) - { - m_type = type; - } - - /** - * This method is for getting Type from Items. - * - * @return Type as string - */ - std::string getType() - { - return m_type; - } - - /** - * This method is for setting RequiredValue to Items - * - * @param reqValue - RequiredValue as string. - */ - void setRequiredValue(const std::string &reqValue) - { - auto it = m_required.begin(); - for (; it != m_required.end(); ++it) - { - if (*it == reqValue) - break; - } - if (m_required.end() == it) - { - m_required.push_back(reqValue); - } - } - - /** - * This method is for getting RequiredValue from Items. - * - * @return list of RequiredValue as string - */ - std::vector const &getRequiredValues() - { - return m_required; - } - - /** - * This method is for setting AllowedValues to Items - * - * @param values -list of AllowedValues. - */ - template - bool setAllowedValues(const std::vector &values) - { - m_allowedValues.addValues(values); - return true; - } - - /** - * This method is for getting size of AllowedValues from Items. - * - * @return size of AllowedValues - */ - inline int getAllowedValuesSize() const - { - return m_allowedValues.size(); - } - - /** - * This method is for getting AllowedValues from Items. - * - * @return list of AllowedValues - */ - inline std::vector getAllowedValues() - { - return m_allowedValues.getValues(); - } - - /** - * This method is for getting AllowedValues as Integer from Items. - * - * @return list of AllowedValues as Integer - */ - inline std::vector getAllowedValuesInt() - { - return m_allowedValues.getValuesInt(); - } - - /** - * This method is for getting AllowedValues as String from Items. - * - * @return list of AllowedValues as String - */ - inline std::vector getAllowedValuesString() - { - return m_allowedValues.getValuesString(); - } - private: - std::map > m_properties; - std::string m_type; - std::vector m_required; - AllowedValues m_allowedValues; - }; - - /** ItemsPtr - shared Ptr to Items.*/ - typedef std::shared_ptr ItemsPtr; -} -#endif - diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp index 0a0c11a..b62dcb5 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp @@ -18,9 +18,14 @@ * ******************************************************************/ -#include "JsonSchema.h" -using namespace std; +/** + * @file JsonSchema.cpp + * + * @brief This file reads data from Json Schema. + */ +#include "JsonSchema.h" +#include namespace RAML { @@ -78,26 +83,11 @@ namespace RAML } if (m_type == "array") { - cJSON *jsonItems = cJSON_GetObjectItem(m_cjson, "items"); - if (jsonItems) - { - if (jsonItems->type == 5) - { - int item_size = cJSON_GetArraySize(jsonItems); - int item_index = 0; - do - { - cJSON *item = cJSON_GetArrayItem(jsonItems, item_index); - setItem(readItems(item)); - } - while ( ++item_index < item_size); - } - else - { - setItem(readItems(jsonItems)); - } - } + PropertiesPtr property = std::make_shared("array"); + readArray(m_cjson, property); + addProperty("array" , property); } + cJSON *jsonAdditionalProperties = cJSON_GetObjectItem(m_cjson, "additionalProperties"); if (jsonAdditionalProperties) m_additionalProperties = jsonAdditionalProperties->type; @@ -107,12 +97,37 @@ namespace RAML cJSON *jsonReference = cJSON_GetObjectItem(m_cjson, "$ref"); if (jsonReference) { - readJsonRef(jsonReference); + JsonParameters param; + readJsonRef(jsonReference, param); + + for (auto it : param.getProperties()) + { + addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + setRequiredValue(it); + } + if (m_type.empty()) + m_type = param.getType(); } cJSON *jsonAllOf = cJSON_GetObjectItem(m_cjson, "allOf"); if (jsonAllOf) { - readAllOf(jsonAllOf); + JsonParameters param; + + readAllOf(jsonAllOf, param); + + for (auto it : param.getProperties()) + { + addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + setRequiredValue(it); + } + if (m_type.empty()) + m_type = param.getType(); } cJSON *jsonRequiredValues = cJSON_GetObjectItem(m_cjson, "required"); if (jsonRequiredValues) @@ -136,6 +151,12 @@ namespace RAML { std::string type = defType->valuestring; definition->setType(type); + if (type == "array") + { + PropertiesPtr property = std::make_shared("array"); + readArray(childDefinitions, property); + definition->addProperty("array" , property); + } } cJSON *defProperties = cJSON_GetObjectItem(childDefinitions, "properties"); if (defProperties) @@ -162,12 +183,36 @@ namespace RAML cJSON *defReference = cJSON_GetObjectItem(childDefinitions, "$ref"); if (defReference) { - readDefRef(defReference, definition); + JsonParameters param; + readJsonRef(defReference, param); + + for (auto it : param.getProperties()) + { + definition->addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + definition->setRequiredValue(it); + } + if (definition->getType().empty()) + definition->setType(param.getType()); } cJSON *defAllOf = cJSON_GetObjectItem(childDefinitions, "allOf"); if (defAllOf) { - readDefAllOf(defAllOf, definition); + JsonParameters param; + readAllOf(defAllOf, param); + + for (auto it : param.getProperties()) + { + definition->addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + definition->setRequiredValue(it); + } + if (definition->getType().empty()) + definition->setType(param.getType()); } return definition; } @@ -182,106 +227,125 @@ namespace RAML property->setDescription(propertyDescription->valuestring); } cJSON *propertyType = cJSON_GetObjectItem(childProperties, "type"); + std::string attType; if (propertyType) { - std::string attType; if (propertyType->type == 4) { attType = propertyType->valuestring; - property->setType(attType); } else if (propertyType->type == 5) { attType = cJSON_GetArrayItem(propertyType, 0)->valuestring; - property->setType(attType); } - readValues(childProperties, property, attType); } - cJSON *defaultValue = cJSON_GetObjectItem(childProperties, "default"); - if (defaultValue) + if (!(attType == "array") && !(attType == "object")) { - if (defaultValue->type == 4) + cJSON *defaultValue = cJSON_GetObjectItem(childProperties, "default"); + if (defaultValue) { - property->setValue((std::string)defaultValue->valuestring); + readDefaultValue(defaultValue, property, attType); } - else if (defaultValue->type == 3) - { - if (property->getType() == "number") - property->setValue((double)defaultValue->valuedouble); - else - property->setValue((int)defaultValue->valueint ); - } - else if (defaultValue->type == 1) - { - property->setValue((bool)true); - } - else if (defaultValue->type == 0) - { - property->setValue((bool)false); - } - } + readValues(childProperties, property, attType); cJSON *allowedvalues = cJSON_GetObjectItem(childProperties, "enum"); if (allowedvalues) { - if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4) + readAllowedValues(allowedvalues, property, attType); + } + property->setTypeString(attType); + return property; + } + + void JsonSchema::readDefaultValue(cJSON *defaultValue, PropertiesPtr &property, + const std::string &attType) + { + if (defaultValue->type == 4) + { + property->setValue((std::string)defaultValue->valuestring); + } + else if (defaultValue->type == 3) + { + if (attType == "number") + property->setValue((double)defaultValue->valuedouble); + else + property->setValue((int)defaultValue->valueint ); + } + else if (defaultValue->type == 1) + { + property->setValue((bool)true); + } + else if (defaultValue->type == 0) + { + property->setValue((bool)false); + } + } + + void JsonSchema::readAllowedValues(cJSON *allowedvalues, PropertiesPtr &property, + std::string &attType) + { + if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4) + { + int size = cJSON_GetArraySize(allowedvalues); + int idx = 0; + std::vector allwdValues; + do { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring); - } - while ( ++idx < size); - property->setAllowedValues(allwdValues); + allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring); } - else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3) + while ( ++idx < size); + property->setValueProperty(std::make_shared(allwdValues)); + if (attType.empty()) + attType = "string"; + } + else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3) + { + int size = cJSON_GetArraySize(allowedvalues); + int idx = 0; + if (attType == "number") { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - if (property->getType() == "number") - { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble); - } - while ( ++idx < size); - property->setAllowedValues(allwdValues); - } - else + std::vector allwdValues; + do { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint); - } - while ( ++idx < size); - property->setAllowedValues(allwdValues); + allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble); } + while ( ++idx < size); + property->setValueProperty(std::make_shared(allwdValues)); } - else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1) - || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0)) + else { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; + std::vector allwdValues; do { - if (cJSON_GetArrayItem(allowedvalues, idx)->type) - allwdValues.push_back(true); - else - allwdValues.push_back(false); + allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint); } while ( ++idx < size); - property->setAllowedValues(allwdValues); + property->setValueProperty(std::make_shared(allwdValues)); + if (attType.empty()) + attType = "integer"; } } - return property; + else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1) + || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0)) + { + int size = cJSON_GetArraySize(allowedvalues); + int idx = 0; + std::vector allwdValues; + do + { + if (cJSON_GetArrayItem(allowedvalues, idx)->type) + allwdValues.push_back(true); + else + allwdValues.push_back(false); + } + while ( ++idx < size); + property->setValueProperty(std::make_shared(allwdValues)); + if (attType.empty()) + attType = "boolean"; + } } - void JsonSchema::readValues(cJSON *childProperties, PropertiesPtr property , + void JsonSchema::readValues(cJSON *childProperties, PropertiesPtr &property , const std::string &attType) { if (attType == "string") @@ -292,31 +356,32 @@ namespace RAML { readInteger(childProperties, property); } - else if (attType == "array") - { - readArray(childProperties, property); - } else if (attType == "number") { readDouble(childProperties, property); } + else if (attType == "array") + { + readArray(childProperties, property); + } } - void JsonSchema::readString(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readString(cJSON *childProperties, PropertiesPtr &property) { cJSON *stringMax = cJSON_GetObjectItem(childProperties, "maxLength"); + int min = INT_MIN, max = INT_MAX; if (stringMax) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMax (--(stringMax->valueint)); + max = --(stringMax->valueint); else - property->setMax(stringMax->valueint); + max = stringMax->valueint; } else - property->setMax(stringMax->valueint); + max = stringMax->valueint; } cJSON *stringMin = cJSON_GetObjectItem(childProperties, "minLength"); if (stringMin) @@ -325,109 +390,89 @@ namespace RAML if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMin( ++(stringMin->valueint)); + min = ++(stringMin->valueint); else - property->setMin(stringMin->valueint); + min = stringMin->valueint; } else - property->setMin(stringMin->valueint); + min = stringMin->valueint; } + if (min != INT_MIN || max != INT_MAX) + property->setValueProperty(std::make_shared(min, max, 0)); + cJSON *stringFormat = cJSON_GetObjectItem(childProperties, "format"); if (stringFormat) { - property->setFormat(stringFormat->valuestring); + property->setValueProperty(std::make_shared + (ValueProperty::Type::FORMAT, (stringFormat->valuestring))); } + cJSON *stringPattern = cJSON_GetObjectItem(childProperties, "pattern"); if (stringPattern) { - property->setPattern(stringPattern->valuestring); + property->setValueProperty(std::make_shared + (ValueProperty::Type::PATTERN, (stringPattern->valuestring))); } } - void JsonSchema::readArray(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readInteger(cJSON *childProperties, PropertiesPtr &property) { - cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items"); - if (itemValues) - { - if (itemValues->type == 5) - { - int item_size = cJSON_GetArraySize(itemValues); - int item_index = 0; - do - { - cJSON *item = cJSON_GetArrayItem(itemValues, item_index); - property->setItem(readItems(item)); - } - while ( ++item_index < item_size); - } - else - { - property->setItem(readItems(itemValues)); - } - } - cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems"); - if (itemsMax) + cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); + int min = INT_MIN, max = INT_MAX, multipleOf = INT_MAX; + if (Max) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMax( --(itemsMax->valueint)); + max = --(Max->valueint); else - property->setMax(itemsMax->valueint); + max = Max->valueint; } else - property->setMax(itemsMax->valueint); + max = Max->valueint; } - cJSON *itemsMin = cJSON_GetObjectItem(childProperties, "minLength"); - if (itemsMin) + cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum"); + if (Min) { cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum"); if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMin( ++(itemsMin->valueint)); + min = ++(Min->valueint); else - property->setMin(itemsMin->valueint); + min = Min->valueint; } else - property->setMin(itemsMin->valueint); - } - cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems"); - if (uniqueItems) - { - property->setUnique(uniqueItems->type); - } - else - { - property->setUnique(cJSON_True); + min = Min->valueint; } - cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems"); - if (additionalItems) - { - property->setAdditionalItems(additionalItems->type); - } - else + cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf"); + if (MultipleOff) { - property->setAdditionalItems(cJSON_True); + multipleOf = MultipleOff->valueint; } + if (min != INT_MIN || max != INT_MAX) + property->setValueProperty(std::make_shared(min, max, multipleOf)); + } - void JsonSchema::readInteger(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readDouble(cJSON *childProperties, PropertiesPtr &property) { cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); + double min = INT_MIN, max = INT_MAX; + int multipleOf = INT_MAX; if (Max) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMax( --(Max->valueint)); + max = --(Max->valuedouble); else - property->setMax(Max->valueint); + max = Max->valuedouble; } else - property->setMax(Max->valueint); + max = Max->valuedouble; } cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum"); if (Min) @@ -436,423 +481,313 @@ namespace RAML if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMin( ++(Min->valueint)); + min = ++(Min->valuedouble); else - property->setMin(Min->valueint); + min = Min->valuedouble; } else - property->setMin(Min->valueint); + min = Min->valuedouble; } - cJSON *multipleOf = cJSON_GetObjectItem(childProperties, "multipleOf"); - if (multipleOf) + + cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf"); + if (MultipleOff) { - property->setMultipleOf(multipleOf->valueint); + multipleOf = MultipleOff->valueint; } + if (min != INT_MIN || max != INT_MAX) + property->setValueProperty(std::make_shared(min, max, multipleOf)); } - void JsonSchema::readDouble(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readArray(cJSON *childProperties, PropertiesPtr &property) { - cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); - if (Max) + cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items"); + if (itemValues) + { + if (itemValues->type == 5) + { + int item_size = cJSON_GetArraySize(itemValues); + int item_index = 0; + do + { + cJSON *item = cJSON_GetArrayItem(itemValues, item_index); + readItems(item, property); + break; + } + while ( ++item_index < item_size); + } + else + { + readItems(itemValues, property); + } + } + cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems"); + int min = INT_MIN, max = INT_MAX; + bool unique = cJSON_True, addItems = cJSON_True; + if (itemsMax) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMax( --(Max->valuedouble)); + max = --(itemsMax->valueint); else - property->setMax(Max->valuedouble); + max = itemsMax->valueint; } else - property->setMax(Max->valuedouble); + max = itemsMax->valueint; } - cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum"); - if (Min) + cJSON *itemsMin = cJSON_GetObjectItem(childProperties, "minItems"); + if (itemsMin) { cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum"); if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMin( ++(Min->valuedouble)); + min = ++(itemsMin->valueint); else - property->setMin(Min->valuedouble); + min = itemsMin->valueint; } else - property->setMin(Min->valuedouble); + min = itemsMin->valueint; } - cJSON *multipleOf = cJSON_GetObjectItem(childProperties, "multipleOf"); - if (multipleOf) + cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems"); + if (uniqueItems) { - property->setMultipleOf(multipleOf->valueint); + unique = uniqueItems->type; } - + cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems"); + if (additionalItems) + { + addItems = additionalItems->type; + } + property->setValueProperty(std::make_shared + (ValueProperty::Type::ARRAY, min, max, unique, addItems)); } - DefinitionsPtr JsonSchema::readRef(std::string m_ref) + void JsonSchema::readItems(cJSON *item, PropertiesPtr &property) { - std::string delimiter1 = "#"; - std::string delimiter2 = "/"; - std::string fileName; - if (! m_ref.empty()) + std::string type; + JsonParameters param; + cJSON *itemType = cJSON_GetObjectItem(item, "type"); + if (itemType) { - std::size_t pos = m_ref.find(delimiter1); - if ( (pos = m_ref.find(delimiter1)) != std::string::npos) - { - fileName = m_ref.substr(0, pos); - m_ref.erase(0, pos); - } - m_ref.erase(0, delimiter1 .length()); - std::string defName; + type = itemType->valuestring; + } - if (! m_ref.empty()) - { - m_ref.erase(0, delimiter2 .length()); - std::string keyName; - if ( (pos = m_ref.find(delimiter2)) != std::string::npos) - { - keyName = m_ref.substr(0, pos); - m_ref.erase(0, pos + delimiter2.length()); - if (keyName == "definitions") - { - if ( (pos = m_ref.find(delimiter2)) != std::string::npos) - { - defName = m_ref.substr(0, pos); - } - else if (! m_ref.empty()) - { - defName = m_ref; - } - } - } - } - if (!fileName.empty()) - { - if (!(defName.empty())) - { - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); - DefinitionsPtr definition = Refparser->getDefinition(defName); - if (definition == nullptr) - throw JsonException("Definition Name Incorrect"); - return definition; - } - } - else - { - if (!(defName.empty())) - { - if (getDefinition(defName) == nullptr) - throw JsonException("Definition Name Incorrect"); - return getDefinition(defName); - } - } + cJSON *itemAllOf = cJSON_GetObjectItem(item, "allOf"); + if (itemAllOf) + { + readAllOf(itemAllOf , param); } - throw JsonException("Definition Name Empty"); - return nullptr; - } - void JsonSchema::readAllOf(cJSON *allofValues) - { - int size = cJSON_GetArraySize(allofValues); - int index = 0; - do + cJSON *itemReference = cJSON_GetObjectItem(item, "$ref"); + if (itemReference) { - cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index); - cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref"); - if (jsonReference) + readJsonRef(itemReference , param); + } + + if (type == "object") + { + cJSON *itemProperties = cJSON_GetObjectItem(item, "properties"); + if (itemProperties) { - readJsonRef(jsonReference ); + cJSON *childProperties = itemProperties->child; + std::vector propertyVector; + while (childProperties) + { + std::string attName = childProperties->string; + PropertiesPtr prop = std::make_shared(attName); + readProp(childProperties, attName); + propertyVector.push_back(*prop); + childProperties = childProperties->next; + } + property->setValue(propertyVector); } - cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required"); - if (jsonRequiredValues) + + cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required"); + if (itemRequiredValues) { - int len = cJSON_GetArraySize(jsonRequiredValues); - int idx = 0; + int size = cJSON_GetArraySize(itemRequiredValues); + int index = 0; do { - setRequiredValue(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring); + property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring); } - while ( ++idx < len); + while ( ++index < size); } } - while ( ++index < size); - } - void JsonSchema::readJsonRef(cJSON *jsonReference) - { - std::string m_ref = jsonReference->valuestring; - std::map properties; - std::vector required; - - std::string web = "http://"; - std::string delimiter = "#"; - std::size_t pos = m_ref.find(web); - if (pos == std::string::npos) // If Web Link Is GIVEN TO READ + else if (param.getType() == "object") { - pos = m_ref.find(delimiter); - if ( pos == (m_ref.length() - 1) ) - { - std::string fileName = m_ref.substr(0, pos); - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); - - properties = Refparser->getProperties(); - required = Refparser->getRequiredValues(); - } - else - { - DefinitionsPtr definition = readRef(m_ref); - properties = definition->getProperties(); - required = definition->getRequiredValues(); - } - for ( auto it : properties) + std::vector propertyVector; + for (auto prop : param.getProperties()) { - std:: string name = it.first; - addProperty(name, it.second); + propertyVector.push_back(*(prop.second)); } - for (auto it : required ) + property->setValue(propertyVector); + + for (auto req : param.getRequired()) { - setRequiredValue(it); + property->setRequiredValue(req); } - } - } - void JsonSchema::readDefAllOf(cJSON *allofValues, DefinitionsPtr definition) - { - int size = cJSON_GetArraySize(allofValues); - int index = 0; - do + else { - cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index); - cJSON *defReference = cJSON_GetObjectItem(childAllOf, "$ref"); - if (defReference) + PropertiesPtr prop = std::make_shared("property"); + + cJSON *defaultValue = cJSON_GetObjectItem(item, "default"); + if (defaultValue) { - readDefRef(defReference , definition); + readDefaultValue(defaultValue, prop, type); } - cJSON *defRequiredValues = cJSON_GetObjectItem(allofValues, "required"); - if (defRequiredValues) + cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum"); + if (allowedvalues) { - int len = cJSON_GetArraySize(defRequiredValues); - int idx = 0; - do - { - definition->setRequiredValue(cJSON_GetArrayItem(defRequiredValues, idx)->valuestring); - } - while ( ++idx < len); + readAllowedValues(allowedvalues, prop, type); } + prop->setTypeString(type); + property->setValue(*prop); } - while ( ++index < size); } - void JsonSchema::readDefRef(cJSON *defReference, DefinitionsPtr definition) + + void JsonSchema::readFile(std::string &fileName , JsonParameters ¶m) { - std::string m_ref = defReference->valuestring; - std::map properties; - std::vector required; - std::string type; + cJSON *json = m_includeResolver->readToJson(fileName); + JsonSchemaPtr Refparser = std::make_shared(json, m_includeResolver); - std::string web = "http://"; - std::string delimiter = "#"; - std::size_t pos = m_ref.find(web); + param.addProperties(Refparser->getProperties()); + param.addRequired(Refparser->getRequiredValues()); + param.setType(Refparser->getType()); + } - if (pos == std::string::npos) // If Web Link Is GIVEN TO READ - { - pos = m_ref.find(delimiter); - if ( pos == (m_ref.length() - 1) ) - { - std::string fileName = m_ref.substr(0, pos); - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); + void JsonSchema::readFile(std::string &fileName , std::string &defName , JsonParameters ¶m) + { + cJSON *json = m_includeResolver->readToJson(fileName); + JsonSchemaPtr Refparser = std::make_shared(json, m_includeResolver); - properties = Refparser->getProperties(); - required = Refparser->getRequiredValues(); - type = Refparser->getType(); - } - else - { - DefinitionsPtr definitionRef = readRef(m_ref); - properties = definitionRef->getProperties(); - required = definitionRef->getRequiredValues(); - type = definitionRef->getType(); - } - for (auto it : properties) - { - definition->addProperty(it.first, it.second); - } - for ( auto it : required) - { - definition->setRequiredValue(it); - } - definition->setType(type); - } + DefinitionsPtr definition = Refparser->getDefinition(defName); + if (definition == nullptr) + throw JsonException("Definition Name Incorrect"); + + param.addProperties(definition->getProperties()); + param.addRequired(definition->getRequiredValues()); + param.setType(definition->getType()); } - ItemsPtr JsonSchema::readItems(cJSON *item) - { - ItemsPtr newItem = std::make_shared(); - cJSON *itemType = cJSON_GetObjectItem(item, "type"); - if (itemType) - { - std::string type = itemType->valuestring; - newItem->setType(type); - } - cJSON *itemProperties = cJSON_GetObjectItem(item, "properties"); - if (itemProperties) + void JsonSchema::readRef(std::string ref , JsonParameters ¶m) + { + std::string delimiter1 = "#"; + std::string delimiter2 = "/"; + std::string fileName; + if (! ref.empty()) { - cJSON *childProperties = itemProperties->child; - while (childProperties) + std::size_t pos = ref.find(delimiter1); + if ( (pos = ref.find(delimiter1)) != std::string::npos) { - std::string attName = childProperties->string; - - newItem->addProperty(attName, readProp(childProperties, attName)); - childProperties = childProperties->next; + fileName = ref.substr(0, pos); + ref.erase(0, pos); } - } + ref.erase(0, delimiter1 .length()); + std::string defName; - cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum"); - if (allowedvalues) - { - if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4) + if (! ref.empty()) { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; - do + ref.erase(0, delimiter2 .length()); + std::string keyName; + if ( (pos = ref.find(delimiter2)) != std::string::npos) { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring); + keyName = ref.substr(0, pos); + ref.erase(0, pos + delimiter2.length()); + if (keyName == "definitions") + { + if ( (pos = ref.find(delimiter2)) != std::string::npos) + { + defName = ref.substr(0, pos); + } + else if (! ref.empty()) + { + defName = ref; + } + } } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); } - else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3) + if (!fileName.empty()) { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - if (newItem->getType() == "number") + if (!(defName.empty())) { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble); - } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); + readFile(fileName, defName, param); } else { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint); - } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); + throw JsonException("Definition Name Empty"); } } - else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1) - || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0)) + else { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; - do + if (!(defName.empty())) { - if (cJSON_GetArrayItem(allowedvalues, idx)->type) - allwdValues.push_back(true); - else - allwdValues.push_back(false); + if (getDefinition(defName) == nullptr) + throw JsonException("Definition Name Incorrect"); + param.addProperties(getDefinition(defName)->getProperties()); + param.addRequired(getDefinition(defName)->getRequiredValues()); + param.setType(getDefinition(defName)->getType()); + } + else + { + throw JsonException("Definition Name Empty"); } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); } } - cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required"); - if (itemRequiredValues) - { - int size = cJSON_GetArraySize(itemRequiredValues); - int index = 0; - do - { - newItem->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring); - } - while ( ++index < size); - } - cJSON *itemReference = cJSON_GetObjectItem(item, "$ref"); - if (itemReference) - { - readItemRef(itemReference , newItem); - } - cJSON *itemAllOf = cJSON_GetObjectItem(item, "allOf"); - if (itemAllOf) - { - readItemAllOf(itemAllOf , newItem); - } - return newItem; } - void JsonSchema::readItemRef(cJSON *itemReference, ItemsPtr item) + void JsonSchema::readJsonRef(cJSON *jsonReference , JsonParameters ¶m) { - std::string m_ref = itemReference->valuestring; - std::map properties; - std::vector required; - std::string type; + std::string ref = jsonReference->valuestring; std::string web = "http://"; std::string delimiter = "#"; - std::size_t pos = m_ref.find(web); + std::size_t pos = ref.find(web); if (pos == std::string::npos) // If Web Link Is GIVEN TO READ { - pos = m_ref.find(delimiter); - if ( pos == (m_ref.length() - 1 ) ) + pos = ref.find(delimiter); + if ( pos == (ref.length() - 1) ) { - std::string fileName = m_ref.substr(0, pos); - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); - - properties = Refparser->getProperties(); - required = Refparser->getRequiredValues(); - type = Refparser->getType(); + std::string fileName = ref.substr(0, pos); + readFile(fileName, param); } else { - DefinitionsPtr definitionRef = readRef(m_ref); - properties = definitionRef->getProperties(); - required = definitionRef->getRequiredValues(); - type = definitionRef->getType(); + readRef(ref, param); } - for ( auto it : properties) - { - std:: string name = it.first; - item->addProperty(name, it.second); - } - for ( auto it : required) - { - item->setRequiredValue(it); - } - item->setType(type); } } - void JsonSchema::readItemAllOf(cJSON *allofValues, ItemsPtr item) + void JsonSchema::readAllOf(cJSON *allofValues , JsonParameters &allParams) { int size = cJSON_GetArraySize(allofValues); int index = 0; do { + JsonParameters param; + cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index); - cJSON *itemReference = cJSON_GetObjectItem(childAllOf, "$ref"); - if (itemReference) + cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref"); + if (jsonReference) { - readItemRef(itemReference, item); + readJsonRef(jsonReference, param); + allParams.addProperties(param.getProperties()); + allParams.addRequired(param.getRequired()); + allParams.setType(param.getType()); } - cJSON *itemRequiredValues = cJSON_GetObjectItem(allofValues, "required"); - if (itemRequiredValues) + cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required"); + if (jsonRequiredValues) { - int len = cJSON_GetArraySize(itemRequiredValues); + int len = cJSON_GetArraySize(jsonRequiredValues); int idx = 0; do { - item->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, idx)->valuestring); + allParams.addRequired(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring); } while ( ++idx < len); } diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h index c52a145..a9fe422 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.h @@ -31,11 +31,8 @@ #include #include #include "Properties.h" -#include "Items.h" #include "Definitions.h" #include "cJSON.h" -#include "Helpers.h" -#include "AllowedValues.h" #include #include "IncludeResolver.h" @@ -52,14 +49,15 @@ namespace RAML /** * Constructor of JsonSchema. */ - JsonSchema() : m_cjson(NULL), m_includeResolver(NULL) {} + JsonSchema() : m_additionalProperties(cJSON_True), m_cjson(NULL), m_includeResolver(NULL) {} /** * Constructor of JsonSchema. * * @param includeResolver - Reference to IncludeResolver for reading external files */ - JsonSchema(const IncludeResolverPtr &includeResolver) : m_cjson(NULL), + JsonSchema(const IncludeResolverPtr &includeResolver) : m_additionalProperties(cJSON_True), + m_cjson(NULL), m_includeResolver(includeResolver) {} /** @@ -68,7 +66,8 @@ namespace RAML * @param cjson - pointer to cjson * @param includeResolver - Reference to IncludeResolver for reading external files */ - JsonSchema(cJSON *cjson , const IncludeResolverPtr &includeResolver) : m_cjson(cjson), + JsonSchema(cJSON *cjson , const IncludeResolverPtr &includeResolver) : m_additionalProperties( + cJSON_True), m_cjson(cjson), m_includeResolver(includeResolver) { readJson(); } @@ -96,7 +95,7 @@ namespace RAML * * @return pointer to Properties */ - inline PropertiesPtr getProperty(const std::string &name) + PropertiesPtr getProperty(const std::string &name) { if (m_properties.end() != m_properties.find(name)) { @@ -110,7 +109,7 @@ namespace RAML * * @return map of Properties name and pointer to Properties */ - inline std::map const &getProperties() + std::map const &getProperties() { return m_properties; } @@ -120,7 +119,7 @@ namespace RAML * * @return map of Definitions name and pointer to Definitions */ - inline std::map const &getDefinitions() + std::map const &getDefinitions() { return m_definition; } @@ -252,46 +251,55 @@ namespace RAML return m_additionalProperties; } - /** - * This method is for setting Items to JsonSchema. - * - * @param item -pointer to Items - */ - void setItem(const ItemsPtr &item) - { - m_items.push_back(item); - } - - /** - * This method is for getting Items from JsonSchema. - * - * @return vector of Items - */ - std::vector const &getItems() + private: + class JsonParameters { - return m_items; - } + public: + std::map getProperties() const { return m_properties; } + void addProperties(const std::string &name, const PropertiesPtr &prop) { m_properties[name] = prop; } + void addProperties(const std::map &properties) + { + for (auto prop : properties) + m_properties[prop.first] = prop.second; + } + std::vector getRequired() const { return m_required; } + void addRequired(const std::string &req) { m_required.push_back(req); } + void addRequired(const std::vector &required) + { + for (auto req : required) + m_required.push_back(req); + } + std::string getType() const { return m_type; } + void setType(const std::string &type) + { + if (m_type.empty()) + m_type = type; + } + + private: + std::map m_properties; + std::vector m_required; + std::string m_type; + }; - private: void readJson(); DefinitionsPtr readDef(cJSON *childDefinitions, const std::string &defName); PropertiesPtr readProp(cJSON *childProperties, const std::string &attName ); - void readValues( cJSON *childProperties, PropertiesPtr property , + void readDefaultValue(cJSON *defaultValue, PropertiesPtr &property, const std::string &attType); + void readAllowedValues(cJSON *allowedvalues, PropertiesPtr &property, std::string &attType); + void readValues( cJSON *childProperties, PropertiesPtr &property , const std::string &attType); - void readString( cJSON *childProperties, PropertiesPtr property); - void readArray( cJSON *childProperties, PropertiesPtr property); - void readInteger( cJSON *childProperties, PropertiesPtr property); - void readDouble( cJSON *childProperties, PropertiesPtr property); - DefinitionsPtr readRef(std::string m_ref); - - - void readJsonRef(cJSON *jsonReference); - void readDefRef(cJSON *defReference, DefinitionsPtr definition); - void readAllOf(cJSON *allofValues); - void readDefAllOf(cJSON *allofValues, DefinitionsPtr definition); - ItemsPtr readItems(cJSON *item); - void readItemRef(cJSON *itemReference, ItemsPtr item); - void readItemAllOf(cJSON *allofValues, ItemsPtr item); + void readString( cJSON *childProperties, PropertiesPtr &property); + void readInteger( cJSON *childProperties, PropertiesPtr &property); + void readDouble( cJSON *childProperties, PropertiesPtr &property); + void readArray( cJSON *childProperties, PropertiesPtr &property); + void readItems(cJSON *item, PropertiesPtr &property); + + void readFile(std::string &fileName , JsonParameters ¶m); + void readFile(std::string &fileName , std::string &defName , JsonParameters ¶m); + void readRef(std::string ref , JsonParameters ¶m); + void readJsonRef(cJSON *jsonReference , JsonParameters ¶m); + void readAllOf(cJSON *allofValues , JsonParameters &allParams); private: std::map m_properties; @@ -304,8 +312,9 @@ namespace RAML std::string m_type; cJSON *m_cjson; std::vector m_required; - std::vector m_items; + PropertiesPtr m_property; IncludeResolverPtr m_includeResolver; + }; /** JsonSchemaPtr - shared Ptr to JsonSchema.*/ diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp new file mode 100755 index 0000000..bc29606 --- /dev/null +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.cpp @@ -0,0 +1,410 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** + * @file Properties.cpp + * + * @brief This file provides data Model for Json Schema Properties. + */ + +#include "Properties.h" +#include +#include +#include "RamlExceptions.h" + +namespace RAML +{ + template + struct TypeConverter {}; + + template <> + struct TypeConverter + { + constexpr static VariantType type = + VariantType::INTEGER; + }; + + template <> + struct TypeConverter + { + constexpr static VariantType type = + VariantType::DOUBLE; + }; + + template <> + struct TypeConverter + { + constexpr static VariantType type = + VariantType::BOOLEAN; + }; + + template <> + struct TypeConverter + { + constexpr static VariantType type = + VariantType::STRING; + }; + + template <> + struct TypeConverter + { + constexpr static VariantType type = + VariantType::PROPERTY; + }; + + template + struct TypeDetails + { + constexpr static VariantType type = + TypeConverter::type; + constexpr static VariantType baseType = + TypeConverter::type; + constexpr static int depth = 0; + }; + + template + struct TypeDetails> + { + constexpr static VariantType type = + VariantType::ARRAY; + constexpr static VariantType baseType = + TypeDetails::baseType; + constexpr static int depth = 1 + TypeDetails::depth; + }; + + class PropertyTypeVisitor : public boost::static_visitor<> + { + public: + PropertyTypeVisitor() : m_type(VariantType::UNKNOWN), + m_baseType(VariantType::UNKNOWN), m_depth(0) {} + + template + void operator ()(const T &) + { + m_type = TypeDetails::type; + m_baseType = TypeDetails::baseType; + m_depth = TypeDetails::depth; + } + + VariantType m_type; + VariantType m_baseType; + int m_depth; + }; + + Properties::TypeInfo Properties::getType() const + { + if (m_value) + { + RAML::PropertyTypeVisitor typeVisitor; + boost::apply_visitor(typeVisitor, *(m_value.get())); + Properties::TypeInfo typeInfo(typeVisitor.m_type, typeVisitor.m_baseType, + typeVisitor.m_depth); + return typeInfo; + } + else if (!m_typeString.empty()) //to read properties even if default value is not present + { + if (m_typeString == "string") + return Properties::TypeInfo(VariantType::STRING, VariantType::STRING, 0); + else if (m_typeString == "integer") + return Properties::TypeInfo(VariantType::INTEGER, VariantType::INTEGER, 0); + else if (m_typeString == "number") + return Properties::TypeInfo(VariantType::DOUBLE, VariantType::DOUBLE, 0); + else if (m_typeString == "boolean") + return Properties::TypeInfo(VariantType::BOOLEAN, VariantType::BOOLEAN, 0); + } + return Properties::TypeInfo(); + } + + void Properties::setTypeString(const std::string &type) + { + m_typeString = type; + } + + Properties::TypeInfo::TypeInfo( + VariantType type = VariantType::UNKNOWN, + VariantType baseType = VariantType::UNKNOWN, + int depth = 0) + : m_type (type), m_baseType(baseType), m_depth(depth) {} + + VariantType Properties::TypeInfo::type() const + { + return m_type; + } + + VariantType Properties::TypeInfo::baseType() const + { + return m_baseType; + } + + int Properties::TypeInfo::depth() const + { + return m_depth; + } + + bool Properties::TypeInfo::operator==( + const Properties::TypeInfo &rhs ) const + { + if (m_type == rhs.type() && m_baseType == rhs.baseType() + && m_depth == rhs.depth()) + return true; + return false; + } + + bool Properties::TypeInfo::operator!=( + const Properties::TypeInfo &rhs ) const + { + if (m_type != rhs.type() || m_baseType != rhs.baseType() + || m_depth != rhs.depth()) + return true; + return false; + } + + ValueVariant Properties::getValue() const + { + if (!isDefaultValue()) + throw JsonException("Reading Empty Property Value"); + return *m_value; + } + + std::string Properties::getName() const + { + return m_name; + } + + void Properties::setName(const std::string &name) + { + m_name = name; + } + std::string Properties::getDescription() const + { + return m_description; + } + + void Properties::setDescription(const std::string &description) + { + m_description = description; + } + std::vector > const &Properties::getValueProperties() + const + { + return m_valueProperty; + } + void Properties::setValueProperty(const std::shared_ptr &value) + { + m_valueProperty.push_back(value); + } + std::vector const &Properties::getRequiredValues() const + { + return m_required; + } + void Properties::setRequiredValue(const std::string &reqValue) + { + auto it = m_required.begin(); + for (; it != m_required.end(); ++it) + { + if (*it == reqValue) + break; + } + if (m_required.end() == it) + { + m_required.push_back(reqValue); + } + } + ValueProperty::ValueProperty() + : m_type(ValueProperty::Type::UNKNOWN), + m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) {} + + ValueProperty::ValueProperty(double min, double max, int multipleOf) + : m_type(ValueProperty::Type::RANGE), + m_min(min), + m_max(max), + m_multipleOf(multipleOf), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) {} + + ValueProperty::ValueProperty( + const std::vector &valueSet) + : m_type(ValueProperty::Type::VALUE_SET), + m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_valueSet(valueSet.begin(), valueSet.end()), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) {} + + ValueProperty::ValueProperty( + const std::vector &valueSet) + : m_type(ValueProperty::Type::VALUE_SET), + m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_valueSet(valueSet.begin(), valueSet.end()), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) {} + + ValueProperty::ValueProperty( + const std::vector &valueSet) + : m_type(ValueProperty::Type::VALUE_SET), + m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_valueSet(valueSet.begin(), valueSet.end()), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) {} + + ValueProperty::ValueProperty( + const std::vector &valueSet) + : m_type(ValueProperty::Type::VALUE_SET), + m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_valueSet(valueSet.begin(), valueSet.end()), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) {} + + ValueProperty::ValueProperty( + const std::vector &valueSet) + : m_type(ValueProperty::Type::VALUE_SET), + m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_valueSet(valueSet.begin(), valueSet.end()), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) {} + + ValueProperty::ValueProperty(Type type, std::string value) + : m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) + { + if (type == ValueProperty::Type::PATTERN) + { + m_type = ValueProperty::Type::PATTERN; + m_pattern = value; + } + else if (type == ValueProperty::Type::FORMAT) + { + m_type = ValueProperty::Type::FORMAT; + m_format = value; + } + else + { + m_type = ValueProperty::Type::UNKNOWN; + } + } + + ValueProperty::ValueProperty(Type type, int minItems, int maxItems, bool unique, + bool additionalItems) + : m_type(ValueProperty::Type::UNKNOWN), + m_min(INT_MIN), + m_max(INT_MAX), + m_multipleOf(INT_MAX), + m_minItems(INT_MIN), + m_maxItems(INT_MAX), + m_unique(false), + m_additionalItems(false) + { + if (type == ValueProperty::Type::ARRAY) + { + m_type = ValueProperty::Type::ARRAY; + m_minItems = minItems; + m_maxItems = maxItems; + m_unique = unique; + m_additionalItems = additionalItems; + } + else + { + m_type = ValueProperty::Type::UNKNOWN; + } + } + + ValueProperty::Type ValueProperty::type() const + { + return m_type; + } + + double ValueProperty::min() const + { + return m_min; + } + + double ValueProperty::max() const + { + return m_max; + } + + int ValueProperty::multipleOf() const + { + return m_multipleOf; + } + + std::string ValueProperty::pattern() const + { + return m_pattern; + } + + std::string ValueProperty::format() const + { + return m_format; + } + + int ValueProperty::valueSetSize() const + { + return m_valueSet.size(); + } + + std::vector ValueProperty::valueSet() const + { + return m_valueSet; + } + + void ValueProperty::valueArray(int &minItems, int &maxItems, bool &unique, + bool &additionalItems) const + { + minItems = m_minItems; + maxItems = m_maxItems; + unique = m_unique; + additionalItems = m_additionalItems; + } + + +} diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h index c4dda1d..af2ae55 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/Properties.h @@ -33,13 +33,210 @@ #include #include #include -#include "Items.h" -#include "AllowedValues.h" #include "cJSON.h" #include namespace RAML { + class Properties; + + /** ValueVariant - Boost Variant to hold type of property*/ + typedef boost::variant < + int, + double, + bool, + std::string, + Properties, + std::vector + + > ValueVariant; + + /** VariantType - enumeration for variant types*/ + enum class VariantType + { + UNKNOWN, + INTEGER, + DOUBLE, + BOOLEAN, + STRING, + PROPERTY, + ARRAY + }; + /** + * @class ValueProperty + * @brief This class provides data Model for Json Schema Value Property. + */ + class ValueProperty + { + public: + /** Type - enumeration for ValueProperty types*/ + enum class Type + { + UNKNOWN, + RANGE, + VALUE_SET, + PATTERN, + FORMAT, + ARRAY + }; + + /** + * Constructor of ValueProperty. + */ + ValueProperty(); + /** + * Copy Constructor of ValueProperty. + * + * @param ValueProperty. + */ + ValueProperty(const ValueProperty &) = default; + /** + * Assignment operator for ValueProperty. + * + * @param ValueProperty. + */ + ValueProperty &operator=(const ValueProperty &) = default; + /** + * Copy Constructor of ValueProperty. + * + * @param ValueProperty. + */ + ValueProperty(ValueProperty &&) = default; + /** + * Assignment operator for ValueProperty. + * + * @param ValueProperty. + */ + ValueProperty &operator=(ValueProperty &&) = default; + + /** + * explicit Constructor of ValueProperty for Type RANGE. + * + * @param min - minimum value of property. + * @param max- maximum value of property. + * @param multipleOf- multipleOf value of property. + */ + explicit ValueProperty(double min, double max, int multipleOf); + /** + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ + explicit ValueProperty(const std::vector &valueSet); + /** + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ + explicit ValueProperty(const std::vector &valueSet); + /** + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ + explicit ValueProperty(const std::vector &valueSet); + /** + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ + explicit ValueProperty(const std::vector &valueSet); + /** + * explicit Constructor of ValueProperty for Type VALUE_SET. + * + * @param valueSet - allowed values in the Properties. + */ + explicit ValueProperty(const std::vector &valueSet); + /** + * explicit Constructor of ValueProperty for Type PATTERN or FORMAT. + * + * @param type - ValueProperty Type. + * @param value - value for the pattern or format. + */ + explicit ValueProperty(Type type, std::string value); + /** + * explicit Constructor of ValueProperty for Type ARRAY. + * + * @param type - ValueProperty Type. + * @param minItems - minimum elements in the Array property. + * @param maxItems - maximum elements in the Array property. + * @param unique - unique elements in the Array property. + * @param additionalItems - additional elements in the Array property. + */ + explicit ValueProperty(Type type, int minItems, int maxItems, bool unique, bool additionalItems); + + /** + * This method is for getting type of ValueProperty. + * + * @return Type of ValueProperty + */ + Type type() const; + /** + * This method is for getting minimum value of ValueProperty. + * + * @return min value of ValueProperty + */ + double min() const; + /** + * This method is for getting maximum value of ValueProperty. + * + * @return max value of ValueProperty + */ + double max() const; + /** + * This method is for getting multipleOf value of ValueProperty. + * + * @return multipleOf value of ValueProperty + */ + int multipleOf() const; + /** + * This method is for getting pattern value of ValueProperty. + * + * @return pattern value of ValueProperty + */ + std::string pattern() const; + /** + * This method is for getting format value of ValueProperty. + * + * @return format value of ValueProperty + */ + std::string format() const; + /** + * This method is for getting valueSet of ValueProperty. + * + * @return valueSet of ValueProperty + */ + int valueSetSize() const; + /** + * This method is for getting valueSet of ValueProperty. + * + * @return valueSet of ValueProperty + */ + std::vector valueSet() const; + /** + * This method is for getting valueArray of ValueProperty. + * + * @param minItems - reference to get minimum elements in the Array property. + * @param maxItems - reference to get maximum elements in the Array property. + * @param unique - reference to get unique elements in the Array property. + * @param additionalItems - reference to get additional elements in the Array property. + */ + void valueArray(int &minItems, int &maxItems, bool &unique, bool &additionalItems) const; + + private: + Type m_type; + double m_min; + double m_max; + int m_multipleOf; + std::vector m_valueSet; + std::string m_pattern; + std::string m_format; + int m_minItems; + int m_maxItems; + bool m_unique; + bool m_additionalItems; + }; + /** * @class Properties * @brief This class provides data Model for Json Schema Properties. @@ -48,420 +245,240 @@ namespace RAML { public: /** - * Constructor of Properties. - */ - Properties(): m_min(INT_MAX), m_max(INT_MAX), - m_multipleOf(INT_MAX), m_unique(false), m_additionalItems(false) {} + * @class TypeInfo + * @brief This class provides type information of Json Properties. + */ + class TypeInfo + { + public: + /** + * Constructor of TypeInfo. + * + * @param VariantType - type of property. + * @param VariantType - type of parent property. + * @param int - depth of property. + */ + TypeInfo(VariantType, VariantType, int); + /** + * Copy Constructor of TypeInfo. + * + * @param TypeInfo. + */ + TypeInfo(const TypeInfo &) = default; + /** + * Assignment operator for TypeInfo. + * + * @param TypeInfo. + */ + TypeInfo &operator=(const TypeInfo &) = default; + /** + * Copy Constructor of TypeInfo. + * + * @param TypeInfo. + */ + TypeInfo(TypeInfo &&) = default; + /** + * Assignment operator for TypeInfo. + * + * @param TypeInfo. + */ + TypeInfo &operator=(TypeInfo &&) = default; + /** + * Constructor of TypeInfo. + */ + TypeInfo() = default; + + /** + * This method is for getting type of properties. + * + * @return VariantType of Property + */ + VariantType type() const; + /** + * This method is for getting base or parent type of properties. + * + * @return VariantType of parent Property + */ + VariantType baseType() const; + /** + * This method is for getting depth of properties. + * + * @return depth as int + */ + int depth() const; + /** + * operator for TypeInfo to check equality. + * + * @param TypeInfo. + */ + bool operator ==(const TypeInfo &) const; + /** + * operator for TypeInfo to check inequality. + * + * @param TypeInfo. + */ + bool operator !=(const TypeInfo &) const; + + private: + VariantType m_type; + VariantType m_baseType; + int m_depth; + }; /** * Constructor of Properties. * * @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_unique(false), m_additionalItems(false) {} - - /** - * This method is for getting Name from Properties. - * - * @return Properties name as string - */ - inline std::string getName(void) const - { - return m_name; - } - - /** - * This method is for setting name to Properties - * - * @param name - Properties name as string. - */ - inline void setName(const std::string &name) - { - m_name = name; - } - - /** - * This method is for getting Value from Properties. - * - * @return Properties Value - */ - template - T getValue() const - { - return boost::get(m_value); - } - - /** - * This method is for getting Value from Properties. - * - * @return Properties Value - */ - ValueVariant &getValue() - { - return m_value; - } - - /** - * This method is for getting ValueVariant type from Properties. - * - * @return Properties Value type as Int - */ - int getValueType() const - { - return m_value.which(); - } - - /** - * This method is for getting ValueVariant type from Properties. - * - * @return Properties VariantType type - */ - VariantType getVariantType() const - { - if (m_value.which() == 3) - return VariantType::STRING; - else if (m_value.which() == 2) - return VariantType::BOOL; - else if (m_value.which() == 1) - return VariantType::DOUBLE; - else - return VariantType::INT; - } - - /** - * This method is for getting Value type as Integer from Properties. - * - * @return Properties Value type as Integer - */ - int getValueInt() - { - return boost::lexical_cast (m_value); - } - - /** - * This method is for getting Value type as String from Properties. - * - * @return Properties Value type as String - */ - std::string getValueString() - { - return boost::lexical_cast (m_value); - } - + Properties(const std::string &name) : m_name(name) {} /** - * This method is for getting Value type as double from Properties. - * - * @return Properties Value type as double - */ - double getValueDouble() - { - return boost::lexical_cast (m_value); - } - - /** - * This method is for getting Value type as bool from Properties. - * - * @return Properties Value type as bool - */ - bool getValueBool() - { - return boost::lexical_cast (m_value); - } - - /** - * This method is for setting Value to Properties - * - * @param value - Properties Value. - */ - template - void setValue(const T &value) - { - m_value = value; - } - - /** - * 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 getRange(double &min, double &max, int &multipleOf) const - { - min = m_min; - max = m_max; - multipleOf = m_multipleOf; - } - - /** - * This method is for setting Minimum to Properties. - * - * @param min - Minimum value of Properties. - */ - inline void setMin(double min) - { - m_min = min; - } - - /** - * This method is for setting Maximum to Properties - * - * @param max - Maximum value of Properties. - */ - inline void setMax(double max) - { - m_max = max; - } - /** - * This method is for setting multipleOf to Properties - * - * @param multipleOf - multipleOf value of Properties. - */ - inline void setMultipleOf(const int &multipleOf) - { - m_multipleOf = multipleOf; - } - + * Constructor of Properties. + */ + Properties() = default; /** - * This method is for setting AllowedValues to Properties - * - * @param values - list of AllowedValues of Properties. - */ - template - bool setAllowedValues(const std::vector &values) - { - ValueVariant temp = values.at(0); - if (temp.which() != m_value.which()) - { - return false; - } - - m_allowedValues.addValues(values); - return true; - } - + * Copy Constructor of Properties. + * + * @param Properties. + */ + Properties(const Properties &) = default; /** - * This method is for getting size of AllowedValues from Properties. - * - * @return size of AllowedValues - */ - inline int getAllowedValuesSize() const - { - return m_allowedValues.size(); - } - + * Assignment operator for Properties. + * + * @param Properties. + */ + Properties &operator=(const Properties &) = default; /** - * This method is for getting AllowedValues from Properties. - * - * @return list of AllowedValues of Properties. - */ - inline std::vector getAllowedValues() - { - return m_allowedValues.getValues(); - } - + * Copy Constructor of Properties. + * + * @param Properties. + */ + Properties(Properties &&) = default; /** - * This method is for getting AllowedValues as integer from Properties. - * - * @return list of AllowedValues as integer - */ - inline std::vector getAllowedValuesInt() - { - return m_allowedValues.getValuesInt(); - } + * Assignment operator for Properties. + * + * @param Properties. + */ + Properties &operator=(Properties &&) = default; /** - * This method is for getting AllowedValues as String from Properties. + * This method is for getting TypeInfo of Properties. * - * @return list of AllowedValues as String + * @return Properties TypeInfo */ - inline std::vector getAllowedValuesString() - { - return m_allowedValues.getValuesString(); - } + TypeInfo getType() const; /** - * This method is for getting AllowedValues as Double from Properties. + * This method is for setting type of Properties. * - * @return list of AllowedValues as Double + * @param type -Propertie's Type */ - inline std::vector getAllowedValuesDouble() - { - return m_allowedValues.getValuesDouble(); - } + void setTypeString(const std::string &type); /** - * This method is for getting AllowedValues as Bool from Properties. + * This method is for getting Name from Properties. * - * @return list of AllowedValues as Bool + * @return Properties name as string */ - inline std::vector getAllowedValuesBool() - { - return m_allowedValues.getValuesBool(); - } - + std::string getName() const; /** - * This method is for setting Description to Properties + * This method is for setting name to Properties * - * @param description - Description as string. + * @param name - Properties name as string. */ - inline void setDescription(const std::string &description) - { - m_description = description; - } - + void setName(const std::string &name); /** * This method is for getting Description from Properties. * * @return Description as string */ - inline std::string getDescription() - { - return m_description; - } + std::string getDescription() const; /** - * This method is for setting Type to Properties + * This method is for setting Description to Properties * - * @param type - Type as string. + * @param description - Description as string. */ - void setType(const std::string &type) - { - m_type = type; - } + void setDescription(const std::string &description); /** - * This method is for getting Type from Properties. + * This method is for setting Value to Properties. * - * @return Type as string + * @param value - Value of Properties */ - std::string getType() + template + void setValue(const T &value) { - return m_type; + m_value = std::make_shared(value); } /** - * This method is for setting Pattern to Properties + * This method is for getting Value from Properties. * - * @param pattern - Pattern as string. + * @return Properties Value */ - void setPattern(const std::string &pattern) - { - m_pattern = pattern; - } - + ValueVariant getValue() const; /** - * This method is for getting Pattern from Properties. + * This method is for getting Value from Properties. * - * @return Pattern as string + * @return Properties Value */ - std::string getPattern() + template + T getValue() const { - return m_pattern; + return boost::get(m_value); } /** - * This method is for setting Format to Properties + * This method is for checking if default Value exists in the Properties. * - * @param format - Format as string. + * @return true if present and false if not present */ - void setFormat(const std::string &format) - { - m_format = format; - } - + bool isDefaultValue() const { return ((m_value != nullptr) ? true : false); } /** - * This method is for getting Format from Properties. + * This method is for getting ValueProperty from Properties. * - * @return Format as string + * @return vector of pointer to ValueProperty */ - std::string getFormat() - { - return m_format; - } + std::vector > const &getValueProperties() const; /** - * This method is for setting Items to Properties + * This method is for setting ValueProperty to Properties * - * @param item - pointer to Items + * @param value - pointer to ValueProperty */ - void setItem(const ItemsPtr &item) - { - m_items.push_back(item); - } + void setValueProperty(const std::shared_ptr &value); - /** - * This method is for getting Items from Properties. - * - * @return list of pointer to Items - */ - std::vector const &getItems() const - { - return m_items; - } /** - * This method is for setting Unique to Properties + * This method is for getting RequiredValue from Properties. * - * @param value - Unique as bool + * @return list of RequiredValue as string */ - void setUnique( int value) - { - if (value == cJSON_True) m_unique = true; - else m_unique = false; - } + std::vector const &getRequiredValues() const; /** - * This method is for getting isUnique from Properties. + * This method is for setting RequiredValue to Properties * - * @return isUnique as bool + * @param reqValue - RequiredValue as string. */ - bool getUnique() - { - return m_unique; - } - - /** - * This method is for setting AdditionalItems to Properties - * - * @param value - AdditionalItems as bool - */ - void setAdditionalItems(int value) - { - if (value == cJSON_True) m_additionalItems = true; - else m_additionalItems = false; - } + void setRequiredValue(const std::string &reqValue); - /** - * This method is for getting AdditionalItems from Properties. - * - * @return AdditionalItems as bool - */ - bool getAdditionalItems() - { - return m_additionalItems; - } private: + TypeInfo m_type; + std::string m_typeString; std::string m_name; - ValueVariant m_value; - double m_min; - double m_max; - int m_multipleOf; - AllowedValues m_allowedValues; - std::string m_type; - std::string m_pattern; - std::string m_format; std::string m_description; - bool m_unique; - bool m_additionalItems; - std::vector m_items; + std::shared_ptr m_value; + std::vector > m_valueProperty; + std::vector m_required; }; /** PropertiesPtr - shared Ptr to Properties.*/ typedef std::shared_ptr PropertiesPtr; + /** ValuePropertyPtr - shared Ptr to ValueProperty.*/ + typedef std::shared_ptr ValuePropertyPtr; + } #endif diff --git a/service/simulator/src/common/request_model_builder.cpp b/service/simulator/src/common/request_model_builder.cpp index b181433..67f2f3e 100755 --- a/service/simulator/src/common/request_model_builder.cpp +++ b/service/simulator/src/common/request_model_builder.cpp @@ -107,6 +107,158 @@ ResponseModelSP RequestModelBuilder::createResponseModel(int code, responseModel->setRepSchema(repSchema); return responseModel; } +template +void RequestModelBuilder::buildValueProperty(SimulatorResourceModel::Attribute &attribute, + const std::vector &valueProperties, T) +{ + for (auto &vp : valueProperties) + { + switch (vp->type()) + { + case RAML::ValueProperty::Type::RANGE : + { + double min = vp->min(); + double max = vp->max(); + int multipleof = vp->multipleOf(); + if (min != INT_MIN && max != INT_MAX) + { + SimulatorResourceModel::AttributeProperty attrProp(min, max); + attribute.setProperty(attrProp); + } + break; + } + case RAML::ValueProperty::Type::VALUE_SET : + { + std::vector allowedValues; + for (auto allow : vp->valueSet()) + allowedValues.push_back(boost::get(allow)); + SimulatorResourceModel::AttributeProperty attrProp(allowedValues); + attribute.setProperty(attrProp); + break; + } + default: + break; + } + } + +} +SimulatorResourceModel::Attribute RequestModelBuilder::buildAttribute( + std::shared_ptr propertyElement) +{ + std::string propName = propertyElement->getName(); + + // Build representation attribute + SimulatorResourceModel::Attribute attribute(propName); + switch (propertyElement->getType().type()) + { + case RAML::VariantType::INTEGER: + { + int attributeValue = 0; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + int type = 0; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); + } + break; + + case RAML::VariantType::DOUBLE: + { + double attributeValue = 0; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + double type = 0; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); + } + break; + + case RAML::VariantType::BOOLEAN: + { + bool attributeValue = false; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + bool type = false; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); + } + break; + + case RAML::VariantType::STRING: + { + std::string attributeValue = ""; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + std::string type = ""; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); + } + break; + case RAML::VariantType::PROPERTY: + { + RAML::Properties arrayProperty = boost::get(propertyElement->getValue()); + SimulatorResourceModel::Attribute arrayAttribute = buildAttribute( + std::make_shared(arrayProperty)); + + switch (arrayAttribute.getType().type()) + { + case SimulatorResourceModel::ValueType::INTEGER : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + int type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } + case SimulatorResourceModel::ValueType::DOUBLE : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + double type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } + case SimulatorResourceModel::ValueType::BOOLEAN : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + bool type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } + case SimulatorResourceModel::ValueType::STRING : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + std::string type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } + } + } + break; + case RAML::VariantType::ARRAY: + { + + std::vector arrayResModel; + SimulatorResourceModel arrayItem; + std::vector arrayProperty = boost::get > + (propertyElement->getValue()); + for (auto val : arrayProperty) + { + arrayItem.add(buildAttribute(std::make_shared(val))); + } + arrayResModel.push_back(arrayItem); + attribute.setValue(arrayResModel); + } + break; + } + return attribute; +} SimulatorResourceModelSP RequestModelBuilder::createRepSchema(const RAML::RequestResponseBodyPtr &rep) @@ -134,79 +286,7 @@ SimulatorResourceModelSP RequestModelBuilder::createRepSchema(const RAML::Reques || "p" == propName || "n" == propName || "id" == propName) continue; - int valueType = propertyEntry.second->getValueType(); - switch (valueType) - { - case 0: // Integer - { - // Add the attribute with value - repSchema->add(propertyEntry.second->getName(), propertyEntry.second->getValue()); - - // Convert supported values - std::vector allowedValues = propertyEntry.second->getAllowedValuesInt(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - repSchema->setAttributeProperty(propName, attrProp); - } - } - break; - - case 1: // Double - { - // Add the attribute with value - repSchema->add(propertyEntry.second->getName(), propertyEntry.second->getValue()); - - // Convert suppoted values - std::vector allowedValues = propertyEntry.second->getAllowedValuesDouble(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - repSchema->setAttributeProperty(propName, attrProp); - } - } - break; - - case 2: // Boolean - { - // Add the attribute with value - repSchema->add(propertyEntry.second->getName(), propertyEntry.second->getValue()); - // Convert supported values - std::vector allowedValues = propertyEntry.second->getAllowedValuesBool(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - repSchema->setAttributeProperty(propName, attrProp); - } - } - break; - - case 3: // String - { - // Add the attribute with value - repSchema->add(propertyEntry.second->getName(), - propertyEntry.second->getValue()); - - // Convert suppored values - std::vector allowedValues = propertyEntry.second->getAllowedValuesString(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - repSchema->setAttributeProperty(propName, attrProp); - } - } - break; - } - - // Set the range property if its present - double min, max; - int multipleof; - propertyEntry.second->getRange(min, max, multipleof); - if (min != INT_MIN && max != INT_MAX) - { - SimulatorResourceModel::AttributeProperty attrProp(min, max); - repSchema->setAttributeProperty(propName, attrProp); - } + repSchema->add(buildAttribute(propertyEntry.second)); } return repSchema; diff --git a/service/simulator/src/common/request_model_builder.h b/service/simulator/src/common/request_model_builder.h old mode 100644 new mode 100755 index f212252..de4baf1 --- a/service/simulator/src/common/request_model_builder.h +++ b/service/simulator/src/common/request_model_builder.h @@ -35,6 +35,11 @@ class RequestModelBuilder ResponseModelSP createResponseModel(int code, const RAML::ResponsePtr &response); SimulatorResourceModelSP createRepSchema(const RAML::RequestResponseBodyPtr &rep); RequestType getRequestType(RAML::ActionType actionType); + SimulatorResourceModel::Attribute buildAttribute( + std::shared_ptr propertyElement); + template + void buildValueProperty(SimulatorResourceModel::Attribute &attribute, + const std::vector &valueProperties, T); std::shared_ptr m_raml; }; diff --git a/service/simulator/src/server/simulator_resource_factory.cpp b/service/simulator/src/server/simulator_resource_factory.cpp old mode 100644 new mode 100755 index 321d425..709f049 --- a/service/simulator/src/server/simulator_resource_factory.cpp +++ b/service/simulator/src/server/simulator_resource_factory.cpp @@ -103,7 +103,41 @@ std::shared_ptr SimulatorResourceFactory::createCol collectionResource->setResourceType(resourceType); return std::shared_ptr(collectionResource); } +template +void SimulatorResourceFactory::buildValueProperty(SimulatorResourceModel::Attribute &attribute, + const std::vector &valueProperties, T) +{ + for (auto &vp : valueProperties) + { + switch (vp->type()) + { + case RAML::ValueProperty::Type::RANGE : + { + double min = vp->min(); + double max = vp->max(); + int multipleof = vp->multipleOf(); + if (min != INT_MIN && max != INT_MAX) + { + SimulatorResourceModel::AttributeProperty attrProp(min, max); + attribute.setProperty(attrProp); + } + break; + } + case RAML::ValueProperty::Type::VALUE_SET : + { + std::vector allowedValues; + for (auto allow : vp->valueSet()) + allowedValues.push_back(boost::get(allow)); + SimulatorResourceModel::AttributeProperty attrProp(allowedValues); + attribute.setProperty(attrProp); + break; + } + default: + break; + } + } +} SimulatorResourceModel::Attribute SimulatorResourceFactory::buildAttribute( std::shared_ptr propertyElement) { @@ -111,103 +145,115 @@ SimulatorResourceModel::Attribute SimulatorResourceFactory::buildAttribute( // Build representation attribute SimulatorResourceModel::Attribute attribute(propName); - switch (propertyElement->getVariantType()) + switch (propertyElement->getType().type()) { - case RAML::VariantType::INT: + case RAML::VariantType::INTEGER: { - attribute.setValue(propertyElement->getValue()); - - // Convert suppoted values - std::vector allowedValues = propertyElement->getAllowedValuesInt(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); - } + int attributeValue = 0; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + int type = 0; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); } break; case RAML::VariantType::DOUBLE: { - attribute.setValue(propertyElement->getValue()); - - // Convert suppoted values - std::vector allowedValues = propertyElement->getAllowedValuesDouble(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); - } + double attributeValue = 0; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + double type = 0; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); } break; - case RAML::VariantType::BOOL: + case RAML::VariantType::BOOLEAN: { - attribute.setValue(propertyElement->getValue()); - - std::vector allowedValues = {true, false}; - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); + bool attributeValue = false; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + bool type = false; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); } break; case RAML::VariantType::STRING: { - attribute.setValue(propertyElement->getValue()); + std::string attributeValue = ""; + if (propertyElement->isDefaultValue()) + attributeValue = boost::get(propertyElement->getValue()); + attribute.setValue(attributeValue); + std::string type = ""; + buildValueProperty(attribute, (propertyElement->getValueProperties()), type); + } + break; + case RAML::VariantType::PROPERTY: + { + RAML::Properties arrayProperty = boost::get(propertyElement->getValue()); + SimulatorResourceModel::Attribute arrayAttribute = buildAttribute( + std::make_shared(arrayProperty)); - // Convert suppoted values - std::vector allowedValues = propertyElement->getAllowedValuesString(); - if (allowedValues.size() > 0) + switch (arrayAttribute.getType().type()) { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); + case SimulatorResourceModel::ValueType::INTEGER : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + int type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } + case SimulatorResourceModel::ValueType::DOUBLE : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + double type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } + case SimulatorResourceModel::ValueType::BOOLEAN : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + bool type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } + case SimulatorResourceModel::ValueType::STRING : + { + std::vector arrValue; + arrValue.push_back(boost::get(arrayAttribute.getValue())); + attribute.setValue(arrValue); + std::string type; + buildValueProperty(attribute, (arrayProperty.getValueProperties()), type); + break; + } } } break; - } - - // Set the range property if its present - double min, max; - int multipleof; - propertyElement->getRange(min, max, multipleof); - if (min != INT_MIN && max != INT_MAX) - { - SimulatorResourceModel::AttributeProperty attrProp(min, max); - attribute.setProperty(attrProp); - } - return attribute; -} - -SimulatorResourceModel SimulatorResourceFactory::buildResourceModel( - std::shared_ptr item) -{ - SimulatorResourceModel itemModel; - for ( auto &propElement : item->getProperties()) - { - if (!propElement.second) - continue; - - std::string propName = propElement.second->getName(); - if ("p" == propName || "n" == propName || "id" == propName) - { - continue; - } - - if ("array" == propElement.second->getType()) - { - std::vector arrayResModel; - for ( auto &propertyItem : propElement.second->getItems()) + case RAML::VariantType::ARRAY: { - arrayResModel.push_back(buildResourceModel(propertyItem)); + + std::vector arrayResModel; + SimulatorResourceModel arrayItem; + std::vector arrayProperty = boost::get > + (propertyElement->getValue()); + for (auto val : arrayProperty) + { + arrayItem.add(buildAttribute(std::make_shared(val))); + } + arrayResModel.push_back(arrayItem); + attribute.setValue(arrayResModel); } - itemModel.add(propName, arrayResModel); - } - else - { - itemModel.add(buildAttribute(propElement.second)); - } + break; } - return itemModel; + return attribute; } RAML::RequestResponseBodyPtr SimulatorResourceFactory::getRAMLResponseBody( @@ -261,25 +307,27 @@ SimulatorResourceModel SimulatorResourceFactory::buildModelFromResponseBody( // Resource type if ("rt" == propName || "resourceType" == propName) { - resourceType = propertyElement.second->getValueString(); + resourceType = boost::get(propertyElement.second->getValue()); continue; } + // TODO: Is "if" required to be part of resource representation? // Interface type if ("if" == propName) { - if ("string" == propertyElement.second->getType()) + if (RAML::VariantType::STRING == propertyElement.second->getType().type()) { - interfaceType.push_back(propertyElement.second->getValueString()); + interfaceType.push_back(boost::get(propertyElement.second->getValue())); } - else if ("array" == propertyElement.second->getType()) + else if (RAML::VariantType::ARRAY == propertyElement.second->getType().type()) { - for (auto &item : propertyElement.second->getItems()) + RAML::Properties ifProperty = boost::get(propertyElement.second->getValue()); + for (auto vp : ifProperty.getValueProperties()) { - if ("string" == item->getType()) + if (vp->type() == RAML::ValueProperty::Type::VALUE_SET) { - interfaceType = item->getAllowedValuesString(); - break; + for (auto allow : vp->valueSet()) + interfaceType.push_back(boost::get(allow)); } } } @@ -292,30 +340,7 @@ SimulatorResourceModel SimulatorResourceFactory::buildModelFromResponseBody( continue; } - // Add the attribute to resource model - if ("array" == propertyElement.second->getType()) - { - std::vector arrayResModel; - for ( auto &propertyItem : propertyElement.second->getItems()) - { - arrayResModel.push_back(buildResourceModel(propertyItem)); - } - resModel.add(propName, arrayResModel); - } - else - { - resModel.add(buildAttribute(propertyElement.second)); - } - } - - if ("array" == resourceProperties->getType()) - { - std::vector arrayResModel; - for ( auto &propertyItem : resourceProperties->getItems()) - { - arrayResModel.push_back(buildResourceModel(propertyItem)); - } - resModel.add("links", arrayResModel); + resModel.add(buildAttribute(propertyElement.second)); } return resModel; diff --git a/service/simulator/src/server/simulator_resource_factory.h b/service/simulator/src/server/simulator_resource_factory.h index 84ff428..d79d849 100755 --- a/service/simulator/src/server/simulator_resource_factory.h +++ b/service/simulator/src/server/simulator_resource_factory.h @@ -29,7 +29,8 @@ namespace RAML { class RamlResource; class Properties; - class Items; + class JsonSchema; + class RamlParser; } class SimulatorResourceFactory @@ -87,9 +88,11 @@ class SimulatorResourceFactory const std::string &name, const std::string &uri, const std::string &resourceType); private: + template + void buildValueProperty(SimulatorResourceModel::Attribute &attribute, + const std::vector &valueProperties, T); SimulatorResourceModel::Attribute buildAttribute( std::shared_ptr propertyElement); - SimulatorResourceModel buildResourceModel(std::shared_ptr item); SimulatorResourceModel buildModelFromResponseBody( RAML::RequestResponseBodyPtr responseBody, std::string &resourceType, std::vector &interfaceType); -- 2.7.4