JSON Marshal/Unmarshal Tags

This commit is contained in:
Brian Buller 2023-08-23 10:58:10 -05:00
parent b3e09780fb
commit 13cd41aa3f
2 changed files with 39 additions and 39 deletions

View File

@ -19,15 +19,15 @@ var (
)
type Timer struct {
Id int // Internal timer id
Original string // Original raw timer text
StartDate time.Time
FinishDate time.Time
Finished bool
Notes string // Notes part of timer text
Projects []string
Contexts []string
AdditionalTags map[string]string // Addon tags will be available here
Id int `json:"id"` // Internal timer id
Original string `json:"original"` // Original raw timer text
StartDate time.Time `json:"startDate"`
FinishDate time.Time `json:"finishDate"`
Finished bool `json:"finished"`
Notes string `json:"notes"` // Notes part of timer text
Projects []string `json:"projects"`
Contexts []string `json:"contexts"`
AdditionalTags map[string]string `json:"additionalTags"` // Addon tags will be available here
}
// String returns a complete timer string in timer.txt format.

View File

@ -14,17 +14,17 @@ import (
// TimerList represents a list of timer.txt timer entries.
// It is usually loaded from a whole timer.txt file.
type TimerList struct {
timers []*Timer
sortFlag int
Timers []*Timer `json:"timers"`
SortFlag int `json:"sortFlag"`
}
// NewTimerList creates a new empty TimerList.
func NewTimerList() *TimerList { return &TimerList{} }
func (timerlist *TimerList) Size() int { return len(timerlist.timers) }
func (timerlist *TimerList) GetTimerSlice() []*Timer { return timerlist.timers }
func (timerlist *TimerList) Size() int { return len(timerlist.Timers) }
func (timerlist *TimerList) GetTimerSlice() []*Timer { return timerlist.Timers }
func (timerlist *TimerList) Contains(t *Timer) bool {
for _, tmr := range timerlist.timers {
for _, tmr := range timerlist.Timers {
if tmr == t {
return true
}
@ -35,11 +35,11 @@ func (timerlist *TimerList) GetActiveOrMostRecent() (*Timer, error) {
var found *Timer
var latest time.Time
activeTimers := timerlist.Filter(func(t *Timer) bool { return !t.Finished })
if len(activeTimers.timers) > 0 {
if len(activeTimers.Timers) > 0 {
return activeTimers.GetMostRecentTimer()
}
for _, t := range timerlist.timers {
for _, t := range timerlist.Timers {
if t.FinishDate.IsZero() {
if t.StartDate.After(latest) {
found = t
@ -60,7 +60,7 @@ func (timerlist *TimerList) GetActiveOrMostRecent() (*Timer, error) {
func (timerlist *TimerList) GetMostRecentTimer() (*Timer, error) {
var found *Timer
var latest time.Time
for _, t := range timerlist.timers {
for _, t := range timerlist.Timers {
if t.FinishDate.IsZero() {
if t.StartDate.After(latest) {
found = t
@ -107,7 +107,7 @@ func (timerlist *TimerList) GetTimersWithProject(project string) *TimerList {
func (timerlist *TimerList) GetContexts() []string {
var ret []string
added := make(map[string]bool)
for _, tmr := range timerlist.timers {
for _, tmr := range timerlist.Timers {
for _, c := range tmr.Contexts {
if !added[c] {
ret = append(ret, c)
@ -121,7 +121,7 @@ func (timerlist *TimerList) GetContexts() []string {
func (timerlist *TimerList) GetProjects() []string {
var ret []string
added := make(map[string]bool)
for _, tmr := range timerlist.timers {
for _, tmr := range timerlist.Timers {
for _, p := range tmr.Projects {
if !added[p] {
ret = append(ret, p)
@ -135,7 +135,7 @@ func (timerlist *TimerList) GetProjects() []string {
func (timerlist *TimerList) GetTagKVList() []string {
var ret []string
added := make(map[string]bool)
for _, tmr := range timerlist.timers {
for _, tmr := range timerlist.Timers {
for k, v := range tmr.AdditionalTags {
tag := fmt.Sprintf("%s:%s", k, v)
if !added[tag] {
@ -150,7 +150,7 @@ func (timerlist *TimerList) GetTagKVList() []string {
func (timerlist *TimerList) GetTagKeys() []string {
var ret []string
added := make(map[string]bool)
for _, tmr := range timerlist.timers {
for _, tmr := range timerlist.Timers {
for k := range tmr.AdditionalTags {
if !added[k] {
ret = append(ret, k)
@ -164,7 +164,7 @@ func (timerlist *TimerList) GetTagKeys() []string {
func (timerlist *TimerList) GetTagValuesForKey(key string) []string {
var ret []string
added := make(map[string]bool)
for _, tmr := range timerlist.timers {
for _, tmr := range timerlist.Timers {
if v, ok := tmr.AdditionalTags[key]; ok && !added[v] {
ret = append(ret, v)
added[v] = true
@ -176,9 +176,9 @@ func (timerlist *TimerList) GetTagValuesForKey(key string) []string {
func (timerlist *TimerList) GetActiveTimers() *TimerList {
t := *NewTimerList()
for _, v := range timerlist.timers {
for _, v := range timerlist.Timers {
if v.FinishDate.IsZero() {
t.timers = append(t.timers, v)
t.Timers = append(t.Timers, v)
}
}
return &t
@ -187,7 +187,7 @@ func (timerlist *TimerList) GetActiveTimers() *TimerList {
// String returns a complete list of timers in timer.txt format.
func (timerlist *TimerList) String() string {
var ret string
for _, timer := range timerlist.timers {
for _, timer := range timerlist.Timers {
ret += fmt.Sprintf("%s\n", timer.String())
}
return ret
@ -195,23 +195,23 @@ func (timerlist *TimerList) String() string {
// AddTimer prepends a Timer to the current TimerList and takes care to set the Timer.Id correctly
func (timerlist *TimerList) AddTimer(timer *Timer) {
timerlist.timers = append(timerlist.timers, timer)
timerlist.Timers = append(timerlist.Timers, timer)
timerlist.refresh()
}
// AddTimers adds all passed in timers to the list, sorts the list, then updates the Timer.Id values.
func (timerlist *TimerList) AddTimers(timers []*Timer) {
timerlist.timers = append(timerlist.timers, timers...)
timerlist.Timers = append(timerlist.Timers, timers...)
timerlist.Sort(SortStartDateAsc)
}
func (timerlist *TimerList) Combine(other *TimerList) { timerlist.AddTimers(other.timers) }
func (timerlist *TimerList) Combine(other *TimerList) { timerlist.AddTimers(other.Timers) }
// GetTimer returns the Timer with the given timer 'id' from the TimerList.
// Returns an error if Timer could not be found.
func (timerlist *TimerList) GetTimer(id int) (*Timer, error) {
for i := range timerlist.timers {
if timerlist.timers[i].Id == id {
return timerlist.timers[i], nil
for i := range timerlist.Timers {
if timerlist.Timers[i].Id == id {
return timerlist.Timers[i], nil
}
}
return nil, errors.New("timer not found")
@ -223,7 +223,7 @@ func (timerlist *TimerList) RemoveTimerById(id int) error {
found := false
var remIdx int
var t *Timer
for remIdx, t = range timerlist.timers {
for remIdx, t = range timerlist.Timers {
if t.Id == id {
found = true
break
@ -232,7 +232,7 @@ func (timerlist *TimerList) RemoveTimerById(id int) error {
if !found {
return errors.New("timer not found")
}
timerlist.timers = append(timerlist.timers[:remIdx], timerlist.timers[remIdx+1:]...)
timerlist.Timers = append(timerlist.Timers[:remIdx], timerlist.Timers[remIdx+1:]...)
timerlist.refresh()
return nil
}
@ -243,7 +243,7 @@ func (timerlist *TimerList) RemoveTimer(timer Timer) error {
found := false
var remIdx int
var t *Timer
for remIdx, t = range timerlist.timers {
for remIdx, t = range timerlist.Timers {
if t.String() == timer.String() {
found = true
break
@ -252,7 +252,7 @@ func (timerlist *TimerList) RemoveTimer(timer Timer) error {
if !found {
return errors.New("timer not found")
}
timerlist.timers = append(timerlist.timers[:remIdx], timerlist.timers[remIdx+1:]...)
timerlist.Timers = append(timerlist.Timers[:remIdx], timerlist.Timers[remIdx+1:]...)
timerlist.refresh()
return nil
}
@ -277,9 +277,9 @@ func (timerlist *TimerList) ArchiveTimerToFile(timer Timer, filename string) err
// bool), and returns a new TimerList. The original TimerList is not modified.
func (timerlist *TimerList) Filter(predicate func(*Timer) bool) *TimerList {
var newList TimerList
for _, t := range timerlist.timers {
for _, t := range timerlist.Timers {
if predicate(t) {
newList.timers = append(newList.timers, t)
newList.Timers = append(newList.Timers, t)
}
}
return &newList
@ -288,7 +288,7 @@ func (timerlist *TimerList) Filter(predicate func(*Timer) bool) *TimerList {
// LoadFromFile loads a TimerList from *os.File.
// Note: This will clear the current TimerList and overwrite it's contents with whatever is in *os.File.
func (timerlist *TimerList) LoadFromFile(file *os.File) error {
timerlist.timers = []*Timer{} // Empty timerlist
timerlist.Timers = []*Timer{} // Empty timerlist
timerId := 1
scanner := bufio.NewScanner(file)
for scanner.Scan() {
@ -303,7 +303,7 @@ func (timerlist *TimerList) LoadFromFile(file *os.File) error {
}
timer.Id = timerId
timerId++
timerlist.timers = append(timerlist.timers, timer)
timerlist.Timers = append(timerlist.Timers, timer)
}
if err := scanner.Err(); err != nil {
return err