Skip to content

Commit

Permalink
allow empty lists for connection_overrides in simulation config (#266)
Browse files Browse the repository at this point in the history
* due to a quirk with how nlohmann::json flatten()/unflatten() work,
  empty arrays aren't preserved, thus our datatype check was failing to
  allow empty arrays.  Work around this
* hat tip to anilbey for noticing: #262 (comment)
  • Loading branch information
mgeplf authored May 5, 2023
1 parent 504f19b commit fa33ccd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 15 additions & 1 deletion python/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,20 @@ def test_expanded_json(self):
config = json.loads(self.config.expanded_json)
self.assertEqual(config['output']['output_dir'], 'some/path/output')

def test_empty_connection_overrides(self):
contents = """
{
"manifest": {
"$CIRCUIT_DIR": "./circuit"
},
"network": "$CIRCUIT_DIR/circuit_config.json",
"run": { "random_seed": 12345, "dt": 0.05, "tstop": 1000 },
"connection_overrides": []
}
"""
conf = SimulationConfig(contents, "./")
self.assertEqual(conf.connection_overrides(), [])

def test_run(self):
contents = """
{
Expand Down Expand Up @@ -557,7 +571,7 @@ def test_simulation_config_failures(self):
},
"network": "$CIRCUIT_DIR/circuit_config.json",
"run": { "random_seed": 12345, "dt": 0.05, "tstop": 1000 },
"connection_overrides": { }
"connection_overrides": {"foo": "bar"}
}
"""
SimulationConfig(contents, "./")
Expand Down
7 changes: 6 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,12 @@ class SimulationConfig::Parser
std::vector<ConnectionOverride> result;

const auto connIt = _json.find("connection_overrides");
if (connIt == _json.end()) {
// nlohmann::json::flatten().unflatten() converts empty containers to `null`:
// https://json.nlohmann.me/api/basic_json/unflatten/#notes
// so we can't tell the difference between {} and []; however, since these are
// empty, we will assume the intent was to have no connection_overrides and forgo
// better error reporting
if (connIt == _json.end() || connIt->is_null()) {
return result;
}

Expand Down

0 comments on commit fa33ccd

Please sign in to comment.