bc296064838d623449130800db896947ab1b9a4a
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / jsonSchemaParser / Properties.cpp
1 /******************************************************************\r
2  *\r
3  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\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
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
18  *\r
19  ******************************************************************/\r
20 \r
21 /**\r
22  * @file   Properties.cpp\r
23  *\r
24  * @brief   This file provides data Model for Json Schema Properties.\r
25  */\r
26 \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
31 \r
32 namespace RAML\r
33 {\r
34     template <typename T>\r
35     struct TypeConverter {};\r
36 \r
37     template <>\r
38     struct TypeConverter<int>\r
39     {\r
40         constexpr static VariantType type =\r
41             VariantType::INTEGER;\r
42     };\r
43 \r
44     template <>\r
45     struct TypeConverter<double>\r
46     {\r
47         constexpr static VariantType type =\r
48             VariantType::DOUBLE;\r
49     };\r
50 \r
51     template <>\r
52     struct TypeConverter<bool>\r
53     {\r
54         constexpr static VariantType type =\r
55             VariantType::BOOLEAN;\r
56     };\r
57 \r
58     template <>\r
59     struct TypeConverter<std::string>\r
60     {\r
61         constexpr static VariantType type =\r
62             VariantType::STRING;\r
63     };\r
64 \r
65     template <>\r
66     struct TypeConverter<Properties>\r
67     {\r
68         constexpr static VariantType type =\r
69             VariantType::PROPERTY;\r
70     };\r
71 \r
72     template <typename T>\r
73     struct TypeDetails\r
74     {\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
80     };\r
81 \r
82     template <typename T>\r
83     struct TypeDetails<std::vector<T>>\r
84     {\r
85         constexpr static VariantType type =\r
86             VariantType::ARRAY;\r
87         constexpr static VariantType baseType =\r
88             TypeDetails<T>::baseType;\r
89         constexpr static int depth = 1 + TypeDetails<T>::depth;\r
90     };\r
91 \r
92     class PropertyTypeVisitor : public boost::static_visitor<>\r
93     {\r
94         public:\r
95             PropertyTypeVisitor() : m_type(VariantType::UNKNOWN),\r
96                 m_baseType(VariantType::UNKNOWN), m_depth(0) {}\r
97 \r
98             template <typename T>\r
99             void operator ()(const T &)\r
100             {\r
101                 m_type = TypeDetails<T>::type;\r
102                 m_baseType = TypeDetails<T>::baseType;\r
103                 m_depth = TypeDetails<T>::depth;\r
104             }\r
105 \r
106             VariantType m_type;\r
107             VariantType m_baseType;\r
108             int m_depth;\r
109     };\r
110 \r
111     Properties::TypeInfo Properties::getType() const\r
112     {\r
113         if (m_value)\r
114         {\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
119             return typeInfo;\r
120         }\r
121         else if (!m_typeString.empty()) //to read properties even if default value is not present\r
122         {\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
131         }\r
132         return Properties::TypeInfo();\r
133     }\r
134 \r
135     void Properties::setTypeString(const std::string &type)\r
136     {\r
137         m_typeString = type;\r
138     }\r
139 \r
140     Properties::TypeInfo::TypeInfo(\r
141         VariantType type = VariantType::UNKNOWN,\r
142         VariantType baseType = VariantType::UNKNOWN,\r
143         int depth = 0)\r
144         :   m_type (type), m_baseType(baseType), m_depth(depth) {}\r
145 \r
146     VariantType Properties::TypeInfo::type() const\r
147     {\r
148         return m_type;\r
149     }\r
150 \r
151     VariantType Properties::TypeInfo::baseType() const\r
152     {\r
153         return m_baseType;\r
154     }\r
155 \r
156     int Properties::TypeInfo::depth() const\r
157     {\r
158         return m_depth;\r
159     }\r
160 \r
161     bool Properties::TypeInfo::operator==(\r
162         const Properties::TypeInfo &rhs ) const\r
163     {\r
164         if (m_type == rhs.type() && m_baseType == rhs.baseType()\r
165             && m_depth == rhs.depth())\r
166             return true;\r
167         return false;\r
168     }\r
169 \r
170     bool Properties::TypeInfo::operator!=(\r
171         const Properties::TypeInfo &rhs ) const\r
172     {\r
173         if (m_type != rhs.type() || m_baseType != rhs.baseType()\r
174             || m_depth != rhs.depth())\r
175             return true;\r
176         return false;\r
177     }\r
178 \r
179     ValueVariant Properties::getValue() const\r
180     {\r
181         if (!isDefaultValue())\r
182             throw JsonException("Reading Empty Property Value");\r
183         return *m_value;\r
184     }\r
185 \r
186     std::string Properties::getName() const\r
187     {\r
188         return m_name;\r
189     }\r
190 \r
191     void Properties::setName(const std::string &name)\r
192     {\r
193         m_name = name;\r
194     }\r
195     std::string Properties::getDescription() const\r
196     {\r
197         return m_description;\r
198     }\r
199 \r
200     void Properties::setDescription(const std::string &description)\r
201     {\r
202         m_description = description;\r
203     }\r
204     std::vector<std::shared_ptr<ValueProperty> > const &Properties::getValueProperties()\r
205     const\r
206     {\r
207         return m_valueProperty;\r
208     }\r
209     void Properties::setValueProperty(const std::shared_ptr<ValueProperty> &value)\r
210     {\r
211         m_valueProperty.push_back(value);\r
212     }\r
213     std::vector<std::string> const &Properties::getRequiredValues() const\r
214     {\r
215         return m_required;\r
216     }\r
217     void Properties::setRequiredValue(const std::string &reqValue)\r
218     {\r
219         auto it = m_required.begin();\r
220         for (; it != m_required.end(); ++it)\r
221         {\r
222             if (*it == reqValue)\r
223                 break;\r
224         }\r
225         if (m_required.end() == it)\r
226         {\r
227             m_required.push_back(reqValue);\r
228         }\r
229     }\r
230     ValueProperty::ValueProperty()\r
231         :   m_type(ValueProperty::Type::UNKNOWN),\r
232             m_min(INT_MIN),\r
233             m_max(INT_MAX),\r
234             m_multipleOf(INT_MAX),\r
235             m_minItems(INT_MIN),\r
236             m_maxItems(INT_MAX),\r
237             m_unique(false),\r
238             m_additionalItems(false) {}\r
239 \r
240     ValueProperty::ValueProperty(double min, double max, int multipleOf)\r
241         :   m_type(ValueProperty::Type::RANGE),\r
242             m_min(min),\r
243             m_max(max),\r
244             m_multipleOf(multipleOf),\r
245             m_minItems(INT_MIN),\r
246             m_maxItems(INT_MAX),\r
247             m_unique(false),\r
248             m_additionalItems(false) {}\r
249 \r
250     ValueProperty::ValueProperty(\r
251         const std::vector<int> &valueSet)\r
252         :   m_type(ValueProperty::Type::VALUE_SET),\r
253             m_min(INT_MIN),\r
254             m_max(INT_MAX),\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
259             m_unique(false),\r
260             m_additionalItems(false)  {}\r
261 \r
262     ValueProperty::ValueProperty(\r
263         const std::vector<double> &valueSet)\r
264         :   m_type(ValueProperty::Type::VALUE_SET),\r
265             m_min(INT_MIN),\r
266             m_max(INT_MAX),\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
271             m_unique(false),\r
272             m_additionalItems(false)  {}\r
273 \r
274     ValueProperty::ValueProperty(\r
275         const std::vector<bool> &valueSet)\r
276         :   m_type(ValueProperty::Type::VALUE_SET),\r
277             m_min(INT_MIN),\r
278             m_max(INT_MAX),\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
283             m_unique(false),\r
284             m_additionalItems(false)  {}\r
285 \r
286     ValueProperty::ValueProperty(\r
287         const std::vector<std::string> &valueSet)\r
288         :   m_type(ValueProperty::Type::VALUE_SET),\r
289             m_min(INT_MIN),\r
290             m_max(INT_MAX),\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
295             m_unique(false),\r
296             m_additionalItems(false)  {}\r
297 \r
298     ValueProperty::ValueProperty(\r
299         const std::vector<ValueVariant> &valueSet)\r
300         :   m_type(ValueProperty::Type::VALUE_SET),\r
301             m_min(INT_MIN),\r
302             m_max(INT_MAX),\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
307             m_unique(false),\r
308             m_additionalItems(false)  {}\r
309 \r
310     ValueProperty::ValueProperty(Type type, std::string value)\r
311         :   m_min(INT_MIN),\r
312             m_max(INT_MAX),\r
313             m_multipleOf(INT_MAX),\r
314             m_minItems(INT_MIN),\r
315             m_maxItems(INT_MAX),\r
316             m_unique(false),\r
317             m_additionalItems(false)\r
318     {\r
319         if (type == ValueProperty::Type::PATTERN)\r
320         {\r
321             m_type = ValueProperty::Type::PATTERN;\r
322             m_pattern = value;\r
323         }\r
324         else if (type == ValueProperty::Type::FORMAT)\r
325         {\r
326             m_type = ValueProperty::Type::FORMAT;\r
327             m_format = value;\r
328         }\r
329         else\r
330         {\r
331             m_type = ValueProperty::Type::UNKNOWN;\r
332         }\r
333     }\r
334 \r
335     ValueProperty::ValueProperty(Type type, int minItems, int maxItems, bool unique,\r
336                                  bool additionalItems)\r
337         :   m_type(ValueProperty::Type::UNKNOWN),\r
338             m_min(INT_MIN),\r
339             m_max(INT_MAX),\r
340             m_multipleOf(INT_MAX),\r
341             m_minItems(INT_MIN),\r
342             m_maxItems(INT_MAX),\r
343             m_unique(false),\r
344             m_additionalItems(false)\r
345     {\r
346         if (type == ValueProperty::Type::ARRAY)\r
347         {\r
348             m_type = ValueProperty::Type::ARRAY;\r
349             m_minItems = minItems;\r
350             m_maxItems = maxItems;\r
351             m_unique = unique;\r
352             m_additionalItems = additionalItems;\r
353         }\r
354         else\r
355         {\r
356             m_type = ValueProperty::Type::UNKNOWN;\r
357         }\r
358     }\r
359 \r
360     ValueProperty::Type ValueProperty::type() const\r
361     {\r
362         return m_type;\r
363     }\r
364 \r
365     double ValueProperty::min() const\r
366     {\r
367         return m_min;\r
368     }\r
369 \r
370     double ValueProperty::max() const\r
371     {\r
372         return m_max;\r
373     }\r
374 \r
375     int ValueProperty::multipleOf() const\r
376     {\r
377         return m_multipleOf;\r
378     }\r
379 \r
380     std::string ValueProperty::pattern() const\r
381     {\r
382         return m_pattern;\r
383     }\r
384 \r
385     std::string ValueProperty::format() const\r
386     {\r
387         return m_format;\r
388     }\r
389 \r
390     int ValueProperty::valueSetSize() const\r
391     {\r
392         return m_valueSet.size();\r
393     }\r
394 \r
395     std::vector<ValueVariant> ValueProperty::valueSet() const\r
396     {\r
397         return m_valueSet;\r
398     }\r
399 \r
400     void ValueProperty::valueArray(int &minItems, int &maxItems, bool &unique,\r
401                                    bool &additionalItems) const\r
402     {\r
403         minItems = m_minItems;\r
404         maxItems = m_maxItems;\r
405         unique = m_unique;\r
406         additionalItems = m_additionalItems;\r
407     }\r
408 \r
409 \r
410 }\r