Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "buffet/commands/prop_constraints.h" |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 6 | |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 7 | #include <base/json/json_writer.h> |
| 8 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 9 | #include "buffet/commands/prop_values.h" |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 10 | #include "buffet/commands/schema_constants.h" |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 11 | |
| 12 | namespace buffet { |
| 13 | |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // Helper function to convert a property value to string, which is used for |
| 17 | // error reporting. |
| 18 | std::string PropValueToString(const PropValue& value) { |
| 19 | std::string result; |
| 20 | auto json = value.ToJson(nullptr); |
| 21 | if (json) |
| 22 | base::JSONWriter::Write(json.get(), &result); |
| 23 | return result; |
| 24 | } |
| 25 | |
| 26 | } // anonymous namespace |
| 27 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 28 | // Constraint ---------------------------------------------------------------- |
| 29 | Constraint::~Constraint() {} |
| 30 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 31 | bool Constraint::ReportErrorLessThan(chromeos::ErrorPtr* error, |
| 32 | const std::string& val, |
| 33 | const std::string& limit) { |
| 34 | chromeos::Error::AddToPrintf( |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 35 | error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 36 | errors::commands::kOutOfRange, |
| 37 | "Value %s is out of range. It must not be less than %s", |
| 38 | val.c_str(), limit.c_str()); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 42 | bool Constraint::ReportErrorGreaterThan(chromeos::ErrorPtr* error, |
| 43 | const std::string& val, |
| 44 | const std::string& limit) { |
| 45 | chromeos::Error::AddToPrintf( |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 46 | error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 47 | errors::commands::kOutOfRange, |
| 48 | "Value %s is out of range. It must not be greater than %s", |
| 49 | val.c_str(), limit.c_str()); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 53 | bool Constraint::ReportErrorNotOneOf(chromeos::ErrorPtr* error, |
| 54 | const std::string& val, |
| 55 | const std::vector<std::string>& values) { |
Vitaly Buka | db770e7 | 2015-03-10 19:33:33 -0700 | [diff] [blame] | 56 | chromeos::Error::AddToPrintf( |
| 57 | error, FROM_HERE, errors::commands::kDomain, |
| 58 | errors::commands::kOutOfRange, |
| 59 | "Value %s is invalid. Expected one of [%s]", val.c_str(), |
| 60 | chromeos::string_utils::Join(",", values).c_str()); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool Constraint::AddToJsonDict(base::DictionaryValue* dict, |
| 65 | bool overridden_only, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 66 | chromeos::ErrorPtr* error) const { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 67 | if (!overridden_only || HasOverriddenAttributes()) { |
| 68 | auto value = ToJson(error); |
| 69 | if (!value) |
| 70 | return false; |
| 71 | dict->SetWithoutPathExpansion(GetDictKey(), value.release()); |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // ConstraintStringLength ----------------------------------------------------- |
| 77 | ConstraintStringLength::ConstraintStringLength( |
| 78 | const InheritableAttribute<int>& limit) : limit_(limit) {} |
| 79 | ConstraintStringLength::ConstraintStringLength(int limit) : limit_(limit) {} |
| 80 | |
| 81 | bool ConstraintStringLength::HasOverriddenAttributes() const { |
| 82 | return !limit_.is_inherited; |
| 83 | } |
| 84 | |
| 85 | std::unique_ptr<base::Value> ConstraintStringLength::ToJson( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 86 | chromeos::ErrorPtr* error) const { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 87 | return TypedValueToJson(limit_.value, error); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // ConstraintStringLengthMin -------------------------------------------------- |
| 91 | ConstraintStringLengthMin::ConstraintStringLengthMin( |
| 92 | const InheritableAttribute<int>& limit) : ConstraintStringLength(limit) {} |
| 93 | ConstraintStringLengthMin::ConstraintStringLengthMin(int limit) |
| 94 | : ConstraintStringLength(limit) {} |
| 95 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 96 | bool ConstraintStringLengthMin::Validate(const PropValue& value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 97 | chromeos::ErrorPtr* error) const { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 98 | CHECK(value.GetString()) << "Expecting a string value for this constraint"; |
| 99 | const std::string& str = value.GetString()->GetValue(); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 100 | int length = static_cast<int>(str.size()); |
| 101 | if (length < limit_.value) { |
| 102 | if (limit_.value == 1) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 103 | chromeos::Error::AddTo(error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 104 | errors::commands::kOutOfRange, |
| 105 | "String must not be empty"); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 106 | } else { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 107 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 108 | errors::commands::kOutOfRange, |
| 109 | "String must be at least %d characters long," |
| 110 | " actual length of string '%s' is %d", |
| 111 | limit_.value, str.c_str(), length); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 118 | std::unique_ptr<Constraint> |
| 119 | ConstraintStringLengthMin::Clone() const { |
| 120 | return std::unique_ptr<Constraint>{new ConstraintStringLengthMin{limit_}}; |
| 121 | } |
| 122 | |
| 123 | std::unique_ptr<Constraint> |
| 124 | ConstraintStringLengthMin::CloneAsInherited() const { |
| 125 | return std::unique_ptr<Constraint>{ |
| 126 | new ConstraintStringLengthMin{limit_.value}}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // ConstraintStringLengthMax -------------------------------------------------- |
| 130 | ConstraintStringLengthMax::ConstraintStringLengthMax( |
| 131 | const InheritableAttribute<int>& limit) : ConstraintStringLength(limit) {} |
| 132 | ConstraintStringLengthMax::ConstraintStringLengthMax(int limit) |
| 133 | : ConstraintStringLength(limit) {} |
| 134 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 135 | bool ConstraintStringLengthMax::Validate(const PropValue& value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 136 | chromeos::ErrorPtr* error) const { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 137 | CHECK(value.GetString()) << "Expecting a string value for this constraint"; |
| 138 | const std::string& str = value.GetString()->GetValue(); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 139 | int length = static_cast<int>(str.size()); |
| 140 | if (length > limit_.value) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 141 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 142 | errors::commands::kOutOfRange, |
| 143 | "String must be no more than %d character(s) " |
| 144 | "long, actual length of string '%s' is %d", |
| 145 | limit_.value, str.c_str(), length); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 151 | std::unique_ptr<Constraint> |
| 152 | ConstraintStringLengthMax::Clone() const { |
| 153 | return std::unique_ptr<Constraint>{new ConstraintStringLengthMax{limit_}}; |
| 154 | } |
| 155 | |
| 156 | std::unique_ptr<Constraint> |
| 157 | ConstraintStringLengthMax::CloneAsInherited() const { |
| 158 | return std::unique_ptr<Constraint>{ |
| 159 | new ConstraintStringLengthMax{limit_.value}}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 162 | // ConstraintOneOf -------------------------------------------------- |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 163 | ConstraintOneOf::ConstraintOneOf(InheritableAttribute<native_types::Array> set) |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 164 | : set_(std::move(set)) {} |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 165 | ConstraintOneOf::ConstraintOneOf(native_types::Array set) |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 166 | : set_(std::move(set)) {} |
| 167 | |
| 168 | bool ConstraintOneOf::Validate(const PropValue& value, |
| 169 | chromeos::ErrorPtr* error) const { |
| 170 | for (const auto& item : set_.value) { |
| 171 | if (value.IsEqual(item.get())) |
| 172 | return true; |
| 173 | } |
| 174 | std::vector<std::string> choice_list; |
| 175 | choice_list.reserve(set_.value.size()); |
| 176 | for (const auto& item : set_.value) { |
| 177 | choice_list.push_back(PropValueToString(*item)); |
| 178 | } |
| 179 | return ReportErrorNotOneOf(error, PropValueToString(value), choice_list); |
| 180 | } |
| 181 | |
| 182 | std::unique_ptr<Constraint> ConstraintOneOf::Clone() const { |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 183 | InheritableAttribute<native_types::Array> attr; |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 184 | attr.is_inherited = set_.is_inherited; |
| 185 | attr.value.reserve(set_.value.size()); |
| 186 | for (const auto& prop_value : set_.value) { |
| 187 | attr.value.push_back(prop_value->Clone()); |
| 188 | } |
| 189 | return std::unique_ptr<Constraint>{new ConstraintOneOf{std::move(attr)}}; |
| 190 | } |
| 191 | |
| 192 | std::unique_ptr<Constraint> ConstraintOneOf::CloneAsInherited() const { |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 193 | native_types::Array cloned; |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 194 | cloned.reserve(set_.value.size()); |
| 195 | for (const auto& prop_value : set_.value) { |
| 196 | cloned.push_back(prop_value->Clone()); |
| 197 | } |
| 198 | return std::unique_ptr<Constraint>{new ConstraintOneOf{std::move(cloned)}}; |
| 199 | } |
| 200 | |
| 201 | std::unique_ptr<base::Value> ConstraintOneOf::ToJson( |
| 202 | chromeos::ErrorPtr* error) const { |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 203 | return TypedValueToJson(set_.value, error); |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | const char* ConstraintOneOf::GetDictKey() const { |
| 207 | return commands::attributes::kOneOf_Enum; |
| 208 | } |
| 209 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 210 | } // namespace buffet |