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