Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIFI-13517 Fix: expand nested vars #95

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 60 additions & 34 deletions src/APConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,48 +79,74 @@ namespace OpenWifi {
return false;
}

void APConfig::ReplaceNestedVariables(const std::string uuid, Poco::JSON::Object &Result) {
/*
Helper method contains code previously in ReplaceVariablesinObject.
Once the top-level variable is resolved, this will be called to resolve any
variables nested within the top-level variable.
*/
ProvObjects::VariableBlock VB;
if (StorageService()->VariablesDB().GetRecord("id", uuid, VB)) {
for (const auto &var: VB.variables) {
Poco::JSON::Parser P;
auto VariableBlockInfo =
P.parse(var.value).extract<Poco::JSON::Object::Ptr>();
auto VarNames = VariableBlockInfo->getNames();
for (const auto &j: VarNames) {
if(VariableBlockInfo->isArray(j)) {
auto Elements = VariableBlockInfo->getArray(j);
if(Elements->size()>0) {
Poco::JSON::Array InnerArray;
ReplaceVariablesInArray(*Elements, InnerArray);
Result.set(j, InnerArray);
} else {
// std::cout << "Empty Array!!!" << std::endl;
}
} else if(VariableBlockInfo->isObject(j)) {
Poco::JSON::Object InnerEval;
auto O = VariableBlockInfo->getObject(j);
ReplaceVariablesInObject(*O,InnerEval);
Result.set(j, InnerEval);
} else {
Result.set(j, VariableBlockInfo->get(j));
}
}
}
}
}

bool APConfig::ReplaceVariablesInObject(const Poco::JSON::Object &Original,
Poco::JSON::Object &Result) {
// get all the names and expand
auto Names = Original.getNames();
for (const auto &i : Names) {
if (i == "__variableBlock") {
if (Original.isArray(i)) {
/*
E.g. of what the variable block would look like in an array:
"ssids": [
{
"__variableBlock": [
"79c083d2-d496-4de0-8600-76a63556851b"
]
}
]
*/
auto UUIDs = Original.getArray(i);
for (const auto &uuid: *UUIDs) {
ProvObjects::VariableBlock VB;
if (StorageService()->VariablesDB().GetRecord("id", uuid, VB)) {
for (const auto &var: VB.variables) {
Poco::JSON::Parser P;
auto VariableBlockInfo =
P.parse(var.value).extract<Poco::JSON::Object::Ptr>();
auto VarNames = VariableBlockInfo->getNames();
for (const auto &j: VarNames) {
// std::cout << "Name: " << j << std::endl;
if(VariableBlockInfo->isArray(j)) {
auto Elements = VariableBlockInfo->getArray(j);
if(Elements->size()>0) {
Poco::JSON::Array InnerArray;
ReplaceVariablesInArray(*Elements, InnerArray);
Result.set(j, InnerArray);
// std::cout << "Array!!!" << std::endl;
} else {
// std::cout << "Empty Array!!!" << std::endl;
}
} else if(VariableBlockInfo->isObject(j)) {
Poco::JSON::Object InnerEval;
// std::cout << "Visiting object " << j << std::endl;
auto O = VariableBlockInfo->getObject(j);
ReplaceVariablesInObject(*O,InnerEval);
Result.set(j, InnerEval);
} else {
Result.set(j, VariableBlockInfo->get(j));
}
}
}
}
}
for (const std::string &uuid: *UUIDs) {
ReplaceNestedVariables(uuid, Result);
}
}
else {
/*
E.g. of what the variable block would look like replacing an entire json blob:
"services" : {
"__variableBlock": "ef8db4c0-f0ef-40d2-b676-c9c02ef39430"
}
*/
const std::string uuid = Original.get(i);
ReplaceNestedVariables(uuid, Result);
}
} else if (i == "__radiusEndpoint") {
auto EndPointId = Original.get(i).toString();
ProvObjects::RADIUSEndPoint RE;
Expand Down Expand Up @@ -434,4 +460,4 @@ namespace OpenWifi {
} else {
}
}
} // namespace OpenWifi
} // namespace OpenWifi
1 change: 1 addition & 0 deletions src/APConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace OpenWifi {

bool ReplaceVariablesInArray(const Poco::JSON::Array &O,
Poco::JSON::Array &Result);
void ReplaceNestedVariables(const std::string uuid, Poco::JSON::Object &Result);
bool ReplaceVariablesInObject(const Poco::JSON::Object &Original,
Poco::JSON::Object &Result);

Expand Down
Loading