-
Notifications
You must be signed in to change notification settings - Fork 0
/
badges.go
49 lines (45 loc) · 869 Bytes
/
badges.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
package stackoverflow
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
"time"
)
// Badge tells which badge a given user has
type Badge struct {
ID int
UserID int
Name string
Date time.Time
}
func decodeBadgeAttr(attr xml.Attr, b *Badge) error {
var err error
name := strings.ToLower(attr.Name.Local)
v := attr.Value
switch name {
case "id":
b.ID, err = strconv.Atoi(v)
case "userid":
b.UserID, err = strconv.Atoi(v)
case "name":
b.Name = v
case "date":
b.Date, err = decodeTime(v)
default:
err = fmt.Errorf("unknown badge field: '%s'", name)
}
return err
}
func decodeBadgeRow(t xml.Token, b *Badge) error {
// have been checked before that this is "row" element
*b = Badge{}
e, _ := t.(xml.StartElement)
for _, attr := range e.Attr {
err := decodeBadgeAttr(attr, b)
if err != nil {
return err
}
}
return nil
}