Skip to content

Commit

Permalink
fix: lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
1379 authored and 1379 committed Jan 7, 2023
1 parent 0e95384 commit 4b8edb4
Show file tree
Hide file tree
Showing 93 changed files with 507 additions and 424 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --config=.github/linters/.golangci.yml
args: --config=.golangci.yml
only-new-issues: true
34 changes: 16 additions & 18 deletions .github/linters/.golangci.yml → .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,39 @@ run:

issues:
new: true
exclude-rules:
- linters:
- staticcheck
text: "SA1019:"
- linters:
- stylecheck
text: "ST1016:"

linters:
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bodyclose
- decorder
- depguard
- dogsled
- errname
- errorlint
- gocritic
- gofmt
- gofumpt
- goimports
- goprintffuncname
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- revive
- rowserrcheck
- staticcheck
- prealloc
- stylecheck
- typecheck
- unconvert
- unparam
- whitespace
disable:
- deadcode
- errcheck
- unused
- usestdlibvars
- wastedassign


linters-settings:
goimports:
Expand Down
2 changes: 1 addition & 1 deletion cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
OutPath: "./dal",
ModelPkgPath: "./model/entity",
/* Mode: gen.WithoutContext,*/
//if you want the nullable field generation property to be pointer type, set FieldNullable true
// if you want the nullable field generation property to be pointer type, set FieldNullable true
FieldNullable: true,
FieldWithIndexTag: true,
FieldWithTypeTag: true,
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func initDirectory(conf *Config) {
err := mkdirFunc(conf.Sonic.LogDir, nil)
err = mkdirFunc(conf.Sonic.UploadDir, err)
if err != nil {
panic(fmt.Errorf("initDirectory err=%v", err))
panic(fmt.Errorf("initDirectory err=%w", err))
}
}

Expand Down
2 changes: 1 addition & 1 deletion consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
)

const (
DefaultThemeId = "caicai_anatole"
DefaultThemeID = "caicai_anatole"
ThemeScreenshotsName = "screenshot"
ThemeCustomSheetPrefix = "sheet_"
ThemeCustomPostPrefix = "post_"
Expand Down
102 changes: 56 additions & 46 deletions consts/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ func (m MFAType) MarshalJSON() ([]byte, error) {

func (m *MFAType) UnmarshalJSON(data []byte) error {
str := string(data)
if str == `"NONE"` {
switch str {
case `"NONE"`:
*m = MFANone
} else if str == `"TFA_TOTP"` {
case `"TFA_TOTP"`:
*m = MFATFATotp
} else {
default:
return xerr.BadParam.New("").WithMsg("unknown MFAType")
}
return nil
Expand Down Expand Up @@ -270,31 +271,33 @@ const (
)

func (c PostStatus) MarshalJSON() ([]byte, error) {
if c == PostStatusPublished {
switch c {
case PostStatusPublished:
return []byte(`"PUBLISHED"`), nil
} else if c == PostStatusDraft {
case PostStatusDraft:
return []byte(`"DRAFT"`), nil
} else if c == PostStatusRecycle {
case PostStatusRecycle:
return []byte(`"RECYCLE"`), nil
} else if c == PostStatusIntimate {
case PostStatusIntimate:
return []byte(`"INTIMATE"`), nil
}
return nil, nil
}

func (c *PostStatus) UnmarshalJSON(data []byte) error {
str := string(data)
if str == `"PUBLISHED"` {
switch str {
case `"PUBLISHED"`:
*c = PostStatusPublished
} else if str == `"DRAFT"` {
case `"DRAFT"`:
*c = PostStatusDraft
} else if str == `"RECYCLE"` {
case `"RECYCLE"`:
*c = PostStatusRecycle
} else if str == `"INTIMATE"` {
case `"INTIMATE"`:
*c = PostStatusIntimate
} else if str == "" {
case "":
*c = PostStatusDraft
} else {
default:
return xerr.BadParam.New("").WithMsg("unknown PostStatus")
}
return nil
Expand Down Expand Up @@ -326,16 +329,18 @@ func (c PostStatus) Ptr() *PostStatus {
}

func PostStatusFromString(str string) (PostStatus, error) {
if str == "PUBLISHED" {
switch str {
case "PUBLISHED":
return PostStatusPublished, nil
} else if str == "DRAFT" {
case "DRAFT":
return PostStatusDraft, nil
} else if str == "RECYCLE" {
case "RECYCLE":
return PostStatusRecycle, nil
} else if str == "INTIMATE" {
case "INTIMATE":
return PostStatusIntimate, nil
default:
return PostStatusDraft, xerr.BadParam.New("").WithMsg("unknown PostStatus")
}
return PostStatusDraft, xerr.BadParam.New("").WithMsg("unknown PostStatus")
}

type CommentStatus int32
Expand All @@ -347,25 +352,28 @@ const (
)

func (c CommentStatus) MarshalJSON() ([]byte, error) {
if c == CommentStatusPublished {
switch c {
case CommentStatusPublished:
return []byte(`"PUBLISHED"`), nil
} else if c == CommentStatusAuditing {
case CommentStatusAuditing:
return []byte(`"AUDITING"`), nil
} else if c == CommentStatusRecycle {
case CommentStatusRecycle:
return []byte(`"RECYCLE"`), nil
}
return nil, nil
}

func (c *CommentStatus) UnmarshalJSON(data []byte) error {
str := string(data)
if str == `"PUBLISHED"` {

switch str {
case `"PUBLISHED"`:
*c = CommentStatusPublished
} else if str == `"AUDITING"` {
case `"AUDITING"`:
*c = CommentStatusAuditing
} else if str == `"RECYCLE"` {
case `"RECYCLE"`:
*c = CommentStatusRecycle
} else {
default:
return xerr.BadParam.New("").WithMsg("unknown CommentStatus")
}
return nil
Expand All @@ -389,13 +397,14 @@ func (c *CommentStatus) Scan(src interface{}) error {
}

func CommentStatusFromString(str string) (CommentStatus, error) {
if str == "PUBLISHED" {
switch str {
case "PUBLISHED":
return CommentStatusPublished, nil
} else if str == "AUDITING" {
case "AUDITING":
return CommentStatusAuditing, nil
} else if str == "RECYCLE" {
case "RECYCLE":
return CommentStatusRecycle, nil
} else {
default:
return CommentStatusPublished, xerr.BadParam.New("").WithMsg("unknown CommentStatus")
}
}
Expand Down Expand Up @@ -462,13 +471,14 @@ func (e EditorType) MarshalJSON() ([]byte, error) {

func (e *EditorType) UnmarshalJSON(data []byte) error {
str := string(data)
if str == `"MARKDOWN"` {
switch str {
case `"MARKDOWN"`:
*e = EditorTypeMarkdown
} else if str == `"RICHTEXT"` {
case `"RICHTEXT"`:
*e = EditorTypeRichText
} else if str == "" {
case "":
*e = EditorTypeMarkdown
} else {
default:
return xerr.BadParam.New("").WithMsg("unknown editorType")
}
return nil
Expand All @@ -491,12 +501,12 @@ func (o OptionType) MarshalJSON() ([]byte, error) {
}

func (o *OptionType) UnmarshalJSON(data []byte) error {
str := string(data)
if str == `"INTERNAL"` {
switch string(data) {
case `"INTERNAL"`:
*o = OptionTypeInternal
} else if str == `"CUSTOM"` {
case `"CUSTOM"`:
*o = OptionTypeCustom
} else {
default:
return xerr.BadParam.New("").WithMsg("unknown OptionType")
}
return nil
Expand Down Expand Up @@ -608,12 +618,12 @@ func (j JournalType) MarshalJSON() ([]byte, error) {
}

func (j *JournalType) UnmarshalJSON(data []byte) error {
str := string(data)
if str == `"PUBLIC"` {
switch string(data) {
case `"PUBLIC"`:
*j = JournalTypePublic
} else if str == `"INTIMATE"` {
case `"INTIMATE"`:
*j = JournalTypeIntimate
} else {
default:
return xerr.BadParam.New("").WithMsg("unknown JournalType")
}
return nil
Expand Down Expand Up @@ -858,7 +868,7 @@ func (t ThemeConfigDataType) FormatToStr(value interface{}) (string, error) {
case float32:
valueStr = strconv.FormatFloat(float64(data), 'f', 5, 32)
case float64:
valueStr = strconv.FormatFloat(float64(data), 'f', 5, 64)
valueStr = strconv.FormatFloat(data, 'f', 5, 64)
default:
return "", xerr.WithErrMsgf(nil, "value invalid ThemeConfigDataType")
}
Expand Down Expand Up @@ -971,12 +981,12 @@ func (c CategoryType) MarshalJSON() ([]byte, error) {
}

func (c *CategoryType) UnmarshalJSON(data []byte) error {
str := string(data)
if str == `"NORMAL"` {
switch string(data) {
case `"NORMAL"`:
*c = CategoryTypeNormal
} else if str == `"INTIMATE"` {
case `"INTIMATE"`:
*c = CategoryTypeIntimate
} else {
default:
return xerr.BadParam.New("").WithMsg("unknown PostStatus")
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (

func NewGormDB(conf *config.Config, gormLogger logger.Interface) *gorm.DB {
var err error

//nolint:gocritic
if conf.SQLite3 != nil && conf.SQLite3.Enable {
DB, err = initSQLite(conf, gormLogger)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type LogEvent struct {
LogKey string
LogType consts.LogType
Content string
IpAddress string
IPAddress string
}

func (*LogEvent) EventType() string {
Expand Down
2 changes: 1 addition & 1 deletion event/listener/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (l *LogEventListener) HandleEvent(ctx context.Context, logEvent event.Event
logDAL := dal.GetQueryByCtx(ctx).Log
logEntity := &entity.Log{
Content: log.Content,
IPAddress: log.IpAddress,
IPAddress: log.IPAddress,
LogKey: log.LogKey,
Type: log.LogType,
}
Expand Down
2 changes: 1 addition & 1 deletion event/listener/template_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (t *TemplateConfigListener) loadOption(ctx context.Context) error {
for _, option := range options {
optionMap[option.Key] = option.Value
}
blogBaseURL := t.OptionService.GetOrByDefault(ctx, property.BlogUrl)
blogBaseURL := t.OptionService.GetOrByDefault(ctx, property.BlogURL)
blogTitle := t.OptionService.GetOrByDefault(ctx, property.BlogTitle)
blogLogo := t.OptionService.GetOrByDefault(ctx, property.BlogLogo)
globalAbsolutePathEnabled := t.OptionService.GetOrByDefault(ctx, property.GlobalAbsolutePathEnabled)
Expand Down
11 changes: 8 additions & 3 deletions handler/admin/admin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package admin

import (
"errors"

"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"

Expand Down Expand Up @@ -35,7 +37,8 @@ func (a *AdminHandler) AuthPreCheck(ctx *gin.Context) (interface{}, error) {
var loginParam param.LoginParam
err := ctx.ShouldBindJSON(&loginParam)
if err != nil {
if e, ok := err.(validator.ValidationErrors); ok {
e := validator.ValidationErrors{}
if errors.As(err, &e) {
return nil, xerr.WithStatus(e, xerr.StatusBadRequest).WithMsg(trans.Translate(e))
}
return nil, xerr.BadParam.Wrapf(err, "")
Expand All @@ -52,7 +55,8 @@ func (a *AdminHandler) Auth(ctx *gin.Context) (interface{}, error) {
var loginParam param.LoginParam
err := ctx.ShouldBindJSON(&loginParam)
if err != nil {
if e, ok := err.(validator.ValidationErrors); ok {
e := validator.ValidationErrors{}
if errors.As(err, &e) {
return nil, xerr.WithStatus(e, xerr.StatusBadRequest).WithMsg(trans.Translate(e))
}
return nil, xerr.BadParam.Wrapf(err, "").WithStatus(xerr.StatusBadRequest)
Expand All @@ -70,7 +74,8 @@ func (a *AdminHandler) SendResetCode(ctx *gin.Context) (interface{}, error) {
var resetPasswordParam param.ResetPasswordParam
err := ctx.ShouldBindJSON(&resetPasswordParam)
if err != nil {
if e, ok := err.(validator.ValidationErrors); ok {
e := validator.ValidationErrors{}
if errors.As(err, &e) {
return nil, xerr.WithStatus(e, xerr.StatusBadRequest).WithMsg(trans.Translate(e))
}
return nil, xerr.BadParam.Wrapf(err, "").WithStatus(xerr.StatusBadRequest)
Expand Down
Loading

0 comments on commit 4b8edb4

Please sign in to comment.