blob: ef0dd694006bf0f325ebc017bb0b2f17cd43eb9e [file] [log] [blame]
#include "redfish_entity_trie_node.h"
#include <cstddef>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include "absl/strings/string_view.h"
#include "authorizer_enums.h"
namespace milotic::authz::internal {
// In GetOrDefault, because it serves for creating a new node, we first check
// if there's a exact match for the path, if not, we create a new node for the
// path and return its pointer. Note that we would create at most one node for
// the wildcard.
RedfishEntityTrieNode* RedfishEntityTrieNode::GetOrDefaultChild(
std::string_view path) {
auto it = children_.find(path);
if (it != children_.end()) {
return it->second.get();
}
if (IsWildcard(path)) {
if (collection_child_ != nullptr) {
return collection_child_;
}
// If there is no collection child, create a new one.
auto [inserted_it, _] = children_.insert(
{std::string(path), std::make_unique<RedfishEntityTrieNode>()});
collection_child_ = inserted_it->second.get();
return collection_child_;
}
// If there is no exact match and no wildcard, create a new node for the path
// and return its pointer.
auto [inserted_it, _] = children_.insert(
{std::string(path), std::make_unique<RedfishEntityTrieNode>()});
return inserted_it->second.get();
}
// In GetChild, we first check if there's a exact match for the path. If not, we
// check if there is a wildcard. If it is, we return the wildcard.
RedfishEntityTrieNode* RedfishEntityTrieNode::GetChild(
std::string_view path) const {
auto it = children_.find(path);
if (it != children_.end()) {
return it->second.get();
}
// If there is no exact match, return the collection child if it exists
// otherwise the collection child is a nullptr.
return collection_child_;
}
ecclesia::ResourceEntity RedfishEntityTrieNode::GetEntityTag() const {
return entity_tag_;
}
void RedfishEntityTrieNode::SetEntityTag(ecclesia::ResourceEntity tag) {
entity_tag_ = tag;
}
void RedfishEntityTrieNode::SetNodeIndexInPatternArray(std::size_t index) {
node_index_in_pattern_array_ = index;
}
std::size_t RedfishEntityTrieNode::GetNodeIndexInPatternArray() const {
return node_index_in_pattern_array_;
}
bool RedfishEntityTrieNode::IsWildcard(absl::string_view path) {
return !path.empty() && path.front() == '{';
}
} // namespace milotic::authz::internal