Skip to content

Commit ace47f7

Browse files
committed
refactor: drop trivial helpers, use new(v) and data.DateLayout
- Remove fmtIntAlways and fmtSize wrappers in cmd/micasa/show.go; inline strconv.Itoa / strconv.FormatInt at call sites. - Replace if-val-"true"/"false" ladder in ops_tree.go with strconv.FormatBool. - Delete ptr[T any] helpers in internal/data and internal/app tests in favor of Go 1.26 new(v) builtin. Deduplicate ptr and ptrTime closures in dashboard_test.go into one package-level ptrTime. - fmtMoney now delegates to fmtMoneyVal instead of duplicating the format string. - Replace hardcoded "2006-01-02" with existing data.DateLayout constant where the same package already used it elsewhere.
1 parent c63e2f3 commit ace47f7

8 files changed

Lines changed: 30 additions & 53 deletions

File tree

cmd/micasa/show.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func fmtMoney(cents *int64) string {
2424
if cents == nil {
2525
return "-"
2626
}
27-
return fmt.Sprintf("$%.2f", float64(*cents)/100)
27+
return fmtMoneyVal(*cents)
2828
}
2929

3030
func fmtDate(t *time.Time) string {
@@ -41,10 +41,6 @@ func fmtInt(n int) string {
4141
return strconv.Itoa(n)
4242
}
4343

44-
func fmtIntAlways(n int) string {
45-
return strconv.Itoa(n)
46-
}
47-
4844
func fmtFloat(f float64) string {
4945
if f == 0 {
5046
return "-"
@@ -130,7 +126,7 @@ func fmtDateVal(t time.Time) string {
130126
if t.IsZero() {
131127
return "-"
132128
}
133-
return t.Format("2006-01-02")
129+
return t.Format(data.DateLayout)
134130
}
135131

136132
// withDeletedCol appends a DELETED column and deleted_at JSON field when includeDeleted is true.
@@ -150,7 +146,7 @@ func withDeletedCol[T any](
150146
value: func(item T) string {
151147
da := deletedAt(item)
152148
if da.Valid {
153-
return da.Time.Format("2006-01-02")
149+
return da.Time.Format(data.DateLayout)
154150
}
155151
return "-"
156152
},
@@ -573,7 +569,7 @@ var maintenanceCols = []showCol[data.MaintenanceItem]{
573569
{"APPLIANCE", func(m data.MaintenanceItem) string { return fmtStr(m.Appliance.Name) }},
574570
{"SEASON", func(m data.MaintenanceItem) string { return fmtStr(m.Season) }},
575571
{"LAST SERVICED", func(m data.MaintenanceItem) string { return fmtDate(m.LastServicedAt) }},
576-
{"INTERVAL", func(m data.MaintenanceItem) string { return fmtIntAlways(m.IntervalMonths) }},
572+
{"INTERVAL", func(m data.MaintenanceItem) string { return strconv.Itoa(m.IntervalMonths) }},
577573
{"DUE", func(m data.MaintenanceItem) string { return fmtDate(m.DueDate) }},
578574
{"COST", func(m data.MaintenanceItem) string { return fmtMoney(m.CostCents) }},
579575
}
@@ -640,16 +636,12 @@ func showServiceLog(w io.Writer, store *data.Store, asJSON, includeDeleted bool)
640636

641637
// --- documents ---
642638

643-
func fmtSize(n int64) string {
644-
return strconv.FormatInt(n, 10)
645-
}
646-
647639
var documentCols = []showCol[data.Document]{
648640
{"TITLE", func(d data.Document) string { return fmtStr(d.Title) }},
649641
{"FILE", func(d data.Document) string { return fmtStr(d.FileName) }},
650642
{"ENTITY", func(d data.Document) string { return fmtStr(d.EntityKind) }},
651643
{"MIME", func(d data.Document) string { return fmtStr(d.MIMEType) }},
652-
{"SIZE", func(d data.Document) string { return fmtSize(d.SizeBytes) }},
644+
{"SIZE", func(d data.Document) string { return strconv.FormatInt(d.SizeBytes, 10) }},
653645
{"NOTES", func(d data.Document) string { return fmtStr(d.Notes) }},
654646
}
655647

internal/app/calendar_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/charmbracelet/x/ansi"
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
15+
16+
"github.com/micasa-dev/micasa/internal/data"
1517
)
1618

1719
const testDate = "2026-02-15"
@@ -396,7 +398,7 @@ func TestCalendarTodayKeyNavigation(t *testing.T) {
396398
sendKey(m, "enter")
397399
assert.True(t, confirmed, "OnConfirm should have been called")
398400
assert.Nil(t, m.calendar, "calendar should be dismissed")
399-
assert.Equal(t, now.Format("2006-01-02"), dateVal,
401+
assert.Equal(t, now.Format(data.DateLayout), dateVal,
400402
"confirmed date should be today")
401403
}
402404

internal/app/handler_crud_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ func TestIncidentHandlerLoadDeleteRestoreRoundTrip(t *testing.T) {
643643
Title: "Broken window",
644644
Status: data.IncidentStatusOpen,
645645
Severity: data.IncidentSeverityUrgent,
646-
DateNoticed: time.Now().Format("2006-01-02"),
646+
DateNoticed: time.Now().Format(data.DateLayout),
647647
}
648648
require.NoError(t, h.SubmitForm(m))
649649

internal/app/model_keys.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"charm.land/bubbles/v2/key"
1717
tea "charm.land/bubbletea/v2"
1818
"charm.land/lipgloss/v2"
19+
20+
"github.com/micasa-dev/micasa/internal/data"
1921
)
2022

2123
// handleDashboardKeys intercepts keys that belong to the dashboard (j/k
@@ -539,7 +541,7 @@ func (m *Model) confirmCalendar() {
539541
if m.calendar == nil {
540542
return
541543
}
542-
dateStr := m.calendar.Cursor.Format("2006-01-02")
544+
dateStr := m.calendar.Cursor.Format(data.DateLayout)
543545
if m.calendar.FieldPtr != nil {
544546
*m.calendar.FieldPtr = dateStr
545547
}
@@ -555,7 +557,7 @@ func (m *Model) openCalendar(fieldPtr *string, onConfirm func()) {
555557
var selected time.Time
556558
hasValue := false
557559
if fieldPtr != nil && *fieldPtr != "" {
558-
if t, err := time.ParseInLocation("2006-01-02", *fieldPtr, time.Local); err == nil {
560+
if t, err := time.ParseInLocation(data.DateLayout, *fieldPtr, time.Local); err == nil {
559561
cursor = t
560562
selected = t
561563
hasValue = true

internal/app/ops_tree.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,7 @@ func classifyValue(v any) (string, treeValueKind) {
632632
}
633633
return fmt.Sprintf("%g", val), tvNumber
634634
case bool:
635-
if val {
636-
return "true", tvBool
637-
}
638-
return "false", tvBool
635+
return strconv.FormatBool(val), tvBool
639636
case nil:
640637
return "null", tvNull
641638
default:

internal/app/view_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -591,27 +591,27 @@ func TestApplianceAge(t *testing.T) {
591591
{"nil purchase", nil, ""},
592592
{
593593
"less than a month",
594-
ptr(time.Date(2026, 1, 20, 0, 0, 0, 0, time.UTC)),
594+
new(time.Date(2026, 1, 20, 0, 0, 0, 0, time.UTC)),
595595
"<1m",
596596
},
597597
{
598598
"a few months",
599-
ptr(time.Date(2025, 10, 5, 0, 0, 0, 0, time.UTC)),
599+
new(time.Date(2025, 10, 5, 0, 0, 0, 0, time.UTC)),
600600
"4m",
601601
},
602602
{
603603
"one year exact",
604-
ptr(time.Date(2025, 2, 10, 0, 0, 0, 0, time.UTC)),
604+
new(time.Date(2025, 2, 10, 0, 0, 0, 0, time.UTC)),
605605
"1y",
606606
},
607607
{
608608
"years and months",
609-
ptr(time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC)),
609+
new(time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC)),
610610
"2y 7m",
611611
},
612612
{
613613
"future date",
614-
ptr(time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC)),
614+
new(time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC)),
615615
"",
616616
},
617617
}
@@ -622,12 +622,6 @@ func TestApplianceAge(t *testing.T) {
622622
}
623623
}
624624

625-
func ptr[T any](
626-
v T,
627-
) *T {
628-
return &v
629-
}
630-
631625
func TestNavBadgeLabel(t *testing.T) {
632626
t.Parallel()
633627
m := newTestModel(t)

internal/data/dashboard_test.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ func TestListMaintenanceWithSchedule(t *testing.T) {
1717
cat := MaintenanceCategory{Name: "TestCat"}
1818
require.NoError(t, store.db.Create(&cat).Error)
1919

20-
ptrTime := func(y, m, d int) *time.Time {
21-
t := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
22-
return &t
23-
}
2420
// Item with interval > 0 should appear.
2521
require.NoError(t, store.db.Create(&MaintenanceItem{
2622
Name: "With Interval", CategoryID: cat.ID,
@@ -43,10 +39,6 @@ func TestListMaintenanceWithScheduleDueDate(t *testing.T) {
4339
cat := MaintenanceCategory{Name: "DueDateCat"}
4440
require.NoError(t, store.db.Create(&cat).Error)
4541

46-
ptrTime := func(y, m, d int) *time.Time {
47-
t := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
48-
return &t
49-
}
5042
// Item with due date (no interval) should appear.
5143
require.NoError(t, store.db.Create(&MaintenanceItem{
5244
Name: "With DueDate", CategoryID: cat.ID,
@@ -204,10 +196,6 @@ func TestListExpiringWarranties(t *testing.T) {
204196
t.Parallel()
205197
store := newTestStore(t)
206198
now := time.Date(2026, 2, 8, 0, 0, 0, 0, time.UTC)
207-
ptrTime := func(y, m, d int) *time.Time {
208-
t := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
209-
return &t
210-
}
211199
// Expiring in 30 days -- should appear.
212200
require.NoError(
213201
t,
@@ -261,7 +249,6 @@ func TestListRecentServiceLogs(t *testing.T) {
261249
func TestYTDSpending(t *testing.T) {
262250
t.Parallel()
263251
store := newTestStore(t)
264-
ptr := func(v int64) *int64 { return &v }
265252

266253
cat := MaintenanceCategory{Name: "SpendCat"}
267254
require.NoError(t, store.db.Create(&cat).Error)
@@ -272,13 +259,13 @@ func TestYTDSpending(t *testing.T) {
272259
require.NoError(t, store.db.Create(&ServiceLogEntry{
273260
MaintenanceItemID: item.ID,
274261
ServicedAt: time.Date(2026, 1, 15, 0, 0, 0, 0, time.UTC),
275-
CostCents: ptr(5000),
262+
CostCents: new(int64(5000)),
276263
}).Error)
277264
// Last year -- should not count.
278265
require.NoError(t, store.db.Create(&ServiceLogEntry{
279266
MaintenanceItemID: item.ID,
280267
ServicedAt: time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC),
281-
CostCents: ptr(9999),
268+
CostCents: new(int64(9999)),
282269
}).Error)
283270

284271
yearStart := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
@@ -292,16 +279,16 @@ func TestYTDSpending(t *testing.T) {
292279
require.NoError(t, store.db.First(&pt).Error)
293280
require.NoError(t, store.db.Create(&Project{
294281
Title: "P1", ProjectTypeID: pt.ID, Status: ProjectStatusCompleted,
295-
ActualCents: ptr(20000),
282+
ActualCents: new(int64(20000)),
296283
}).Error)
297284
require.NoError(t, store.db.Create(&Project{
298285
Title: "P2", ProjectTypeID: pt.ID, Status: ProjectStatusInProgress,
299-
ActualCents: ptr(10000),
286+
ActualCents: new(int64(10000)),
300287
}).Error)
301288
// Project updated last year — still included (no date filter).
302289
oldProj := Project{
303290
Title: "P3", ProjectTypeID: pt.ID, Status: ProjectStatusCompleted,
304-
ActualCents: ptr(7777),
291+
ActualCents: new(int64(7777)),
305292
}
306293
require.NoError(t, store.db.Create(&oldProj).Error)
307294
require.NoError(t, store.db.Exec(
@@ -320,13 +307,12 @@ func TestTotalProjectSpendUnaffectedByEdits(t *testing.T) {
320307
// not change the spending total. The old updated_at filter caused edits
321308
// to inflate/deflate the YTD figure.
322309
store := newTestStore(t)
323-
ptr := func(v int64) *int64 { return &v }
324310

325311
var pt ProjectType
326312
require.NoError(t, store.db.First(&pt).Error)
327313
p := Project{
328314
Title: "Kitchen Remodel", ProjectTypeID: pt.ID,
329-
Status: ProjectStatusCompleted, ActualCents: ptr(50000),
315+
Status: ProjectStatusCompleted, ActualCents: new(int64(50000)),
330316
}
331317
require.NoError(t, store.db.Create(&p).Error)
332318

internal/data/store_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func TestServiceLogCRUD(t *testing.T) {
240240
require.NoError(t, store.CreateServiceLog(&ServiceLogEntry{
241241
MaintenanceItemID: maintID,
242242
ServicedAt: time.Date(2026, 2, 1, 0, 0, 0, 0, time.UTC),
243-
CostCents: func() *int64 { v := int64(15000); return &v }(),
243+
CostCents: new(int64(15000)),
244244
Notes: "vendor did it",
245245
}, Vendor{Name: "Test Plumber", Phone: "555-555-0001"}))
246246

@@ -1807,6 +1807,10 @@ func newTestStoreWithDemoData(t *testing.T, seed uint64) *Store {
18071807
return store
18081808
}
18091809

1810+
func ptrTime(y, m, d int) *time.Time {
1811+
return new(time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC))
1812+
}
1813+
18101814
func TestCountQuotesByProject(t *testing.T) {
18111815
t.Parallel()
18121816
store := newTestStore(t)

0 commit comments

Comments
 (0)