Skip to content

Commit

Permalink
feat: add .golangci-lint.yml and apply lint (casbin#1372)
Browse files Browse the repository at this point in the history
* feat: add golangci-lint config

* ci: update lint ci config

* ci: disable noisy lint rules

* refactor: apply lint

* ci: disable lint rules for future improvement
  • Loading branch information
MuZhou233 authored Mar 10, 2024
1 parent caebc40 commit 2858196
Show file tree
Hide file tree
Showing 39 changed files with 546 additions and 224 deletions.
17 changes: 1 addition & 16 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,8 @@ jobs:
- name: Run go test bench
run: make benchmark

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.20'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51

semantic-release:
needs: [test, lint]
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: golangci-lint

on:
push:
branches:
- master
- main
pull_request:

jobs:
golangci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'

- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.56.2
22 changes: 0 additions & 22 deletions .github/workflows/staticcheck.yaml

This file was deleted.

344 changes: 344 additions & 0 deletions .golangci.yml

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ import (
)

var (
// DEFAULT_SECTION specifies the name of a section if no name provided
// DEFAULT_SECTION specifies the name of a section if no name provided.
DEFAULT_SECTION = "default"
// DEFAULT_COMMENT defines what character(s) indicate a comment `#`
// DEFAULT_COMMENT defines what character(s) indicate a comment `#`.
DEFAULT_COMMENT = []byte{'#'}
// DEFAULT_COMMENT_SEM defines what alternate character(s) indicate a comment `;`
// DEFAULT_COMMENT_SEM defines what alternate character(s) indicate a comment `;`.
DEFAULT_COMMENT_SEM = []byte{';'}
// DEFAULT_MULTI_LINE_SEPARATOR defines what character indicates a multi-line content
// DEFAULT_MULTI_LINE_SEPARATOR defines what character indicates a multi-line content.
DEFAULT_MULTI_LINE_SEPARATOR = []byte{'\\'}
)

// ConfigInterface defines the behavior of a Config implementation
// ConfigInterface defines the behavior of a Config implementation.
type ConfigInterface interface {
String(key string) string
Strings(key string) []string
Expand All @@ -47,7 +47,7 @@ type ConfigInterface interface {
Set(key string, value string) error
}

// Config represents an implementation of the ConfigInterface
// Config represents an implementation of the ConfigInterface.
type Config struct {
// Section:key=value
data map[string]map[string]string
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *Config) parseBuffer(buf *bufio.Reader) error {
if err == io.EOF {
// force write when buffer is not flushed yet
if buffer.Len() > 0 {
if err := c.write(section, lineNum, &buffer); err != nil {
if err = c.write(section, lineNum, &buffer); err != nil {
return err
}
}
Expand Down Expand Up @@ -185,33 +185,33 @@ func (c *Config) write(section string, lineNum int, b *bytes.Buffer) error {
return nil
}

// Bool lookups up the value using the provided key and converts the value to a bool
// Bool lookups up the value using the provided key and converts the value to a bool.
func (c *Config) Bool(key string) (bool, error) {
return strconv.ParseBool(c.get(key))
}

// Int lookups up the value using the provided key and converts the value to a int
// Int lookups up the value using the provided key and converts the value to a int.
func (c *Config) Int(key string) (int, error) {
return strconv.Atoi(c.get(key))
}

// Int64 lookups up the value using the provided key and converts the value to a int64
// Int64 lookups up the value using the provided key and converts the value to a int64.
func (c *Config) Int64(key string) (int64, error) {
return strconv.ParseInt(c.get(key), 10, 64)
}

// Float64 lookups up the value using the provided key and converts the value to a float64
// Float64 lookups up the value using the provided key and converts the value to a float64.
func (c *Config) Float64(key string) (float64, error) {
return strconv.ParseFloat(c.get(key), 64)
}

// String lookups up the value using the provided key and converts the value to a string
// String lookups up the value using the provided key and converts the value to a string.
func (c *Config) String(key string) string {
return c.get(key)
}

// Strings lookups up the value using the provided key and converts the value to an array of string
// by splitting the string by comma
// by splitting the string by comma.
func (c *Config) Strings(key string) []string {
v := c.get(key)
if v == "" {
Expand All @@ -220,7 +220,7 @@ func (c *Config) Strings(key string) []string {
return strings.Split(v, ",")
}

// Set sets the value for the specific key in the Config
// Set sets the value for the specific key in the Config.
func (c *Config) Set(key string, value string) error {
if len(key) == 0 {
return errors.New("key is empty")
Expand All @@ -243,7 +243,7 @@ func (c *Config) Set(key string, value string) error {
return nil
}

// section.key or key
// section.key or key.
func (c *Config) get(key string) string {
var (
section string
Expand Down
34 changes: 17 additions & 17 deletions enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Enforcer struct {
logger log.Logger
}

// EnforceContext is used as the first element of the parameter "rvals" in method "enforce"
// EnforceContext is used as the first element of the parameter "rvals" in method "enforce".
type EnforceContext struct {
RType string
PType string
Expand Down Expand Up @@ -539,7 +539,7 @@ func (e *Enforcer) EnableAutoBuildRoleLinks(autoBuildRoleLinks bool) {
e.autoBuildRoleLinks = autoBuildRoleLinks
}

// EnableAcceptJsonRequest controls whether to accept json as a request parameter
// EnableAcceptJsonRequest controls whether to accept json as a request parameter.
func (e *Enforcer) EnableAcceptJsonRequest(acceptJsonRequest bool) {
e.acceptJsonRequest = acceptJsonRequest
}
Expand Down Expand Up @@ -571,7 +571,7 @@ func (e *Enforcer) BuildIncrementalConditionalRoleLinks(op model.PolicyOp, ptype
return e.model.BuildIncrementalConditionalRoleLinks(e.condRmMap, op, "g", ptype, rules)
}

// NewEnforceContext Create a default structure based on the suffix
// NewEnforceContext Create a default structure based on the suffix.
func NewEnforceContext(suffix string) EnforceContext {
return EnforceContext{
RType: "r" + suffix,
Expand Down Expand Up @@ -654,7 +654,8 @@ func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interfac
for i, rval := range rvals {
switch rval := rval.(type) {
case string:
mapValue, err := util.JsonToMap(rval)
var mapValue map[string]interface{}
mapValue, err = util.JsonToMap(rval)
if err == nil {
rvals[i] = mapValue
}
Expand Down Expand Up @@ -757,7 +758,6 @@ func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interfac
}
}
} else {

if hasEval && len(e.model["p"][pType].Policy) == 0 {
return false, errors.New("please make sure rule exists in policy when using eval() in matcher")
}
Expand Down Expand Up @@ -836,21 +836,21 @@ func (e *Enforcer) EnforceWithMatcher(matcher string, rvals ...interface{}) (boo
return e.enforce(matcher, nil, rvals...)
}

// EnforceEx explain enforcement by informing matched rules
// EnforceEx explain enforcement by informing matched rules.
func (e *Enforcer) EnforceEx(rvals ...interface{}) (bool, []string, error) {
explain := []string{}
result, err := e.enforce("", &explain, rvals...)
return result, explain, err
}

// EnforceExWithMatcher use a custom matcher and explain enforcement by informing matched rules
// EnforceExWithMatcher use a custom matcher and explain enforcement by informing matched rules.
func (e *Enforcer) EnforceExWithMatcher(matcher string, rvals ...interface{}) (bool, []string, error) {
explain := []string{}
result, err := e.enforce(matcher, &explain, rvals...)
return result, explain, err
}

// BatchEnforce enforce in batches
// BatchEnforce enforce in batches.
func (e *Enforcer) BatchEnforce(requests [][]interface{}) ([]bool, error) {
var results []bool
for _, request := range requests {
Expand All @@ -863,7 +863,7 @@ func (e *Enforcer) BatchEnforce(requests [][]interface{}) ([]bool, error) {
return results, nil
}

// BatchEnforceWithMatcher enforce with matcher in batches
// BatchEnforceWithMatcher enforce with matcher in batches.
func (e *Enforcer) BatchEnforceWithMatcher(matcher string, requests [][]interface{}) ([]bool, error) {
var results []bool
for _, request := range requests {
Expand All @@ -876,7 +876,7 @@ func (e *Enforcer) BatchEnforceWithMatcher(matcher string, requests [][]interfac
return results, nil
}

// AddNamedMatchingFunc add MatchingFunc by ptype RoleManager
// AddNamedMatchingFunc add MatchingFunc by ptype RoleManager.
func (e *Enforcer) AddNamedMatchingFunc(ptype, name string, fn rbac.MatchingFunc) bool {
if rm, ok := e.rmMap[ptype]; ok {
rm.AddMatchingFunc(name, fn)
Expand All @@ -885,7 +885,7 @@ func (e *Enforcer) AddNamedMatchingFunc(ptype, name string, fn rbac.MatchingFunc
return false
}

// AddNamedDomainMatchingFunc add MatchingFunc by ptype to RoleManager
// AddNamedDomainMatchingFunc add MatchingFunc by ptype to RoleManager.
func (e *Enforcer) AddNamedDomainMatchingFunc(ptype, name string, fn rbac.MatchingFunc) bool {
if rm, ok := e.rmMap[ptype]; ok {
rm.AddDomainMatchingFunc(name, fn)
Expand All @@ -895,7 +895,7 @@ func (e *Enforcer) AddNamedDomainMatchingFunc(ptype, name string, fn rbac.Matchi
}

// AddNamedLinkConditionFunc Add condition function fn for Link userName->roleName,
// when fn returns true, Link is valid, otherwise invalid
// when fn returns true, Link is valid, otherwise invalid.
func (e *Enforcer) AddNamedLinkConditionFunc(ptype, user, role string, fn rbac.LinkConditionFunc) bool {
if rm, ok := e.condRmMap[ptype]; ok {
rm.AddLinkConditionFunc(user, role, fn)
Expand All @@ -905,7 +905,7 @@ func (e *Enforcer) AddNamedLinkConditionFunc(ptype, user, role string, fn rbac.L
}

// AddNamedDomainLinkConditionFunc Add condition function fn for Link userName-> {roleName, domain},
// when fn returns true, Link is valid, otherwise invalid
// when fn returns true, Link is valid, otherwise invalid.
func (e *Enforcer) AddNamedDomainLinkConditionFunc(ptype, user, role string, domain string, fn rbac.LinkConditionFunc) bool {
if rm, ok := e.condRmMap[ptype]; ok {
rm.AddDomainLinkConditionFunc(user, role, domain, fn)
Expand All @@ -914,7 +914,7 @@ func (e *Enforcer) AddNamedDomainLinkConditionFunc(ptype, user, role string, dom
return false
}

// SetNamedLinkConditionFuncParams Sets the parameters of the condition function fn for Link userName->roleName
// SetNamedLinkConditionFuncParams Sets the parameters of the condition function fn for Link userName->roleName.
func (e *Enforcer) SetNamedLinkConditionFuncParams(ptype, user, role string, params ...string) bool {
if rm, ok := e.condRmMap[ptype]; ok {
rm.SetLinkConditionFuncParams(user, role, params...)
Expand All @@ -924,7 +924,7 @@ func (e *Enforcer) SetNamedLinkConditionFuncParams(ptype, user, role string, par
}

// SetNamedDomainLinkConditionFuncParams Sets the parameters of the condition function fn
// for Link userName->{roleName, domain}
// for Link userName->{roleName, domain}.
func (e *Enforcer) SetNamedDomainLinkConditionFuncParams(ptype, user, role, domain string, params ...string) bool {
if rm, ok := e.condRmMap[ptype]; ok {
rm.SetDomainLinkConditionFuncParams(user, role, domain, params...)
Expand All @@ -933,7 +933,7 @@ func (e *Enforcer) SetNamedDomainLinkConditionFuncParams(ptype, user, role, doma
return false
}

// assumes bounds have already been checked
// assumes bounds have already been checked.
type enforceParameters struct {
rTokens map[string]int
rVals []interface{}
Expand All @@ -942,7 +942,7 @@ type enforceParameters struct {
pVals []string
}

// implements govaluate.Parameters
// implements govaluate.Parameters.
func (p enforceParameters) Get(name string) (interface{}, error) {
if name == "" {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions enforcer_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/casbin/casbin/v2/persist/cache"
)

// CachedEnforcer wraps Enforcer and provides decision cache
// CachedEnforcer wraps Enforcer and provides decision cache.
type CachedEnforcer struct {
*Enforcer
expireTime time.Duration
Expand Down Expand Up @@ -61,7 +61,7 @@ func (e *CachedEnforcer) EnableCache(enableCache bool) {
}

// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
// if rvals is not string , ingore the cache
// if rvals is not string , ingore the cache.
func (e *CachedEnforcer) Enforce(rvals ...interface{}) (bool, error) {
if atomic.LoadInt32(&e.enableCache) == 0 {
return e.Enforcer.Enforce(rvals...)
Expand Down
6 changes: 3 additions & 3 deletions enforcer_cached_synced.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/casbin/casbin/v2/persist/cache"
)

// SyncedCachedEnforcer wraps Enforcer and provides decision sync cache
// SyncedCachedEnforcer wraps Enforcer and provides decision sync cache.
type SyncedCachedEnforcer struct {
*SyncedEnforcer
expireTime time.Duration
Expand Down Expand Up @@ -56,7 +56,7 @@ func (e *SyncedCachedEnforcer) EnableCache(enableCache bool) {
}

// Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
// if rvals is not string , ingore the cache
// if rvals is not string , ingore the cache.
func (e *SyncedCachedEnforcer) Enforce(rvals ...interface{}) (bool, error) {
if atomic.LoadInt32(&e.enableCache) == 0 {
return e.SyncedEnforcer.Enforce(rvals...)
Expand Down Expand Up @@ -129,7 +129,7 @@ func (e *SyncedCachedEnforcer) SetExpireTime(expireTime time.Duration) {
e.expireTime = expireTime
}

// SetCache need to be sync cache
// SetCache need to be sync cache.
func (e *SyncedCachedEnforcer) SetCache(c cache.Cache) {
e.locker.Lock()
defer e.locker.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions enforcer_distributed.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (d *DistributedEnforcer) RemovePoliciesSelf(shouldPersist func() bool, sec
d.m.Lock()
defer d.m.Unlock()
if shouldPersist != nil && shouldPersist() {
if err := d.adapter.(persist.BatchAdapter).RemovePolicies(sec, ptype, rules); err != nil {
if err = d.adapter.(persist.BatchAdapter).RemovePolicies(sec, ptype, rules); err != nil {
if err.Error() != notImplemented {
return nil, err
}
Expand All @@ -74,7 +74,7 @@ func (d *DistributedEnforcer) RemovePoliciesSelf(shouldPersist func() bool, sec
affected = d.model.RemovePoliciesWithAffected(sec, ptype, rules)

if sec == "g" {
err := d.BuildIncrementalRoleLinks(model.PolicyRemove, ptype, affected)
err = d.BuildIncrementalRoleLinks(model.PolicyRemove, ptype, affected)
if err != nil {
return affected, err
}
Expand Down
2 changes: 1 addition & 1 deletion enforcer_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ IEnforcer = &Enforcer{}
var _ IEnforcer = &SyncedEnforcer{}
var _ IEnforcer = &CachedEnforcer{}

// IEnforcer is the API interface of Enforcer
// IEnforcer is the API interface of Enforcer.
type IEnforcer interface {
/* Enforcer API */
InitWithFile(modelPath string, policyPath string) error
Expand Down
Loading

0 comments on commit 2858196

Please sign in to comment.