From 92606579d3af5b23b79161d889a795ac13d8fb77 Mon Sep 17 00:00:00 2001 From: Charlie Hess Date: Fri, 29 Apr 2016 17:32:25 -0700 Subject: [PATCH] Generalize this mate converter for reuse. --- atom/common/api/atom_api_crash_reporter.cc | 19 +---------- .../native_mate_converters/string_map_converter.cc | 37 ++++++++++++++++++++++ .../native_mate_converters/string_map_converter.h | 28 ++++++++++++++++ filenames.gypi | 2 ++ 4 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 atom/common/native_mate_converters/string_map_converter.cc create mode 100644 atom/common/native_mate_converters/string_map_converter.h diff --git a/atom/common/api/atom_api_crash_reporter.cc b/atom/common/api/atom_api_crash_reporter.cc index e1932ad..5db1461 100644 --- a/atom/common/api/atom_api_crash_reporter.cc +++ b/atom/common/api/atom_api_crash_reporter.cc @@ -6,6 +6,7 @@ #include #include "atom/common/crash_reporter/crash_reporter.h" +#include "atom/common/native_mate_converters/string_map_converter.h" #include "base/bind.h" #include "native_mate/dictionary.h" @@ -16,24 +17,6 @@ using crash_reporter::CrashReporter; namespace mate { template<> -struct Converter > { - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - std::map* out) { - if (!val->IsObject()) - return false; - - v8::Local dict = val->ToObject(); - v8::Local keys = dict->GetOwnPropertyNames(); - for (uint32_t i = 0; i < keys->Length(); ++i) { - v8::Local key = keys->Get(i); - (*out)[V8ToString(key)] = V8ToString(dict->Get(key)); - } - return true; - } -}; - -template<> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const CrashReporter::UploadReportResult& reports) { diff --git a/atom/common/native_mate_converters/string_map_converter.cc b/atom/common/native_mate_converters/string_map_converter.cc new file mode 100644 index 0000000..dce3c38 --- /dev/null +++ b/atom/common/native_mate_converters/string_map_converter.cc @@ -0,0 +1,37 @@ +// Copyright (c) 2014 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/common/native_mate_converters/string_map_converter.h" + +namespace mate { + +bool Converter>::FromV8(v8::Isolate* isolate, + v8::Local val, + std::map* out) { + + if (!val->IsObject()) + return false; + + v8::Local dict = val->ToObject(); + v8::Local keys = dict->GetOwnPropertyNames(); + for (uint32_t i = 0; i < keys->Length(); ++i) { + v8::Local key = keys->Get(i); + (*out)[V8ToString(key)] = V8ToString(dict->Get(key)); + } + return true; +} + +v8::Local Converter>::ToV8(v8::Isolate* isolate, + const std::map& in) { + + mate::Dictionary dict(isolate, v8::Object::New(isolate)); + + for (auto const &pair : in) { + dict.Set(pair.first, pair.second); + } + + return dict.GetHandle(); +} + +} // namespace mate \ No newline at end of file diff --git a/atom/common/native_mate_converters/string_map_converter.h b/atom/common/native_mate_converters/string_map_converter.h new file mode 100644 index 0000000..bdab0be --- /dev/null +++ b/atom/common/native_mate_converters/string_map_converter.h @@ -0,0 +1,28 @@ +// Copyright (c) 2014 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING_MAP_CONVERTER_H_ +#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING_MAP_CONVERTER_H_ + +#include +#include + +#include "native_mate/converter.h" +#include "native_mate/dictionary.h" + +namespace mate { + +template<> +struct Converter> { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + std::map* out); + + static v8::Local ToV8(v8::Isolate* isolate, + const std::map& in); +}; + +} // namespace mate + +#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING_MAP_CONVERTER_H_ \ No newline at end of file diff --git a/filenames.gypi b/filenames.gypi index 1c21394..783b5cb 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -361,6 +361,8 @@ 'atom/common/native_mate_converters/image_converter.h', 'atom/common/native_mate_converters/net_converter.cc', 'atom/common/native_mate_converters/net_converter.h', + 'atom/common/native_mate_converters/string_map_converter.cc', + 'atom/common/native_mate_converters/string_map_converter.h', 'atom/common/native_mate_converters/string16_converter.h', 'atom/common/native_mate_converters/v8_value_converter.cc', 'atom/common/native_mate_converters/v8_value_converter.h', -- 2.7.4