/
util.go
79 lines (69 loc) · 1.62 KB
/
util.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 greader_api
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
)
func (r *GReader) renderErr(ctx context.Context, writer http.ResponseWriter, err error) {
if err != nil {
writer.WriteHeader(400)
writer.Write([]byte(err.Error()))
}
}
func (r *GReader) renderData(ctx context.Context, writer http.ResponseWriter, data interface{}) {
if data == nil {
writer.WriteHeader(200)
return
}
switch data := data.(type) {
case string:
writer.Write([]byte(data))
default:
bs, _ := json.Marshal(data)
writer.Header().Set("Content-Type", "application/json; charset=UTF-8")
writer.Write(bs)
}
}
func (r *GReader) mustJson(req HttpReader) error {
if res := req.QueryString("output"); res != "" && res != "json" {
panic("output must be json")
return errors.New("output must be json")
}
return nil
}
// get label tag name
//
// "user/-/label/<name>" -> "<name>"
func getUserLabelName(s string) string {
if !strings.HasPrefix(s, "user/-/label/") {
return s
}
return s[strings.Index(s, "/label/")+len("/label/"):]
}
func buildUserLabelName(s string) string {
if strings.HasPrefix(s, "user/-/label/") {
return s
}
return "user/-/label/" + s
}
func getFeedID(feedID string) string {
if strings.HasPrefix(feedID, "feed/") {
return feedID[len("feed/"):]
}
return feedID
}
// get tagged item id
//
// tag:google.com,2005:reader/item/<id> -> <id>
func getTaggedItemHexID(s string) string {
return s[strings.Index(s, "/item/")+len("/item/"):]
}
func hex16ToInt(s string) (int64, error) {
return strconv.ParseInt(s, 16, 64)
}
func intToHex16(i int64) string {
return strconv.FormatInt(i, 16)
}