Handle UDAs for editing; Fix layout; Add annotations
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package taskwarrior
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math"
|
||||
@ -13,6 +14,23 @@ const (
|
||||
dtformat = "20060102T150405Z"
|
||||
)
|
||||
|
||||
type UdaType string
|
||||
|
||||
const (
|
||||
UdaTypeString UdaType = "string"
|
||||
UdaTypeDate UdaType = "date"
|
||||
UdaTypeNumeric UdaType = "numeric"
|
||||
UdaTypeDuration UdaType = "duration"
|
||||
)
|
||||
|
||||
type Uda struct {
|
||||
Name string
|
||||
Type UdaType
|
||||
Label string
|
||||
Values []string
|
||||
Default string
|
||||
}
|
||||
|
||||
type Annotation struct {
|
||||
Entry string `json:"entry,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
@ -22,12 +40,14 @@ func (a Annotation) String() string {
|
||||
return fmt.Sprintf("%s %s", a.Entry, a.Description)
|
||||
}
|
||||
|
||||
type Tasks []*Task
|
||||
|
||||
type Task struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
Uuid string `json:"uuid,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Project string `json:"project"`
|
||||
Priority string `json:"priority"`
|
||||
Id int64 `json:"id,omitempty"`
|
||||
Uuid string `json:"uuid,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Project string `json:"project"`
|
||||
// Priority string `json:"priority"`
|
||||
Status string `json:"status,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
VirtualTags []string `json:"-"`
|
||||
@ -114,8 +134,8 @@ func (t *Task) GetString(fieldWFormat string) string {
|
||||
}
|
||||
return t.Project
|
||||
|
||||
case "priority":
|
||||
return t.Priority
|
||||
// case "priority":
|
||||
// return t.Priority
|
||||
|
||||
case "status":
|
||||
return t.Status
|
||||
@ -186,6 +206,7 @@ func (t *Task) GetString(fieldWFormat string) string {
|
||||
return t.Recur
|
||||
|
||||
default:
|
||||
// TODO: format according to UDA type
|
||||
if val, ok := t.Udas[field]; ok {
|
||||
if strVal, ok := val.(string); ok {
|
||||
return strVal
|
||||
@ -230,7 +251,69 @@ func (t *Task) RemoveTag(tag string) {
|
||||
}
|
||||
}
|
||||
|
||||
type Tasks []*Task
|
||||
func (t *Task) UnmarshalJSON(data []byte) error {
|
||||
type Alias Task
|
||||
task := Alias{}
|
||||
|
||||
if err := json.Unmarshal(data, &task); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*t = Task(task)
|
||||
|
||||
m := make(map[string]any)
|
||||
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
delete(m, "id")
|
||||
delete(m, "uuid")
|
||||
delete(m, "description")
|
||||
delete(m, "project")
|
||||
// delete(m, "priority")
|
||||
delete(m, "status")
|
||||
delete(m, "tags")
|
||||
delete(m, "depends")
|
||||
delete(m, "urgency")
|
||||
delete(m, "parent")
|
||||
delete(m, "due")
|
||||
delete(m, "wait")
|
||||
delete(m, "scheduled")
|
||||
delete(m, "until")
|
||||
delete(m, "start")
|
||||
delete(m, "end")
|
||||
delete(m, "entry")
|
||||
delete(m, "modified")
|
||||
delete(m, "recur")
|
||||
delete(m, "annotations")
|
||||
t.Udas = m
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) MarshalJSON() ([]byte, error) {
|
||||
type Alias Task
|
||||
task := Alias(*t)
|
||||
|
||||
knownFields, err := json.Marshal(task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var knownMap map[string]any
|
||||
if err := json.Unmarshal(knownFields, &knownMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range t.Udas {
|
||||
if value != nil && value != "" {
|
||||
knownMap[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return json.Marshal(knownMap)
|
||||
}
|
||||
|
||||
type Context struct {
|
||||
Name string
|
||||
@ -418,3 +501,15 @@ func ValidateDate(s string) error {
|
||||
|
||||
return fmt.Errorf("invalid date")
|
||||
}
|
||||
|
||||
func ValidateNumeric(s string) error {
|
||||
if _, err := strconv.ParseFloat(s, 64); err != nil {
|
||||
return fmt.Errorf("invalid number")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateDuration(s string) error {
|
||||
// TODO: implement duration validation
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user