Skip to content

Commit 81bdb6e

Browse files
committed
src: unify option parsing methods
1 parent 738f977 commit 81bdb6e

File tree

2 files changed

+30
-69
lines changed

2 files changed

+30
-69
lines changed

src/node_config_file.cc

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -148,52 +148,27 @@ ParseResult ConfigReader::ProcessOptionValue(
148148
return ParseResult::Valid;
149149
}
150150

151-
ParseResult ConfigReader::ParseNodeOptions(
152-
simdjson::ondemand::object* node_options_object) {
153-
auto env_options_map = options_parser::MapEnvOptionsFlagInputType();
154-
simdjson::ondemand::value ondemand_value;
155-
std::string_view key;
156-
157-
for (auto field : *node_options_object) {
158-
if (field.unescaped_key().get(key) || field.value().get(ondemand_value)) {
159-
return ParseResult::InvalidContent;
160-
}
161-
162-
// The key needs to match the CLI option
163-
std::string prefix = "--";
164-
auto it = env_options_map.find(prefix.append(key));
165-
if (it != env_options_map.end()) {
166-
// If the option has already been set, return an error
167-
if (unique_options_.contains(it->first)) {
168-
FPrintF(stderr, "Option %s is already defined\n", it->first.c_str());
169-
return ParseResult::InvalidContent;
170-
}
171-
ParseResult result = ProcessOptionValue(key,
172-
it->first,
173-
ondemand_value,
174-
it->second,
175-
&node_options_,
176-
&unique_options_);
177-
if (result != ParseResult::Valid) {
178-
return result;
179-
}
180-
} else {
181-
FPrintF(stderr, "Unknown or not allowed option %s\n", key.data());
182-
return ParseResult::InvalidContent;
183-
}
184-
}
185-
return ParseResult::Valid;
186-
}
187-
188-
ParseResult ConfigReader::ParseNamespaceOptions(
151+
ParseResult ConfigReader::ParseOptions(
189152
simdjson::ondemand::object* options_object,
190153
const std::string& namespace_name) {
191-
// MapOptions could send also options non settable via nodeOptions
192-
auto options_map = options_parser::MapOptionsByNamespace(namespace_name);
193-
194-
if (!env_options_initialized_) {
195-
env_options_map_ = options_parser::MapEnvOptionsFlagInputType();
196-
env_options_initialized_ = true;
154+
// Determine which options map to use and output vector
155+
std::unordered_map<std::string, options_parser::OptionType> options_map;
156+
std::vector<std::string>* output_vector;
157+
158+
if (namespace_name == "nodeOptions") {
159+
// Special case for backward compatibility: handle nodeOptions with env
160+
// options map
161+
options_map = options_parser::MapEnvOptionsFlagInputType();
162+
output_vector = &node_options_;
163+
} else {
164+
// Handle other namespaces
165+
options_map = options_parser::MapOptionsByNamespace(namespace_name);
166+
output_vector = &namespace_options_;
167+
168+
if (!env_options_initialized_) {
169+
env_options_map_ = options_parser::MapEnvOptionsFlagInputType();
170+
env_options_initialized_ = true;
171+
}
197172
}
198173

199174
simdjson::ondemand::value ondemand_value;
@@ -204,7 +179,7 @@ ParseResult ConfigReader::ParseNamespaceOptions(
204179
return ParseResult::InvalidContent;
205180
}
206181

207-
// The key needs to match the option for this namespace
182+
// The key needs to match the CLI option
208183
std::string prefix = "--";
209184
auto it = options_map.find(prefix.append(key));
210185
if (it != options_map.end()) {
@@ -213,12 +188,11 @@ ParseResult ConfigReader::ParseNamespaceOptions(
213188
FPrintF(stderr, "Option %s is already defined\n", it->first.c_str());
214189
return ParseResult::InvalidContent;
215190
}
216-
// Process the option for env options
217191
ParseResult result = ProcessOptionValue(key,
218192
it->first,
219193
ondemand_value,
220194
it->second,
221-
&namespace_non_env_options_,
195+
output_vector,
222196
&unique_options_);
223197
if (result != ParseResult::Valid) {
224198
return result;
@@ -304,20 +278,10 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
304278
return ParseResult::InvalidContent;
305279
}
306280

307-
// Special case for backward compatibility: handle nodeOptions with existing
308-
// method
309-
if (namespace_name == "nodeOptions") {
310-
ParseResult result = ParseNodeOptions(&namespace_object);
311-
if (result != ParseResult::Valid) {
312-
return result;
313-
}
314-
} else {
315-
// Process options for this namespace
316-
ParseResult result =
317-
ParseNamespaceOptions(&namespace_object, namespace_name);
318-
if (result != ParseResult::Valid) {
319-
return result;
320-
}
281+
// Process options for this namespace using the unified method
282+
ParseResult result = ParseOptions(&namespace_object, namespace_name);
283+
if (result != ParseResult::Valid) {
284+
return result;
321285
}
322286
}
323287

@@ -335,7 +299,7 @@ std::string ConfigReader::AssignNodeOptions() {
335299
}
336300

337301
std::vector<std::string> ConfigReader::AssignNodeNonEnvOptions() {
338-
return namespace_non_env_options_;
302+
return namespace_options_;
339303
}
340304

341305
size_t ConfigReader::GetFlagsSize() {

src/node_config_file.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ class ConfigReader {
3838
size_t GetFlagsSize();
3939

4040
private:
41-
// Parse the nodeOptions object from the configuration file
42-
ParseResult ParseNodeOptions(simdjson::ondemand::object* node_options_object);
43-
44-
// Parse options for a specific namespace
45-
ParseResult ParseNamespaceOptions(simdjson::ondemand::object* options_object,
46-
const std::string& namespace_name);
41+
// Parse options for a specific namespace (including nodeOptions for backward
42+
// compatibility)
43+
ParseResult ParseOptions(simdjson::ondemand::object* options_object,
44+
const std::string& namespace_name);
4745

4846
// Process a single option value based on its type
4947
ParseResult ProcessOptionValue(
@@ -57,7 +55,6 @@ class ConfigReader {
5755
std::unordered_set<std::string> unique_options_;
5856
std::vector<std::string> node_options_;
5957
std::vector<std::string> namespace_options_;
60-
std::vector<std::string> namespace_non_env_options_;
6158

6259
// Cache for fast lookup of environment options
6360
std::unordered_map<std::string, options_parser::OptionType> env_options_map_;

0 commit comments

Comments
 (0)