-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
239 lines (197 loc) · 5.13 KB
/
types.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
package lingotek
import (
"encoding/json"
"strconv"
"time"
)
// Lingotek sends us a microsecond timestamp, so we
// need to add our own unmarshal logic
type LingoTime struct {
time.Time
}
func (l *LingoTime) UnmarshalJSON(data []byte) error {
i, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return err
}
l.Time = time.Unix(i/1000, 0)
return nil
}
// API Response
// An API response will have multiple unknown entites based
// on what method was called. These must be unmarshalled after
// the type has been determined.
type Response struct {
Class []string `json:"class"`
Properties ResponseProperty `json:"properties"`
Entities json.RawMessage `json:"entities"`
Links []Link `json:"links"`
}
type ResponseProperty struct {
Title string `json:"title"`
Offset int32 `json:"offset"`
Total int32 `json:"total"`
Limit int32 `json:"limit"`
Size int32 `json:"size"`
}
type Entity struct {
Class []string `json:"class"`
}
// When an error occurs, we are given some information
// from the server
type Messages struct {
Messages []string `json:"messages"`
}
type CommunityProperty struct {
Title string `json:"title"`
Id string `json:"id"`
}
type Community struct {
Actions []Action `json:"actions"`
Property CommunityProperty `json:"properties"`
Rel []string `json:"rel"`
Links []Link `json:"links"`
}
type ProjectProperty struct {
CreationDate LingoTime `json:"creation_date"`
WorkflowId string `json:"workflow_id"`
CallbackUrl string `json:"callback_url"`
DueDate LingoTime `json:"due_date"`
Title string `json:"title"`
CommunityId string `json:"community_id"`
Id string `json:"id"`
}
type Project struct {
Property ProjectProperty `json:"properties"`
Rel []string `json:"rel"`
Links []Link `json:"links"`
}
type StatusCountPart struct {
Total int `json:"total"`
Unique int `json:"unique"`
}
type StatusCount struct {
Segment StatusCountPart `json:"segment"`
Word StatusCountPart `json:"word"`
FormatTag StatusCountPart `json:"format_tag"`
}
type StatusProperty struct {
Title string `json:"title"`
Id string `json:"id"`
Progress int32 `json:"progress"`
Count StatusCount `json:"count"`
}
type Status struct {
Property StatusProperty `json:"properties"`
Links []Link `json:"links"`
Messages
}
type Link struct {
Rel []string `json:"rel"`
Href string `json:"href"`
}
type Field struct {
Name string
Type string
Required bool
}
type Action struct {
Name string `json:"name"`
Method string `json:"method"`
Href string `json:"href"`
Title string `json:"title"`
Type string `json:"type"`
Fields []Field `json:"fields"`
}
type LocaleProperty struct {
Code string `json:"code"`
LanguageCode string `json:"lanuage_code"`
CountryCode string `json:"country_code"`
Title string `json:"title"`
Language string `json:"language"`
Country string `json:"country"`
}
type Locale struct {
Property LocaleProperty `json:"properties"`
Rel []string `json:"rel"`
Links []Link `json:"links"`
}
type DocumentProperty struct {
ProjectId string `json:"project_id"`
UploadDate LingoTime `json:"upload_date"`
Title string `json:"title"`
ExternalUrl string `json:"external_url"`
Name string `json:"name"`
Id string `json:"id"`
Extension string `json:"extension"`
}
type Document struct {
Property DocumentProperty
Locale Locale
Status Status
}
func (d *Document) UnmarshalJSON(data []byte) error {
docObject := make(map[string]json.RawMessage)
entities := make([]json.RawMessage, 2)
err := json.Unmarshal(data, &docObject)
if err != nil {
return err
}
err = json.Unmarshal(docObject["properties"], &d.Property)
if err != nil {
return err
}
err = json.Unmarshal(docObject["entities"], &entities)
if err != nil {
return err
}
err = json.Unmarshal(entities[0], &d.Locale)
if err != nil {
return err
}
err = json.Unmarshal(entities[1], &d.Status)
return err
}
type PhaseProperty struct {
Order int `json:"order"`
PercentCompleted int `json:"percent_completed"`
Name string `json:"name"`
}
type Phase struct {
Property PhaseProperty `json:"properties"`
}
type TranslationProperty struct {
DueDate LingoTime `json:"due_date"`
PercentComplete int `json:"percent_complete"`
LocaleCode string `json:"local_code"`
}
type Translation struct {
Property TranslationProperty
Phases []Phase
Locale Locale
}
func (t *Translation) UnmarshalJson(data []byte) error {
tObj := make(map[string]json.RawMessage)
entities := make([]json.RawMessage, 2)
err := json.Unmarshal(data, &tObj)
if err != nil {
return err
}
err = json.Unmarshal(tObj["properties"], &t.Property)
if err != nil {
return err
}
err = json.Unmarshal(tObj["entities"], &entities)
if err != nil {
return err
}
err = json.Unmarshal(entities[0], &t.Phases)
if err != nil {
return err
}
err = json.Unmarshal(entities[1], &t.Locale)
if err != nil {
return err
}
return err
}