fix gopls lintings

This commit is contained in:
dhax 2025-03-05 15:41:29 +01:00
parent a2e2ba39f9
commit dd2412463b
7 changed files with 19 additions and 25 deletions

View file

@ -47,6 +47,7 @@ func (a *API) Router() *chi.Mux {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello Admin"))
log(r).Debug("admin access")
})
r.Mount("/accounts", a.Accounts.router())

View file

@ -2,6 +2,7 @@ package authorize
import (
"net/http"
"slices"
"github.com/go-chi/render"
@ -24,10 +25,5 @@ func RequiresRole(role string) func(next http.Handler) http.Handler {
}
func hasRole(role string, roles []string) bool {
for _, r := range roles {
if r == role {
return true
}
}
return false
return slices.Contains(roles, role)
}

View file

@ -20,7 +20,7 @@ type AppClaims struct {
}
// ParseClaims parses JWT claims into AppClaims.
func (c *AppClaims) ParseClaims(claims map[string]interface{}) error {
func (c *AppClaims) ParseClaims(claims map[string]any) error {
id, ok := claims["id"]
if !ok {
return errors.New("could not parse claim id")
@ -40,7 +40,7 @@ func (c *AppClaims) ParseClaims(claims map[string]interface{}) error {
var roles []string
if rl != nil {
for _, v := range rl.([]interface{}) {
for _, v := range rl.([]any) {
roles = append(roles, v.(string))
}
}
@ -57,7 +57,7 @@ type RefreshClaims struct {
}
// ParseClaims parses the JWT claims into RefreshClaims.
func (c *RefreshClaims) ParseClaims(claims map[string]interface{}) error {
func (c *RefreshClaims) ParseClaims(claims map[string]any) error {
token, ok := claims["token"]
if !ok {
return errors.New("could not parse claim token")

View file

@ -65,8 +65,8 @@ func (a *TokenAuth) CreateJWT(c AppClaims) (string, error) {
return tokenString, err
}
func ParseStructToMap(c interface{}) (map[string]interface{}, error) {
var claims map[string]interface{}
func ParseStructToMap(c any) (map[string]any, error) {
var claims map[string]any
inrec, _ := json.Marshal(c)
err := json.Unmarshal(inrec, &claims)
if err != nil {

View file

@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@ -349,7 +348,7 @@ func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
return nil, ""
@ -372,7 +371,7 @@ func genRefreshJWT(c jwt.RefreshClaims) string {
return tokenString
}
func encode(v interface{}) (*bytes.Buffer, error) {
func encode(v any) (*bytes.Buffer, error) {
data := new(bytes.Buffer)
err := json.NewEncoder(data).Encode(v)
return data, err

View file

@ -35,12 +35,12 @@ func NewAdmAccountStore(db *bun.DB) *AdmAccountStore {
type AccountFilter struct {
Limit int
Offset int
Filter map[string]interface{}
Filter map[string]any
Order []string
}
// NewAccountFilter returns an AccountFilter with options parsed from request url values.
func NewAccountFilter(params interface{}) (*AccountFilter, error) {
func NewAccountFilter(params any) (*AccountFilter, error) {
v, ok := params.(url.Values)
if !ok {
return nil, ErrBadParams
@ -48,7 +48,7 @@ func NewAccountFilter(params interface{}) (*AccountFilter, error) {
f := &AccountFilter{
Limit: 10, // Default limit
Offset: 0, // Default offset
Filter: make(map[string]interface{}),
Filter: make(map[string]any),
Order: v["order"],
}
// Parse limit and offset

View file

@ -12,10 +12,8 @@ import (
"github.com/spf13/viper"
)
var (
// Logger is a configured logrus.Logger.
Logger *logrus.Logger
)
var Logger *logrus.Logger
// StructuredLogger is a structured logrus Logger.
type StructuredLogger struct {
@ -90,7 +88,7 @@ type StructuredLoggerEntry struct {
Logger logrus.FieldLogger
}
func (l *StructuredLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
func (l *StructuredLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra any) {
l.Logger = l.Logger.WithFields(logrus.Fields{
"resp_status": status,
"resp_bytes_length": bytes,
@ -101,7 +99,7 @@ func (l *StructuredLoggerEntry) Write(status, bytes int, header http.Header, ela
}
// Panic prints stack trace
func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte) {
func (l *StructuredLoggerEntry) Panic(v any, stack []byte) {
l.Logger = l.Logger.WithFields(logrus.Fields{
"stack": string(stack),
"panic": fmt.Sprintf("%+v", v),
@ -118,14 +116,14 @@ func GetLogEntry(r *http.Request) logrus.FieldLogger {
}
// LogEntrySetField adds a field to the request scoped logrus.FieldLogger.
func LogEntrySetField(r *http.Request, key string, value interface{}) {
func LogEntrySetField(r *http.Request, key string, value any) {
if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok {
entry.Logger = entry.Logger.WithField(key, value)
}
}
// LogEntrySetFields adds multiple fields to the request scoped logrus.FieldLogger.
func LogEntrySetFields(r *http.Request, fields map[string]interface{}) {
func LogEntrySetFields(r *http.Request, fields map[string]any) {
if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok {
entry.Logger = entry.Logger.WithFields(fields)
}