From 89a17f7839bfedeee84ae1dbc178fb13dfe03eba Mon Sep 17 00:00:00 2001 From: reillyg Date: Tue, 5 May 2015 19:34:27 -0700 Subject: Add support for passing errors through PopulateArrayFromList. When a JSON schema includes an array of objects PopulateArrayFromList calls a specialized PopulateItem instance for the generated object type. When compiled with generate_error_messages=True the generated type's Populate method expects to receive an error parameter. This patch adds versions of the PopulateArrayFromList and PopulateItem functions that pass through and provide that parameter. This capability is required for a new manifest key that will include an array of objects being added as part of issue 468955. Review URL: https://codereview.chromium.org/1128523003 Cr-Commit-Position: refs/heads/master@{#328471} --- tools/json_schema_compiler/util.cc | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) (limited to 'tools/json_schema_compiler/util.cc') diff --git a/tools/json_schema_compiler/util.cc b/tools/json_schema_compiler/util.cc index 3e36cfc..b7c4ecc 100644 --- a/tools/json_schema_compiler/util.cc +++ b/tools/json_schema_compiler/util.cc @@ -5,6 +5,7 @@ #include "tools/json_schema_compiler/util.h" #include "base/stl_util.h" +#include "base/strings/utf_string_conversions.h" #include "base/values.h" namespace json_schema_compiler { @@ -14,18 +15,68 @@ bool PopulateItem(const base::Value& from, int* out) { return from.GetAsInteger(out); } +bool PopulateItem(const base::Value& from, int* out, base::string16* error) { + if (!from.GetAsInteger(out)) { + if (error->length()) { + error->append(base::UTF8ToUTF16("; ")); + } + error->append(base::UTF8ToUTF16("expected integer, got " + + ValueTypeToString(from.GetType()))); + return false; + } + return true; +} + bool PopulateItem(const base::Value& from, bool* out) { return from.GetAsBoolean(out); } +bool PopulateItem(const base::Value& from, bool* out, base::string16* error) { + if (!from.GetAsBoolean(out)) { + if (error->length()) { + error->append(base::UTF8ToUTF16("; ")); + } + error->append(base::UTF8ToUTF16("expected boolean, got " + + ValueTypeToString(from.GetType()))); + return false; + } + return true; +} + bool PopulateItem(const base::Value& from, double* out) { return from.GetAsDouble(out); } +bool PopulateItem(const base::Value& from, double* out, base::string16* error) { + if (!from.GetAsDouble(out)) { + if (error->length()) { + error->append(base::UTF8ToUTF16("; ")); + } + error->append(base::UTF8ToUTF16("expected double, got " + + ValueTypeToString(from.GetType()))); + return false; + } + return true; +} + bool PopulateItem(const base::Value& from, std::string* out) { return from.GetAsString(out); } +bool PopulateItem(const base::Value& from, + std::string* out, + base::string16* error) { + if (!from.GetAsString(out)) { + if (error->length()) { + error->append(base::UTF8ToUTF16("; ")); + } + error->append(base::UTF8ToUTF16("expected string, got " + + ValueTypeToString(from.GetType()))); + return false; + } + return true; +} + bool PopulateItem(const base::Value& from, std::vector* out) { const base::BinaryValue* binary = nullptr; if (!from.GetAsBinary(&binary)) @@ -34,12 +85,35 @@ bool PopulateItem(const base::Value& from, std::vector* out) { return true; } +bool PopulateItem(const base::Value& from, + std::vector* out, + base::string16* error) { + const base::BinaryValue* binary = nullptr; + if (!from.GetAsBinary(&binary)) { + if (error->length()) { + error->append(base::UTF8ToUTF16("; ")); + } + error->append(base::UTF8ToUTF16("expected binary, got " + + ValueTypeToString(from.GetType()))); + return false; + } + out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize()); + return true; +} + bool PopulateItem(const base::Value& from, linked_ptr* out) { *out = make_linked_ptr(from.DeepCopy()); return true; } bool PopulateItem(const base::Value& from, + linked_ptr* out, + base::string16* error) { + *out = make_linked_ptr(from.DeepCopy()); + return true; +} + +bool PopulateItem(const base::Value& from, linked_ptr* out) { const base::DictionaryValue* dict = nullptr; if (!from.GetAsDictionary(&dict)) @@ -48,6 +122,22 @@ bool PopulateItem(const base::Value& from, return true; } +bool PopulateItem(const base::Value& from, + linked_ptr* out, + base::string16* error) { + const base::DictionaryValue* dict = nullptr; + if (!from.GetAsDictionary(&dict)) { + if (error->length()) { + error->append(base::UTF8ToUTF16("; ")); + } + error->append(base::UTF8ToUTF16("expected dictionary, got " + + ValueTypeToString(from.GetType()))); + return false; + } + *out = make_linked_ptr(dict->DeepCopy()); + return true; +} + void AddItemToList(const int from, base::ListValue* out) { out->Append(new base::FundamentalValue(from)); } -- cgit v1.1