OmniSciDB  2e3a973ef4
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 #include <string>
20 #include <unordered_map>
21 
22 #include "rapidjson/document.h"
23 #include "rapidjson/stringbuffer.h"
24 #include "rapidjson/writer.h"
25 
26 #include "Logger/Logger.h"
27 #include "Shared/StringTransform.h"
28 
29 namespace foreign_storage {
31  std::map<std::string, std::string, std::less<>> options;
32 
34 
35  OptionsContainer(const std::map<std::string, std::string, std::less<>>& options)
36  : options(options) {}
37 
38  OptionsContainer(const std::string& options_str) { populateOptionsMap(options_str); }
39 
40  void populateOptionsMap(const rapidjson::Value& ddl_options) {
41  CHECK(ddl_options.IsObject());
42  for (const auto& member : ddl_options.GetObject()) {
43  std::string key = to_upper(member.name.GetString());
44  std::string value = member.value.GetString();
45  options[key] = value;
46  }
47  }
48 
49  void populateOptionsMap(const std::string& options_json, bool clear = false) {
50  CHECK(!options_json.empty());
51  if (clear) {
52  options.clear();
53  }
54  rapidjson::Document options;
55  options.Parse(options_json);
56  populateOptionsMap(options);
57  }
58 
59  std::string getOptionsAsJsonString() const {
60  rapidjson::Document document;
61  document.SetObject();
62 
63  for (const auto& [key, value] : options) {
64  document.AddMember(rapidjson::Value().SetString(
65  key.c_str(), key.length(), document.GetAllocator()),
66  rapidjson::Value().SetString(
67  value.c_str(), value.length(), document.GetAllocator()),
68  document.GetAllocator());
69  }
70 
71  rapidjson::StringBuffer buffer;
72  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
73  document.Accept(writer);
74  return buffer.GetString();
75  }
76 };
77 } // namespace foreign_storage
void populateOptionsMap(const std::string &options_json, bool clear=false)
std::map< std::string, std::string, std::less<> > options
std::string getOptionsAsJsonString() const
void populateOptionsMap(const rapidjson::Value &ddl_options)
OptionsContainer(const std::string &options_str)
std::string to_upper(const std::string &str)
OptionsContainer(const std::map< std::string, std::string, std::less<>> &options)
#define CHECK(condition)
Definition: Logger.h:197