MCPcopy Create free account
hub / github.com/chigraph/chigraph / jsonToGraphFunction

Function jsonToGraphFunction

libchigraph/src/JsonDeserializer.cpp:230–446  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

228}
229
230Result jsonToGraphFunction(GraphFunction& createInside, const nlohmann::json& input) {
231 Result res;
232
233 // read the local variables
234 if (input.find("local_variables") == input.end() || !input["local_variables"].is_object()) {
235 res.addEntry("E45", "JSON in graph doesn't have a local_variables object", {});
236
237 return res;
238 }
239
240 for (auto localiter = input["local_variables"].begin();
241 localiter != input["local_variables"].end(); ++localiter) {
242 std::string localName = localiter.key();
243
244 if (!localiter.value().is_string()) {
245 res.addEntry("E46", "Local variable vaue in json wasn't a string",
246 {{"Given local variable json", localiter.value()}});
247
248 continue;
249 }
250
251 // parse the type names
252 std::string qualifiedType = localiter.value();
253
254 std::string moduleName, typeName;
255 std::tie(moduleName, typeName) = parseColonPair(qualifiedType);
256
257 DataType ty;
258 res += createInside.context().typeFromModule(moduleName, typeName, &ty);
259
260 if (!res) { continue; }
261
262 createInside.getOrCreateLocalVariable(localName, ty);
263 }
264
265 // read the nodes
266 if (input.find("nodes") == input.end() || !input["nodes"].is_object()) {
267 res.addEntry("E5", "JSON in graph doesn't have nodes object", {});
268 return res;
269 }
270
271 for (auto nodeiter = input["nodes"].begin(); nodeiter != input["nodes"].end(); ++nodeiter) {
272 auto node = nodeiter.value();
273 std::string nodeid = nodeiter.key();
274 if (node.find("type") == node.end() || !node.find("type")->is_string()) {
275 res.addEntry("E6", R"(Node doesn't have a "type" string)", {{"Node ID", nodeid}});
276 return res;
277 }
278 std::string fullType = node["type"];
279 std::string moduleName, typeName;
280 std::tie(moduleName, typeName) = parseColonPair(fullType);
281
282 if (moduleName.empty() || typeName.empty()) {
283 res.addEntry("E7", "Incorrect qualified module name (should be module:type)",
284 {{"Node ID", nodeid}, {"Requested Qualified Name", fullType}});
285 return res;
286 }
287

Callers 2

mainFunction · 0.85
jsonToGraphModuleFunction · 0.85

Calls 15

parseColonPairFunction · 0.85
connectDataFunction · 0.85
connectExecFunction · 0.85
endMethod · 0.80
is_objectMethod · 0.80
addEntryMethod · 0.80
beginMethod · 0.80
is_stringMethod · 0.80
typeFromModuleMethod · 0.80
nodeTypeFromModuleMethod · 0.80
is_arrayMethod · 0.80

Tested by

no test coverage detected