]> git.lizzy.rs Git - go-anidb.git/blob - group.go
Modernize
[go-anidb.git] / group.go
1 package anidb
2
3 import (
4         "encoding/json"
5         "strconv"
6         "time"
7 )
8
9 // See the constants list for valid values.
10 type GroupRelationType int
11
12 const (
13         GroupParticipantIn = GroupRelationType(1 + iota)
14         GroupParentOf
15         _
16         GroupMergedFrom
17         GroupNowKnownAs
18         GroupOther
19
20         GroupChildOf = GroupRelationType(102)
21 )
22
23 func (gr GroupRelationType) String() string {
24         switch gr {
25         case GroupParticipantIn:
26                 return "Participated In"
27         case GroupParentOf:
28                 return "Parent Of"
29         case GroupMergedFrom:
30                 return "Merged From"
31         case GroupNowKnownAs:
32                 return "Now Known As"
33         case GroupOther:
34                 return "Other"
35         case GroupChildOf:
36                 return "Child Of"
37         default:
38                 return "Unknown"
39         }
40 }
41
42 type GroupRelations map[GID]GroupRelationType
43
44 func (gr GroupRelations) MarshalJSON() ([]byte, error) {
45         generic := make(map[string]int, len(gr))
46         for k, v := range gr {
47                 generic[strconv.Itoa(int(k))] = int(v)
48         }
49         return json.Marshal(generic)
50 }
51
52 func (gr GroupRelations) UnmarshalJSON(b []byte) error {
53         var generic map[string]int
54         if err := json.Unmarshal(b, &generic); err != nil {
55                 return err
56         }
57         for k, v := range generic {
58                 ik, err := strconv.ParseInt(k, 10, 32)
59                 if err != nil {
60                         return err
61                 }
62
63                 gr[GID(ik)] = GroupRelationType(v)
64         }
65
66         return nil
67 }
68
69 type Group struct {
70         GID GID
71
72         Name      string // Full name
73         ShortName string // Abbreviated name
74
75         IRC     string // irc: schema format
76         URL     string
77         Picture string
78
79         Founded   time.Time
80         Disbanded time.Time
81
82         LastRelease  time.Time
83         LastActivity time.Time
84
85         Rating     Rating
86         AnimeCount int // Number of anime this group has worked on
87         FileCount  int // Number of files this group has released
88
89         RelatedGroups GroupRelations
90
91         Cached time.Time
92 }