-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
79 lines (75 loc) · 1.58 KB
/
users.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package stackoverflow
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
"time"
)
// User describes a user
type User struct {
ID int
Reputation int
CreationDate time.Time
DisplayName string
LastAccessDate time.Time
WebsiteURL string
Location string
AboutMe string
Views int
UpVotes int
DownVotes int
Age int
AccountID int
ProfileImageURL string
}
func decodeUserAttr(attr xml.Attr, u *User) error {
var err error
name := strings.ToLower(attr.Name.Local)
v := attr.Value
switch name {
case "id":
u.ID, err = strconv.Atoi(v)
case "reputation":
u.Reputation, err = strconv.Atoi(v)
case "creationdate":
u.CreationDate, err = decodeTime(v)
case "displayname":
u.DisplayName = v
case "lastaccessdate":
u.LastAccessDate, err = decodeTime(v)
case "websiteurl":
u.WebsiteURL = v
case "location":
u.Location = v
case "aboutme":
u.AboutMe = v
case "views":
u.Views, err = strconv.Atoi(v)
case "upvotes":
u.UpVotes, err = strconv.Atoi(v)
case "downvotes":
u.DownVotes, err = strconv.Atoi(v)
case "accountid":
u.AccountID, err = strconv.Atoi(v)
case "age":
u.Age, err = strconv.Atoi(v)
case "profileimageurl":
u.ProfileImageURL = v
default:
err = fmt.Errorf("unknown user field: '%s'", name)
}
return err
}
func decodeUserRow(t xml.Token, u *User) error {
// have been checked before that this is "row" element
*u = User{}
e, _ := t.(xml.StartElement)
for _, attr := range e.Attr {
err := decodeUserAttr(attr, u)
if err != nil {
return err
}
}
return nil
}