Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_COMMANDS_SCHEMA_UTILS_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_SCHEMA_UTILS_H_ |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 7 | |
| 8 | #include <limits> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <type_traits> |
| 13 | #include <vector> |
| 14 | |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 15 | #include <base/logging.h> |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 16 | #include <base/values.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 17 | #include <weave/error.h> |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 18 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 19 | namespace weave { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 21 | class PropType; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 22 | class PropValue; |
| 23 | class ObjectSchema; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 24 | class ObjectValue; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 25 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 26 | // C++ representation of object values. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 27 | using ValueMap = std::map<std::string, std::shared_ptr<const PropValue>>; |
| 28 | |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 29 | // C++ representation of array of values. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 30 | using ValueVector = std::vector<std::shared_ptr<const PropValue>>; |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 31 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 32 | // Converts an object to string. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 33 | std::string ToString(const ValueMap& obj); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 34 | |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 35 | // Converts an array to string. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 36 | std::string ToString(const ValueVector& arr); |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 37 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 38 | // InheritableAttribute class is used for specifying various command parameter |
| 39 | // attributes that can be inherited from a base (parent) schema. |
| 40 | // The |value| still specifies the actual attribute values, whether it |
| 41 | // is inherited or overridden, while |is_inherited| can be used to identify |
| 42 | // if the attribute was inherited (true) or overridden (false). |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 43 | template <typename T> |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 44 | class InheritableAttribute final { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 45 | public: |
| 46 | InheritableAttribute() = default; |
| 47 | explicit InheritableAttribute(T val) |
| 48 | : value(std::move(val)), is_inherited(true) {} |
| 49 | InheritableAttribute(T val, bool inherited) |
| 50 | : value(std::move(val)), is_inherited(inherited) {} |
| 51 | T value{}; |
| 52 | bool is_inherited{true}; |
| 53 | }; |
| 54 | |
| 55 | // A bunch of helper function to create base::Value for specific C++ classes, |
| 56 | // including vectors of types. These are used in template classes below |
| 57 | // to simplify specialization logic. |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 58 | std::unique_ptr<base::FundamentalValue> TypedValueToJson(bool value); |
| 59 | std::unique_ptr<base::FundamentalValue> TypedValueToJson(int value); |
| 60 | std::unique_ptr<base::FundamentalValue> TypedValueToJson(double value); |
| 61 | std::unique_ptr<base::StringValue> TypedValueToJson(const std::string& value); |
| 62 | std::unique_ptr<base::DictionaryValue> TypedValueToJson(const ValueMap& value); |
| 63 | std::unique_ptr<base::ListValue> TypedValueToJson(const ValueVector& value); |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 64 | template <typename T> |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 65 | std::unique_ptr<base::ListValue> TypedValueToJson( |
| 66 | const std::vector<T>& values) { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 67 | std::unique_ptr<base::ListValue> list(new base::ListValue); |
| 68 | for (const auto& v : values) { |
Vitaly Buka | 6942e1f | 2015-07-28 15:33:55 -0700 | [diff] [blame] | 69 | auto json = TypedValueToJson(v); |
| 70 | CHECK(json); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 71 | list->Append(json.release()); |
| 72 | } |
Vitaly Buka | 39ef195 | 2015-07-21 20:32:11 -0700 | [diff] [blame] | 73 | return list; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 76 | // Similarly to TypedValueToJson() function above, the following overloaded |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 77 | // helper methods allow to extract specific C++ data types from base::Value. |
| 78 | // Also used in template classes below to simplify specialization logic. |
Vitaly Buka | d7be905 | 2015-07-28 19:22:55 -0700 | [diff] [blame] | 79 | // TODO(vitalybuka): Fix this. Interface is misleading. Seeing PropType internal |
| 80 | // type validation is expected. In reality only ValueMap and ValueVector do |
| 81 | // validation. |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 82 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 83 | const PropType* type, |
| 84 | bool* value_out, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 85 | ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 86 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 87 | const PropType* type, |
| 88 | int* value_out, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 89 | ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 90 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 91 | const PropType* type, |
| 92 | double* value_out, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 93 | ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 94 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 95 | const PropType* type, |
| 96 | std::string* value_out, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 97 | ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 98 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 99 | const PropType* type, |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 100 | ValueMap* value_out, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 101 | ErrorPtr* error); |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 102 | bool TypedValueFromJson(const base::Value* value_in, |
| 103 | const PropType* type, |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 104 | ValueVector* value_out, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 105 | ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 106 | |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 107 | bool operator==(const ValueMap& obj1, const ValueMap& obj2); |
| 108 | bool operator==(const ValueVector& arr1, const ValueVector& arr2); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 109 | |
| 110 | // CompareValue is a helper function to help with implementing EqualsTo operator |
| 111 | // for various data types. For most scalar types it is using operator==(), |
| 112 | // however, for floating point values, rounding errors in binary representation |
| 113 | // of IEEE floats/doubles can cause straight == comparison to fail for seemingly |
| 114 | // equivalent values. For these, use approximate comparison with the error |
| 115 | // margin equal to the epsilon value defined for the corresponding data type. |
| 116 | // This is used when looking up values for implementation of OneOf constraints |
| 117 | // which should work reliably for floating points also ("number" type). |
| 118 | |
| 119 | // Compare exact types using ==. |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 120 | template <typename T> |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 121 | inline typename std::enable_if<!std::is_floating_point<T>::value, bool>::type |
| 122 | CompareValue(const T& v1, const T& v2) { |
| 123 | return v1 == v2; |
| 124 | } |
| 125 | |
| 126 | // Compare non-exact types (such as double) using precision margin (epsilon). |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 127 | template <typename T> |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 128 | inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type |
| 129 | CompareValue(const T& v1, const T& v2) { |
| 130 | return std::abs(v1 - v2) <= std::numeric_limits<T>::epsilon(); |
| 131 | } |
| 132 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 133 | } // namespace weave |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 134 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 135 | #endif // LIBWEAVE_SRC_COMMANDS_SCHEMA_UTILS_H_ |