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