-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.go
105 lines (95 loc) · 2.72 KB
/
item.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package qiisync
import (
"strings"
"time"
)
const defaultDataFormat = "20060102"
// Item is a structure that represents the QiitaAPI.
//
// See also https://qiita.com/api/v2/docs#%E6%8A%95%E7%A8%BF.
type Item struct {
ID string `json:"id"`
URL string `json:"url"`
User User `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
RenderedBody string `json:"rendered_body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Tags []*Tag `json:"tags"`
Private bool `json:"private"`
}
// Tag is a structure that represents the QiitaAPI.
//
// See also https://qiita.com/api/v2/docs#%E6%8A%95%E7%A8%BF.
type Tag struct {
Name string `json:"name"`
Versions []string `json:"versions"`
}
// User is a structure that represents the QiitaAPI.
//
// See also https://qiita.com/api/v2/docs#%E6%8A%95%E7%A8%BF.
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
// PostItem is a structure that represents the Qiita API
// required to post an Article to Qiita.
//
// See also https://qiita.com/api/v2/docs#post-apiv2items.
type PostItem struct {
Body string `json:"body"`
Private bool `json:"private"`
Tags []*Tag `json:"tags"`
Title string `json:"title"`
ID string
URL string
FilePath string
}
// PostItemResult is a structure that represents the response body
// when an Article is posted to Qiita.
//
// See also https://qiita.com/api/v2/docs#post-apiv2items.
type PostItemResult struct {
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
ID string `json:"id"`
Private bool `json:"private"`
Tags []*Tag `json:"tags"`
Title string `json:"title"`
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
User User `json:"user"`
}
func dateFormat(time time.Time) string {
return time.Format(defaultDataFormat)
}
// unmarshalTag converts a Tag to a string.
func unmarshalTag(Tags []*Tag) string {
tags := make([]string, len(Tags))
for i := range Tags {
tags[i] = Tags[i].Name
if len(Tags[i].Versions) >= 1 {
tags[i] += ":" + strings.Join(Tags[i].Versions, ":")
}
}
return strings.Join(tags, ",")
}
// MarshalTag converts a string to a Tag.
func MarshalTag(tagString string) []*Tag {
var tags []*Tag
for _, v := range strings.Split(tagString, ",") {
tag := strings.Split(v, ":")
// Encoding a nil slice into a JSON will result in a null slice,
// so we define an empty slice.
version := []string{}
for i := 1; i < len(tag); i++ {
version = append(version, tag[i])
}
tags = append(tags, &Tag{
Name: tag[0],
Versions: version,
})
}
return tags
}