-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubjects.go
59 lines (49 loc) · 1.43 KB
/
subjects.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gol
import "fmt"
type Subject struct {
Container
name string
works []Work
}
// GetSubject returns the chosen subject's information
func GetSubject(subject string) (sbj Subject, err error) {
return getSubject(subject, false)
}
// GetSubject returns the chosen subject's information with more details
func GetSubjectDetails(subject string) (sbj Subject, err error) {
return getSubject(subject, true)
}
// GeneralGetSubject is a general function for GetSubject and GetSubjectDetails functions
func getSubject(s string, detail bool) (sbj Subject, err error) {
if !detail {
sbj.Container, err = MakeSubjectRequest(s)
} else {
sbj.Container, err = MakeDetailedSubjectRequest(s)
}
if err != nil {
return sbj, err
}
// verify if an error field is present in the returned data
if err := HasError(sbj.Container); err != nil {
return sbj, err
}
if v, ok := sbj.Path("name").Data().(string); ok {
sbj.name = v
}
return
}
// Works returns the works related to that subject.
// Note that the work fields won't get loaded (using the Work.load() method);
// If the works were loaded before, it will return sbj.works
func (sbj *Subject) Works() ([]Work, error) {
if len(sbj.works) > 0 {
return sbj.works, nil
}
for _, child := range sbj.Path("works").Children() {
sbj.works = append(sbj.works, Work{Container: child})
}
if len(sbj.works) == 0 {
return sbj.works, fmt.Errorf("Could not find works")
}
return sbj.works, nil
}