android_webview
apps
ash
base
build
build_overrides
buildtools
cc
chrome
chromecast
chromeos
codelabs
components
content
courgette
crypto
dbus
device
docs
extensions
fuchsia
gin
google_apis
google_update
gpu
headless
infra
ios
ipc
media
mojo
native_client_sdk
net
pdf
ppapi
printing
remoting
rlz
sandbox
services
skia
sql
storage
styleguide
testing
third_party
tools
accessibility
aggregation_service
android
attribution_reporting
binary_size
bisect
bisect_repackage
captured_sites
cast3p
cfi
check_ecs_deps
checkbins
checklicenses
checkperms
checkteamtags
chrome_extensions
clang
code_coverage
compile_test
cr
crates
crbug
cros
cygprofile
determinism
diagnosis
disable_tests
dromaeo_benchmark_runner
dump_process_memory
emacs
find_runtime_symbols
flags
flakiness
fuchsia
gdb
generate_library_loader
generate_shim_headers
generate_stubs
get_swarming_logs
git
gn
grit
gritsettings
idl_parser
imagediff
infra
ipc_fuzzer
json_comment_eater
json_data_generator
json_schema_compiler
highlighters
test
BUILD.gn
DIR_METADATA
OWNERS
PRESUBMIT.py
cc_generator.py
code.py
code_test.py
compiler.py
cpp_bundle_generator.py
cpp_bundle_generator_test.py
cpp_generator.py
cpp_namespace_environment.py
cpp_type_generator.py
cpp_type_generator_test.py
cpp_util.py
cpp_util_test.py
feature_compiler.py
feature_compiler_test.py
features_cc_generator.py
features_compiler.py
features_h_generator.py
h_generator.py
idl_schema.py
idl_schema_test.py
js_externs_generator.py
js_externs_generator_test.py
js_interface_generator.py
js_interface_generator_test.py
js_util.py
json_features.gni
json_parse.py
json_schema.py
json_schema_api.gni
json_schema_test.py
manifest_parse_util.cc
manifest_parse_util.h
memoize.py
model.py
model_test.py
namespace_resolver.py
preview.py
schema_loader.py
schema_util.py
schema_util_test.py
util.cc
util.h
util_cc_helper.py
json_to_struct
l10n
linux
lldb
luci-go
mac
mb
md_browser
media_engagement_preload
memory
metrics
msan
oopif
origin_trials
page_cycler
perf
perfbot-analysis
polymer
privacy_budget
protoc_wrapper
python
real_world_impact
resources
resultdb
rust
security
site_compare
stats_viewer
strict_enum_value_checker
style_variable_generator
sublime
symsrc
tests
traceline
tracing
traffic_annotation
translation
typescript
ubsan
usb_gadget
v8_context_snapshot
valgrind
variations
vim
visual_debugger
vscode
web_bluetooth
web_dev_style
win
.style.yapf
DEPS
DIR_METADATA
OWNERS
add_header.py
add_header_test.py
apply_cpplint_header_guard.py
auto-nav.py
autotest.py
bash-completion
bisect-builds.py
bisect_test.py
boilerplate.py
buildstate.bat
buildstate.py
check_git_config.py
check_grd_for_unused_strings.py
clang-format-js
diagnose-me.py
download_optimization_profile.py
gypv8sh.py
hresult_to_enum.py
include_tracer.py
ipc_messages_log.py
licenses.py
make_gtest_filter.py
make_gtest_filter_test.py
multi_process_rss.py
nocompile_driver.py
omahaproxy.py
perry.py
remove_duplicate_includes.py
remove_stale_pyc_files.py
roll_webgl_conformance.py
run-swarmed.py
sort_sources.py
uberblame.py
unused-symbols-report.py
update_pgo_profiles.py
yes_no.py
ui
url
weblayer
.clang-format
.clang-tidy
.eslintrc.js
.git-blame-ignore-revs
.gitattributes
.gitignore
.gn
.mailmap
.rustfmt.toml
.vpython
.vpython3
.yapfignore
AUTHORS
BUILD.gn
CODE_OF_CONDUCT.md
DEPS
DIR_METADATA
ENG_REVIEW_OWNERS
LICENSE
LICENSE.chromium_os
OWNERS
PRESUBMIT.py
PRESUBMIT_test.py
PRESUBMIT_test_mocks.py
README.md
WATCHLISTS
codereview.settings

Bug: 1187011 Change-Id: I0b2ebbe74c6e182f0db2aa481d824d90510885a2 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3353024 Auto-Submit: Austin Sullivan <asully@chromium.org> Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org> Commit-Queue: Austin Sullivan <asully@chromium.org> Cr-Commit-Position: refs/heads/main@{#961786}
188 lines
6.4 KiB
C++
188 lines
6.4 KiB
C++
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
|
|
#define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "base/format_macros.h"
|
|
#include "base/strings/stringprintf.h"
|
|
#include "base/strings/utf_string_conversions.h"
|
|
#include "base/values.h"
|
|
|
|
namespace json_schema_compiler {
|
|
|
|
namespace util {
|
|
|
|
// Populates the item |out| from the value |from|. These are used by template
|
|
// specializations of |Get(Optional)ArrayFromList|.
|
|
bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
|
|
|
|
bool PopulateItem(const base::Value& from, int* out);
|
|
bool PopulateItem(const base::Value& from, int* out, std::u16string* error);
|
|
bool PopulateItem(const base::Value& from, bool* out);
|
|
bool PopulateItem(const base::Value& from, bool* out, std::u16string* error);
|
|
bool PopulateItem(const base::Value& from, double* out);
|
|
bool PopulateItem(const base::Value& from, double* out, std::u16string* error);
|
|
bool PopulateItem(const base::Value& from, std::string* out);
|
|
bool PopulateItem(const base::Value& from,
|
|
std::string* out,
|
|
std::u16string* error);
|
|
bool PopulateItem(const base::Value& from, std::vector<uint8_t>* out);
|
|
bool PopulateItem(const base::Value& from,
|
|
std::vector<uint8_t>* out,
|
|
std::u16string* error);
|
|
bool PopulateItem(const base::Value& from,
|
|
std::unique_ptr<base::Value>* out,
|
|
std::u16string* error);
|
|
bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
|
|
|
|
// This template is used for types generated by tools/json_schema_compiler.
|
|
template <class T>
|
|
bool PopulateItem(const base::Value& from, T* out) {
|
|
if (!from.is_dict())
|
|
return false;
|
|
T obj;
|
|
if (!T::Populate(from, &obj))
|
|
return false;
|
|
*out = std::move(obj);
|
|
return true;
|
|
}
|
|
|
|
// This template is used for types generated by tools/json_schema_compiler with
|
|
// error generation enabled.
|
|
template <class T>
|
|
bool PopulateItem(const base::Value& from, T* out, std::u16string* error) {
|
|
T obj;
|
|
if (!T::Populate(from, &obj, error))
|
|
return false;
|
|
*out = std::move(obj);
|
|
return true;
|
|
}
|
|
|
|
// Populates |out| with |list|. Returns false if there is no list at the
|
|
// specified key or if the list has anything other than |T|.
|
|
template <class T>
|
|
bool PopulateArrayFromList(const base::Value::ConstListView& list,
|
|
std::vector<T>* out) {
|
|
out->clear();
|
|
T item;
|
|
for (const auto& value : list) {
|
|
if (!PopulateItem(value, &item))
|
|
return false;
|
|
// T might not be movable, but in that case it should be copyable, and this
|
|
// will still work.
|
|
out->push_back(std::move(item));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Populates |out| with |list|. Returns false and sets |error| if there is no
|
|
// list at the specified key or if the list has anything other than |T|.
|
|
template <class T>
|
|
bool PopulateArrayFromList(const base::Value::ConstListView& list,
|
|
std::vector<T>* out,
|
|
std::u16string* error) {
|
|
out->clear();
|
|
T item;
|
|
std::u16string item_error;
|
|
for (size_t i = 0; i < list.size(); ++i) {
|
|
if (!PopulateItem(list[i], &item, &item_error)) {
|
|
DCHECK(error->empty());
|
|
*error = base::ASCIIToUTF16(
|
|
base::StringPrintf("Parsing array failed at index %" PRIuS ": %s", i,
|
|
base::UTF16ToASCII(item_error).c_str()));
|
|
return false;
|
|
}
|
|
out->push_back(std::move(item));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Creates a new vector containing |list| at |out|. Returns
|
|
// true on success or if there is nothing at the specified key. Returns false
|
|
// if anything other than a list of |T| is at the specified key.
|
|
template <class T>
|
|
bool PopulateOptionalArrayFromList(const base::Value::ConstListView& list,
|
|
std::unique_ptr<std::vector<T>>* out) {
|
|
out->reset(new std::vector<T>());
|
|
if (!PopulateArrayFromList(list, out->get())) {
|
|
out->reset();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template <class T>
|
|
bool PopulateOptionalArrayFromList(const base::Value::ConstListView& list,
|
|
std::unique_ptr<std::vector<T>>* out,
|
|
std::u16string* error) {
|
|
out->reset(new std::vector<T>());
|
|
if (!PopulateArrayFromList(list, out->get(), error)) {
|
|
out->reset();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Appends a Value newly created from |from| to |out|. These used by template
|
|
// specializations of |Set(Optional)ArrayToList|.
|
|
void AddItemToList(const int from, base::ListValue* out);
|
|
void AddItemToList(const bool from, base::ListValue* out);
|
|
void AddItemToList(const double from, base::ListValue* out);
|
|
void AddItemToList(const std::string& from, base::ListValue* out);
|
|
void AddItemToList(const std::vector<uint8_t>& from, base::ListValue* out);
|
|
void AddItemToList(const std::unique_ptr<base::Value>& from,
|
|
base::ListValue* out);
|
|
void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from,
|
|
base::ListValue* out);
|
|
|
|
// This template is used for types generated by tools/json_schema_compiler.
|
|
template <class T>
|
|
void AddItemToList(const std::unique_ptr<T>& from, base::ListValue* out) {
|
|
out->Append(from->ToValue());
|
|
}
|
|
|
|
// This template is used for types generated by tools/json_schema_compiler.
|
|
template <class T>
|
|
void AddItemToList(const T& from, base::ListValue* out) {
|
|
out->Append(from.ToValue());
|
|
}
|
|
|
|
// Set |out| to the the contents of |from|. Requires PopulateItem to be
|
|
// implemented for |T|.
|
|
template <class T>
|
|
void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
|
|
out->ClearList();
|
|
for (const T& item : from)
|
|
AddItemToList(item, out);
|
|
}
|
|
|
|
// Set |out| to the the contents of |from| if |from| is not null. Requires
|
|
// PopulateItem to be implemented for |T|.
|
|
template <class T>
|
|
void PopulateListFromOptionalArray(const std::unique_ptr<std::vector<T>>& from,
|
|
base::ListValue* out) {
|
|
if (from)
|
|
PopulateListFromArray(*from, out);
|
|
}
|
|
|
|
template <class T>
|
|
std::unique_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) {
|
|
std::unique_ptr<base::ListValue> list(new base::ListValue());
|
|
PopulateListFromArray(from, list.get());
|
|
return std::move(list);
|
|
}
|
|
|
|
} // namespace util
|
|
} // namespace json_schema_compiler
|
|
|
|
#endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
|