-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
121 lines (105 loc) · 2.55 KB
/
article.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package lingvo
//go:generate stringer -type=NodeType
import (
"context"
"encoding/json"
"fmt"
)
const (
endpointArticle = "api/v1/Article"
)
// NodeType is an article node type
type NodeType int
// Possible node types
const (
Comment NodeType = iota
Paragraph
Text
List
ListItem
Examples
ExampleItem
Example
CardRefs
CardRefItem
CardRef
Transcription
Abbrev
Caption
Sound
Ref
Unsupported
)
var nodeTypes = []NodeType{
Comment, Paragraph, Text, List, ListItem, Examples,
ExampleItem, Example, CardRefs, CardRefItem, CardRef,
Transcription, Abbrev, Caption, Sound, Ref, Unsupported,
}
var str2nodeType = make(map[string]NodeType)
func init() {
for _, n := range nodeTypes {
str2nodeType[n.String()] = n
}
}
// MarshalJSON implement json.Marshaler interface
func (n NodeType) MarshalJSON() ([]byte, error) {
return json.Marshal(n.String())
}
// UnmarshalJSON implements json.Unmarshaler interface
func (n *NodeType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
nt, ok := str2nodeType[s]
if !ok {
return ErrInvalidNodeType
}
*n = nt
return nil
}
// Article is a single article
type Article struct {
Title string `json:"Title"`
Markup []*ArticleNode `json:"TitleMarkup"`
Dictionary string `json:"Dictionary"`
ID string `json:"ArticleId"`
Body []*ArticleNode `json:"Body"`
}
// ArticleNode is a single entry in an article
type ArticleNode struct {
Node NodeType `json:"Node"`
FullText string `json:"FullText"`
FileName string `json:"FileName"`
Text string `json:"Text"`
Dictionary string `json:"Dictionary"`
ID string `json:"ArticleId"`
IsItalics bool `json:"IsItalics"`
IsAccent bool `json:"IsAccent"`
IsOptional bool `json:"IsOptional"`
Items []*ArticleNode `json:"Items"`
Markup []*ArticleNode `json:"Markup"`
}
// GetArticle returns the article with heading from dict.
func (c *Client) GetArticle(ctx context.Context, heading, dict string, from, to Lang) (*Article, error) {
dict = fmt.Sprintf("%s (%s-%s)", dict, from, to)
u, err := addOptions(endpointArticle,
option{"heading", heading},
option{"dict", dict},
option{"srcLang", from.code()},
option{"dstLang", to.code()},
)
if err != nil {
return nil, err
}
req, err := c.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
article := new(Article)
err = c.Do(ctx, req, article)
if err != nil {
return nil, err
}
return article, nil
}