refactor(utils): one PgTextString for the four pgtype.Text unwrappers

handlers textToString, router textToString, router getText, and utils
normText were four copies of the same NULL-to-empty unwrap (99 call
sites). One exported utils.PgTextString replaces them all; the private
normText (comparison normalizer) stays but is now the odd one out.

Harness fix found on the way: setupTestServer built a full wiring
literal for route registration and then RETURNED a second, sparser
literal that never copied AnnotationService — setup.AnnotationService
was always nil on the struct every test receives. Nobody noticed until
cross_library_move_test became the first caller (it previously used
the now-retired plain CreateMediaHighlight query); it now exercises
the real SaveHighlight path through the wired service.
This commit is contained in:
John O'Keefe committed 2026-10-17 16:36:58 -04:00
1 parent 00305ffc7b
commit 188ae076ab
15 files changed
+140 -139

No files matched your search

+21 -28
View File
@@ -116,9 +116,9 @@ func (h *CollectionHandler) CreateCollection(c *echo.Context) error {
"id": uuid.UUID(collection.ID.Bytes).String(),
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
"name": collection.Name,
"description": textToString(collection.Description),
"color": textToString(collection.Color),
"icon": textToString(collection.Icon),
"description": utils.PgTextString(collection.Description),
"color": utils.PgTextString(collection.Color),
"icon": utils.PgTextString(collection.Icon),
"collection_type": map[bool]string{true: "smart", false: "static"}[isSmart],
"auto_assign_rules": collection.AutoAssignRules,
"view_settings": collection.ViewSettings,
@@ -204,9 +204,9 @@ func (h *CollectionHandler) GetCollections(c *echo.Context) error {
response = append(response, CollectionResponse{
ID: col.ID.Bytes,
Name: col.Name,
Description: textToString(col.Description),
Color: textToString(col.Color),
Icon: textToString(col.Icon),
Description: utils.PgTextString(col.Description),
Color: utils.PgTextString(col.Color),
Icon: utils.PgTextString(col.Icon),
AutoAssignRules: col.AutoAssignRules,
CreatedAt: col.CreatedAt.Time.String(),
BookCount: bookCount,
@@ -325,9 +325,9 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error {
"id": uuid.UUID(collection.ID.Bytes).String(),
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
"name": collection.Name,
"description": textToString(collection.Description),
"color": textToString(collection.Color),
"icon": textToString(collection.Icon),
"description": utils.PgTextString(collection.Description),
"color": utils.PgTextString(collection.Color),
"icon": utils.PgTextString(collection.Icon),
"auto_assign_rules": json.RawMessage(collection.AutoAssignRules),
"view_settings": viewSettings,
"books": bookList,
@@ -369,9 +369,9 @@ func (h *CollectionHandler) UpdateCollection(c *echo.Context) error {
"id": uuid.UUID(collection.ID.Bytes).String(),
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
"name": collection.Name,
"description": textToString(collection.Description),
"color": textToString(collection.Color),
"icon": textToString(collection.Icon),
"description": utils.PgTextString(collection.Description),
"color": utils.PgTextString(collection.Color),
"icon": utils.PgTextString(collection.Icon),
"auto_assign_rules": json.RawMessage(collection.AutoAssignRules),
"view_settings": json.RawMessage(collection.ViewSettings),
"created_at": collection.CreatedAt.Time.String(),
@@ -580,8 +580,8 @@ func (h *CollectionHandler) GetDeviceMappings(c *echo.Context) error {
ID: m.ID.Bytes,
CollectionID: m.CollectionID.Bytes,
CollectionName: m.CollectionName,
DeviceShelfName: textToString(m.DeviceShelfName),
SyncDirection: textToString(m.SyncDirection),
DeviceShelfName: utils.PgTextString(m.DeviceShelfName),
SyncDirection: utils.PgTextString(m.SyncDirection),
CreatedAt: m.CreatedAt.Time.String(),
})
}
@@ -627,8 +627,8 @@ func (h *CollectionHandler) CreateDeviceMapping(c *echo.Context) error {
"id": uuid.UUID(mapping.ID.Bytes).String(),
"collection_id": uuid.UUID(mapping.CollectionID.Bytes).String(),
"device_id": uuid.UUID(mapping.DeviceID.Bytes).String(),
"device_shelf_name": textToString(mapping.DeviceShelfName),
"sync_direction": textToString(mapping.SyncDirection),
"device_shelf_name": utils.PgTextString(mapping.DeviceShelfName),
"sync_direction": utils.PgTextString(mapping.SyncDirection),
"created_at": mapping.CreatedAt.Time.String(),
})
}
@@ -661,8 +661,8 @@ func (h *CollectionHandler) UpdateDeviceMapping(c *echo.Context) error {
"id": uuid.UUID(mapping.ID.Bytes).String(),
"collection_id": uuid.UUID(mapping.CollectionID.Bytes).String(),
"device_id": uuid.UUID(mapping.DeviceID.Bytes).String(),
"device_shelf_name": textToString(mapping.DeviceShelfName),
"sync_direction": textToString(mapping.SyncDirection),
"device_shelf_name": utils.PgTextString(mapping.DeviceShelfName),
"sync_direction": utils.PgTextString(mapping.SyncDirection),
"created_at": mapping.CreatedAt.Time.String(),
})
}
@@ -705,9 +705,9 @@ func (h *CollectionHandler) GetBookCollections(c *echo.Context) error {
response = append(response, CollectionResponse{
ID: col.ID.Bytes,
Name: col.Name,
Description: textToString(col.Description),
Color: textToString(col.Color),
Icon: textToString(col.Icon),
Description: utils.PgTextString(col.Description),
Color: utils.PgTextString(col.Color),
Icon: utils.PgTextString(col.Icon),
})
}
@@ -718,13 +718,6 @@ func (h *CollectionHandler) GetBookCollections(c *echo.Context) error {
})
}
func textToString(t pgtype.Text) string {
if t.Valid {
return t.String
}
return ""
}
func (h *CollectionHandler) GetCollectionsData(c *echo.Context) ([]database.Collections, error) {
user := c.Get("user").(database.Users)
userUUID := uuid.UUID(user.ID.Bytes)