New lint rules

This commit is contained in:
Jamie Curnow
2024-11-21 19:07:36 +10:00
parent 4e6d65645f
commit 152b7666d8
85 changed files with 385 additions and 259 deletions

View File

@ -11,7 +11,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Name string `json:"name" gorm:"column:name" filter:"name,string"`
Meta types.JSONB `json:"meta" gorm:"column:meta"`

View File

@ -139,13 +139,11 @@ func (s *testsuite) TestSave() {
func (s *testsuite) TestSetPassword() {
// goleak is used to detect goroutine leaks
defer goleak.VerifyNone(s.T(), goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
m := Model{UserID: 100}
err := m.SetPassword("abc123")
require.NoError(s.T(), err)
assert.Equal(s.T(), TypeLocal, m.Type)
assert.Greater(s.T(), len(m.Secret), 15)
}
func (s *testsuite) TestValidateSecret() {

View File

@ -22,7 +22,7 @@ func GetByUserIDType(userID uint, authType string) (Model, error) {
return auth, result.Error
}
// GetByUserIDType finds a user by id and type
// GetByIdenityType finds a user by identity and type
func GetByIdenityType(identity string, authType string) (Model, error) {
var auth Model
db := database.GetDB()

View File

@ -17,7 +17,7 @@ const (
// Model is the model
type Model struct {
model.ModelBase
model.Base
UserID uint `json:"user_id" gorm:"column:user_id"`
Type string `json:"type" gorm:"column:type;default:local"`
Identity string `json:"identity,omitempty" gorm:"column:identity"`

View File

@ -32,9 +32,9 @@ func OAuthCacheInit() {
// OAuthUser is the OAuth User
type OAuthUser struct {
Identifier string `json:"identifier"`
Token string `json:"token"`
Resource map[string]interface{} `json:"resource"`
Identifier string `json:"identifier"`
Token string `json:"token"`
Resource map[string]any `json:"resource"`
}
// GetResourceField will attempt to get a field from the resource

View File

@ -105,7 +105,7 @@ func TestGetEmail(t *testing.T) {
{
name: "Email in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"email": "user@example.com",
},
},
@ -128,7 +128,7 @@ func TestGetEmail(t *testing.T) {
{
name: "No email or identifier",
oauthUser: OAuthUser{
Resource: map[string]interface{}{},
Resource: map[string]any{},
},
expected: "",
},
@ -151,7 +151,7 @@ func TestGetName(t *testing.T) {
{
name: "Nickname in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"nickname": "user_nick",
},
},
@ -160,7 +160,7 @@ func TestGetName(t *testing.T) {
{
name: "Given name in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"given_name": "User Given",
},
},
@ -169,7 +169,7 @@ func TestGetName(t *testing.T) {
{
name: "Name in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"name": "User Name",
},
},
@ -178,7 +178,7 @@ func TestGetName(t *testing.T) {
{
name: "Preferred username in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"preferred_username": "preferred_user",
},
},
@ -187,7 +187,7 @@ func TestGetName(t *testing.T) {
{
name: "Username in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"username": "user123",
},
},
@ -197,14 +197,14 @@ func TestGetName(t *testing.T) {
name: "No name fields in resource, fallback to identifier",
oauthUser: OAuthUser{
Identifier: "fallback_identifier",
Resource: map[string]interface{}{},
Resource: map[string]any{},
},
expected: "fallback_identifier",
},
{
name: "No name fields and no identifier",
oauthUser: OAuthUser{
Resource: map[string]interface{}{},
Resource: map[string]any{},
},
expected: "",
},
@ -212,7 +212,7 @@ func TestGetName(t *testing.T) {
name: "All fields",
oauthUser: OAuthUser{
Identifier: "fallback_identifier",
Resource: map[string]interface{}{
Resource: map[string]any{
"nickname": "user_nick",
"given_name": "User Given",
"name": "User Name",
@ -248,7 +248,7 @@ func TestGetID(t *testing.T) {
{
name: "UID in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"uid": "uid123",
},
},
@ -257,7 +257,7 @@ func TestGetID(t *testing.T) {
{
name: "User ID in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"user_id": "user_id123",
},
},
@ -266,7 +266,7 @@ func TestGetID(t *testing.T) {
{
name: "Username in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"username": "username123",
},
},
@ -275,7 +275,7 @@ func TestGetID(t *testing.T) {
{
name: "Preferred username in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"preferred_username": "preferred_user",
},
},
@ -284,7 +284,7 @@ func TestGetID(t *testing.T) {
{
name: "Email in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"email": "user@example.com",
},
},
@ -293,7 +293,7 @@ func TestGetID(t *testing.T) {
{
name: "Mail in resource",
oauthUser: OAuthUser{
Resource: map[string]interface{}{
Resource: map[string]any{
"mail": "mail@example.com",
},
},
@ -302,7 +302,7 @@ func TestGetID(t *testing.T) {
{
name: "No identifier or resource fields",
oauthUser: OAuthUser{
Resource: map[string]interface{}{},
Resource: map[string]any{},
},
expected: "",
},
@ -310,7 +310,7 @@ func TestGetID(t *testing.T) {
name: "All fields",
oauthUser: OAuthUser{
Identifier: "user123",
Resource: map[string]interface{}{
Resource: map[string]any{
"uid": "uid123",
"user_id": "user_id123",
"username": "username123",

View File

@ -44,7 +44,7 @@ const (
// Model is the model
type Model struct {
model.ModelBase
model.Base
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`
CertificateAuthorityID types.NullableDBUint `json:"certificate_authority_id" gorm:"column:certificate_authority_id" filter:"certificate_authority_id,integer"`

View File

@ -210,7 +210,7 @@ func (s *testsuite) TestDelete() {
assert.Equal(s.T(), "Unable to delete a new object", err.Error())
m2 := Model{
ModelBase: model.ModelBase{
Base: model.Base{
ID: 10,
},
}

View File

@ -13,7 +13,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
Name string `json:"name" gorm:"column:name" filter:"name,string"`
AcmeshServer string `json:"acmesh_server" gorm:"column:acmesh_server" filter:"acmesh_server,string"`
CABundle string `json:"ca_bundle" gorm:"column:ca_bundle" filter:"ca_bundle,string"`

View File

@ -296,7 +296,7 @@ func (s *testsuite) TestDelete() {
assert.Equal(s.T(), "Unable to delete a new object", err.Error())
m2 := Model{
ModelBase: model.ModelBase{
Base: model.Base{
ID: 10,
},
}

View File

@ -14,7 +14,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Name string `json:"name" gorm:"column:name" filter:"name,string"`
AcmeshName string `json:"acmesh_name" gorm:"column:acmesh_name" filter:"acmesh_name,string"`
@ -70,8 +70,8 @@ func (m *Model) GetAcmeShEnvVars() ([]string, error) {
return envs, nil
}
func getEnvsFromMeta(meta interface{}) []string {
if rec, ok := meta.(map[string]interface{}); ok {
func getEnvsFromMeta(meta any) []string {
if rec, ok := meta.(map[string]any); ok {
envs := make([]string, 0)
for key, val := range rec {
if f, ok := val.(string); ok {
@ -81,8 +81,8 @@ func getEnvsFromMeta(meta interface{}) []string {
}
}
return envs
} else {
logger.Debug("getEnvsFromMeta: meta is not an map of strings")
return nil
}
logger.Debug("getEnvsFromMeta: meta is not an map of strings")
return nil
}

View File

@ -6,12 +6,13 @@ import (
)
// GetFilterMap returns the filter map
func GetFilterMap(m interface{}, includeBaseEntity bool) map[string]model.FilterMapValue {
// _ was called `includeBaseEntity`
func GetFilterMap(m any, _ bool) map[string]model.FilterMapValue {
filterMap := tags.GetFilterMap(m, "")
// TODO: this is done in GetFilterMap isn't it?
// if includeBaseEntity {
// return mergeFilterMaps(tags.GetFilterMap(model.ModelBase{}, ""), filterMap)
// return mergeFilterMaps(tags.GetFilterMap(model.Base{}, ""), filterMap)
// }
return filterMap

View File

@ -189,7 +189,7 @@ func (s *testsuite) TestDelete() {
assert.Equal(s.T(), "Unable to delete a new object", err.Error())
m2 := Model{
ModelBase: model.ModelBase{
Base: model.Base{
ID: 10,
},
}
@ -203,7 +203,7 @@ func (s *testsuite) TestGetTemplate() {
defer goleak.VerifyNone(s.T(), goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
m := Model{
ModelBase: model.ModelBase{
Base: model.Base{
ID: 10,
CreatedAt: time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC).UnixMilli(),
UpdatedAt: time.Date(2018, 8, 12, 7, 30, 24, 16, time.UTC).UnixMilli(),

View File

@ -27,7 +27,7 @@ const (
// Model is the model
type Model struct {
model.ModelBase
model.Base
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`
NginxTemplateID uint `json:"nginx_template_id" gorm:"column:nginx_template_id" filter:"nginx_template_id,integer"`

View File

@ -14,12 +14,12 @@ type ListResponse struct {
Limit int `json:"limit"`
Sort []model.Sort `json:"sort"`
Filter []model.Filter `json:"filter,omitempty"`
Items interface{} `json:"items,omitempty"`
Items any `json:"items,omitempty"`
}
// ListQueryBuilder is used to setup queries for lists
func ListQueryBuilder(
pageInfo *model.PageInfo,
_ *model.PageInfo,
filters []model.Filter,
filterMap map[string]model.FilterMapValue,
) *gorm.DB {

View File

@ -9,7 +9,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Name string `json:"name" gorm:"column:name" filter:"name,string"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`

View File

@ -10,6 +10,7 @@ import (
"gorm.io/gorm"
)
// ScopeOffsetLimit ...
func ScopeOffsetLimit(pageInfo *model.PageInfo) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if pageInfo.Offset > 0 || pageInfo.Limit > 0 {
@ -19,6 +20,7 @@ func ScopeOffsetLimit(pageInfo *model.PageInfo) func(db *gorm.DB) *gorm.DB {
}
}
// ScopeOrderBy ...
func ScopeOrderBy(sort []model.Sort, defaultSort model.Sort) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if sort != nil {
@ -36,6 +38,7 @@ func ScopeOrderBy(sort []model.Sort, defaultSort model.Sort) func(db *gorm.DB) *
}
}
// ScopeFilters ...
func ScopeFilters(filters []model.Filter, filterMap map[string]model.FilterMapValue) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
like := database.GetCaseInsensitiveLike()

View File

@ -11,7 +11,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
Name string `json:"name" gorm:"column:name" filter:"name,string"`
Description string `json:"description" gorm:"column:description" filter:"description,string"`
Value datatypes.JSON `json:"value" gorm:"column:value"`

View File

@ -10,7 +10,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
ExpiresOn types.DBDate `json:"expires_on" gorm:"column:expires_on" filter:"expires_on,integer"`
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Provider string `json:"provider" gorm:"column:provider" filter:"provider,string"`

View File

@ -17,7 +17,7 @@ import (
// Model is the model
// See: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
type Model struct {
model.ModelBase
model.Base
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Name string `json:"name" gorm:"column:name" filter:"name,string"`
NginxTemplateID uint `json:"nginx_template_id" gorm:"column:nginx_template_id" filter:"nginx_template_id,integer"`

View File

@ -7,7 +7,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
UpstreamID uint `json:"upstream_id" gorm:"column:upstream_id" filter:"upstream_id,integer"`
Server string `json:"server" gorm:"column:server" filter:"server,string"`
Weight int `json:"weight" gorm:"column:weight" filter:"weight,integer"`

View File

@ -226,7 +226,7 @@ func (s *testsuite) TestDelete() {
assert.Equal(s.T(), "Unable to delete a new object", err.Error())
m2 := Model{
ModelBase: model.ModelBase{
Base: model.Base{
ID: 10,
},
Name: "John Doe",
@ -442,7 +442,7 @@ func (s *testsuite) TestSaveCapabilitiesInvalid() {
// Empty model returns error
m := Model{
ModelBase: model.ModelBase{
Base: model.Base{
ID: 10,
},
Capabilities: []string{"doesnotexist", "hosts.manage"},

View File

@ -108,7 +108,7 @@ func DeleteAll() error {
// GetCapabilities gets capabilities for a user
func GetCapabilities(userID uint) ([]string, error) {
capabilities := make([]string, 0)
var hasCapabilities []UserHasCapabilityModel
var hasCapabilities []HasCapabilityModel
db := database.GetDB()
if result := db.Where("user_id = ?", userID).Find(&hasCapabilities); result.Error != nil {
return nil, result.Error

View File

@ -16,7 +16,7 @@ import (
// Model is the model
type Model struct {
model.ModelBase
model.Base
Name string `json:"name" gorm:"column:name" filter:"name,string"`
Email string `json:"email" gorm:"column:email" filter:"email,email"`
IsDisabled bool `json:"is_disabled" gorm:"column:is_disabled" filter:"is_disabled,boolean"`
@ -33,14 +33,14 @@ func (Model) TableName() string {
return "user"
}
// UserHasCapabilityModel is the model
type UserHasCapabilityModel struct {
// HasCapabilityModel is the model
type HasCapabilityModel struct {
UserID uint `json:"user_id" gorm:"column:user_id"`
CapabilityName string `json:"name" gorm:"column:capability_name"`
}
// TableName overrides the table name used by gorm
func (UserHasCapabilityModel) TableName() string {
func (HasCapabilityModel) TableName() string {
return "user_has_capability"
}
@ -99,15 +99,15 @@ func (m *Model) SetPermissions(permissions []string) error {
db := database.GetDB()
// Wipe out previous permissions
if result := db.Where("user_id = ?", m.ID).Delete(&UserHasCapabilityModel{}); result.Error != nil {
if result := db.Where("user_id = ?", m.ID).Delete(&HasCapabilityModel{}); result.Error != nil {
return result.Error
}
if len(permissions) > 0 {
// Add new permissions
objs := []*UserHasCapabilityModel{}
objs := []*HasCapabilityModel{}
for _, permission := range permissions {
objs = append(objs, &UserHasCapabilityModel{UserID: m.ID, CapabilityName: permission})
objs = append(objs, &HasCapabilityModel{UserID: m.ID, CapabilityName: permission})
}
if result := db.Create(objs); result.Error != nil {
return result.Error