Commit 61709246 authored by thestig's avatar thestig Committed by Commit bot

Add base::Value::GetTypeName().

Review-Url: https://codereview.chromium.org/2151793002
Cr-Commit-Position: refs/heads/master@{#406161}
parent 6f6e8feb
......@@ -21,6 +21,11 @@ namespace base {
namespace {
const char* const kTypeNames[] = {"null", "boolean", "integer", "double",
"string", "binary", "dictionary", "list"};
static_assert(arraysize(kTypeNames) == Value::TYPE_LIST + 1,
"kTypeNames Has Wrong Size");
std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node);
// Make a deep copy of |node|, but don't include empty lists or dictionaries
......@@ -77,6 +82,13 @@ std::unique_ptr<Value> Value::CreateNullValue() {
return WrapUnique(new Value(TYPE_NULL));
}
// static
const char* Value::GetTypeName(Value::Type type) {
DCHECK_GE(type, 0);
DCHECK_LT(static_cast<size_t>(type), arraysize(kTypeNames));
return kTypeNames[type];
}
bool Value::GetAsBinary(const BinaryValue** out_value) const {
return false;
}
......
......@@ -65,6 +65,9 @@ class BASE_EXPORT Value {
static std::unique_ptr<Value> CreateNullValue();
// Returns the name for a given |type|.
static const char* GetTypeName(Type type);
// Returns the type of the value stored by the current Value object.
// Each type will be implemented by only one subclass of Value, so it's
// safe to use the Type to determine whether you can cast from
......
......@@ -30,15 +30,6 @@ std::vector<T> toVector(T const (&array)[N]) {
return std::vector<T>(array, array + N);
}
// Copied from policy/configuration_policy_handler.cc.
// TODO(pneubeck): move to a common place like base/.
std::string ValueTypeToString(base::Value::Type type) {
const char* const strings[] = {"null", "boolean", "integer", "double",
"string", "binary", "dictionary", "list"};
CHECK(static_cast<size_t>(type) < arraysize(strings));
return strings[type];
}
} // namespace
Validator::Validator(bool error_on_unknown_field,
......@@ -70,15 +61,8 @@ std::unique_ptr<base::DictionaryValue> Validator::ValidateAndRepairObject(
*result = VALID_WITH_WARNINGS;
}
// The return value should be NULL if, and only if, |result| equals INVALID.
DCHECK_EQ(result_value.get() == NULL, *result == INVALID);
base::DictionaryValue* result_dict = NULL;
if (result_value) {
result_value.release()->GetAsDictionary(&result_dict);
CHECK(result_dict);
}
return base::WrapUnique(result_dict);
DCHECK_EQ(!result_value, *result == INVALID);
return base::DictionaryValue::From(std::move(result_value));
}
std::unique_ptr<base::Value> Validator::MapValue(
......@@ -87,8 +71,9 @@ std::unique_ptr<base::Value> Validator::MapValue(
bool* error) {
if (onc_value.GetType() != signature.onc_type) {
LOG(ERROR) << MessageHeader() << "Found value '" << onc_value
<< "' of type '" << ValueTypeToString(onc_value.GetType())
<< "', but type '" << ValueTypeToString(signature.onc_type)
<< "' of type '" << base::Value::GetTypeName(onc_value.GetType())
<< "', but type '"
<< base::Value::GetTypeName(signature.onc_type)
<< "' is required.";
error_or_warning_found_ = *error = true;
return std::unique_ptr<base::Value>();
......@@ -146,13 +131,12 @@ std::unique_ptr<base::DictionaryValue> Validator::MapObject(
}
}
if (valid) {
if (valid)
return repaired;
} else {
DCHECK(error_or_warning_found_);
error_or_warning_found_ = *error = true;
return std::unique_ptr<base::DictionaryValue>();
}
DCHECK(error_or_warning_found_);
error_or_warning_found_ = *error = true;
return std::unique_ptr<base::DictionaryValue>();
}
std::unique_ptr<base::Value> Validator::MapField(
......@@ -288,10 +272,10 @@ bool Validator::ValidateRecommendedField(
if (error_on_wrong_recommended_) {
LOG(ERROR) << message;
return false;
} else {
LOG(WARNING) << message;
continue;
}
LOG(WARNING) << message;
continue;
}
repaired_recommended->AppendString(field_name);
......@@ -446,12 +430,11 @@ bool Validator::ValidateSSIDAndHexSSID(base::DictionaryValue* object) {
// If the HexSSID field is present, ignore errors in SSID because these
// might be caused by the usage of a non-UTF-8 encoding when the SSID
// field was automatically added (see FillInHexSSIDField).
if (object->HasKey(::onc::wifi::kHexSSID)) {
LOG(WARNING) << msg;
} else {
if (!object->HasKey(::onc::wifi::kHexSSID)) {
LOG(ERROR) << msg;
return false;
}
LOG(WARNING) << msg;
}
// Check HexSSID validity.
......
......@@ -30,18 +30,7 @@ namespace policy {
// static
std::string ConfigurationPolicyHandler::ValueTypeToString(
base::Value::Type type) {
static const char* strings[] = {
"null",
"boolean",
"integer",
"double",
"string",
"binary",
"dictionary",
"list"
};
CHECK(static_cast<size_t>(type) < arraysize(strings));
return std::string(strings[type]);
return std::string(base::Value::GetTypeName(type));
}
ConfigurationPolicyHandler::ConfigurationPolicyHandler() {
......
......@@ -36,6 +36,7 @@ struct POLICY_EXPORT PolicyToPreferenceMapEntry {
// their corresponding preferences, and to check whether the policies are valid.
class POLICY_EXPORT ConfigurationPolicyHandler {
public:
// TODO(thestig): Replace with base::Value::GetTypeName().
static std::string ValueTypeToString(base::Value::Type type);
ConfigurationPolicyHandler();
......
......@@ -7,49 +7,33 @@
#include <stddef.h>
#include "base/macros.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "extensions/common/manifest_constants.h"
using base::UTF8ToUTF16;
using base::StringPrintf;
namespace extensions {
namespace {
const char* ValueTypeToString(const base::Value* value) {
const base::Value::Type type = value->GetType();
static const char* strings[] = {"null",
"boolean",
"integer",
"double",
"string",
"binary",
"dictionary",
"list"};
CHECK(static_cast<size_t>(type) < arraysize(strings));
return strings[type];
}
class ErrorBuilder {
public:
explicit ErrorBuilder(base::string16* error) : error_(error) {}
// Appends a literal string |error|.
void Append(const char* error) {
if (error_->length())
error_->append(UTF8ToUTF16("; "));
error_->append(UTF8ToUTF16(error));
void Append(base::StringPiece error) {
if (!error_->empty())
error_->append(base::ASCIIToUTF16("; "));
error_->append(base::UTF8ToUTF16(error));
}
// Appends a string |error| with the first %s replaced by |sub|.
void Append(const char* error, const char* sub) {
Append(base::StringPrintf(error, sub).c_str());
void Append(base::StringPiece error, base::StringPiece sub) {
Append(base::StringPrintf(error.data(), sub.data()));
}
private:
base::string16* error_;
base::string16* const error_;
DISALLOW_COPY_AND_ASSIGN(ErrorBuilder);
};
......@@ -61,12 +45,12 @@ class ErrorBuilder {
bool ConvertManifestRule(const linked_ptr<DeclarativeManifestData::Rule>& rule,
ErrorBuilder* error_builder) {
auto convert_list =
[error_builder](std::vector<std::unique_ptr<base::Value>>& list) {
[error_builder](const std::vector<std::unique_ptr<base::Value>>& list) {
for (const std::unique_ptr<base::Value>& value : list) {
base::DictionaryValue* dictionary = nullptr;
if (!value->GetAsDictionary(&dictionary)) {
error_builder->Append("expected dictionary, got %s",
ValueTypeToString(value.get()));
base::Value::GetTypeName(value->GetType()));
return false;
}
std::string type;
......@@ -139,19 +123,15 @@ std::unique_ptr<DeclarativeManifestData> DeclarativeManifestData::FromValue(
const base::ListValue* list = nullptr;
if (!value.GetAsList(&list)) {
error_builder.Append("'event_rules' expected list, got %s",
ValueTypeToString(&value));
base::Value::GetTypeName(value.GetType()));
return std::unique_ptr<DeclarativeManifestData>();
}
for (size_t i = 0; i < list->GetSize(); ++i) {
for (const auto& element : *list) {
const base::DictionaryValue* dict = nullptr;
if (!list->GetDictionary(i, &dict)) {
const base::Value* value = nullptr;
if (list->Get(i, &value))
error_builder.Append("expected dictionary, got %s",
ValueTypeToString(value));
else
error_builder.Append("expected dictionary");
if (!element->GetAsDictionary(&dict)) {
error_builder.Append("expected dictionary, got %s",
base::Value::GetTypeName(element->GetType()));
return std::unique_ptr<DeclarativeManifestData>();
}
std::string event;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment