style: replace type interface{} with any

This commit is contained in:
ae 2025-04-01 18:50:46 +03:00
parent 18e650c898
commit 5de5c8c285
Signed by: ae
GPG Key ID: 995EFD5C1B532B3E
4 changed files with 19 additions and 8 deletions

View File

@ -14,6 +14,8 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
type userCtxKey struct{}
// Mockable database operations interface // Mockable database operations interface
type UserStore interface { type UserStore interface {
CreateUser(ctx context.Context, arg data.CreateUserParams) (data.User, error) CreateUser(ctx context.Context, arg data.CreateUserParams) (data.User, error)
@ -124,9 +126,9 @@ func (rs usersResource) List(w http.ResponseWriter, r *http.Request) {
} }
// Output sanitization // Output sanitization
var output []map[string]interface{} var output []map[string]any
for _, user := range users { for _, user := range users {
output = append(output, map[string]interface{}{ output = append(output, map[string]any{
"id": user.ID, "id": user.ID,
"username": user.Username, "username": user.Username,
"created_at": user.CreatedAt, "created_at": user.CreatedAt,
@ -144,7 +146,7 @@ func (rs usersResource) Get(w http.ResponseWriter, r *http.Request) {
return return
} }
respondJSON(w, http.StatusOK, map[string]interface{}{ respondJSON(w, http.StatusOK, map[string]any{
"id": user.ID, "id": user.ID,
"username": user.Username, "username": user.Username,
"created_at": user.CreatedAt, "created_at": user.CreatedAt,

View File

@ -154,7 +154,7 @@ func TestListUsers_Success(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var response []map[string]interface{} var response []map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -30,7 +30,7 @@ var (
usernameRegex = regexp.MustCompile("^[a-z0-9_]+$") usernameRegex = regexp.MustCompile("^[a-z0-9_]+$")
) )
func respondJSON(w http.ResponseWriter, status int, data interface{}) { func respondJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status) w.WriteHeader(status)
json.NewEncoder(w).Encode(data) json.NewEncoder(w).Encode(data)

View File

@ -89,9 +89,18 @@ func TestPasswordEntropyCalculation(t *testing.T) {
password string password string
entropy float64 entropy float64
}{ }{
{"password", 37.6}, {
{"SecurePassw0rd!123", 103.12}, "password",
{"aaaaaaaaaaaaaaaa", 9.5}, 37.6,
},
{
"SecurePassw0rd!123",
103.12,
},
{
"aaaaaaaaaaaaaaaa",
9.5,
},
} }
for _, tt := range tests { for _, tt := range tests {