Skip to content

Commit 02a54a2

Browse files
committed
feat: Streamlined types
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent f00c5d5 commit 02a54a2

File tree

7 files changed

+99
-52
lines changed

7 files changed

+99
-52
lines changed

casbin/abac_data.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,22 @@
2424

2525
namespace casbin {
2626

27-
/**
28-
* @brief Get casbin::ABACData object
29-
*
30-
* @param attribs Should be of the format: {
31-
* { "attrib_name1", value1 },
32-
* { "attrib_name2", value2 },
33-
* ...
34-
* }
35-
*
36-
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
37-
* @return Pointer to casbin::ABACData entity
38-
*/
39-
const std::shared_ptr<ABACData> GetData(const ABACData::VariantMap& attribs) {
27+
const std::shared_ptr<ABACData> GetDataObject(const AttributeMap& attribs) {
4028
return std::make_shared<ABACData>(attribs);
4129
}
4230

43-
ABACData::ABACData(const VariantMap& attrib)
44-
: m_attributes(attrib)
31+
ABACData::ABACData(const AttributeMap& attribs)
32+
: m_attributes(std::move(attribs))
4533
{}
4634

47-
bool ABACData::AddAttribute(const std::string& key, const VariantType& value) {
35+
bool ABACData::AddAttribute(const std::string& key, const AttributeValue& value) {
4836
m_attributes[key] = value;
4937
return true;
5038
}
5139

52-
bool ABACData::AddAttributes(const VariantMap& attribs) {
53-
for(auto attrib : attribs) {
54-
m_attributes[attrib.first] = attrib.second;
55-
}
40+
bool ABACData::AddAttributes(const AttributeList& attribs) {
41+
for(auto [name, value] : attribs)
42+
m_attributes[name] = value;
5643
return true;
5744
}
5845

@@ -68,12 +55,12 @@ bool ABACData::DeleteAttribute(const std::string& key) {
6855
return true;
6956
}
7057

71-
bool ABACData::UpdateAttribute(const std::string& key, const VariantType& value) {
58+
bool ABACData::UpdateAttribute(const std::string& key, const AttributeValue& value) {
7259
m_attributes[key] = value;
7360
return true;
7461
}
7562

76-
const ABACData::VariantMap& ABACData::GetAttributes() {
63+
const AttributeMap& ABACData::GetAttributes() {
7764
return m_attributes;
7865
}
7966

casbin/abac_data.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
#ifndef ABAC_H
1818
#define ABAC_H
1919

20-
#include <unordered_map>
21-
#include <string>
22-
#include <vector>
23-
#include <variant>
24-
#include <memory>
20+
#include "attribute_types.h"
2521

2622
namespace casbin {
2723

@@ -31,16 +27,10 @@ namespace casbin {
3127
*/
3228
class ABACData {
3329

34-
public:
35-
36-
// Intrinsic definitions
37-
typedef std::variant<std::string, int32_t, float> VariantType;
38-
typedef std::unordered_map<std::string, VariantType> VariantMap;
39-
4030
private:
4131

4232
// HashMap containing attributes as key-value pairs
43-
VariantMap m_attributes;
33+
AttributeMap m_attributes;
4434

4535
public:
4636
/**
@@ -54,15 +44,15 @@ class ABACData {
5444
*
5545
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
5646
*/
57-
ABACData(const VariantMap& attribs);
47+
ABACData(const AttributeMap& attribs);
5848
/**
5949
* @brief Add attribute to the corresponding ABAC entity
6050
*
6151
* @param key Name of the attribute
6252
* @param value Value of the attribute
6353
* @return true when attribute is added successfully, false otherwise
6454
*/
65-
bool AddAttribute(const std::string& key, const VariantType& value);
55+
bool AddAttribute(const std::string& key, const AttributeValue& value);
6656
/**
6757
* @brief Add attributes to the corresponding ABAC entity
6858
*
@@ -75,7 +65,7 @@ class ABACData {
7565
* Key's type is std::string and value's type can be one of std::string, int32_t, and float only
7666
* @return true if attributes are added successfully, false otherwise
7767
*/
78-
bool AddAttributes(const VariantMap& attribs);
68+
bool AddAttributes(const AttributeList& attribs);
7969
/**
8070
* @brief Delete attribute of the corresponding ABAC entity
8171
*
@@ -92,19 +82,31 @@ class ABACData {
9282
* @return true
9383
* @return false
9484
*/
95-
bool UpdateAttribute(const std::string& key, const VariantType& value);
85+
bool UpdateAttribute(const std::string& key, const AttributeValue& value);
9686
/**
9787
* @brief Get the Attributes of the corresponding ABAC entity
9888
*
9989
* @return const reference to the hashmap containing attributes in key-value pairs
10090
*/
101-
const VariantMap& GetAttributes();
91+
const AttributeMap& GetAttributes();
10292
};
10393

10494
// Casbin ABAC entity type
10595
typedef ABACData ABACData;
10696

107-
const std::shared_ptr<ABACData> GetData(const ABACData::VariantMap& attribs);
97+
/**
98+
* @brief Get casbin::ABACData object
99+
*
100+
* @param attribs Should be of the format: {
101+
* { "attrib_name1", value1 },
102+
* { "attrib_name2", value2 },
103+
* ...
104+
* }
105+
*
106+
* Key's type is std::string and value's type can be one of std::string, int32_t, double, and float only
107+
* @return Pointer to casbin::ABACData entity
108+
*/
109+
const std::shared_ptr<ABACData> GetDataObject(const AttributeMap& attribs);
108110
}
109111

110112
#endif

casbin/attribute_types.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <variant>
18+
#include <vector>
19+
#include <initializer_list>
20+
#include <unordered_map>
21+
#include <memory>
22+
23+
namespace casbin {
24+
25+
typedef std::variant<std::string, int32_t, float, double> AttributeValue;
26+
typedef std::pair<std::string, AttributeValue> Attribute;
27+
typedef std::vector<Attribute> AttributeVector;
28+
typedef std::initializer_list<Attribute> AttributeList;
29+
typedef std::unordered_map<std::string, AttributeValue> AttributeMap;
30+
31+
} // namespace casbin

casbin/data_types.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <variant>
18+
#include <vector>
19+
#include <initializer_list>
20+
#include <unordered_map>
21+
#include "abac_data.h"
22+
23+
namespace casbin {
24+
typedef std::variant<std::string, std::shared_ptr<ABACData>> Data;
25+
typedef std::vector<Data> DataVector;
26+
typedef std::initializer_list<Data> DataList;
27+
typedef std::unordered_map<std::string, Data> DataMap;
28+
}

casbin/model/model.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ void Model::LoadModelFromConfig(std::shared_ptr<ConfigInterface> cfg) {
4444
LoadSection(this, cfg, it->first);
4545

4646
std::vector<std::string> ms;
47-
for(int i=0 ; i < required_sections.size() ; i++)
48-
if(!this->HasSection(required_sections[i]))
49-
ms.push_back(section_name_map[required_sections[i]]);
47+
for(const auto& required_section : required_sections)
48+
if(!this->HasSection(required_section))
49+
ms.push_back(section_name_map[required_section]);
5050

5151
if(ms.size() > 0)
5252
throw MissingRequiredSections("missing required sections: " + Join(ms, ","));
@@ -70,11 +70,7 @@ void Model::LoadSection(Model* model, std::shared_ptr<ConfigInterface> cfg, cons
7070
std::string Model ::GetKeySuffix(int i) {
7171
if (i == 1)
7272
return "";
73-
std::stringstream ss;
74-
ss<<i;
75-
std::string s;
76-
ss>>s;
77-
return s;
73+
return std::to_string(i);
7874
}
7975

8076
bool Model::LoadAssertion(Model* model, std::shared_ptr<ConfigInterface> cfg, const std::string& sec, const std::string& key) {
@@ -92,8 +88,8 @@ bool Model::AddDef(const std::string& sec, const std::string& key, const std::st
9288
ast->value = value;
9389
if (sec == "r" || sec == "p") {
9490
ast->tokens = Split(ast->value, ",");
95-
for (int i = 0; i < ast->tokens.size() ; i++)
96-
ast->tokens[i] = key + "_" + Trim(ast->tokens[i]);
91+
for (auto& token : ast->tokens)
92+
token = key + "_" + Trim(token);
9793
}
9894
else
9995
ast->value = RemoveComments(ast->value);

casbin/pch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@
3838
#include <future>
3939
#include <condition_variable>
4040

41+
#include "attribute_types.h"
42+
#include "data_types.h"
43+
4144

4245
#endif //PCH_H

tests/enforcer_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ TEST(TestEnforcer, TestMapParams) {
9797
}
9898

9999
TEST(TestEnforcer, ABACData) {
100-
casbin::ABACData::VariantMap params = {
100+
casbin::AttributeMap params = {
101101
{ "Name", "Yash" },
102102
{ "Grade", 8.6f },
103103
{ "Age", 18 },
104104
};
105105

106-
auto data = casbin::GetData(params);
106+
auto data = casbin::GetDataObject(params);
107107
ASSERT_TRUE(params == data->GetAttributes());
108108

109109
data->DeleteAttribute("Name");

0 commit comments

Comments
 (0)