refactor: remove unnecessary type conversions and handle ignored errors across codebase
Remove redundant type conversions that Go 1.26 makes unnecessary or that
were already no-ops:
- uuid.UUID(x.Bytes) → x.Bytes (uuid.UUID is [16]byte, same as pgtype UUID Bytes)
- pgtype.UUID{Bytes: [16]byte(u), Valid: true} → pgtype.UUID{Bytes: u, Valid: true}
- (*time.Time)(&x.Time) → &x.Time
- json.RawMessage(x) → x where x is already []byte
- []byte(stringVal) → stringVal where []byte is expected
- int()/int64()/byte() casts on values already of the target type
- Decompressor(fn) → fn (type is identical)
Handle previously ignored error returns:
- collections.go: check json.Unmarshal error in GetCollection
- conversion_service.go: check fileSize.Scan() error
- app_test.go: check app.Shutdown() error in benchmark
This commit is contained in:
@@ -262,7 +262,7 @@ func (h *AuthHandler) Register(c *echo.Context) error {
|
||||
}
|
||||
c.SetCookie(cookie)
|
||||
|
||||
_, refreshToken, err := h.CreateRefreshToken(uuid.UUID(user.ID.Bytes))
|
||||
_, refreshToken, err := h.CreateRefreshToken(user.ID.Bytes)
|
||||
if err != nil {
|
||||
if c.Request().Header.Get("HX-Request") == "true" {
|
||||
return c.HTML(http.StatusInternalServerError, `<div class="text-red-500">Failed to generate refresh token</div>`)
|
||||
@@ -408,7 +408,7 @@ func (h *AuthHandler) Login(c *echo.Context) error {
|
||||
}
|
||||
c.SetCookie(cookie)
|
||||
|
||||
_, refreshToken, err := h.CreateRefreshToken(uuid.UUID(user.ID.Bytes))
|
||||
_, refreshToken, err := h.CreateRefreshToken(user.ID.Bytes)
|
||||
if err != nil {
|
||||
if c.Request().Header.Get("HX-Request") == "true" {
|
||||
return c.HTML(http.StatusInternalServerError, `<div class="text-red-500">Failed to generate refresh token</div>`)
|
||||
@@ -496,7 +496,7 @@ func (h *AuthHandler) UpdateProfile(c *echo.Context) error {
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user id"})
|
||||
}
|
||||
targetUserUUID = pgtype.UUID{Bytes: [16]byte(parsedUUID), Valid: true}
|
||||
targetUserUUID = pgtype.UUID{Bytes: parsedUUID, Valid: true}
|
||||
} else {
|
||||
// Self-edit mode
|
||||
targetUserUUID = currentUser.ID
|
||||
@@ -760,7 +760,7 @@ func (h *AuthHandler) UpdatePassword(c *echo.Context) error {
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user id"})
|
||||
}
|
||||
targetUserUUID = pgtype.UUID{Bytes: [16]byte(parsedUUID), Valid: true}
|
||||
targetUserUUID = pgtype.UUID{Bytes: parsedUUID, Valid: true}
|
||||
} else {
|
||||
// Self-change mode
|
||||
targetUserUUID = currentUser.ID
|
||||
@@ -840,7 +840,7 @@ func (h *AuthHandler) DeleteUser(c *echo.Context) error {
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user id"})
|
||||
}
|
||||
targetUserUUID = pgtype.UUID{Bytes: [16]byte(parsedUUID), Valid: true}
|
||||
targetUserUUID = pgtype.UUID{Bytes: parsedUUID, Valid: true}
|
||||
} else {
|
||||
// Self-deletion mode
|
||||
targetUserUUID = currentUser.ID
|
||||
|
||||
@@ -160,12 +160,12 @@ func (h *CollectionHandler) GetCollections(c *echo.Context) error {
|
||||
continue
|
||||
}
|
||||
response = append(response, CollectionResponse{
|
||||
ID: uuid.UUID(col.ID.Bytes),
|
||||
ID: col.ID.Bytes,
|
||||
Name: col.Name,
|
||||
Description: textToString(col.Description),
|
||||
Color: textToString(col.Color),
|
||||
Icon: textToString(col.Icon),
|
||||
AutoAssignRules: json.RawMessage(col.AutoAssignRules),
|
||||
AutoAssignRules: col.AutoAssignRules,
|
||||
CreatedAt: col.CreatedAt.Time.String(),
|
||||
})
|
||||
}
|
||||
@@ -214,7 +214,10 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error {
|
||||
|
||||
var viewSettings map[string]interface{}
|
||||
if len(collection.ViewSettings) > 0 {
|
||||
json.Unmarshal(collection.ViewSettings, &viewSettings)
|
||||
err := json.Unmarshal(collection.ViewSettings, &viewSettings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
@@ -460,8 +463,8 @@ func (h *CollectionHandler) GetDeviceMappings(c *echo.Context) error {
|
||||
response := make([]MappingResponse, 0, len(mappings))
|
||||
for _, m := range mappings {
|
||||
response = append(response, MappingResponse{
|
||||
ID: uuid.UUID(m.ID.Bytes),
|
||||
CollectionID: uuid.UUID(m.CollectionID.Bytes),
|
||||
ID: m.ID.Bytes,
|
||||
CollectionID: m.CollectionID.Bytes,
|
||||
CollectionName: m.CollectionName,
|
||||
DeviceShelfName: textToString(m.DeviceShelfName),
|
||||
SyncDirection: textToString(m.SyncDirection),
|
||||
@@ -586,7 +589,7 @@ func (h *CollectionHandler) GetBookCollections(c *echo.Context) error {
|
||||
response := make([]CollectionResponse, 0, len(collections))
|
||||
for _, col := range collections {
|
||||
response = append(response, CollectionResponse{
|
||||
ID: uuid.UUID(col.ID.Bytes),
|
||||
ID: col.ID.Bytes,
|
||||
Name: col.Name,
|
||||
Description: textToString(col.Description),
|
||||
Color: textToString(col.Color),
|
||||
|
||||
@@ -257,8 +257,8 @@ func (h *DeviceHandler) ListDevices(c *echo.Context) error {
|
||||
ID: device.ID.Bytes,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceType: device.DeviceType,
|
||||
LastSync: (*time.Time)(&device.LastSync.Time),
|
||||
LastSeen: (*time.Time)(&device.LastSeen.Time),
|
||||
LastSync: &device.LastSync.Time,
|
||||
LastSeen: &device.LastSeen.Time,
|
||||
SyncEnabled: syncEnabled,
|
||||
AutoSync: autoSync,
|
||||
SyncFrequency: syncFreq,
|
||||
@@ -301,8 +301,8 @@ func (h *DeviceHandler) GetDevicesData(c *echo.Context) ([]DeviceInfo, error) {
|
||||
ID: device.ID.Bytes,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceType: device.DeviceType,
|
||||
LastSync: (*time.Time)(&device.LastSync.Time),
|
||||
LastSeen: (*time.Time)(&device.LastSeen.Time),
|
||||
LastSync: &device.LastSync.Time,
|
||||
LastSeen: &device.LastSeen.Time,
|
||||
SyncEnabled: syncEnabled,
|
||||
AutoSync: autoSync,
|
||||
SyncFrequency: syncFreq,
|
||||
@@ -353,8 +353,8 @@ func (h *DeviceHandler) GetDevice(c *echo.Context) error {
|
||||
ID: device.ID.Bytes,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceType: device.DeviceType,
|
||||
LastSync: (*time.Time)(&device.LastSync.Time),
|
||||
LastSeen: (*time.Time)(&device.LastSeen.Time),
|
||||
LastSync: &device.LastSync.Time,
|
||||
LastSeen: &device.LastSeen.Time,
|
||||
SyncEnabled: syncEnabled,
|
||||
AutoSync: autoSync,
|
||||
SyncFrequency: syncFreq,
|
||||
@@ -447,8 +447,8 @@ func (h *DeviceHandler) UpdateDevice(c *echo.Context) error {
|
||||
ID: updatedDevice.ID.Bytes,
|
||||
DeviceName: updatedDevice.DeviceName,
|
||||
DeviceType: updatedDevice.DeviceType,
|
||||
LastSync: (*time.Time)(&updatedDevice.LastSync.Time),
|
||||
LastSeen: (*time.Time)(&updatedDevice.LastSeen.Time),
|
||||
LastSync: &updatedDevice.LastSync.Time,
|
||||
LastSeen: &updatedDevice.LastSeen.Time,
|
||||
SyncEnabled: syncEnabled,
|
||||
AutoSync: autoSync,
|
||||
SyncFrequency: syncFreq, // Now correctly returns the updated value
|
||||
@@ -566,8 +566,8 @@ func (h *DeviceHandler) RegenerateDeviceToken(c *echo.Context) error {
|
||||
ID: updatedDevice.ID.Bytes,
|
||||
DeviceName: updatedDevice.DeviceName,
|
||||
DeviceType: updatedDevice.DeviceType,
|
||||
LastSync: (*time.Time)(&updatedDevice.LastSync.Time),
|
||||
LastSeen: (*time.Time)(&updatedDevice.LastSeen.Time),
|
||||
LastSync: &updatedDevice.LastSync.Time,
|
||||
LastSeen: &updatedDevice.LastSeen.Time,
|
||||
SyncEnabled: syncEnabled,
|
||||
AutoSync: autoSync,
|
||||
SyncFrequency: syncFreq,
|
||||
|
||||
@@ -77,7 +77,7 @@ func (h *FiltersHandler) GetSavedFilters(c *echo.Context) error {
|
||||
ID: uuid.Must(uuid.FromBytes(f.ID.Bytes[:])),
|
||||
Name: f.Name,
|
||||
ResourceType: f.ResourceType,
|
||||
Filters: json.RawMessage(f.Filters), // Return JSONB as-is
|
||||
Filters: f.Filters,
|
||||
CreatedAt: f.CreatedAt.Time.Format(time.RFC3339),
|
||||
UpdatedAt: f.UpdatedAt.Time.Format(time.RFC3339),
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (h *KoboHandler) mapContentIdToBookhoardUUID(ctx *echo.Context, contentId s
|
||||
catalog, err := h.db.GetDeviceCatalogByKoboContentId(ctx.Request().Context(), contentId)
|
||||
if err == nil && catalog.ID.Valid {
|
||||
// Found! Use canonical Bookhoard UUID
|
||||
return uuid.UUID(catalog.BookhoardUuid.Bytes), nil, "catalog_match"
|
||||
return catalog.BookhoardUuid.Bytes, nil, "catalog_match"
|
||||
}
|
||||
|
||||
// Step 2: ContentId not found - check if it looks like a SHA-256 hash
|
||||
@@ -52,7 +52,7 @@ func (h *KoboHandler) mapContentIdToBookhoardUUID(ctx *echo.Context, contentId s
|
||||
DeliveryDate: pgtype.Timestamptz{Time: time.Now(), Valid: true},
|
||||
DeliveryMethod: pgtype.Text{String: "sync", Valid: true},
|
||||
})
|
||||
return uuid.UUID(mediaItem.ID.Bytes), nil, "sha256_match"
|
||||
return mediaItem.ID.Bytes, nil, "sha256_match"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -544,7 +544,7 @@ func (h *KOReaderHandler) updateProgressForBook(c *echo.Context, userID pgtype.U
|
||||
}
|
||||
|
||||
h.connManager.BroadcastProgressUpdate(
|
||||
uuid.UUID(mediaItemID.Bytes),
|
||||
mediaItemID.Bytes,
|
||||
book.Percentage,
|
||||
wsync.SourceDevice{
|
||||
ID: uuid.UUID(userID.Bytes).String(),
|
||||
@@ -608,7 +608,7 @@ func (h *KOReaderHandler) GetMetadata(c *echo.Context) error {
|
||||
progressData.ChapterProgress = new(progress.ChapterProgress.Float64)
|
||||
}
|
||||
if progress.CharacterOffset.Valid {
|
||||
progressData.Character = new(int64(progress.CharacterOffset.Int64))
|
||||
progressData.Character = new(progress.CharacterOffset.Int64)
|
||||
}
|
||||
if progress.CurrentPage.Valid {
|
||||
progressData.Page = new(int(progress.CurrentPage.Int32))
|
||||
|
||||
@@ -320,7 +320,7 @@ func parseUUID(uuidStr string) (pgtype.UUID, error) {
|
||||
if err != nil {
|
||||
return pgtype.UUID{}, err
|
||||
}
|
||||
return pgtype.UUID{Bytes: [16]byte(parsedUUID), Valid: true}, nil
|
||||
return pgtype.UUID{Bytes: parsedUUID, Valid: true}, nil
|
||||
}
|
||||
|
||||
// GetUserVisibleLibrariesData returns libraries for SSR (not JSON response)
|
||||
|
||||
@@ -194,7 +194,7 @@ func (h *OPDSHandler) GetDeviceCatalog(c *echo.Context) error {
|
||||
if err == nil {
|
||||
collectionScheme := fmt.Sprintf("%s/collections", baseURL)
|
||||
for _, col := range collections {
|
||||
if col.UserID.Valid && uuid.UUID(col.UserID.Bytes) == userUUID {
|
||||
if col.UserID.Valid && col.UserID.Bytes == userUUID {
|
||||
entry.AddCategory(collectionScheme, col.Name)
|
||||
}
|
||||
}
|
||||
@@ -312,7 +312,7 @@ func (h *OPDSHandler) SearchDeviceCatalog(c *echo.Context) error {
|
||||
if err == nil {
|
||||
collectionScheme := fmt.Sprintf("%s/collections", baseURL)
|
||||
for _, col := range collections {
|
||||
if col.UserID.Valid && uuid.UUID(col.UserID.Bytes) == userUUID {
|
||||
if col.UserID.Valid && col.UserID.Bytes == userUUID {
|
||||
entry.AddCategory(collectionScheme, col.Name)
|
||||
}
|
||||
}
|
||||
@@ -367,7 +367,7 @@ func (h *OPDSHandler) DownloadBook(c *echo.Context) error {
|
||||
// Check if book is in visible library
|
||||
visible := false
|
||||
for _, lib := range libraries {
|
||||
if uuid.UUID(lib.ID.Bytes) == uuid.UUID(mediaItem.LibraryID.Bytes) {
|
||||
if lib.ID.Bytes == mediaItem.LibraryID.Bytes {
|
||||
visible = true
|
||||
break
|
||||
}
|
||||
@@ -514,7 +514,7 @@ func (h *OPDSHandler) GetCoverImage(c *echo.Context) error {
|
||||
// Check if book is in visible library
|
||||
visible := false
|
||||
for _, lib := range libraries {
|
||||
if uuid.UUID(lib.ID.Bytes) == uuid.UUID(mediaItem.LibraryID.Bytes) {
|
||||
if lib.ID.Bytes == mediaItem.LibraryID.Bytes {
|
||||
visible = true
|
||||
break
|
||||
}
|
||||
@@ -655,7 +655,7 @@ func (h *OPDSHandler) ListFormats(c *echo.Context) error {
|
||||
// Check if book is in visible library
|
||||
visible := false
|
||||
for _, lib := range libraries {
|
||||
if uuid.UUID(lib.ID.Bytes) == uuid.UUID(mediaItem.LibraryID.Bytes) {
|
||||
if lib.ID.Bytes == mediaItem.LibraryID.Bytes {
|
||||
visible = true
|
||||
break
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func (h *Handler) GetUniversalProgress(c *echo.Context) error {
|
||||
response["location_references"].(map[string]interface{})["chapter_progress"] = progress.ChapterProgress.Float64
|
||||
}
|
||||
if progress.CharacterOffset.Valid {
|
||||
response["location_references"].(map[string]interface{})["character"] = int64(progress.CharacterOffset.Int64)
|
||||
response["location_references"].(map[string]interface{})["character"] = progress.CharacterOffset.Int64
|
||||
}
|
||||
|
||||
deviceSync := map[string]interface{}{}
|
||||
|
||||
@@ -362,7 +362,7 @@ func (h *ReaderHandler) UpdateReadingSpeed(c *echo.Context) error {
|
||||
// Update reading speed using service
|
||||
err = h.readerService.CalculateReadingSpeed(
|
||||
c.Request().Context(),
|
||||
uuid.UUID(user.ID.Bytes),
|
||||
user.ID.Bytes,
|
||||
parsedUUID,
|
||||
req.PagesRead,
|
||||
req.TimeSpentMinutes,
|
||||
@@ -477,7 +477,7 @@ func (h *ReaderHandler) GetSettings(c *echo.Context) error {
|
||||
user := c.Get("user").(database.Users)
|
||||
|
||||
// Use reader service to get settings
|
||||
settings, err := h.readerService.GetSettings(c.Request().Context(), uuid.UUID(user.ID.Bytes))
|
||||
settings, err := h.readerService.GetSettings(c.Request().Context(), user.ID.Bytes)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch settings"})
|
||||
}
|
||||
@@ -510,13 +510,13 @@ func (h *ReaderHandler) UpdateSettings(c *echo.Context) error {
|
||||
}
|
||||
|
||||
// Use reader service to update settings
|
||||
err := h.readerService.UpdateSettings(c.Request().Context(), uuid.UUID(user.ID.Bytes), settings)
|
||||
err := h.readerService.UpdateSettings(c.Request().Context(), user.ID.Bytes, settings)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to update settings"})
|
||||
}
|
||||
|
||||
// Return updated settings
|
||||
updatedSettings, _ := h.readerService.GetSettings(c.Request().Context(), uuid.UUID(user.ID.Bytes))
|
||||
updatedSettings, _ := h.readerService.GetSettings(c.Request().Context(), user.ID.Bytes)
|
||||
return c.JSON(http.StatusOK, updatedSettings)
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ func parseTokenUUID(tokenStr string) (pgtype.UUID, error) {
|
||||
if err != nil {
|
||||
return pgtype.UUID{}, err
|
||||
}
|
||||
return pgtype.UUID{Bytes: [16]byte(tokenUUID), Valid: true}, nil
|
||||
return pgtype.UUID{Bytes: tokenUUID, Valid: true}, nil
|
||||
}
|
||||
|
||||
type RefreshTokenResponse struct {
|
||||
@@ -103,8 +103,8 @@ func (h *AuthHandler) CreateRefreshToken(userID uuid.UUID) (string, string, erro
|
||||
|
||||
expiresAt := time.Now().Add(refreshTokenExpiration)
|
||||
_, err := h.db.CreateRefreshToken(context.Background(), database.CreateRefreshTokenParams{
|
||||
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
|
||||
Token: pgtype.UUID{Bytes: [16]byte(tokenUUID), Valid: true},
|
||||
UserID: pgtype.UUID{Bytes: userID, Valid: true},
|
||||
Token: pgtype.UUID{Bytes: tokenUUID, Valid: true},
|
||||
ExpiresAt: pgtype.Timestamptz{Time: expiresAt, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -62,7 +62,7 @@ func (h *Handler) ScanLibrary(c *echo.Context) error {
|
||||
}
|
||||
|
||||
// Fetch library folders from database
|
||||
libraryFolders, err := h.db.GetLibraryFolders(c.Request().Context(), pgtype.UUID{Bytes: [16]byte(libraryUUID), Valid: true})
|
||||
libraryFolders, err := h.db.GetLibraryFolders(c.Request().Context(), pgtype.UUID{Bytes: libraryUUID, Valid: true})
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "library not found or has no folders"})
|
||||
}
|
||||
@@ -261,7 +261,7 @@ func (h *Handler) StartWatchMode(c *echo.Context) error {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid library id"})
|
||||
}
|
||||
|
||||
if err := h.StartWatchModeForLibrary(c.Request().Context(), pgtype.UUID{Bytes: [16]byte(libraryID), Valid: true}, pgtype.UUID{Bytes: [16]byte(userUUID), Valid: true}); err != nil {
|
||||
if err := h.StartWatchModeForLibrary(c.Request().Context(), pgtype.UUID{Bytes: libraryID, Valid: true}, pgtype.UUID{Bytes: userUUID, Valid: true}); err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
@@ -289,7 +289,7 @@ func (h *Handler) StopWatchMode(c *echo.Context) error {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid library id"})
|
||||
}
|
||||
|
||||
if err := h.StopWatchModeForLibrary(pgtype.UUID{Bytes: [16]byte(libraryID), Valid: true}); err != nil {
|
||||
if err := h.StopWatchModeForLibrary(pgtype.UUID{Bytes: libraryID, Valid: true}); err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user