Repository: casbin/casbin
Pull request #464 (2020-05-19 13:01:22)

fix: support comment after effect and matcher
Mentioned #453

coveralls: 
[![Coverage Status](https://coveralls.io/builds/30887628/badge)](https://coveralls.io/builds/30887628)

Coverage increased (+0.05%) to 74.657% when pulling **3883b6868300614fc5dd9b67e05b9b4060311913 on dovics:comment** into **0aab1e445eb0e74491982b53c2d63c57880812c4 on casbin:master**.

nodece: @dovics The commit type should be `fix`.


Patch:
diff --git a/config/config.go b/config/config.go
index 4a2a5c6..529fc46 100644
--- a/config/config.go
+++ b/config/config.go
@@ -155,7 +155,14 @@ func (c *Config) parseBuffer(buf *bufio.Reader) error {
 				canWrite = true
 			}
 
-			if _, err := buffer.Write(p); err != nil {
+			end := len(p)
+			for i, value := range p {
+				if value == DEFAULT_COMMENT[0] || value == DEFAULT_COMMENT_SEM[0] {
+					end = i
+					break
+				}
+			}
+			if _, err := buffer.Write(p[:end]); err != nil {
 				return err
 			}
 		}
diff --git a/examples/comment_model.conf b/examples/comment_model.conf
new file mode 100644
index 0000000..a4200eb
--- /dev/null
+++ b/examples/comment_model.conf
@@ -0,0 +1,12 @@
+[request_definition]
+r = sub, obj, act ; Request definition
+
+[policy_definition]
+p = sub, obj, act 
+
+[policy_effect]
+e = some(where (p.eft == allow)) # This is policy effect.
+
+# Matchers
+[matchers]
+m = r.sub == p.sub && r.obj == p.obj && r.act == p.act  
\ No newline at end of file


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index 3e10c5c..da8a9d7 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -44,7 +44,12 @@ func TestGet(t *testing.T) {
 	if v := config.String("mysql::mysql.master.host"); v != "10.0.0.1" {
 		t.Errorf("Get failure: expected different value for mysql::mysql.master.host (expected: [%#v] got: [%#v])", "10.0.0.1", v)
 	}
-
+	if v := config.String("mysql::mysql.master.user"); v != "root" {
+		t.Errorf("Get failure: expected different value for mysql::mysql.master.user (expected: [%#v] got: [%#v])", "root", v)
+	}
+	if v := config.String("mysql::mysql.master.pass"); v != "89dds)2$" {
+		t.Errorf("Get failure: expected different value for mysql::mysql.master.pass (expected: [%#v] got: [%#v])", "89dds)2$", v)
+	}
 	// math::key test
 	if v, err := config.Int64("math::math.i64"); err != nil || v != 64 {
 		t.Errorf("Get failure: expected different value for math::math.i64 (expected: [%#v] got: [%#v])", 64, v)
diff --git a/config/testdata/testini.ini b/config/testdata/testini.ini
index 58946c2..cbb9bab 100644
--- a/config/testdata/testini.ini
+++ b/config/testdata/testini.ini
@@ -13,8 +13,8 @@ mysql.dev.user = root
 mysql.dev.pass = 123456
 mysql.dev.db = test
 
-mysql.master.host = 10.0.0.1
-mysql.master.user = root
+mysql.master.host = 10.0.0.1 # host # 10.0.0.1
+mysql.master.user = root ; user name
 mysql.master.pass = 89dds)2$#d
 mysql.master.db = act
 
diff --git a/model_test.go b/model_test.go
index bd631f0..c18cff1 100644
--- a/model_test.go
+++ b/model_test.go
@@ -560,3 +560,16 @@ func TestABACPolicy(t *testing.T) {
 	testEnforce(t, e, sub3, "/data1", "write", false)
 	testEnforce(t, e, sub3, "/data2", "write", false)
 }
+
+func TestCommentModel(t *testing.T) {
+	e, _ := NewEnforcer("examples/comment_model.conf", "examples/basic_policy.csv")
+
+	testEnforce(t, e, "alice", "data1", "read", true)
+	testEnforce(t, e, "alice", "data1", "write", false)
+	testEnforce(t, e, "alice", "data2", "read", false)
+	testEnforce(t, e, "alice", "data2", "write", false)
+	testEnforce(t, e, "bob", "data1", "read", false)
+	testEnforce(t, e, "bob", "data1", "write", false)
+	testEnforce(t, e, "bob", "data2", "read", false)
+	testEnforce(t, e, "bob", "data2", "write", true)
+}