-
Notifications
You must be signed in to change notification settings - Fork 1
/
gloabalid.go
41 lines (33 loc) · 1.1 KB
/
gloabalid.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
package grokeddit
import (
"errors"
"strings"
)
/* Uniquely identifies any "thing" in reddit. Things processed by grokeddit are:
subreddits, links, and comments. */
type GlobalId struct {
Id ThingId // unique identifier
Kind KindType // the type of thing
}
/* Parses the global id format, as used by reddit. The format is "type_id".
Type must be t1, t2, t3, t4, or t5. id must be a base36 number. Error is
returned if the format is invalid. */
func ParseGlobalId(globalId string) (GlobalId, error) {
splitId := strings.Split(globalId, "_")
if len(splitId) != 2 {
return GlobalId{}, errors.New("Invalid global id \"" + globalId + "\"")
}
kind, error := ParseKind(splitId[0])
if error != nil {
return GlobalId{}, errors.New("Invalid global id : " + error.Error())
}
id, error := ParseId(splitId[1])
if error != nil {
return GlobalId{}, errors.New("Invalid global id : " + error.Error())
}
return GlobalId{id, kind}, nil
}
// Returns a string representation for the global ID, as used by reddit.
func (globalId GlobalId) String() string {
return globalId.Kind.String() + "_" + globalId.Id.String()
}