From bc440a26ad66f8084e4cb86dc7b2048990d6c07b Mon Sep 17 00:00:00 2001 From: Iman Mirazimi Date: Mon, 1 Jan 2024 10:52:14 +0330 Subject: [PATCH] fix(niki): path linter errors --- delivery/http_server/server.go | 4 +- entity/kind_box_status.go | 2 + go.mod | 3 ++ go.sum | 6 +++ pkg/http_msg/mapper.go | 35 ++++++++----- pkg/rich_error/rich_error.go | 15 ++++-- repository/mysql/db.go | 23 +++++--- repository/mysql/mysql_kind_box/db.go | 4 +- service/admin/kind_box/add.go | 1 + service/admin/kind_box/get_all.go | 1 + validator/admin/kind_box/add.go | 8 +-- validator/admin/kind_box/delete.go | 8 +-- validator/admin/kind_box/get.go | 8 +-- validator/admin/kind_box/update.go | 14 ++--- validator/admin/kind_box/validator.go | 52 +++++++++++++------ validator/admin/kind_box_req/add.go | 8 +-- validator/admin/kind_box_req/delete.go | 8 +-- validator/admin/kind_box_req/get.go | 8 +-- validator/admin/kind_box_req/update.go | 10 ++-- validator/admin/kind_box_req/validator.go | 27 +++++++--- validator/benefactor/kind_box/get.go | 8 +-- validator/benefactor/kind_box/get_all.go | 8 +-- validator/benefactor/kind_box/validator.go | 19 +++++-- validator/benefactor/kind_box_req/add.go | 8 +-- validator/benefactor/kind_box_req/delete.go | 8 +-- validator/benefactor/kind_box_req/get.go | 8 +-- validator/benefactor/kind_box_req/get_all.go | 8 +-- validator/benefactor/kind_box_req/update.go | 10 ++-- .../benefactor/kind_box_req/validator.go | 29 ++++++++--- 29 files changed, 238 insertions(+), 113 deletions(-) diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index f9d867d..8805d70 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -6,9 +6,9 @@ type Server struct { config config.Config } -func New(config config.Config) Server { +func New(cfg config.Config) Server { return Server{ - config: config, + config: cfg, } } diff --git a/entity/kind_box_status.go b/entity/kind_box_status.go index a6d803d..c3d76ec 100644 --- a/entity/kind_box_status.go +++ b/entity/kind_box_status.go @@ -28,6 +28,7 @@ func AllKindBoxStatus() []string { for status, str := range kindBoxStatusStrings { statusStrings[int(status)-1] = str } + return statusStrings } @@ -38,5 +39,6 @@ func MapToKindBoxStatus(statusStr string) KindBoxStatus { return status } } + return KindBoxStatus(0) } diff --git a/go.mod b/go.mod index 8ba6f1d..a32d8ef 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,14 @@ module git.gocasts.ir/ebhomengo/niki go 1.21.3 require ( + github.com/go-ozzo/ozzo-validation v3.6.0+incompatible + github.com/go-ozzo/ozzo-validation/v4 v4.3.0 github.com/knadh/koanf v1.5.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( + github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect github.com/fatih/structs v1.1.0 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect diff --git a/go.sum b/go.sum index 3d7f634..4086825 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0= +github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= github.com/aws/aws-sdk-go-v2 v1.9.2/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= github.com/aws/aws-sdk-go-v2/config v1.8.3/go.mod h1:4AEiLtAb8kLs7vgw2ZV3p2VZ1+hBavOc84hqxVNpCyw= github.com/aws/aws-sdk-go-v2/credentials v1.4.3/go.mod h1:FNNC6nQZQUuyhq5aE5c7ata8o9e4ECGmS4lAXC7o1mQ= @@ -55,6 +57,10 @@ github.com/go-ldap/ldap v3.0.2+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE= +github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU= +github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es= +github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= diff --git a/pkg/http_msg/mapper.go b/pkg/http_msg/mapper.go index 458a047..8147f3a 100644 --- a/pkg/http_msg/mapper.go +++ b/pkg/http_msg/mapper.go @@ -1,42 +1,49 @@ package httpmsg import ( + "errors" "net/http" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) +// TODO: this temperary to ignore linter error.(maggic number). +const ( + internalStatus = 500 +) + func Error(err error) (message string, code int) { - switch err.(type) { - case richerror.RichError: - re := err.(richerror.RichError) - msg := re.Message() - - code := mapKindToHTTPStatusCode(re.Kind()) - - // we should not expose unexpected error messages - if code >= 500 { - msg = errmsg.ErrorMsgSomethingWentWrong - } - - return msg, code - default: + var re *richerror.RichError + if !errors.As(err, &re) { return err.Error(), http.StatusBadRequest } + msg := re.Message() + code = mapKindToHTTPStatusCode(re.Kind()) + // we should not expose unexpected error messages + if code >= internalStatus { + msg = errmsg.ErrorMsgSomethingWentWrong + } + + return msg, code } func mapKindToHTTPStatusCode(kind richerror.Kind) int { switch kind { case richerror.KindInvalid: + return http.StatusUnprocessableEntity case richerror.KindNotFound: + return http.StatusNotFound case richerror.KindForbidden: + return http.StatusForbidden case richerror.KindUnexpected: + return http.StatusInternalServerError default: + return http.StatusBadRequest } } diff --git a/pkg/rich_error/rich_error.go b/pkg/rich_error/rich_error.go index ce13110..88fe2bc 100644 --- a/pkg/rich_error/rich_error.go +++ b/pkg/rich_error/rich_error.go @@ -1,5 +1,7 @@ package richerror +import "errors" + type Kind int const ( @@ -25,26 +27,31 @@ func New(op Op) RichError { func (r RichError) WithOp(op Op) RichError { r.operation = op + return r } func (r RichError) WithErr(err error) RichError { r.wrappedError = err + return r } func (r RichError) WithMessage(message string) RichError { r.message = message + return r } func (r RichError) WithKind(kind Kind) RichError { r.kind = kind + return r } func (r RichError) WithMeta(meta map[string]interface{}) RichError { r.meta = meta + return r } @@ -61,8 +68,8 @@ func (r RichError) Kind() Kind { return r.kind } - re, ok := r.wrappedError.(RichError) - if !ok { + var re *RichError + if !errors.As(r.wrappedError, re) { return 0 } @@ -74,8 +81,8 @@ func (r RichError) Message() string { return r.message } - re, ok := r.wrappedError.(RichError) - if !ok { + var re *RichError + if !errors.As(r.wrappedError, re) { return r.wrappedError.Error() } diff --git a/repository/mysql/db.go b/repository/mysql/db.go index 4d3dd62..db43a88 100644 --- a/repository/mysql/db.go +++ b/repository/mysql/db.go @@ -14,29 +14,36 @@ type Config struct { DBName string `koanf:"db_name"` } -type MySQLDB struct { +type DB struct { config Config db *sql.DB } -func (m *MySQLDB) Conn() *sql.DB { +func (m *DB) Conn() *sql.DB { return m.db } -func New(config Config) *MySQLDB { +// TODO: this temperary to ignore linter error (magic number). +const ( + dbMaxConnLifetime = time.Minute * 3 + dbMaxOpenConns = 10 + dbMaxIdleConns = 10 +) + +func New(config Config) *DB { // parseTime=true changes the output type of DATE and DATETIME values to time.Time // instead of []byte / string // The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", config.Username, config.Password, config.Host, config.Port, config.DBName)) if err != nil { - panic(fmt.Errorf("can't open mysql db: %v", err)) + panic(fmt.Errorf("can't open mysql db: %w", err)) } // See "Important settings" section. - db.SetConnMaxLifetime(time.Minute * 3) - db.SetMaxOpenConns(10) - db.SetMaxIdleConns(10) + db.SetConnMaxLifetime(dbMaxConnLifetime) + db.SetMaxOpenConns(dbMaxOpenConns) + db.SetMaxIdleConns(dbMaxIdleConns) - return &MySQLDB{config: config, db: db} + return &DB{config: config, db: db} } diff --git a/repository/mysql/mysql_kind_box/db.go b/repository/mysql/mysql_kind_box/db.go index 6893e17..8178944 100644 --- a/repository/mysql/mysql_kind_box/db.go +++ b/repository/mysql/mysql_kind_box/db.go @@ -3,10 +3,10 @@ package mysqlkindbox import "git.gocasts.ir/ebhomengo/niki/repository/mysql" type DB struct { - conn *mysql.MySQLDB + conn *mysql.DB } -func New(conn *mysql.MySQLDB) *DB { +func New(conn *mysql.DB) *DB { return &DB{ conn: conn, } diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index 288b059..bff284e 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -21,5 +21,6 @@ func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.Ki if err != nil { return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } + return param.KindBoxAddResponse{KindBox: kindBox}, nil } diff --git a/service/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go index 697cc79..446d3a6 100644 --- a/service/admin/kind_box/get_all.go +++ b/service/admin/kind_box/get_all.go @@ -13,5 +13,6 @@ func (s Service) GetAll(ctx context.Context) (param.KindBoxGetAllResponse, error if err != nil { return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } + return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil } diff --git a/validator/admin/kind_box/add.go b/validator/admin/kind_box/add.go index 5efab62..7174962 100644 --- a/validator/admin/kind_box/add.go +++ b/validator/admin/kind_box/add.go @@ -1,6 +1,8 @@ package adminkindboxvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -27,8 +29,8 @@ func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[str ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -43,5 +45,5 @@ func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[str WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box/delete.go b/validator/admin/kind_box/delete.go index fc37e80..32b0635 100644 --- a/validator/admin/kind_box/delete.go +++ b/validator/admin/kind_box/delete.go @@ -1,6 +1,8 @@ package adminkindboxvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -23,8 +25,8 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[st ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -39,5 +41,5 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[st WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box/get.go b/validator/admin/kind_box/get.go index c50c6fc..8da426b 100644 --- a/validator/admin/kind_box/get.go +++ b/validator/admin/kind_box/get.go @@ -1,6 +1,8 @@ package adminkindboxvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -22,8 +24,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -38,5 +40,5 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box/update.go b/validator/admin/kind_box/update.go index 5813bcd..220eef5 100644 --- a/validator/admin/kind_box/update.go +++ b/validator/admin/kind_box/update.go @@ -1,6 +1,8 @@ package adminkindboxvalidator import ( + "errors" + "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -21,24 +23,24 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st validation.Required, validation.By(v.doesKindBoxExist), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), - + validation.Field(&req.SerialNumber, is.Alphanumeric), validation.Field(&req.TotalAmount, validation.Min(0)), validation.Field(&req.SenderID, - validation.By(v.doesEmployeeExist(req.SenderID))), + validation.By(v.doesEmployeeExist)), validation.Field(&req.ReceiverID, - validation.By(v.doesEmployeeExist(req.ReceiverID))), + validation.By(v.doesEmployeeExist)), validation.Field(&req.Status, validation.In(entity.AllKindBoxStatus())), ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -53,5 +55,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index 692de58..1345a1d 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -26,7 +26,10 @@ func New(repo Repository) Validator { } func (v Validator) doesKindBoxRequestExist(value interface{}) error { - receiverID := value.(uint) + receiverID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.KindBoxRequestExist(receiverID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -36,7 +39,10 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error { } func (v Validator) doesEmployeeExist(value interface{}) error { - senderID := value.(uint) + senderID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.EmployeeExist(senderID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -46,7 +52,10 @@ func (v Validator) doesEmployeeExist(value interface{}) error { } func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID := value.(uint) + benefactorID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -56,7 +65,10 @@ func (v Validator) doesBenefactorExist(value interface{}) error { } func (v Validator) doesKindBoxExist(value interface{}) error { - kindboxID := value.(uint) + kindboxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } _, err := v.repo.KindBoxExist(kindboxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -67,8 +79,11 @@ func (v Validator) doesKindBoxExist(value interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { return func(value interface{}) error { - kbId := value.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + kbID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -77,18 +92,25 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R } } -func (v Validator) hasCorrectStatus(value interface{}) error { - status := value.(string) - _, err := v.repo.CheckStatus(status) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } +// TODO: this temperary to ignore linter error. (unused function) +// func (v Validator) hasCorrectStatus(value interface{}) error { +// status, ok := value.(string) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.CheckStatus(status) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } - return nil -} +// return nil +// } func (v Validator) hasPendingStatus(value interface{}) error { - kindboxID := value.(uint) + kindboxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.PendingStatus(kindboxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) diff --git a/validator/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go index 7ea2da2..0a7a466 100644 --- a/validator/admin/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -1,6 +1,8 @@ package adminkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -24,8 +26,8 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -40,5 +42,5 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box_req/delete.go b/validator/admin/kind_box_req/delete.go index 9555eaf..de63922 100644 --- a/validator/admin/kind_box_req/delete.go +++ b/validator/admin/kind_box_req/delete.go @@ -1,6 +1,8 @@ package adminkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -23,8 +25,8 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -39,5 +41,5 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box_req/get.go b/validator/admin/kind_box_req/get.go index 068e227..c904363 100644 --- a/validator/admin/kind_box_req/get.go +++ b/validator/admin/kind_box_req/get.go @@ -1,6 +1,8 @@ package adminkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -22,8 +24,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[s ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -38,5 +40,5 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[s WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go index 89ac9ea..9559899 100644 --- a/validator/admin/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -1,6 +1,8 @@ package adminkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -24,12 +26,12 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), validation.Field(&req.TypeID, - validation.By(v.doesTypeExist(req.TypeID))), + validation.By(v.doesTypeExist)), ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -44,5 +46,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 17a978b..83023f5 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -29,7 +29,10 @@ func New(repo Repository) Validator { } func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID := value.(int) + benefactorID, ok := value.(int) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -40,8 +43,11 @@ func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { return func(value interface{}) error { - kbId := value.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + kbID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -51,7 +57,10 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R } func (v Validator) doesKindBoxRequestExist(value interface{}) error { - kindboxreqID := value.(int) + kindboxreqID, ok := value.(int) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.KindBoxRequestExist(kindboxreqID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -61,7 +70,10 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error { } func (v Validator) doesTypeExist(value interface{}) error { - typeID := value.(int) + typeID, ok := value.(int) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.TypeExist(typeID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -71,7 +83,10 @@ func (v Validator) doesTypeExist(value interface{}) error { } func (v Validator) hasPendingStatus(value interface{}) error { - kindboxID := value.(uint) + kindboxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.PendingStatus(kindboxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) diff --git a/validator/benefactor/kind_box/get.go b/validator/benefactor/kind_box/get.go index d8fa4cd..f3207ee 100644 --- a/validator/benefactor/kind_box/get.go +++ b/validator/benefactor/kind_box/get.go @@ -1,6 +1,8 @@ package userkindboxvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -22,8 +24,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -38,5 +40,5 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box/get_all.go b/validator/benefactor/kind_box/get_all.go index 46a74b0..3232f2a 100644 --- a/validator/benefactor/kind_box/get_all.go +++ b/validator/benefactor/kind_box/get_all.go @@ -1,6 +1,8 @@ package userkindboxvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -17,8 +19,8 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[st ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -33,5 +35,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[st WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box/validator.go b/validator/benefactor/kind_box/validator.go index 40fb16c..7ea99f9 100644 --- a/validator/benefactor/kind_box/validator.go +++ b/validator/benefactor/kind_box/validator.go @@ -10,7 +10,7 @@ import ( type Repository interface { BenefactorExist(id uint) (bool, error) KindBoxExist(id uint) (bool, error) - KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error) + KindBoxBelongToBenefactor(bID uint, kbID uint) (bool, error) } type Validator struct { @@ -22,7 +22,10 @@ func New(repo Repository) Validator { } func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID := value.(uint) + benefactorID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -32,7 +35,10 @@ func (v Validator) doesBenefactorExist(value interface{}) error { } func (v Validator) doesKindBoxExist(value interface{}) error { - kindBoxID := value.(uint) + kindBoxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.KindBoxExist(kindBoxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -43,8 +49,11 @@ func (v Validator) doesKindBoxExist(value interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { return func(value interface{}) error { - kbId := value.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + kbID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index 6aa19e1..2544dca 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -1,6 +1,8 @@ package userkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -24,8 +26,8 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -40,5 +42,5 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box_req/delete.go b/validator/benefactor/kind_box_req/delete.go index 021ecec..e355e65 100644 --- a/validator/benefactor/kind_box_req/delete.go +++ b/validator/benefactor/kind_box_req/delete.go @@ -1,6 +1,8 @@ package userkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -23,8 +25,8 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -39,5 +41,5 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box_req/get.go b/validator/benefactor/kind_box_req/get.go index bf87f15..25a220a 100644 --- a/validator/benefactor/kind_box_req/get.go +++ b/validator/benefactor/kind_box_req/get.go @@ -1,6 +1,8 @@ package userkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -22,8 +24,8 @@ func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[strin ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -38,5 +40,5 @@ func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[strin WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box_req/get_all.go b/validator/benefactor/kind_box_req/get_all.go index 58fd30a..3d8886e 100644 --- a/validator/benefactor/kind_box_req/get_all.go +++ b/validator/benefactor/kind_box_req/get_all.go @@ -1,6 +1,8 @@ package userkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -17,8 +19,8 @@ func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -33,5 +35,5 @@ func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box_req/update.go b/validator/benefactor/kind_box_req/update.go index 4b42110..9587fee 100644 --- a/validator/benefactor/kind_box_req/update.go +++ b/validator/benefactor/kind_box_req/update.go @@ -1,6 +1,8 @@ package userkindboxreqvalidator import ( + "errors" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -25,12 +27,12 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map validation.Field(&req.TypeID, validation.Required, - validation.By(v.doesTypeExist(req.TypeID))), + validation.By(v.doesTypeExist)), ); err != nil { fieldErrors := make(map[string]string) - errV, ok := err.(validation.Errors) - if ok { + var errV validation.Errors + if errors.As(err, &errV) { for key, value := range errV { if value != nil { fieldErrors[key] = value.Error() @@ -45,5 +47,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map WithErr(err) } - return nil, nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index bde8aac..986ae8e 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -16,7 +16,7 @@ type Repository interface { BenefactorExist(id uint) (bool, error) KindBoxReqExist(id uint) (bool, error) TypeExist(id uint) (bool, error) - KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error) + KindBoxBelongToBenefactor(bID uint, kbID uint) (bool, error) PendingStatus(id uint) (bool, error) } @@ -29,7 +29,10 @@ func New(repo Repository) Validator { } func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID := value.(uint) + benefactorID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -39,7 +42,10 @@ func (v Validator) doesBenefactorExist(value interface{}) error { } func (v Validator) doesTypeExist(value interface{}) error { - typeID := value.(uint) + typeID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.TypeExist(typeID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -49,7 +55,10 @@ func (v Validator) doesTypeExist(value interface{}) error { } func (v Validator) doesKindBoxRequestExist(value interface{}) error { - kindBoxReqID := value.(uint) + kindBoxReqID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.KindBoxReqExist(kindBoxReqID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -60,8 +69,11 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { return func(value interface{}) error { - kbId := value.(uint) - _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) + kbID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -71,7 +83,10 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R } func (v Validator) hasPendingStatus(value interface{}) error { - kindboxID := value.(uint) + kindboxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } _, err := v.repo.PendingStatus(kindboxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound)