OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionsContainer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 /*
20  * Note on WIN32 GetObject error in rapidson.
21  *
22  * Rapidjson has defined a GetObject method which
23  * wil; 'conflict' with the GetObject defined in Window.h
24  *
25  * Further boost/regex.hpp includes windows.h. If that files
26  * is included in the same compilation unit as this one this
27  * can cause issues.
28  *
29  * At the moment the best way to solve the boost problem is to
30  * wrap the boost include in #defines which clean the name space.
31  *
32  * TODO work out exactly what is causing the boost/regex.hpp rapidjson
33  * conflict.
34  *
35 
36 */
37 #include <string>
38 #include <unordered_map>
39 
40 #include "rapidjson/document.h"
41 #include "rapidjson/stringbuffer.h"
42 #include "rapidjson/writer.h"
43 
44 #include "Logger/Logger.h"
45 #include "Shared/StringTransform.h"
46 
47 namespace foreign_storage {
48 using OptionsMap = std::map<std::string, std::string, std::less<>>;
51 
53 
54  OptionsContainer(const OptionsMap& options) : options(options) {}
55 
56  OptionsContainer(const std::string& options_str) { populateOptionsMap(options_str); }
57 
58  void populateOptionsMap(OptionsMap&& options_map, bool clear = false) {
59  if (clear) {
60  options = options_map;
61  } else {
62  // The merge order here is to make sure we overwrite duplicates in options. If we
63  // used options.merge(options_map) we would preserve existing entries.
64  options_map.merge(options);
65  options = options_map;
66  }
67  }
68 
69  void populateOptionsMap(const rapidjson::Value& ddl_options, bool clear = false) {
70  CHECK(ddl_options.IsObject());
71  if (clear) {
72  options.clear();
73  }
74  for (const auto& member : ddl_options.GetObject()) {
75  std::string key = to_upper(member.name.GetString());
76  options[key] = member.value.GetString();
77  }
78  }
79 
80  void populateOptionsMap(const std::string& options_json, bool clear = false) {
81  CHECK(!options_json.empty());
82  rapidjson::Document options;
83  options.Parse(options_json);
84  populateOptionsMap(options);
85  }
86 
87  std::string getOptionsAsJsonString() const {
88  rapidjson::Document document;
89  document.SetObject();
90 
91  for (const auto& [key, value] : options) {
92  document.AddMember(rapidjson::Value().SetString(
93  key.c_str(), key.length(), document.GetAllocator()),
94  rapidjson::Value().SetString(
95  value.c_str(), value.length(), document.GetAllocator()),
96  document.GetAllocator());
97  }
98 
99  rapidjson::StringBuffer buffer;
100  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
101  document.Accept(writer);
102  return buffer.GetString();
103  }
104 
105  std::optional<std::string> getOption(const std::string_view& key) const {
106  if (auto it = options.find(key); it != options.end()) {
107  return it->second;
108  } else {
109  return {};
110  }
111  }
112 };
113 } // namespace foreign_storage
void populateOptionsMap(const rapidjson::Value &ddl_options, bool clear=false)
void populateOptionsMap(const std::string &options_json, bool clear=false)
std::string getOptionsAsJsonString() const
OptionsContainer(const std::string &options_str)
void populateOptionsMap(OptionsMap &&options_map, bool clear=false)
std::optional< std::string > getOption(const std::string_view &key) const
OptionsContainer(const OptionsMap &options)
std::string to_upper(const std::string &str)
#define CHECK(condition)
Definition: Logger.h:209
std::map< std::string, std::string, std::less<>> OptionsMap