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(VariantType type,
\r
141 VariantType baseType, int depth)
\r
142 : m_type (type), m_baseType(baseType), m_depth(depth) {}
\r
144 VariantType Properties::TypeInfo::type() const
\r
149 VariantType Properties::TypeInfo::baseType() const
\r
154 int Properties::TypeInfo::depth() const
\r
159 bool Properties::TypeInfo::operator==(
\r
160 const Properties::TypeInfo &rhs ) const
\r
162 if (m_type == rhs.type() && m_baseType == rhs.baseType()
\r
163 && m_depth == rhs.depth())
\r
168 bool Properties::TypeInfo::operator!=(
\r
169 const Properties::TypeInfo &rhs ) const
\r
171 if (m_type != rhs.type() || m_baseType != rhs.baseType()
\r
172 || m_depth != rhs.depth())
\r
177 ValueVariant Properties::getValue() const
\r
179 if (!isDefaultValue())
\r
180 throw JsonException("Reading Empty Property Value");
\r
184 std::string Properties::getName() const
\r
189 void Properties::setName(const std::string &name)
\r
193 std::string Properties::getDescription() const
\r
195 return m_description;
\r
198 void Properties::setDescription(const std::string &description)
\r
200 m_description = description;
\r
202 std::vector<std::shared_ptr<ValueProperty> > const &Properties::getValueProperties()
\r
205 return m_valueProperty;
\r
207 void Properties::setValueProperty(const std::shared_ptr<ValueProperty> &value)
\r
209 m_valueProperty.push_back(value);
\r
211 std::vector<std::string> const &Properties::getRequiredValues() const
\r
215 void Properties::setRequiredValue(const std::string &reqValue)
\r
217 auto it = m_required.begin();
\r
218 for (; it != m_required.end(); ++it)
\r
220 if (*it == reqValue)
\r
223 if (m_required.end() == it)
\r
225 m_required.push_back(reqValue);
\r
228 ValueProperty::ValueProperty()
\r
229 : m_type(ValueProperty::Type::UNKNOWN),
\r
232 m_multipleOf(INT_MAX),
\r
233 m_minItems(INT_MIN),
\r
234 m_maxItems(INT_MAX),
\r
236 m_additionalItems(false) {}
\r
238 ValueProperty::ValueProperty(double min, double max, int multipleOf)
\r
239 : m_type(ValueProperty::Type::RANGE),
\r
242 m_multipleOf(multipleOf),
\r
243 m_minItems(INT_MIN),
\r
244 m_maxItems(INT_MAX),
\r
246 m_additionalItems(false) {}
\r
248 ValueProperty::ValueProperty(
\r
249 const std::vector<int> &valueSet)
\r
250 : m_type(ValueProperty::Type::VALUE_SET),
\r
253 m_multipleOf(INT_MAX),
\r
254 m_valueSet(valueSet.begin(), valueSet.end()),
\r
255 m_minItems(INT_MIN),
\r
256 m_maxItems(INT_MAX),
\r
258 m_additionalItems(false) {}
\r
260 ValueProperty::ValueProperty(
\r
261 const std::vector<double> &valueSet)
\r
262 : m_type(ValueProperty::Type::VALUE_SET),
\r
265 m_multipleOf(INT_MAX),
\r
266 m_valueSet(valueSet.begin(), valueSet.end()),
\r
267 m_minItems(INT_MIN),
\r
268 m_maxItems(INT_MAX),
\r
270 m_additionalItems(false) {}
\r
272 ValueProperty::ValueProperty(
\r
273 const std::vector<bool> &valueSet)
\r
274 : m_type(ValueProperty::Type::VALUE_SET),
\r
277 m_multipleOf(INT_MAX),
\r
278 m_valueSet(valueSet.begin(), valueSet.end()),
\r
279 m_minItems(INT_MIN),
\r
280 m_maxItems(INT_MAX),
\r
282 m_additionalItems(false) {}
\r
284 ValueProperty::ValueProperty(
\r
285 const std::vector<std::string> &valueSet)
\r
286 : m_type(ValueProperty::Type::VALUE_SET),
\r
289 m_multipleOf(INT_MAX),
\r
290 m_valueSet(valueSet.begin(), valueSet.end()),
\r
291 m_minItems(INT_MIN),
\r
292 m_maxItems(INT_MAX),
\r
294 m_additionalItems(false) {}
\r
296 ValueProperty::ValueProperty(
\r
297 const std::vector<ValueVariant> &valueSet)
\r
298 : m_type(ValueProperty::Type::VALUE_SET),
\r
301 m_multipleOf(INT_MAX),
\r
302 m_valueSet(valueSet.begin(), valueSet.end()),
\r
303 m_minItems(INT_MIN),
\r
304 m_maxItems(INT_MAX),
\r
306 m_additionalItems(false) {}
\r
308 ValueProperty::ValueProperty(Type type, std::string value)
\r
311 m_multipleOf(INT_MAX),
\r
312 m_minItems(INT_MIN),
\r
313 m_maxItems(INT_MAX),
\r
315 m_additionalItems(false)
\r
317 if (type == ValueProperty::Type::PATTERN)
\r
319 m_type = ValueProperty::Type::PATTERN;
\r
322 else if (type == ValueProperty::Type::FORMAT)
\r
324 m_type = ValueProperty::Type::FORMAT;
\r
329 m_type = ValueProperty::Type::UNKNOWN;
\r
333 ValueProperty::ValueProperty(Type type, int minItems, int maxItems, bool unique,
\r
334 bool additionalItems)
\r
335 : m_type(ValueProperty::Type::UNKNOWN),
\r
338 m_multipleOf(INT_MAX),
\r
342 m_additionalItems(false)
\r
344 if (type == ValueProperty::Type::ARRAY)
\r
346 m_type = ValueProperty::Type::ARRAY;
\r
349 m_minItems = minItems;
\r
351 if (maxItems != INT_MAX && maxItems > m_minItems)
\r
352 m_maxItems = maxItems;
\r
355 m_additionalItems = additionalItems;
\r
359 ValueProperty::Type ValueProperty::type() const
\r
364 double ValueProperty::min() const
\r
369 double ValueProperty::max() const
\r
374 int ValueProperty::multipleOf() const
\r
376 return m_multipleOf;
\r
379 std::string ValueProperty::pattern() const
\r
384 std::string ValueProperty::format() const
\r
389 int ValueProperty::valueSetSize() const
\r
391 return m_valueSet.size();
\r
394 std::vector<ValueVariant> ValueProperty::valueSet() const
\r
399 void ValueProperty::valueArray(int &minItems, int &maxItems, bool &unique,
\r
400 bool &additionalItems) const
\r
402 minItems = m_minItems;
\r
403 maxItems = m_maxItems;
\r
405 additionalItems = m_additionalItems;
\r