Skip to content

Commit 8576493

Browse files
authored
Remove JSON from PipelineReaderJSON (PDAL#4727)
* Remove private functions from stac reader header. * Move interface functions out of EsriReader. * Make writeHierarchy a non-member. * Add missed file. * Move functions/data to Private. * Remove handleReaderArgs() and setReaderOptions() from header. * Move functions to cpp file.
1 parent 986f6e5 commit 8576493

File tree

2 files changed

+143
-152
lines changed

2 files changed

+143
-152
lines changed

pdal/PipelineReaderJSON.cpp

Lines changed: 143 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@
4949
namespace pdal
5050
{
5151

52-
PipelineReaderJSON::PipelineReaderJSON(PipelineManager& manager) :
53-
m_manager(manager)
54-
{}
52+
using TagMap = std::map<std::string, Stage *>;
53+
54+
namespace
55+
{
5556

57+
std::string extractType(NL::json& node);
58+
std::string extractTag(NL::json& node, TagMap& tags);
59+
FileSpec extractFilename(NL::json& node);
60+
std::vector<Stage *> extractInputs(NL::json& node, TagMap& tags);
61+
Options extractOptions(NL::json& node);
62+
bool extractOption(Options& options, const std::string& name, const NL::json& node);
63+
void handleInputTag(const std::string& tag, const TagMap& tags, std::vector<Stage *>& inputs);
5664

57-
void PipelineReaderJSON::parsePipeline(NL::json& tree)
65+
void parsePipeline(NL::json& tree, PipelineManager& manager)
5866
{
5967
TagMap tags;
6068
std::vector<Stage*> inputs;
@@ -101,7 +109,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
101109
{
102110
spec.m_path = path;
103111
ReaderCreationOptions ops { spec, type, nullptr, options, tag };
104-
s = &m_manager.makeReader(ops);
112+
s = &manager.makeReader(ops);
105113

106114
if (specifiedInputs.size())
107115
throw pdal_error("JSON pipeline: Inputs not permitted for "
@@ -112,7 +120,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
112120
else if (type.empty() || Utils::startsWith(type, "writers."))
113121
{
114122
StageCreationOptions ops { spec.m_path.string(), type, nullptr, options, tag };
115-
s = &m_manager.makeWriter(ops);
123+
s = &manager.makeWriter(ops);
116124
for (Stage *ts : inputs)
117125
s->setInput(*ts);
118126
inputs.clear();
@@ -123,7 +131,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
123131
if (spec.valid())
124132
options.add("filename", spec.m_path.string());
125133
StageCreationOptions ops { "", type, nullptr, options, tag };
126-
s = &m_manager.makeFilter(ops);
134+
s = &manager.makeFilter(ops);
127135
for (Stage *ts : inputs)
128136
s->setInput(*ts);
129137
inputs.clear();
@@ -136,10 +144,10 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
136144
}
137145

138146
// Tell user if the pipeline seems wacky.
139-
const std::vector<Stage *> llist = m_manager.leaves();
147+
const std::vector<Stage *> llist = manager.leaves();
140148
if (llist.size() > 1)
141149
{
142-
const LogPtr& log = m_manager.log();
150+
const LogPtr& log = manager.log();
143151
log->get(LogLevel::Error) << "Pipeline has multiple leaf nodes.\n";
144152
log->get(LogLevel::Error) << "Only the first of the following leaf nodes will be run.\n";
145153
for (Stage *s : llist)
@@ -150,99 +158,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
150158
}
151159
}
152160

153-
154-
void PipelineReaderJSON::readPipeline(std::istream& input)
155-
{
156-
NL::json root;
157-
158-
try
159-
{
160-
root = NL::json::parse(input, /* callback */ nullptr,
161-
/* allow exceptions */ true,
162-
/* ignore_comments */ true);
163-
}
164-
catch (NL::json::parse_error& err)
165-
{
166-
// Look for a right bracket -- this indicates the start of the
167-
// actual message from the parse error.
168-
std::string s(err.what());
169-
auto pos = s.find("]");
170-
if (pos != std::string::npos)
171-
s = s.substr(pos + 1);
172-
throw pdal_error("Pipeline:" + s);
173-
}
174-
175-
auto it = root.find("pipeline");
176-
if (root.is_object() && it != root.end())
177-
parsePipeline(*it);
178-
else if (root.is_array())
179-
parsePipeline(root);
180-
else
181-
throw pdal_error("Pipeline: root element is not a pipeline.");
182-
}
183-
184-
185-
void PipelineReaderJSON::readPipeline(const std::string& filename)
186-
{
187-
std::istream* input = Utils::openFile(filename);
188-
if (!input)
189-
{
190-
throw pdal_error("Pipeline: Unable to open stream for "
191-
"file \"" + filename + "\"");
192-
}
193-
194-
try
195-
{
196-
readPipeline(*input);
197-
}
198-
catch (...)
199-
{
200-
Utils::closeFile(input);
201-
throw;
202-
}
203-
204-
Utils::closeFile(input);
205-
}
206-
207-
208-
std::string PipelineReaderJSON::extractType(NL::json& node)
209-
{
210-
std::string type;
211-
212-
auto it = node.find("type");
213-
if (it != node.end())
214-
{
215-
NL::json& val = *it;
216-
if (!val.is_null())
217-
{
218-
if (val.is_string())
219-
type = val.get<std::string>();
220-
else
221-
throw pdal_error("JSON pipeline: 'type' must be specified as "
222-
"a string.");
223-
}
224-
node.erase(it);
225-
}
226-
return type;
227-
}
228-
229-
230-
FileSpec PipelineReaderJSON::extractFilename(NL::json& node)
231-
{
232-
FileSpec spec;
233-
234-
auto it = node.find("filename");
235-
if (it == node.end())
236-
return spec;
237-
238-
Utils::StatusWithReason status = spec.parse(*it);
239-
if (!status)
240-
throw pdal_error(status.what());
241-
node.erase(it);
242-
return spec;
243-
}
244-
245-
std::string PipelineReaderJSON::extractTag(NL::json& node, TagMap& tags)
161+
std::string extractTag(NL::json& node, TagMap& tags)
246162
{
247163
std::string tag;
248164

@@ -273,21 +189,22 @@ std::string PipelineReaderJSON::extractTag(NL::json& node, TagMap& tags)
273189
return tag;
274190
}
275191

276-
277-
void PipelineReaderJSON::handleInputTag(const std::string& tag,
278-
const TagMap& tags, std::vector<Stage *>& inputs)
192+
FileSpec extractFilename(NL::json& node)
279193
{
280-
auto ii = tags.find(tag);
281-
if (ii == tags.end())
282-
throw pdal_error("JSON pipeline: Invalid pipeline: "
283-
"undefined stage tag '" + tag + "'.");
284-
else
285-
inputs.push_back(ii->second);
286-
}
194+
FileSpec spec;
195+
196+
auto it = node.find("filename");
197+
if (it == node.end())
198+
return spec;
287199

200+
Utils::StatusWithReason status = spec.parse(*it);
201+
if (!status)
202+
throw pdal_error(status.what());
203+
node.erase(it);
204+
return spec;
205+
}
288206

289-
std::vector<Stage *> PipelineReaderJSON::extractInputs(NL::json& node,
290-
TagMap& tags)
207+
std::vector<Stage *> extractInputs(NL::json& node, TagMap& tags)
291208
{
292209
std::vector<Stage *> inputs;
293210
std::string filename;
@@ -316,34 +233,7 @@ std::vector<Stage *> PipelineReaderJSON::extractInputs(NL::json& node,
316233
return inputs;
317234
}
318235

319-
namespace
320-
{
321-
322-
bool extractOption(Options& options, const std::string& name,
323-
const NL::json& node)
324-
{
325-
if (node.is_string())
326-
options.add(name, node.get<std::string>());
327-
else if (node.is_number_unsigned())
328-
options.add(name, node.get<uint64_t>());
329-
else if (node.is_number_integer())
330-
options.add(name, node.get<int64_t>());
331-
else if (node.is_number_float())
332-
options.add(name, node.get<double>());
333-
else if (node.is_boolean())
334-
options.add(name, node.get<bool>());
335-
else if (node.is_array())
336-
options.add(name, node.get<NL::json::array_t>());
337-
else if (node.is_null())
338-
options.add(name, "");
339-
else
340-
return false;
341-
return true;
342-
}
343-
344-
} // unnamed namespace
345-
346-
Options PipelineReaderJSON::extractOptions(NL::json& node)
236+
Options extractOptions(NL::json& node)
347237
{
348238
Options options;
349239

@@ -380,4 +270,115 @@ Options PipelineReaderJSON::extractOptions(NL::json& node)
380270
return options;
381271
}
382272

273+
std::string extractType(NL::json& node)
274+
{
275+
std::string type;
276+
277+
auto it = node.find("type");
278+
if (it != node.end())
279+
{
280+
NL::json& val = *it;
281+
if (!val.is_null())
282+
{
283+
if (val.is_string())
284+
type = val.get<std::string>();
285+
else
286+
throw pdal_error("JSON pipeline: 'type' must be specified as "
287+
"a string.");
288+
}
289+
node.erase(it);
290+
}
291+
return type;
292+
}
293+
294+
bool extractOption(Options& options, const std::string& name, const NL::json& node)
295+
{
296+
if (node.is_string())
297+
options.add(name, node.get<std::string>());
298+
else if (node.is_number_unsigned())
299+
options.add(name, node.get<uint64_t>());
300+
else if (node.is_number_integer())
301+
options.add(name, node.get<int64_t>());
302+
else if (node.is_number_float())
303+
options.add(name, node.get<double>());
304+
else if (node.is_boolean())
305+
options.add(name, node.get<bool>());
306+
else if (node.is_array())
307+
options.add(name, node.get<NL::json::array_t>());
308+
else if (node.is_null())
309+
options.add(name, "");
310+
else
311+
return false;
312+
return true;
313+
}
314+
315+
void handleInputTag(const std::string& tag, const TagMap& tags, std::vector<Stage *>& inputs)
316+
{
317+
auto ii = tags.find(tag);
318+
if (ii == tags.end())
319+
throw pdal_error("JSON pipeline: Invalid pipeline: "
320+
"undefined stage tag '" + tag + "'.");
321+
else
322+
inputs.push_back(ii->second);
323+
}
324+
325+
326+
} // unnamed namespace
327+
328+
PipelineReaderJSON::PipelineReaderJSON(PipelineManager& manager) :
329+
m_manager(manager)
330+
{}
331+
332+
void PipelineReaderJSON::readPipeline(const std::string& filename)
333+
{
334+
std::istream* input = Utils::openFile(filename);
335+
if (!input)
336+
{
337+
throw pdal_error("Pipeline: Unable to open stream for "
338+
"file \"" + filename + "\"");
339+
}
340+
341+
try
342+
{
343+
readPipeline(*input);
344+
}
345+
catch (...)
346+
{
347+
Utils::closeFile(input);
348+
throw;
349+
}
350+
351+
Utils::closeFile(input);
352+
}
353+
354+
void PipelineReaderJSON::readPipeline(std::istream& input)
355+
{
356+
NL::json root;
357+
358+
try
359+
{
360+
root = NL::json::parse(input, /* callback */ nullptr,
361+
/* allow exceptions */ true,
362+
/* ignore_comments */ true);
363+
}
364+
catch (NL::json::parse_error& err)
365+
{
366+
// Look for a right bracket -- this indicates the start of the
367+
// actual message from the parse error.
368+
std::string s(err.what());
369+
auto pos = s.find("]");
370+
if (pos != std::string::npos)
371+
s = s.substr(pos + 1);
372+
throw pdal_error("Pipeline:" + s);
373+
}
374+
375+
auto it = root.find("pipeline");
376+
if (root.is_object() && it != root.end())
377+
parsePipeline(*it, m_manager);
378+
else if (root.is_array())
379+
parsePipeline(root, m_manager);
380+
else
381+
throw pdal_error("Pipeline: root element is not a pipeline.");
382+
}
383+
383384
} // namespace pdal

pdal/PipelineReaderJSON.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,8 @@ class PDAL_EXPORT PipelineReaderJSON
6161
PipelineReaderJSON& operator=(const PipelineReaderJSON&) = delete;
6262
PipelineReaderJSON(const PipelineReaderJSON&) = delete;
6363

64-
typedef std::map<std::string, Stage *> TagMap;
65-
66-
void parsePipeline(NL::json&);
6764
void readPipeline(const std::string& filename);
6865
void readPipeline(std::istream& input);
69-
std::string extractType(NL::json& node);
70-
FileSpec extractFilename(NL::json& node);
71-
std::string extractTag(NL::json& node, TagMap& tags);
72-
std::vector<Stage *> extractInputs(NL::json& node, TagMap& tags);
73-
Options extractOptions(NL::json& node);
74-
void handleInputTag(const std::string& tag, const TagMap& tags,
75-
std::vector<Stage *>& inputs);
7666

7767
PipelineManager& m_manager;
7868
};

0 commit comments

Comments
 (0)