-
Notifications
You must be signed in to change notification settings - Fork 2
/
subject.go
311 lines (260 loc) · 11.1 KB
/
subject.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package wanikaniapi
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Exported functions
//
//
//
//////////////////////////////////////////////////////////////////////////////
// SubjectGet retrieves a specific subject by its ID. The structure of the
// response depends on the subject type.
func (c *Client) SubjectGet(params *SubjectGetParams) (*Subject, error) {
obj := &Subject{}
err := c.request("GET", "/v2/subjects/"+strconv.FormatInt(int64(*params.ID), 10), params, nil, obj)
return obj, err
}
// SubjectList returns a collection of all subjects, ordered by ascending
// CreatedAt, 1000 at a time.
func (c *Client) SubjectList(params *SubjectListParams) (*SubjectPage, error) {
obj := &SubjectPage{}
err := c.request("GET", "/v2/subjects", params, nil, obj)
return obj, err
}
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Exported constants/types
//
//
//
//////////////////////////////////////////////////////////////////////////////
// Subject represents radicals, kanji, and vocabulary that are learned through
// lessons and reviews. They contain basic dictionary information, such as
// meanings and/or readings, and information about their relationship to other
// items with WaniKani, like their level.
type Subject struct {
Object
// KanjiData is data on a kanji subject. Populated only if he subject is a
// kanji.
KanjiData *SubjectKanjiData
// RadicalData is data on a radical subject. Populated only if he subject
// is a radical.
RadicalData *SubjectRadicalData
// VocabularyData is data on a vocabulary subject. Populated only if he
// subject is a vocabulary.
VocabularyData *SubjectVocabularyData
}
// UnmarshalJSON is a custom JSON unmarshaling function for Subject.
func (s *Subject) UnmarshalJSON(data []byte) error {
type subject Subject
var v subject
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*s = Subject(v)
var objMap map[string]json.RawMessage
if err := json.Unmarshal(data, &objMap); err != nil {
return err
}
switch s.Object.ObjectType {
case ObjectTypeKanji:
if _, ok := objMap["data"]; ok {
s.KanjiData = &SubjectKanjiData{}
if err := json.Unmarshal(objMap["data"], s.KanjiData); err != nil {
return fmt.Errorf("decoding kanji from subject: %w", err)
}
}
case ObjectTypeRadical:
if _, ok := objMap["data"]; ok {
s.RadicalData = &SubjectRadicalData{}
if err := json.Unmarshal(objMap["data"], s.RadicalData); err != nil {
return fmt.Errorf("decoding radical from subject: %w", err)
}
}
case ObjectTypeVocabulary:
if _, ok := objMap["data"]; ok {
s.VocabularyData = &SubjectVocabularyData{}
if err := json.Unmarshal(objMap["data"], s.VocabularyData); err != nil {
return fmt.Errorf("decoding vocabulary from subject: %w", err)
}
}
}
return nil
}
// SubjectCommonData is common data available on all subject types regardless
// of whether they're kanji, radical, or vocabulary.
type SubjectCommonData struct {
AuxiliaryMeanings []*SubjectAuxiliaryMeaningObject `json:"auxiliary_meanings"`
CreatedAt time.Time `json:"created_at"`
DocumentURL string `json:"document_url"`
HiddenAt *time.Time `json:"hidden_at"`
Level int `json:"level"`
LessonPosition int `json:"lesson_position"`
Meanings []*SubjectMeaningObject `json:"meanings"`
Slug string `json:"slug"`
SpacedRepetitionSystemID WKID `json:"spaced_repetition_system_id"`
}
// SubjectKanjiData is data on a kanji subject.
type SubjectKanjiData struct {
SubjectCommonData
AmalgamationSubjectIDs []WKID `json:"amalgamation_subject_ids"`
Characters string `json:"characters"`
ComponentSubjectIDs []WKID `json:"component_subject_ids"`
MeaningHint *string `json:"meaning_hint"`
ReadingHint *string `json:"reading_hint"`
ReadingMnemonic string `json:"mnemonic_hint"`
Readings []*SubjectKanjiReading `json:"readings"`
VisuallySimilarSubjectIDs []WKID `json:"visually_similar_subject_ids"`
}
// SubjectKanjiReading is reading data on a kanji subject.
type SubjectKanjiReading struct {
AcceptedAnswer bool `json:"accepted_answer"`
Primary bool `json:"primary"`
Reading string `json:"reading"`
Type SubjectKanjiReadingType `json:"type"`
}
// SubjectKanjiReadingType is the type of a kanji reading.
type SubjectKanjiReadingType string
// All possible types of kanji readings.
const (
SubjectKanjiReadingTypeKunyomi SubjectKanjiReadingType = "kunyomi"
SubjectKanjiReadingTypeNanori SubjectKanjiReadingType = "nanori"
SubjectKanjiReadingTypeOnyomi SubjectKanjiReadingType = "onyomi"
)
// SubjectRadicalCharacterImage represents an image for a radical. Unlike kanji
// or vocabulary, radicals cannot always be represented by a unicode glyph.
type SubjectRadicalCharacterImage struct {
ContentType string `json:"content_type"`
Metadata map[SubjectRadicalCharacterImageMetadataKey]interface{} `json:"metadata"`
URL string `json:"url"`
}
// SubjectRadicalCharacterImageMetadataKey is a key for character image metadata.
type SubjectRadicalCharacterImageMetadataKey string
// All possible values of radical character image metadata keys.
const (
SubjectRadicalCharacterImageMetadataKeyColor SubjectRadicalCharacterImageMetadataKey = "color"
SubjectRadicalCharacterImageMetadataKeyDimensions SubjectRadicalCharacterImageMetadataKey = "dimensions"
SubjectRadicalCharacterImageMetadataKeyInlineStyles SubjectRadicalCharacterImageMetadataKey = "inline_styles"
SubjectRadicalCharacterImageMetadataKeyStyleName SubjectRadicalCharacterImageMetadataKey = "style_name"
)
// SubjectRadicalData is data on a radical subject.
type SubjectRadicalData struct {
SubjectCommonData
AmalgamationSubjectIDs []WKID `json:"amalgamation_subject_ids"`
CharacterImages []*SubjectRadicalCharacterImage `json:"character_images"`
Characters *string `json:"characters"`
}
// SubjectVocabularyContextSentence represents a vocabulary context sentence.
type SubjectVocabularyContextSentence struct {
// EN is the English translation of the sentence.
EN string `json:"en"`
// JA is a Japanese context context sentence.
JA string `json:"ja"`
}
// SubjectVocabularyData is data on a vocabulary subject.
type SubjectVocabularyData struct {
SubjectCommonData
Characters string `json:"characters"`
ComponentSubjectIDs []WKID `json:"component_subject_ids"`
ContextSentences []*SubjectVocabularyContextSentence `json:"context_sentences"`
MeaningMnemonic string `json:"meaning_mnenomic"`
PartsOfSpeech []string `json:"parts_of_speech"`
PronounciationAudios []*SubjectVocabularyPronounciationAudio `json:"pronounciation_audios"`
Readings []*SubjectVocabularyReading `json:"subject_vocabulary_reading"`
}
// SubjectVocabularyPronounciationAudio represets an audio object for
// vocabulary pronounciation.
type SubjectVocabularyPronounciationAudio struct {
ContentType string `json:"content_type"`
Metadata map[SubjectVocabularyPronounciationAudioMetadataKey]interface{} `json:"metadata"`
URL string `json:"url"`
}
// SubjectVocabularyPronounciationAudioMetadataKey is a key for pronounciation
// audio metadata.
type SubjectVocabularyPronounciationAudioMetadataKey string
// All possible values of vocabulary pronounciation audio metadata keys.
const (
SubjectVocabularyPronounciationAudioMetadataKeyGender SubjectVocabularyPronounciationAudioMetadataKey = "gender"
SubjectVocabularyPronounciationAudioMetadataKeyPronounciation SubjectVocabularyPronounciationAudioMetadataKey = "pronounciation"
SubjectVocabularyPronounciationAudioMetadataKeySourceID SubjectVocabularyPronounciationAudioMetadataKey = "source_id"
SubjectVocabularyPronounciationAudioMetadataKeyVoiceActorID SubjectVocabularyPronounciationAudioMetadataKey = "voice_actor_id"
SubjectVocabularyPronounciationAudioMetadataKeyVoiceActorName SubjectVocabularyPronounciationAudioMetadataKey = "voice_actor_name"
SubjectVocabularyPronounciationAudioMetadataKeyVoiceDescription SubjectVocabularyPronounciationAudioMetadataKey = "voice_description"
)
// SubjectVocabularyReading is reading data on a vocabulary subject.
type SubjectVocabularyReading struct {
AcceptedAnswer bool `json:"accepted_answer"`
Primary bool `json:"primary"`
Reading string `json:"reading"`
}
// SubjectGetParams are parameters for SubjectGet.
type SubjectGetParams struct {
Params
ID *WKID
}
// SubjectListParams are parameters for SubjectList.
type SubjectListParams struct {
ListParams
Params
IDs []WKID
Hidden *bool
Levels []int
Slugs []string
Types []string
UpdatedAfter *WKTime
}
// EncodeToQuery encodes parametes to a query string.
func (p *SubjectListParams) EncodeToQuery() string {
values := p.encodeToURLValues()
if p.IDs != nil {
values.Add("ids", joinIDs(p.IDs, ","))
}
if p.Hidden != nil {
values.Add("hidden", strconv.FormatBool(*p.Hidden))
}
if p.Levels != nil {
values.Add("levels", joinInts(p.Levels, ","))
}
if p.Slugs != nil {
values.Add("slugs", joinStrings(p.Slugs, ","))
}
if p.Types != nil {
values.Add("types", joinStrings(p.Types, ","))
}
if p.UpdatedAfter != nil {
values.Add("updated_after", p.UpdatedAfter.Encode())
}
return values.Encode()
}
// SubjectAuxiliaryMeaningObject represents an auxiliary meaning for a subject.
type SubjectAuxiliaryMeaningObject struct {
Meaning string `json:"meaning"`
Type SubjectAuxiliaryMeaningObjectType `json:"type"`
}
// SubjectAuxiliaryMeaningObjectType is the type of object of an auxiliary meaning.
type SubjectAuxiliaryMeaningObjectType string
// All possible values of auxiliary meaning object type.
const (
SubjectAuxiliaryMeaningObjectTypeBlacklist SubjectAuxiliaryMeaningObjectType = "blacklist"
SubjectAuxiliaryMeaningObjectTypeWhitelist SubjectAuxiliaryMeaningObjectType = "whitelist"
)
// SubjectMeaningObject represents a meaning for a subject.
type SubjectMeaningObject struct {
AcceptedAnswer bool `json:"accepted_answer"`
Meaning string `json:"meaning"`
Primary bool `json:"primary"`
}
// SubjectPage represents a single page of Subjects.
type SubjectPage struct {
PageObject
Data []*Subject `json:"data"`
}