Skip to content

Commit

Permalink
[pocketbase#38] added lint and used the lint suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ValleyZw authored Jul 9, 2022
1 parent dfd9528 commit d64fbf9
Show file tree
Hide file tree
Showing 36 changed files with 110 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.vscode/
.idea

.DS_Store

Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
lint:
golangci-lint run -c ./golangci.yml ./...

test:
go test -v --cover ./...

test-report:
go test -v --cover -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
8 changes: 4 additions & 4 deletions apis/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (api *adminApi) refresh(c echo.Context) error {
func (api *adminApi) emailAuth(c echo.Context) error {
form := forms.NewAdminLogin(api.app)
if readErr := c.Bind(form); readErr != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", readErr)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", readErr)
}

admin, submitErr := form.Submit()
Expand All @@ -80,11 +80,11 @@ func (api *adminApi) emailAuth(c echo.Context) error {
func (api *adminApi) requestPasswordReset(c echo.Context) error {
form := forms.NewAdminPasswordResetRequest(api.app)
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", err)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", err)
}

if err := form.Validate(); err != nil {
return rest.NewBadRequestError("An error occured while validating the form.", err)
return rest.NewBadRequestError("An error occurred while validating the form.", err)
}

// run in background because we don't need to show the result
Expand All @@ -101,7 +101,7 @@ func (api *adminApi) requestPasswordReset(c echo.Context) error {
func (api *adminApi) confirmPasswordReset(c echo.Context) error {
form := forms.NewAdminPasswordResetConfirm(api.app)
if readErr := c.Bind(form); readErr != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", readErr)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", readErr)
}

admin, submitErr := form.Submit()
Expand Down
3 changes: 2 additions & 1 deletion apis/file_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package apis_test

import (
"github.com/pocketbase/pocketbase/tests"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"testing"

"github.com/pocketbase/pocketbase/tests"
)

func TestFileDownload(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion apis/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package apis
import (
"net/http"

"github.com/pocketbase/dbx"
"github.com/labstack/echo/v5"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/rest"
Expand Down
14 changes: 8 additions & 6 deletions apis/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ func (api *realtimeApi) bindEvents() {
modelTable := data.Model.TableName()

var contextKey string
if modelTable == userTable {
switch modelTable {
case userTable:
contextKey = ContextUserKey
} else if modelTable == adminTable {
case adminTable:
contextKey = ContextAdminKey
} else {
default:
return nil
}

Expand All @@ -186,11 +187,12 @@ func (api *realtimeApi) bindEvents() {
modelTable := data.Model.TableName()

var contextKey string
if modelTable == userTable {
switch modelTable {
case userTable:
contextKey = ContextUserKey
} else if modelTable == adminTable {
case adminTable:
contextKey = ContextAdminKey
} else {
default:
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion apis/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"strings"

"github.com/pocketbase/dbx"
"github.com/labstack/echo/v5"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/forms"
Expand Down
4 changes: 2 additions & 2 deletions apis/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (api *settingsApi) list(c echo.Context) error {
func (api *settingsApi) set(c echo.Context) error {
form := forms.NewSettingsUpsert(api.app)
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", err)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", err)
}

event := &core.SettingsUpdateEvent{
Expand All @@ -52,7 +52,7 @@ func (api *settingsApi) set(c echo.Context) error {

handlerErr := api.app.OnSettingsBeforeUpdateRequest().Trigger(event, func(e *core.SettingsUpdateEvent) error {
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("An error occured while submitting the form.", err)
return rest.NewBadRequestError("An error occurred while submitting the form.", err)
}

redactedSettings, err := api.app.Settings().RedactClone()
Expand Down
22 changes: 11 additions & 11 deletions apis/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (api *userApi) authMethods(c echo.Context) error {
func (api *userApi) oauth2Auth(c echo.Context) error {
form := forms.NewUserOauth2Login(api.app)
if readErr := c.Bind(form); readErr != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", readErr)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", readErr)
}

user, authData, submitErr := form.Submit()
Expand All @@ -170,7 +170,7 @@ func (api *userApi) emailAuth(c echo.Context) error {

form := forms.NewUserEmailLogin(api.app)
if readErr := c.Bind(form); readErr != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", readErr)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", readErr)
}

user, submitErr := form.Submit()
Expand All @@ -184,11 +184,11 @@ func (api *userApi) emailAuth(c echo.Context) error {
func (api *userApi) requestPasswordReset(c echo.Context) error {
form := forms.NewUserPasswordResetRequest(api.app)
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", err)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", err)
}

if err := form.Validate(); err != nil {
return rest.NewBadRequestError("An error occured while validating the form.", err)
return rest.NewBadRequestError("An error occurred while validating the form.", err)
}

// run in background because we don't need to show
Expand All @@ -205,7 +205,7 @@ func (api *userApi) requestPasswordReset(c echo.Context) error {
func (api *userApi) confirmPasswordReset(c echo.Context) error {
form := forms.NewUserPasswordResetConfirm(api.app)
if readErr := c.Bind(form); readErr != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", readErr)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", readErr)
}

user, submitErr := form.Submit()
Expand All @@ -224,7 +224,7 @@ func (api *userApi) requestEmailChange(c echo.Context) error {

form := forms.NewUserEmailChangeRequest(api.app, loggedUser)
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", err)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", err)
}

if err := form.Submit(); err != nil {
Expand All @@ -237,7 +237,7 @@ func (api *userApi) requestEmailChange(c echo.Context) error {
func (api *userApi) confirmEmailChange(c echo.Context) error {
form := forms.NewUserEmailChangeConfirm(api.app)
if readErr := c.Bind(form); readErr != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", readErr)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", readErr)
}

user, submitErr := form.Submit()
Expand All @@ -251,11 +251,11 @@ func (api *userApi) confirmEmailChange(c echo.Context) error {
func (api *userApi) requestVerification(c echo.Context) error {
form := forms.NewUserVerificationRequest(api.app)
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", err)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", err)
}

if err := form.Validate(); err != nil {
return rest.NewBadRequestError("An error occured while validating the form.", err)
return rest.NewBadRequestError("An error occurred while validating the form.", err)
}

// run in background because we don't need to show
Expand All @@ -272,12 +272,12 @@ func (api *userApi) requestVerification(c echo.Context) error {
func (api *userApi) confirmVerification(c echo.Context) error {
form := forms.NewUserVerificationConfirm(api.app)
if readErr := c.Bind(form); readErr != nil {
return rest.NewBadRequestError("An error occured while reading the submitted data.", readErr)
return rest.NewBadRequestError("An error occurred while reading the submitted data.", readErr)
}

user, submitErr := form.Submit()
if submitErr != nil {
return rest.NewBadRequestError("An error occured while submitting the form.", submitErr)
return rest.NewBadRequestError("An error occurred while submitting the form.", submitErr)
}

return api.authResponse(c, user, nil)
Expand Down
4 changes: 2 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func NewServeCommand(app core.App, showStartBanner bool) *cobra.Command {
}

// configure cors
router.Use(middleware.CORSWithConfig(middleware.CORSConfig(middleware.CORSConfig{
router.Use(middleware.CORSWithConfig(middleware.CORSConfig{
Skipper: middleware.DefaultSkipper,
AllowOrigins: allowedOrigins,
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
})))
}))

// ensure that the latest migrations are applied before starting the server
if err := runMigrations(app); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ type App interface {
// entry in the DB, allowing you to modify or validate the stored data.
OnModelBeforeCreate() *hook.Hook[*ModelEvent]

// OnModelAfterCreate hook is triggered after successfuly
// OnModelAfterCreate hook is triggered after successfully
// inserting a new entry in the DB.
OnModelAfterCreate() *hook.Hook[*ModelEvent]

// OnModelBeforeUpdate hook is triggered before updating existing
// entry in the DB, allowing you to modify or validate the stored data.
OnModelBeforeUpdate() *hook.Hook[*ModelEvent]

// OnModelAfterUpdate hook is triggered after successfuly updating
// OnModelAfterUpdate hook is triggered after successfully updating
// existing entry in the DB.
OnModelAfterUpdate() *hook.Hook[*ModelEvent]

// OnModelBeforeDelete hook is triggered before deleting an
// existing entry from the DB.
OnModelBeforeDelete() *hook.Hook[*ModelEvent]

// OnModelAfterDelete is triggered after successfuly deleting an
// OnModelAfterDelete is triggered after successfully deleting an
// existing entry from the DB.
OnModelAfterDelete() *hook.Hook[*ModelEvent]

Expand All @@ -123,7 +123,7 @@ type App interface {
OnMailerBeforeAdminResetPasswordSend() *hook.Hook[*MailerAdminEvent]

// OnMailerAfterAdminResetPasswordSend hook is triggered after
// admin password reset email was successfuly sent.
// admin password reset email was successfully sent.
OnMailerAfterAdminResetPasswordSend() *hook.Hook[*MailerAdminEvent]

// OnMailerBeforeUserResetPasswordSend hook is triggered right before
Expand All @@ -134,7 +134,7 @@ type App interface {
OnMailerBeforeUserResetPasswordSend() *hook.Hook[*MailerUserEvent]

// OnMailerAfterUserResetPasswordSend hook is triggered after
// a user password reset email was successfuly sent.
// a user password reset email was successfully sent.
OnMailerAfterUserResetPasswordSend() *hook.Hook[*MailerUserEvent]

// OnMailerBeforeUserVerificationSend hook is triggered right before
Expand All @@ -145,7 +145,7 @@ type App interface {
OnMailerBeforeUserVerificationSend() *hook.Hook[*MailerUserEvent]

// OnMailerAfterUserVerificationSend hook is triggered after a user
// verification email was successfuly sent.
// verification email was successfully sent.
OnMailerAfterUserVerificationSend() *hook.Hook[*MailerUserEvent]

// OnMailerBeforeUserChangeEmailSend hook is triggered right before
Expand All @@ -156,7 +156,7 @@ type App interface {
OnMailerBeforeUserChangeEmailSend() *hook.Hook[*MailerUserEvent]

// OnMailerAfterUserChangeEmailSend hook is triggered after a user
// change address email was successfuly sent.
// change address email was successfully sent.
OnMailerAfterUserChangeEmailSend() *hook.Hook[*MailerUserEvent]

// ---------------------------------------------------------------
Expand All @@ -180,7 +180,7 @@ type App interface {
// Settings API event hooks
// ---------------------------------------------------------------

// OnSettingsListRequest hook is triggered on each successfull
// OnSettingsListRequest hook is triggered on each successful
// API Settings list request.
//
// Could be used to validate or modify the response before
Expand Down
2 changes: 1 addition & 1 deletion core/db_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package core
import (
"fmt"

"github.com/pocketbase/dbx"
_ "github.com/mattn/go-sqlite3"
"github.com/pocketbase/dbx"
)

func connectDB(dbPath string) (*dbx.DB, error) {
Expand Down
4 changes: 2 additions & 2 deletions core/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,11 @@ func (c AuthProviderConfig) SetupProvider(provider auth.Provider) error {
}

if c.ClientId != "" {
provider.SetClientId(string(c.ClientId))
provider.SetClientId(c.ClientId)
}

if c.ClientSecret != "" {
provider.SetClientSecret(string(c.ClientSecret))
provider.SetClientSecret(c.ClientSecret)
}

if c.AuthUrl != "" {
Expand Down
1 change: 0 additions & 1 deletion daos/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func TestDeleteCollection(t *testing.T) {
t.Errorf("(%d) Expected hasErr %v, got %v", i, scenario.expectError, hasErr)
}
}

}

func TestSaveCollectionCreate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion daos/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (dao *Dao) SyncRecordTableSchema(newCollection *models.Collection, oldColle
newSchema := newCollection.Schema

// check for renamed table
if strings.ToLower(oldTableName) != strings.ToLower(newTableName) {
if !strings.EqualFold(oldTableName, newTableName) {
_, err := dao.DB().RenameTable(oldTableName, newTableName).Execute()
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions daos/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ func TestSyncRecordTableSchema(t *testing.T) {
t.Fatal(err)
}
updatedCollection, err := app.Dao().FindCollectionByNameOrId("demo")
if err != nil {
t.Fatal(err)
}
updatedCollection.Name = "demo_renamed"
updatedCollection.Schema.RemoveField(updatedCollection.Schema.GetFieldByName("file").Id)
updatedCollection.Schema.AddField(
Expand Down
2 changes: 1 addition & 1 deletion forms/admin_password_reset_confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestAdminPasswordResetConfirmSubmit(t *testing.T) {
}

claims, _ := security.ParseUnverifiedJWT(s.token)
tokenAdminId, _ := claims["id"]
tokenAdminId := claims["id"]

if admin.Id != tokenAdminId {
t.Errorf("(%d) Expected admin with id %s to be returned, got %v", i, tokenAdminId, admin)
Expand Down
2 changes: 1 addition & 1 deletion forms/collection_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (form *CollectionUpsert) checkUniqueName(value any) error {
return validation.NewError("validation_collection_name_exists", "Collection name must be unique (case insensitive).")
}

if (form.isCreate || strings.ToLower(v) != strings.ToLower(form.collection.Name)) && form.app.Dao().HasTable(v) {
if (form.isCreate || !strings.EqualFold(v, form.collection.Name)) && form.app.Dao().HasTable(v) {
return validation.NewError("validation_collection_name_table_exists", "The collection name must be also unique table name.")
}

Expand Down
3 changes: 1 addition & 2 deletions forms/record_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func NewRecordUpsert(app core.App, record *models.Record) *RecordUpsert {
form.Data = map[string]any{}
for _, field := range record.Collection().Schema.Fields() {
form.Data[field.Name] = record.GetDataValue(field.Name)

}

return form
Expand Down Expand Up @@ -147,7 +146,7 @@ func (form *RecordUpsert) LoadData(r *http.Request) error {

for _, field := range form.record.Collection().Schema.Fields() {
key := field.Name
value, _ := extendedData[key]
value := extendedData[key]
value = field.PrepareValue(value)

if field.Type == schema.FieldTypeFile {
Expand Down
Loading

0 comments on commit d64fbf9

Please sign in to comment.