Skip to content

Commit

Permalink
feat: enable goconst lint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Mar 18, 2024
1 parent 8631a93 commit 8cec408
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ linters:
- gochecknoinits # checks that no init functions are present in Go code
- gochecksumtype # checks exhaustiveness on Go "sum types"
#- gocognit # computes and checks the cognitive complexity of functions
#- goconst # finds repeated strings that could be replaced by a constant
- goconst # finds repeated strings that could be replaced by a constant
#- gocritic # provides diagnostics that check for bugs, performance and style issues
#- gocyclo # computes and checks the cyclomatic complexity of functions
- godot # checks if comments end in a period
Expand Down
14 changes: 8 additions & 6 deletions enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
"sync"

"github.com/casbin/casbin/v2/effector"
casbinerrors "github.com/casbin/casbin/v2/errors"
"github.com/casbin/casbin/v2/log"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
fileadapter "github.com/casbin/casbin/v2/persist/file-adapter"
"github.com/casbin/casbin/v2/rbac"
defaultrolemanager "github.com/casbin/casbin/v2/rbac/default-role-manager"
"github.com/casbin/casbin/v2/util"

"github.com/casbin/govaluate"
)

Expand Down Expand Up @@ -337,7 +337,7 @@ func (e *Enforcer) LoadPolicy() error {
}
}()

if err = e.adapter.LoadPolicy(newModel); err != nil && err.Error() != "invalid file path, file path cannot be empty" {
if err = e.adapter.LoadPolicy(newModel); err != nil && !errors.Is(err, casbinerrors.ErrInvalidFilePath) {
return err
}

Expand Down Expand Up @@ -410,7 +410,7 @@ func (e *Enforcer) loadFilteredPolicy(filter interface{}) error {
default:
return errors.New("filtered policies are not supported by this adapter")
}
if err := filteredAdapter.LoadFilteredPolicy(e.model, filter); err != nil && err.Error() != "invalid file path, file path cannot be empty" {
if err := filteredAdapter.LoadFilteredPolicy(e.model, filter); err != nil && !errors.Is(err, casbinerrors.ErrInvalidFilePath) {
return err
}

Expand Down Expand Up @@ -948,21 +948,23 @@ func (p enforceParameters) Get(name string) (interface{}, error) {
return nil, nil
}

err := errors.New("No parameter '" + name + "' found.")

switch name[0] {
case 'p':
i, ok := p.pTokens[name]
if !ok {
return nil, errors.New("No parameter '" + name + "' found.")
return nil, err
}
return p.pVals[i], nil
case 'r':
i, ok := p.rTokens[name]
if !ok {
return nil, errors.New("No parameter '" + name + "' found.")
return nil, err
}
return p.rVals[i], nil
default:
return nil, errors.New("No parameter '" + name + "' found.")
return nil, err
}
}

Expand Down
5 changes: 4 additions & 1 deletion enforcer_synced.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package casbin

import (
"errors"
"sync"
"sync/atomic"
"time"

casbinerrors "github.com/casbin/casbin/v2/errors"

"github.com/casbin/govaluate"

"github.com/casbin/casbin/v2/persist"
Expand Down Expand Up @@ -132,7 +135,7 @@ func (e *SyncedEnforcer) LoadPolicyFast() error {
newRmMap := map[string]rbac.RoleManager{}
var err error

if err = e.adapter.LoadPolicy(newModel); err != nil && err.Error() != "invalid file path, file path cannot be empty" {
if err = e.adapter.LoadPolicy(newModel); err != nil && !errors.Is(err, casbinerrors.ErrInvalidFilePath) {
return err
}

Expand Down
11 changes: 11 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errors

import "errors"

var (
ErrInvalidFilePath = errors.New("invalid file path, file path cannot be empty")
)

func Is(err, target error) bool {
return errors.Is(err, target)
}
4 changes: 0 additions & 4 deletions errors/rbac_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@ var (
ErrObjCondition = errors.New("need to meet the prefix required by the object condition")
ErrEmptyCondition = errors.New("GetAllowedObjectConditions have an empty condition")
)

func Is(err, target error) bool {
return errors.Is(err, target)
}
5 changes: 3 additions & 2 deletions persist/file-adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"strings"

casbinerrors "github.com/casbin/casbin/v2/errors"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
"github.com/casbin/casbin/v2/util"
Expand Down Expand Up @@ -52,7 +53,7 @@ func NewAdapter(filePath string) *Adapter {
// LoadPolicy loads all policy rules from the storage.
func (a *Adapter) LoadPolicy(model model.Model) error {
if a.filePath == "" {
return errors.New("invalid file path, file path cannot be empty")
return casbinerrors.ErrInvalidFilePath
}

return a.loadPolicyFile(model, persist.LoadPolicyLine)
Expand All @@ -61,7 +62,7 @@ func (a *Adapter) LoadPolicy(model model.Model) error {
// SavePolicy saves all policy rules to the storage.
func (a *Adapter) SavePolicy(model model.Model) error {
if a.filePath == "" {
return errors.New("invalid file path, file path cannot be empty")
return casbinerrors.ErrInvalidFilePath
}

var tmp bytes.Buffer
Expand Down
3 changes: 2 additions & 1 deletion persist/file-adapter/adapter_filtered.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"strings"

casbinerrors "github.com/casbin/casbin/v2/errors"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
)
Expand Down Expand Up @@ -63,7 +64,7 @@ func (a *FilteredAdapter) LoadFilteredPolicy(model model.Model, filter interface
return a.LoadPolicy(model)
}
if a.filePath == "" {
return errors.New("invalid file path, file path cannot be empty")
return casbinerrors.ErrInvalidFilePath
}

filterValue, ok := filter.(*Filter)
Expand Down

0 comments on commit 8cec408

Please sign in to comment.