Repository: casbin/casbin
Pull request #1255 (2023-05-23 08:39:30)

fix: include staticcheck Github action and fix errors
* ci: include staticcheck Github action and fix errors

casbin-bot:  @tangyang9464 @JalinWang please review
hsluoyz: @sarroutbi plz fix CI: https://github.com/casbin/casbin/actions/runs/5055180258/jobs/9072551704?pr=1255

![image](https://github.com/casbin/casbin/assets/3787410/15c4a9b9-a448-410a-b145-6bb74230f364)

sarroutbi: > @sarroutbi plz fix CI: https://github.com/casbin/casbin/actions/runs/5055180258/jobs/9072551704?pr=1255
> 
> ![image](https://user-images.githubusercontent.com/3787410/240222703-15c4a9b9-a448-410a-b145-6bb74230f364.png)

done

Patch:
diff --git a/.github/workflows/staticcheck.yaml b/.github/workflows/staticcheck.yaml
new file mode 100644
index 0000000..7cbb6f6
--- /dev/null
+++ b/.github/workflows/staticcheck.yaml
@@ -0,0 +1,22 @@
+---
+name: "staticcheck"
+on:
+  push:
+    branches:
+      - main
+    paths:
+      - '**.go'
+  pull_request:
+    paths:
+      - '**.go'
+jobs:
+  ci:
+    name: "staticcheck"
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v1
+        with:
+          fetch-depth: 1
+      - uses: dominikh/staticcheck-action@v1.2.0
+        with:
+          version: "2022.1.1"
diff --git a/enforcer.go b/enforcer.go
index 277ce42..bacd20c 100644
--- a/enforcer.go
+++ b/enforcer.go
@@ -828,17 +828,17 @@ func (p enforceParameters) Get(name string) (interface{}, error) {
 func generateEvalFunction(functions map[string]govaluate.ExpressionFunction, parameters *enforceParameters) govaluate.ExpressionFunction {
 	return func(args ...interface{}) (interface{}, error) {
 		if len(args) != 1 {
-			return nil, fmt.Errorf("Function eval(subrule string) expected %d arguments, but got %d", 1, len(args))
+			return nil, fmt.Errorf("function eval(subrule string) expected %d arguments, but got %d", 1, len(args))
 		}
 
 		expression, ok := args[0].(string)
 		if !ok {
-			return nil, errors.New("Argument of eval(subrule string) must be a string")
+			return nil, errors.New("argument of eval(subrule string) must be a string")
 		}
 		expression = util.EscapeAssertion(expression)
 		expr, err := govaluate.NewEvaluableExpressionWithFunctions(expression, functions)
 		if err != nil {
-			return nil, fmt.Errorf("Error while parsing eval parameter: %s, %s", expression, err.Error())
+			return nil, fmt.Errorf("error while parsing eval parameter: %s, %s", expression, err.Error())
 		}
 		return expr.Eval(parameters)
 	}
diff --git a/errors/rbac_errors.go b/errors/rbac_errors.go
index 7eca045..2f8974b 100644
--- a/errors/rbac_errors.go
+++ b/errors/rbac_errors.go
@@ -18,13 +18,13 @@ import "errors"
 
 // Global errors for rbac defined here
 var (
-	ERR_NAME_NOT_FOUND            = errors.New("error: name does not exist")
-	ERR_DOMAIN_PARAMETER          = errors.New("error: domain should be 1 parameter")
-	ERR_LINK_NOT_FOUND            = errors.New("error: link between name1 and name2 does not exist")
-	ERR_USE_DOMAIN_PARAMETER      = errors.New("error: useDomain should be 1 parameter")
-	INVALID_FIELDVAULES_PARAMETER = errors.New("fieldValues requires at least one parameter")
+	ErrNameNotFound                = errors.New("error: name does not exist")
+	ErrDomainParameter             = errors.New("error: domain should be 1 parameter")
+	ErrLinkNotFound                = errors.New("error: link between name1 and name2 does not exist")
+	ErrUseDomainParameter          = errors.New("error: useDomain should be 1 parameter")
+	ErrInvalidFieldValuesParameter = errors.New("fieldValues requires at least one parameter")
 
 	// GetAllowedObjectConditions errors
-	ERR_OBJ_CONDITION   = errors.New("need to meet the prefix required by the object condition")
-	ERR_EMPTY_CONDITION = errors.New("GetAllowedObjectConditions have an empty condition")
+	ErrObjCondition   = errors.New("need to meet the prefix required by the object condition")
+	ErrEmptyCondition = errors.New("GetAllowedObjectConditions have an empty condition")
 )
diff --git a/internal_api.go b/internal_api.go
index b14e5b4..d3d5640 100644
--- a/internal_api.go
+++ b/internal_api.go
@@ -227,7 +227,7 @@ func (e *Enforcer) removePoliciesWithoutNotify(sec string, ptype string, rules [
 // removeFilteredPolicy removes rules based on field filters from the current policy.
 func (e *Enforcer) removeFilteredPolicyWithoutNotify(sec string, ptype string, fieldIndex int, fieldValues []string) (bool, error) {
 	if len(fieldValues) == 0 {
-		return false, Err.INVALID_FIELDVAULES_PARAMETER
+		return false, Err.ErrInvalidFieldValuesParameter
 	}
 
 	if e.dispatcher != nil && e.autoNotifyDispatcher {
diff --git a/rbac/default-role-manager/role_manager.go b/rbac/default-role-manager/role_manager.go
index 8939a48..9a42781 100644
--- a/rbac/default-role-manager/role_manager.go
+++ b/rbac/default-role-manager/role_manager.go
@@ -507,7 +507,7 @@ func (dm *DomainManager) getDomain(domains ...string) (domain string, err error)
 	case 1:
 		return domains[0], nil
 	default:
-		return "", errors.ERR_DOMAIN_PARAMETER
+		return "", errors.ErrDomainParameter
 	}
 }
 
diff --git a/rbac_api.go b/rbac_api.go
index 7816101..bc1abb1 100644
--- a/rbac_api.go
+++ b/rbac_api.go
@@ -86,7 +86,7 @@ func (e *Enforcer) DeleteRolesForUser(user string, domain ...string) (bool, erro
 	if len(domain) == 0 {
 		args = []string{user}
 	} else if len(domain) > 1 {
-		return false, errors.ERR_DOMAIN_PARAMETER
+		return false, errors.ErrDomainParameter
 	} else {
 		args = []string{user, "", domain[0]}
 	}
@@ -306,7 +306,7 @@ func (e *Enforcer) GetNamedImplicitPermissionsForUser(ptype string, user string,
 				permission = append(permission, deepCopyPolicy(rule))
 			}
 		} else if len(domain) > 1 {
-			return nil, errors.ERR_DOMAIN_PARAMETER
+			return nil, errors.ErrDomainParameter
 		} else {
 			d := domain[0]
 			matched := rm.Match(d, rule[domainIndex])
@@ -440,14 +440,14 @@ func (e *Enforcer) GetAllowedObjectConditions(user string, action string, prefix
 		// policy {sub, obj, act}
 		if policy[2] == action {
 			if !strings.HasPrefix(policy[1], prefix) {
-				return nil, errors.ERR_OBJ_CONDITION
+				return nil, errors.ErrObjCondition
 			}
 			objectConditions = append(objectConditions, strings.TrimPrefix(policy[1], prefix))
 		}
 	}
 
 	if len(objectConditions) == 0 {
-		return nil, errors.ERR_EMPTY_CONDITION
+		return nil, errors.ErrEmptyCondition
 	}
 
 	return objectConditions, nil
diff --git a/util/builtin_operators.go b/util/builtin_operators.go
index 878a4e1..4171380 100644
--- a/util/builtin_operators.go
+++ b/util/builtin_operators.go
@@ -34,13 +34,13 @@ var (
 // validate the variadic parameter size and type as string
 func validateVariadicArgs(expectedLen int, args ...interface{}) error {
 	if len(args) != expectedLen {
-		return fmt.Errorf("Expected %d arguments, but got %d", expectedLen, len(args))
+		return fmt.Errorf("expected %d arguments, but got %d", expectedLen, len(args))
 	}
 
 	for _, p := range args {
 		_, ok := p.(string)
 		if !ok {
-			return errors.New("Argument must be a string")
+			return errors.New("argument must be a string")
 		}
 	}
 


Test patch:
diff --git a/enforcer_synced_test.go b/enforcer_synced_test.go
index 3704c43..92c7dac 100644
--- a/enforcer_synced_test.go
+++ b/enforcer_synced_test.go
@@ -477,7 +477,7 @@ func testSyncedEnforcerGetUsers(t *testing.T, e *SyncedEnforcer, res []string, n
 	switch err {
 	case nil:
 		break
-	case errors.ERR_NAME_NOT_FOUND:
+	case errors.ErrNameNotFound:
 		t.Log("No name found")
 	default:
 		t.Error("Users for ", name, " could not be fetched: ", err.Error())
diff --git a/rbac_api_test.go b/rbac_api_test.go
index cf42ddd..fd1b9f5 100644
--- a/rbac_api_test.go
+++ b/rbac_api_test.go
@@ -43,7 +43,7 @@ func testGetUsers(t *testing.T, e *Enforcer, res []string, name string, domain .
 	switch err {
 	case nil:
 		break
-	case errors.ERR_NAME_NOT_FOUND:
+	case errors.ErrNameNotFound:
 		t.Log("No name found")
 	default:
 		t.Error("Users for ", name, " could not be fetched: ", err.Error())
@@ -564,15 +564,15 @@ func TestGetAllowedObjectConditions(t *testing.T) {
 	testGetAllowedObjectConditions(t, e, "admin", "read", "r.obj.", []string{"category_id = 2"}, nil)
 	testGetAllowedObjectConditions(t, e, "bob", "write", "r.obj.", []string{"author = bob"}, nil)
 
-	// test ERR_EMPTY_CONDITION
-	testGetAllowedObjectConditions(t, e, "alice", "write", "r.obj.", []string{}, errors.ERR_EMPTY_CONDITION)
-	testGetAllowedObjectConditions(t, e, "bob", "read", "r.obj.", []string{}, errors.ERR_EMPTY_CONDITION)
+	// test ErrEmptyCondition
+	testGetAllowedObjectConditions(t, e, "alice", "write", "r.obj.", []string{}, errors.ErrEmptyCondition)
+	testGetAllowedObjectConditions(t, e, "bob", "read", "r.obj.", []string{}, errors.ErrEmptyCondition)
 
-	// test ERR_OBJ_CONDITION
+	// test ErrObjCondition
 	// should : e.AddPolicy("alice", "r.obj.price > 50", "read")
 	ok, _ := e.AddPolicy("alice", "price > 50", "read")
 	if ok {
-		testGetAllowedObjectConditions(t, e, "alice", "read", "r.obj.", []string{}, errors.ERR_OBJ_CONDITION)
+		testGetAllowedObjectConditions(t, e, "alice", "read", "r.obj.", []string{}, errors.ErrObjCondition)
 	}
 
 	// test prefix
diff --git a/util/builtin_operators_test.go b/util/builtin_operators_test.go
index 22ae2e7..aa5987e 100644
--- a/util/builtin_operators_test.go
+++ b/util/builtin_operators_test.go
@@ -413,25 +413,25 @@ func testIPMatchFunc(t *testing.T, res bool, err string, args ...interface{}) {
 }
 
 func TestRegexMatchFunc(t *testing.T) {
-	testRegexMatchFunc(t, false, "regexMatch: Expected 2 arguments, but got 1", "/topic/create")
-	testRegexMatchFunc(t, false, "regexMatch: Expected 2 arguments, but got 3", "/topic/create/123", "/topic/create", "/topic/update")
-	testRegexMatchFunc(t, false, "regexMatch: Argument must be a string", "/topic/create", false)
+	testRegexMatchFunc(t, false, "regexMatch: expected 2 arguments, but got 1", "/topic/create")
+	testRegexMatchFunc(t, false, "regexMatch: expected 2 arguments, but got 3", "/topic/create/123", "/topic/create", "/topic/update")
+	testRegexMatchFunc(t, false, "regexMatch: argument must be a string", "/topic/create", false)
 	testRegexMatchFunc(t, true, "", "/topic/create/123", "/topic/create")
 }
 
 func TestKeyMatchFunc(t *testing.T) {
-	testKeyMatchFunc(t, false, "keyMatch: Expected 2 arguments, but got 1", "/foo")
-	testKeyMatchFunc(t, false, "keyMatch: Expected 2 arguments, but got 3", "/foo/create/123", "/foo/*", "/foo/update/123")
-	testKeyMatchFunc(t, false, "keyMatch: Argument must be a string", "/foo", true)
+	testKeyMatchFunc(t, false, "keyMatch: expected 2 arguments, but got 1", "/foo")
+	testKeyMatchFunc(t, false, "keyMatch: expected 2 arguments, but got 3", "/foo/create/123", "/foo/*", "/foo/update/123")
+	testKeyMatchFunc(t, false, "keyMatch: argument must be a string", "/foo", true)
 	testKeyMatchFunc(t, false, "", "/foo/bar", "/foo")
 	testKeyMatchFunc(t, true, "", "/foo/bar", "/foo/*")
 	testKeyMatchFunc(t, true, "", "/foo/bar", "/foo*")
 }
 
 func TestKeyMatch2Func(t *testing.T) {
-	testKeyMatch2Func(t, false, "keyMatch2: Expected 2 arguments, but got 1", "/")
-	testKeyMatch2Func(t, false, "keyMatch2: Expected 2 arguments, but got 3", "/foo/create/123", "/*", "/foo/update/123")
-	testKeyMatch2Func(t, false, "keyMatch2: Argument must be a string", "/foo", true)
+	testKeyMatch2Func(t, false, "keyMatch2: expected 2 arguments, but got 1", "/")
+	testKeyMatch2Func(t, false, "keyMatch2: expected 2 arguments, but got 3", "/foo/create/123", "/*", "/foo/update/123")
+	testKeyMatch2Func(t, false, "keyMatch2: argument must be a string", "/foo", true)
 
 	testKeyMatch2Func(t, false, "", "/", "/:resource")
 	testKeyMatch2Func(t, true, "", "/resource1", "/:resource")
@@ -442,9 +442,9 @@ func TestKeyMatch2Func(t *testing.T) {
 }
 
 func TestKeyMatch3Func(t *testing.T) {
-	testKeyMatch3Func(t, false, "keyMatch3: Expected 2 arguments, but got 1", "/")
-	testKeyMatch3Func(t, false, "keyMatch3: Expected 2 arguments, but got 3", "/foo/create/123", "/*", "/foo/update/123")
-	testKeyMatch3Func(t, false, "keyMatch3: Argument must be a string", "/foo", true)
+	testKeyMatch3Func(t, false, "keyMatch3: expected 2 arguments, but got 1", "/")
+	testKeyMatch3Func(t, false, "keyMatch3: expected 2 arguments, but got 3", "/foo/create/123", "/*", "/foo/update/123")
+	testKeyMatch3Func(t, false, "keyMatch3: argument must be a string", "/foo", true)
 
 	testKeyMatch3Func(t, true, "", "/foo", "/foo")
 	testKeyMatch3Func(t, true, "", "/foo", "/foo*")
@@ -470,9 +470,9 @@ func TestKeyMatch3Func(t *testing.T) {
 }
 
 func TestKeyMatch4Func(t *testing.T) {
-	testKeyMatch4Func(t, false, "keyMatch4: Expected 2 arguments, but got 1", "/parent/123/child/123")
-	testKeyMatch4Func(t, false, "keyMatch4: Expected 2 arguments, but got 3", "/parent/123/child/123", "/parent/{id}/child/{id}", true)
-	testKeyMatch4Func(t, false, "keyMatch4: Argument must be a string", "/parent/123/child/123", true)
+	testKeyMatch4Func(t, false, "keyMatch4: expected 2 arguments, but got 1", "/parent/123/child/123")
+	testKeyMatch4Func(t, false, "keyMatch4: expected 2 arguments, but got 3", "/parent/123/child/123", "/parent/{id}/child/{id}", true)
+	testKeyMatch4Func(t, false, "keyMatch4: argument must be a string", "/parent/123/child/123", true)
 
 	testKeyMatch4Func(t, true, "", "/parent/123/child/123", "/parent/{id}/child/{id}")
 	testKeyMatch4Func(t, false, "", "/parent/123/child/456", "/parent/{id}/child/{id}")
@@ -483,9 +483,9 @@ func TestKeyMatch4Func(t *testing.T) {
 }
 
 func TestKeyMatch5Func(t *testing.T) {
-	testKeyMatch5Func(t, false, "keyMatch5: Expected 2 arguments, but got 1", "/foo")
-	testKeyMatch5Func(t, false, "keyMatch5: Expected 2 arguments, but got 3", "/foo/create/123", "/foo/*", "/foo/update/123")
-	testKeyMatch5Func(t, false, "keyMatch5: Argument must be a string", "/parent/123", true)
+	testKeyMatch5Func(t, false, "keyMatch5: expected 2 arguments, but got 1", "/foo")
+	testKeyMatch5Func(t, false, "keyMatch5: expected 2 arguments, but got 3", "/foo/create/123", "/foo/*", "/foo/update/123")
+	testKeyMatch5Func(t, false, "keyMatch5: argument must be a string", "/parent/123", true)
 
 	testKeyMatch5Func(t, true, "", "/parent/child?status=1&type=2", "/parent/child")
 	testKeyMatch5Func(t, false, "", "/parent?status=1&type=2", "/parent/child")
@@ -526,8 +526,8 @@ func TestKeyMatch5Func(t *testing.T) {
 }
 
 func TestIPMatchFunc(t *testing.T) {
-	testIPMatchFunc(t, false, "ipMatch: Expected 2 arguments, but got 1", "192.168.2.123")
-	testIPMatchFunc(t, false, "ipMatch: Argument must be a string", "192.168.2.123", 128)
+	testIPMatchFunc(t, false, "ipMatch: expected 2 arguments, but got 1", "192.168.2.123")
+	testIPMatchFunc(t, false, "ipMatch: argument must be a string", "192.168.2.123", 128)
 	testIPMatchFunc(t, true, "", "192.168.2.123", "192.168.2.0/24")
 }