diff options
author | Teddy Wing | 2017-03-12 17:43:20 +0100 |
---|---|---|
committer | Teddy Wing | 2017-03-12 17:49:36 +0100 |
commit | 89f766854443a6f7ae20c330547083fd0e075ba9 (patch) | |
tree | da28ed20680fbff2de7c6adb43906ba96d1e2e1c /timetask/fields.go | |
parent | d9ca7b7d412f34f0d1f93a04c7c566f041b99524 (diff) | |
download | timetasker-89f766854443a6f7ae20c330547083fd0e075ba9.tar.bz2 |
fields.go: Add methods that allow us to extract objects by name
We want to be able to get Client, Project, etc. objects given their
name. These functions will look through the fields or objects containing
a list of the thing looked for and return if if its name matches the
search string.
Originally had the idea to generalise the body of the function, but had
trouble making the types generic. Still on the table for later if we
want to deduplicate the code. For now I'm leaving in the repetition
because it works and I don't care with this project.
Diffstat (limited to 'timetask/fields.go')
-rw-r--r-- | timetask/fields.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/timetask/fields.go b/timetask/fields.go index 4e3ac7c..6d1fa69 100644 --- a/timetask/fields.go +++ b/timetask/fields.go @@ -1,5 +1,7 @@ package timetask +import "fmt" + type Client struct { ID uint Name string @@ -31,3 +33,64 @@ type Fields struct { PersonID uint `yaml:"person_id"` Clients []Client } + +// func thingByName(things []interface{}, name string) (*interface{}, error) { +// for _, thing := range things { +// t = IDName(thing.(IDName)) +// if t.Name == name { +// return &thing, nil +// } +// } +// +// return nil, fmt.Errorf("Thing not found") +// } + +func (f *Fields) ClientByName(client_name string) (*Client, error) { + for _, client := range f.Clients { + if client.Name == client_name { + return &client, nil + } + } + + return nil, fmt.Errorf("Client %s not found", client_name) +} + +func (c *Client) ProjectByName(project_name string) (*Project, error) { + for _, project := range c.Projects { + if project.Name == project_name { + return &project, nil + } + } + + return nil, fmt.Errorf("Project %s not found", project_name) +} + +func (p *Project) ModuleByName(module_name string) (*Module, error) { + for _, module := range p.Modules { + if module.Name == module_name { + return &module, nil + } + } + + return nil, fmt.Errorf("Module %s not found", module_name) +} + +func (p *Project) TaskByName(task_name string) (*Task, error) { + for _, task := range p.Tasks { + if task.Name == task_name { + return &task, nil + } + } + + return nil, fmt.Errorf("Task %s not found", task_name) +} + +func (p *Project) WorkTypeByName(work_type_name string) (*WorkType, error) { + for _, work_type := range p.WorkTypes { + if work_type.Name == work_type_name { + return &work_type, nil + } + } + + return nil, fmt.Errorf("Work type %s not found", work_type_name) +} |