@@ -27,43 +27,13 @@ std::optional<std::string_view> ConfigReader::GetDataFromArgs(
27
27
return std::nullopt;
28
28
}
29
29
30
- ParseResult ConfigReader::ParseConfig (const std::string_view& config_path) {
31
- std::string file_content;
32
- // Read the configuration file
33
- int r = ReadFileSync (&file_content, config_path.data ());
34
- if (r != 0 ) {
35
- const char * err = uv_strerror (r);
36
- FPrintF (
37
- stderr, " Cannot read configuration from %s: %s\n " , config_path, err);
38
- return ParseResult::FileError;
39
- }
40
-
41
- // Parse the configuration file
42
- simdjson::ondemand::parser json_parser;
43
- simdjson::ondemand::document document;
44
- if (json_parser.iterate(file_content).get (document)) {
45
- FPrintF (stderr, " Can't parse %s\n " , config_path.data ());
46
- return ParseResult::InvalidContent;
47
- }
48
-
49
- simdjson::ondemand::object main_object;
50
- // If document is not an object, throw an error.
51
- if (auto root_error = document.get_object ().get (main_object)) {
52
- if (root_error == simdjson::error_code::INCORRECT_TYPE) {
53
- FPrintF (stderr,
54
- " Root value unexpected not an object for %s\n\n " ,
55
- config_path.data ());
56
- } else {
57
- FPrintF (stderr, " Can't parse %s\n " , config_path.data ());
58
- }
59
- return ParseResult::InvalidContent;
60
- }
61
-
30
+ ParseResult ConfigReader::ParseNodeOptions (
31
+ simdjson::ondemand::object* node_options_object) {
62
32
auto env_options_map = options_parser::MapEnvOptionsFlagInputType ();
63
33
simdjson::ondemand::value ondemand_value;
64
34
std::string_view key;
65
35
66
- for (auto field : main_object ) {
36
+ for (auto field : *node_options_object ) {
67
37
if (field.unescaped_key ().get (key) || field.value ().get (ondemand_value)) {
68
38
return ParseResult::InvalidContent;
69
39
}
@@ -79,7 +49,8 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
79
49
FPrintF (stderr, " Invalid value for %s\n " , it->first .c_str ());
80
50
return ParseResult::InvalidContent;
81
51
}
82
- flags_.push_back (it->first + " =" + (result ? " true" : " false" ));
52
+ node_options_.push_back (it->first + " =" +
53
+ (result ? " true" : " false" ));
83
54
break ;
84
55
}
85
56
// String array can allow both string and array types
@@ -102,7 +73,7 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
102
73
FPrintF (stderr, " Invalid value for %s\n " , it->first .c_str ());
103
74
return ParseResult::InvalidContent;
104
75
}
105
- flags_ .push_back (it->first + " =" + std::string (import));
76
+ node_options_ .push_back (it->first + " =" + std::string (import));
106
77
}
107
78
break ;
108
79
}
@@ -112,7 +83,7 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
112
83
FPrintF (stderr, " Invalid value for %s\n " , it->first .c_str ());
113
84
return ParseResult::InvalidContent;
114
85
}
115
- flags_ .push_back (it->first + " =" + result);
86
+ node_options_ .push_back (it->first + " =" + result);
116
87
break ;
117
88
}
118
89
default :
@@ -127,7 +98,7 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
127
98
FPrintF (stderr, " Invalid value for %s\n " , it->first .c_str ());
128
99
return ParseResult::InvalidContent;
129
100
}
130
- flags_ .push_back (it->first + " =" + result);
101
+ node_options_ .push_back (it->first + " =" + result);
131
102
break ;
132
103
}
133
104
case options_parser::OptionType::kInteger : {
@@ -136,7 +107,7 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
136
107
FPrintF (stderr, " Invalid value for %s\n " , it->first .c_str ());
137
108
return ParseResult::InvalidContent;
138
109
}
139
- flags_ .push_back (it->first + " =" + std::to_string (result));
110
+ node_options_ .push_back (it->first + " =" + std::to_string (result));
140
111
break ;
141
112
}
142
113
case options_parser::OptionType::kHostPort :
@@ -146,7 +117,7 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
146
117
FPrintF (stderr, " Invalid value for %s\n " , it->first .c_str ());
147
118
return ParseResult::InvalidContent;
148
119
}
149
- flags_ .push_back (it->first + " =" + std::to_string (result));
120
+ node_options_ .push_back (it->first + " =" + std::to_string (result));
150
121
break ;
151
122
}
152
123
case options_parser::OptionType::kNoOp : {
@@ -170,26 +141,74 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
170
141
return ParseResult::InvalidContent;
171
142
}
172
143
}
144
+ return ParseResult::Valid;
145
+ }
146
+
147
+ ParseResult ConfigReader::ParseConfig (const std::string_view& config_path) {
148
+ std::string file_content;
149
+ // Read the configuration file
150
+ int r = ReadFileSync (&file_content, config_path.data ());
151
+ if (r != 0 ) {
152
+ const char * err = uv_strerror (r);
153
+ FPrintF (
154
+ stderr, " Cannot read configuration from %s: %s\n " , config_path, err);
155
+ return ParseResult::FileError;
156
+ }
157
+
158
+ // Parse the configuration file
159
+ simdjson::ondemand::parser json_parser;
160
+ simdjson::ondemand::document document;
161
+ if (json_parser.iterate(file_content).get (document)) {
162
+ FPrintF (stderr, " Can't parse %s\n " , config_path.data ());
163
+ return ParseResult::InvalidContent;
164
+ }
165
+
166
+ simdjson::ondemand::object main_object;
167
+ // If document is not an object, throw an error.
168
+ if (auto root_error = document.get_object ().get (main_object)) {
169
+ if (root_error == simdjson::error_code::INCORRECT_TYPE) {
170
+ FPrintF (stderr,
171
+ " Root value unexpected not an object for %s\n\n " ,
172
+ config_path.data ());
173
+ } else {
174
+ FPrintF (stderr, " Can't parse %s\n " , config_path.data ());
175
+ }
176
+ return ParseResult::InvalidContent;
177
+ }
178
+
179
+ simdjson::ondemand::object node_options_object;
180
+ // If "nodeOptions" is an object, parse it
181
+ if (auto node_options_error =
182
+ main_object[" nodeOptions" ].get_object ().get (node_options_object)) {
183
+ if (node_options_error != simdjson::error_code::NO_SUCH_FIELD) {
184
+ FPrintF (stderr,
185
+ " \" nodeOptions\" value unexpected for %s\n\n " ,
186
+ config_path.data ());
187
+ return ParseResult::InvalidContent;
188
+ }
189
+ } else {
190
+ return ParseNodeOptions (&node_options_object);
191
+ }
173
192
174
193
return ParseResult::Valid;
175
194
}
176
195
177
196
std::string ConfigReader::AssignNodeOptions () {
178
- if (flags_ .empty ()) {
197
+ if (node_options_ .empty ()) {
179
198
return " " ;
180
199
} else {
181
- DCHECK_GT (flags_ .size (), 0 );
200
+ DCHECK_GT (node_options_ .size (), 0 );
182
201
std::string acc;
183
- acc.reserve (flags_ .size () * 2 );
184
- for (size_t i = 0 ; i < flags_ .size (); ++i) {
202
+ acc.reserve (node_options_ .size () * 2 );
203
+ for (size_t i = 0 ; i < node_options_ .size (); ++i) {
185
204
// The space is necessary at the beginning of the string
186
- acc += " " + flags_ [i];
205
+ acc += " " + node_options_ [i];
187
206
}
188
207
return acc;
189
208
}
190
209
}
191
210
192
211
size_t ConfigReader::GetFlagsSize () {
193
- return flags_ .size ();
212
+ return node_options_ .size ();
194
213
}
195
214
} // namespace node
0 commit comments