1 /******************************************************************
\r
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
19 ******************************************************************/
\r
22 * @file Properties.cpp
\r
24 * @brief This file provides data Model for Json Schema Properties.
\r
27 #include "Properties.h"
\r
28 #include <boost/lexical_cast.hpp>
\r
29 #include <boost/variant/static_visitor.hpp>
\r
30 #include "RamlExceptions.h"
\r
34 template <typename T>
\r
35 struct TypeConverter {};
\r
38 struct TypeConverter<int>
\r
40 constexpr static VariantType type =
\r
41 VariantType::INTEGER;
\r
45 struct TypeConverter<double>
\r
47 constexpr static VariantType type =
\r
48 VariantType::DOUBLE;
\r
52 struct TypeConverter<bool>
\r
54 constexpr static VariantType type =
\r
55 VariantType::BOOLEAN;
\r
59 struct TypeConverter<std::string>
\r
61 constexpr static VariantType type =
\r
62 VariantType::STRING;
\r
66 struct TypeConverter<Properties>
\r
68 constexpr static VariantType type =
\r
69 VariantType::PROPERTY;
\r
72 template <typename T>
\r
75 constexpr static VariantType type =
\r
76 TypeConverter<T>::type;
\r
77 constexpr static VariantType baseType =
\r
78 TypeConverter<T>::type;
\r
79 constexpr static int depth = 0;
\r
82 template <typename T>
\r
83 struct TypeDetails<std::vector<T>>
\r
85 constexpr static VariantType type =
\r
87 constexpr static VariantType baseType =
\r
88 TypeDetails<T>::baseType;
\r
89 constexpr static int depth = 1 + TypeDetails<T>::depth;
\r
92 class PropertyTypeVisitor : public boost::static_visitor<>
\r
95 PropertyTypeVisitor() : m_type(VariantType::UNKNOWN),
\r
96 m_baseType(VariantType::UNKNOWN), m_depth(0) {}
\r
98 template <typename T>
\r
99 void operator ()(const T &)
\r
101 m_type = TypeDetails<T>::type;
\r
102 m_baseType = TypeDetails<T>::baseType;
\r
103 m_depth = TypeDetails<T>::depth;
\r
106 VariantType m_type;
\r
107 VariantType m_baseType;
\r
111 Properties::TypeInfo Properties::getType() const
\r
115 RAML::PropertyTypeVisitor typeVisitor;
\r
116 boost::apply_visitor(typeVisitor, *(m_value.get()));
\r
117 Properties::TypeInfo typeInfo(typeVisitor.m_type, typeVisitor.m_baseType,
\r
118 typeVisitor.m_depth);
\r
121 else if (!m_typeString.empty()) //to read properties even if default value is not present
\r
123 if (m_typeString == "string")
\r
124 return Properties::TypeInfo(VariantType::STRING, VariantType::STRING, 0);
\r
125 else if (m_typeString == "integer")
\r
126 return Properties::TypeInfo(VariantType::INTEGER, VariantType::INTEGER, 0);
\r
127 else if (m_typeString == "number")
\r
128 return Properties::TypeInfo(VariantType::DOUBLE, VariantType::DOUBLE, 0);
\r
129 else if (m_typeString == "boolean")
\r
130 return Properties::TypeInfo(VariantType::BOOLEAN, VariantType::BOOLEAN, 0);
\r
132 return Properties::TypeInfo();
\r
135 void Properties::setTypeString(const std::string &type)
\r
137 m_typeString = type;
\r
140 Properties::TypeInfo::TypeInfo(
\r
141 VariantType type = VariantType::UNKNOWN,
\r
142 VariantType baseType = VariantType::UNKNOWN,
\r
144 : m_type (type), m_baseType(baseType), m_depth(depth) {}
\r
146 VariantType Properties::TypeInfo::type() const
\r
151 VariantType Properties::TypeInfo::baseType() const
\r
156 int Properties::TypeInfo::depth() const
\r
161 bool Properties::TypeInfo::operator==(
\r
162 const Properties::TypeInfo &rhs ) const
\r
164 if (m_type == rhs.type() && m_baseType == rhs.baseType()
\r
165 && m_depth == rhs.depth())
\r
170 bool Properties::TypeInfo::operator!=(
\r
171 const Properties::TypeInfo &rhs ) const
\r
173 if (m_type != rhs.type() || m_baseType != rhs.baseType()
\r
174 || m_depth != rhs.depth())
\r
179 ValueVariant Properties::getValue() const
\r
181 if (!isDefaultValue())
\r
182 throw JsonException("Reading Empty Property Value");
\r
186 std::string Properties::getName() const
\r
191 void Properties::setName(const std::string &name)
\r
195 std::string Properties::getDescription() const
\r
197 return m_description;
\r
200 void Properties::setDescription(const std::string &description)
\r
202 m_description = description;
\r
204 std::vector<std::shared_ptr<ValueProperty> > const &Properties::getValueProperties()
\r
207 return m_valueProperty;
\r
209 void Properties::setValueProperty(const std::shared_ptr<ValueProperty> &value)
\r
211 m_valueProperty.push_back(value);
\r
213 std::vector<std::string> const &Properties::getRequiredValues() const
\r
217 void Properties::setRequiredValue(const std::string &reqValue)
\r
219 auto it = m_required.begin();
\r
220 for (; it != m_required.end(); ++it)
\r
222 if (*it == reqValue)
\r
225 if (m_required.end() == it)
\r
227 m_required.push_back(reqValue);
\r
230 ValueProperty::ValueProperty()
\r
231 : m_type(ValueProperty::Type::UNKNOWN),
\r
234 m_multipleOf(INT_MAX),
\r
235 m_minItems(INT_MIN),
\r
236 m_maxItems(INT_MAX),
\r
238 m_additionalItems(false) {}
\r
240 ValueProperty::ValueProperty(double min, double max, int multipleOf)
\r
241 : m_type(ValueProperty::Type::RANGE),
\r
244 m_multipleOf(multipleOf),
\r
245 m_minItems(INT_MIN),
\r
246 m_maxItems(INT_MAX),
\r
248 m_additionalItems(false) {}
\r
250 ValueProperty::ValueProperty(
\r
251 const std::vector<int> &valueSet)
\r
252 : m_type(ValueProperty::Type::VALUE_SET),
\r
255 m_multipleOf(INT_MAX),
\r
256 m_valueSet(valueSet.begin(), valueSet.end()),
\r
257 m_minItems(INT_MIN),
\r
258 m_maxItems(INT_MAX),
\r
260 m_additionalItems(false) {}
\r
262 ValueProperty::ValueProperty(
\r
263 const std::vector<double> &valueSet)
\r
264 : m_type(ValueProperty::Type::VALUE_SET),
\r
267 m_multipleOf(INT_MAX),
\r
268 m_valueSet(valueSet.begin(), valueSet.end()),
\r
269 m_minItems(INT_MIN),
\r
270 m_maxItems(INT_MAX),
\r
272 m_additionalItems(false) {}
\r
274 ValueProperty::ValueProperty(
\r
275 const std::vector<bool> &valueSet)
\r
276 : m_type(ValueProperty::Type::VALUE_SET),
\r
279 m_multipleOf(INT_MAX),
\r
280 m_valueSet(valueSet.begin(), valueSet.end()),
\r
281 m_minItems(INT_MIN),
\r
282 m_maxItems(INT_MAX),
\r
284 m_additionalItems(false) {}
\r
286 ValueProperty::ValueProperty(
\r
287 const std::vector<std::string> &valueSet)
\r
288 : m_type(ValueProperty::Type::VALUE_SET),
\r
291 m_multipleOf(INT_MAX),
\r
292 m_valueSet(valueSet.begin(), valueSet.end()),
\r
293 m_minItems(INT_MIN),
\r
294 m_maxItems(INT_MAX),
\r
296 m_additionalItems(false) {}
\r
298 ValueProperty::ValueProperty(
\r
299 const std::vector<ValueVariant> &valueSet)
\r
300 : m_type(ValueProperty::Type::VALUE_SET),
\r
303 m_multipleOf(INT_MAX),
\r
304 m_valueSet(valueSet.begin(), valueSet.end()),
\r
305 m_minItems(INT_MIN),
\r
306 m_maxItems(INT_MAX),
\r
308 m_additionalItems(false) {}
\r
310 ValueProperty::ValueProperty(Type type, std::string value)
\r
313 m_multipleOf(INT_MAX),
\r
314 m_minItems(INT_MIN),
\r
315 m_maxItems(INT_MAX),
\r
317 m_additionalItems(false)
\r
319 if (type == ValueProperty::Type::PATTERN)
\r
321 m_type = ValueProperty::Type::PATTERN;
\r
324 else if (type == ValueProperty::Type::FORMAT)
\r
326 m_type = ValueProperty::Type::FORMAT;
\r
331 m_type = ValueProperty::Type::UNKNOWN;
\r
335 ValueProperty::ValueProperty(Type type, int minItems, int maxItems, bool unique,
\r
336 bool additionalItems)
\r
337 : m_type(ValueProperty::Type::UNKNOWN),
\r
340 m_multipleOf(INT_MAX),
\r
341 m_minItems(INT_MIN),
\r
342 m_maxItems(INT_MAX),
\r
344 m_additionalItems(false)
\r
346 if (type == ValueProperty::Type::ARRAY)
\r
348 m_type = ValueProperty::Type::ARRAY;
\r
349 m_minItems = minItems;
\r
350 m_maxItems = maxItems;
\r
352 m_additionalItems = additionalItems;
\r
356 m_type = ValueProperty::Type::UNKNOWN;
\r
360 ValueProperty::Type ValueProperty::type() const
\r
365 double ValueProperty::min() const
\r
370 double ValueProperty::max() const
\r
375 int ValueProperty::multipleOf() const
\r
377 return m_multipleOf;
\r
380 std::string ValueProperty::pattern() const
\r
385 std::string ValueProperty::format() const
\r
390 int ValueProperty::valueSetSize() const
\r
392 return m_valueSet.size();
\r
395 std::vector<ValueVariant> ValueProperty::valueSet() const
\r
400 void ValueProperty::valueArray(int &minItems, int &maxItems, bool &unique,
\r
401 bool &additionalItems) const
\r
403 minItems = m_minItems;
\r
404 maxItems = m_maxItems;
\r
406 additionalItems = m_additionalItems;
\r