refactor: deduplicate engine internals, replace bubble sorts, remove dead code
Extract shared code that was duplicated across functions: - taskJSON struct (MarshalJSON/UnmarshalJSON) to package-level type - scanTask(scanner) helper for GetTask/GetTasks (~70 identical lines) - monthNames map for parseMonthName/parseDayAndMonth - applyNonDateAttribute helper for Apply/ApplyToNew - resolveDisplayID calls replace inline loops in FormatTaskListWithFormat Replace O(n²) bubble sorts with sort.Slice in all four report sort functions (sortByUrgency, NewestReport, NextReport, OldestReport). Remove dead code: formatTimeWithColor (unused, also used time.Now() instead of timeNow()), getCurrentTimestamp (unnecessary wrapper). Remove ~20 comments that restated the next line of code. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -83,30 +83,31 @@ type Task struct {
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
// taskJSON is the wire format for Task, using unix timestamps instead of time.Time.
|
||||
type taskJSON struct {
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
// MarshalJSON emits Task with unix timestamps (int64) instead of RFC3339 strings.
|
||||
func (t Task) MarshalJSON() ([]byte, error) {
|
||||
type taskJSON struct {
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
toUnix := func(tp *time.Time) *int64 {
|
||||
if tp == nil {
|
||||
return nil
|
||||
@@ -146,28 +147,6 @@ func (t Task) MarshalJSON() ([]byte, error) {
|
||||
|
||||
// UnmarshalJSON parses Task from JSON with unix timestamps (int64) and duration in seconds.
|
||||
func (t *Task) UnmarshalJSON(data []byte) error {
|
||||
type taskJSON struct {
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
var raw taskJSON
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
@@ -366,21 +345,13 @@ func CreateTaskWithModifier(description string, mod *Modifier) (*Task, error) {
|
||||
return task, nil
|
||||
}
|
||||
|
||||
// GetTask retrieves a task by UUID
|
||||
func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
db := GetDB()
|
||||
if db == nil {
|
||||
return nil, fmt.Errorf("database not initialized")
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
FROM tasks
|
||||
WHERE uuid = ?
|
||||
`
|
||||
// scanner is satisfied by both *sql.Row and *sql.Rows.
|
||||
type scanner interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
// scanTask reads a single task row from a scanner and populates all fields including tags.
|
||||
func scanTask(s scanner) (*Task, error) {
|
||||
task := &Task{}
|
||||
var (
|
||||
uuidStr string
|
||||
@@ -398,7 +369,7 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
annotationsStr interface{}
|
||||
)
|
||||
|
||||
err := db.QueryRow(query, taskUUID.String()).Scan(
|
||||
err := s.Scan(
|
||||
&task.ID,
|
||||
&uuidStr,
|
||||
&task.Status,
|
||||
@@ -417,22 +388,18 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
&parentUUIDStr,
|
||||
&annotationsStr,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get task: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Parse UUID
|
||||
task.UUID, err = uuid.Parse(uuidStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse UUID: %w", err)
|
||||
}
|
||||
|
||||
// Convert timestamps
|
||||
task.Created = time.Unix(created, 0)
|
||||
task.Modified = time.Unix(modified, 0)
|
||||
|
||||
// Convert nullable fields
|
||||
task.Project = sqlToStringPtr(project)
|
||||
task.Start = sqlToTime(start)
|
||||
task.End = sqlToTime(end)
|
||||
@@ -444,7 +411,6 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
|
||||
task.Annotations = sqlToAnnotations(annotationsStr)
|
||||
|
||||
// Load tags
|
||||
tags, err := task.GetTags()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load tags: %w", err)
|
||||
@@ -454,6 +420,29 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
return task, nil
|
||||
}
|
||||
|
||||
// GetTask retrieves a task by UUID
|
||||
func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
db := GetDB()
|
||||
if db == nil {
|
||||
return nil, fmt.Errorf("database not initialized")
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
FROM tasks
|
||||
WHERE uuid = ?
|
||||
`
|
||||
|
||||
task, err := scanTask(db.QueryRow(query, taskUUID.String()))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get task: %w", err)
|
||||
}
|
||||
|
||||
return task, nil
|
||||
}
|
||||
|
||||
// GetTasks retrieves all tasks with optional filtering
|
||||
func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
db := GetDB()
|
||||
@@ -489,76 +478,10 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
tasks := []*Task{}
|
||||
|
||||
for rows.Next() {
|
||||
task := &Task{}
|
||||
var (
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
annotationsStr interface{}
|
||||
)
|
||||
|
||||
err := rows.Scan(
|
||||
&task.ID,
|
||||
&uuidStr,
|
||||
&task.Status,
|
||||
&task.Description,
|
||||
&project,
|
||||
&task.Priority,
|
||||
&created,
|
||||
&modified,
|
||||
&start,
|
||||
&end,
|
||||
&due,
|
||||
&scheduled,
|
||||
&wait,
|
||||
&until,
|
||||
&recurDuration,
|
||||
&parentUUIDStr,
|
||||
&annotationsStr,
|
||||
)
|
||||
|
||||
task, err := scanTask(rows)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan task: %w", err)
|
||||
}
|
||||
|
||||
// Parse UUID
|
||||
task.UUID, err = uuid.Parse(uuidStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse UUID: %w", err)
|
||||
}
|
||||
|
||||
// Convert timestamps
|
||||
task.Created = time.Unix(created, 0)
|
||||
task.Modified = time.Unix(modified, 0)
|
||||
|
||||
// Convert nullable fields
|
||||
task.Project = sqlToStringPtr(project)
|
||||
task.Start = sqlToTime(start)
|
||||
task.End = sqlToTime(end)
|
||||
task.Due = sqlToTime(due)
|
||||
task.Scheduled = sqlToTime(scheduled)
|
||||
task.Wait = sqlToTime(wait)
|
||||
task.Until = sqlToTime(until)
|
||||
task.RecurrenceDuration = sqlToDuration(recurDuration)
|
||||
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
|
||||
task.Annotations = sqlToAnnotations(annotationsStr)
|
||||
|
||||
// Load tags
|
||||
tags, err := task.GetTags()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load tags: %w", err)
|
||||
}
|
||||
task.Tags = tags
|
||||
|
||||
tasks = append(tasks, task)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user