-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfield.go
541 lines (450 loc) · 12.4 KB
/
field.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// (C) 2014 Cybozu. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
package kintone
import (
"encoding/json"
"strconv"
"time"
)
// Field type identifiers.
const (
FT_SINGLE_LINE_TEXT = "SINGLE_LINE_TEXT"
FT_MULTI_LINE_TEXT = "MULTI_LINE_TEXT"
FT_RICH_TEXT = "RICH_TEXT"
FT_DECIMAL = "NUMBER"
FT_CALC = "CALC"
FT_CHECK_BOX = "CHECK_BOX"
FT_RADIO = "RADIO_BUTTON"
FT_SINGLE_SELECT = "DROP_DOWN"
FT_MULTI_SELECT = "MULTI_SELECT"
FT_FILE = "FILE"
FT_LINK = "LINK"
FT_DATE = "DATE"
FT_TIME = "TIME"
FT_DATETIME = "DATETIME"
FT_USER = "USER_SELECT"
FT_ORGANIZATION = "ORGANIZATION_SELECT"
FT_GROUP = "GROUP_SELECT"
FT_CATEGORY = "CATEGORY"
FT_STATUS = "STATUS"
FT_ASSIGNEE = "STATUS_ASSIGNEE"
FT_RECNUM = "RECORD_NUMBER"
FT_CREATOR = "CREATOR"
FT_CTIME = "CREATED_TIME"
FT_MODIFIER = "MODIFIER"
FT_MTIME = "UPDATED_TIME"
FT_SUBTABLE = "SUBTABLE"
FT_ID = "__ID__"
FT_REVISION = "__REVISION__"
)
// SingleLineTextField is a field type for single-line texts.
type SingleLineTextField string
func (f SingleLineTextField) JSONValue() interface{} {
return string(f)
}
func (f SingleLineTextField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_SINGLE_LINE_TEXT,
"value": f.JSONValue(),
})
}
// MultiLineTextField is a field type for multi-line texts.
type MultiLineTextField string
func (f MultiLineTextField) JSONValue() interface{} {
return string(f)
}
func (f MultiLineTextField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_MULTI_LINE_TEXT,
"value": f.JSONValue(),
})
}
// RichTextField is a field type for HTML rich texts.
type RichTextField string
func (f RichTextField) JSONValue() interface{} {
return string(f)
}
func (f RichTextField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_RICH_TEXT,
"value": f.JSONValue(),
})
}
// DecimalField is a field type for decimal numbers.
type DecimalField string
func (f DecimalField) JSONValue() interface{} {
return string(f)
}
func (f DecimalField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_DECIMAL,
"value": f.JSONValue(),
})
}
// CalcField is a field type for auto-calculated values.
type CalcField string
func (f CalcField) JSONValue() interface{} {
return string(f)
}
func (f CalcField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_CALC,
"value": f.JSONValue(),
})
}
// CheckBoxField is a field type for selected values in a check-box.
type CheckBoxField []string
func (f CheckBoxField) JSONValue() interface{} {
return []string(f)
}
func (f CheckBoxField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_CHECK_BOX,
"value": f.JSONValue(),
})
}
// RadioButtonField is a field type for the selected value by a radio-button.
type RadioButtonField string
func (f RadioButtonField) JSONValue() interface{} {
return string(f)
}
func (f RadioButtonField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_RADIO,
"value": f.JSONValue(),
})
}
// SingleSelectField is a field type for the selected value in a selection box.
type SingleSelectField struct {
String string // Selected value.
Valid bool // If not selected, false.
}
func (f SingleSelectField) JSONValue() interface{} {
if f.Valid {
return f.String
} else {
return nil
}
}
func (f SingleSelectField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_SINGLE_SELECT,
"value": f.JSONValue(),
})
}
// MultiSelectField is a field type for selected values in a selection box.
type MultiSelectField []string
func (f MultiSelectField) JSONValue() interface{} {
return []string(f)
}
func (f MultiSelectField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_MULTI_SELECT,
"value": f.JSONValue(),
})
}
// File is a struct for an uploaded file.
type File struct {
ContentType string `json:"contentType"` // MIME type of the file
FileKey string `json:"fileKey"` // BLOB ID of the file
Name string `json:"name"` // File name
Size uint64 `json:"size,string"` // The file size
}
func (f *File) MarshalJSON() ([]byte, error) {
return json.Marshal(
map[string]interface{}{
"contentType": f.ContentType,
"fileKey": f.FileKey,
"name": f.Name,
"size": strconv.FormatUint(f.Size, 10),
})
}
// FileField is a field type for uploaded files.
type FileField []File
func (f FileField) JSONValue() interface{} {
return []File(f)
}
func (f FileField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_FILE,
"value": f.JSONValue(),
})
}
// LinkField is a field type for hyper-links.
type LinkField string
func (f LinkField) JSONValue() interface{} {
return string(f)
}
func (f LinkField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_LINK,
"value": f.JSONValue(),
})
}
// DateField is a field type for dates.
type DateField struct {
Date time.Time // stores date information.
Valid bool // false when not set.
}
// NewDateField returns an instance of DateField.
func NewDateField(year int, month time.Month, day int) DateField {
return DateField{
time.Date(year, month, day, 0, 0, 0, 0, time.UTC),
true,
}
}
func (f DateField) JSONValue() interface{} {
if f.Valid {
return f.Date.Format("2006-01-02")
} else {
return nil
}
}
func (f DateField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_DATE,
"value": f.JSONValue(),
})
}
// TimeField is a field type for times.
type TimeField struct {
Time time.Time // stores time information.
Valid bool // false when not set.
}
// NewTimeField returns an instance of TimeField.
func NewTimeField(hour, min int) TimeField {
return TimeField{
time.Date(1, time.January, 1, hour, min, 0, 0, time.UTC),
true,
}
}
func (f TimeField) JSONValue() interface{} {
if f.Valid {
return f.Time.Format("15:04:05")
} else {
return nil
}
}
func (f TimeField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_TIME,
"value": f.JSONValue(),
})
}
// DateTimeField is a field type for date & time.
type DateTimeField struct {
Time time.Time // stores time information.
Valid bool // false when not set.
}
// NewDateTimeField returns an instance of DateTimeField.
func NewDateTimeField(year int, month time.Month, day, hour, min int) DateTimeField {
return DateTimeField{
time.Date(year, month, day, hour, min, 0, 0, time.UTC),
true,
}
}
func (f DateTimeField) JSONValue() interface{} {
if f.Valid {
return f.Time.Format(time.RFC3339)
} else {
return nil
}
}
func (f DateTimeField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_DATETIME,
"value": f.JSONValue(),
})
}
// User represents a user entry.
type User struct {
Code string `json:"code"` // A unique identifer of the user.
Name string `json:"name"` // The user name.
}
// UserField is a field type for user entries.
type UserField []User
func (f UserField) JSONValue() interface{} {
return []User(f)
}
func (f UserField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_USER,
"value": f.JSONValue(),
})
}
// Organization represents a department entry.
type Organization struct {
Code string `json:"code"` // A unique identifer of the department.
Name string `json:"name"` // The department name.
}
// OrganizationField is a field type for department entries.
type OrganizationField []Organization
func (f OrganizationField) JSONValue() interface{} {
return []Organization(f)
}
func (f OrganizationField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_ORGANIZATION,
"value": f.JSONValue(),
})
}
// Group represents a group(or role) entry.
type Group struct {
Code string `json:"code"` // A unique identifer of the group(or role).
Name string `json:"name"` // The group name.
}
// GroupField is a field type for group(or role) entries.
type GroupField []Group
func (f GroupField) JSONValue() interface{} {
return []Group(f)
}
func (f GroupField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_GROUP,
"value": f.JSONValue(),
})
}
// CategoryField is a list of category names.
type CategoryField []string
func (f CategoryField) JSONValue() interface{} {
return []string(f)
}
func (f CategoryField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_CATEGORY,
"value": f.JSONValue(),
})
}
// StatusField is a string label of a record status.
type StatusField string
func (f StatusField) JSONValue() interface{} {
return string(f)
}
func (f StatusField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_STATUS,
"value": f.JSONValue(),
})
}
// AssigneeField is a list of user entries who are assigned to a record.
type AssigneeField []User
func (f AssigneeField) JSONValue() interface{} {
return []User(f)
}
func (f AssigneeField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_ASSIGNEE,
"value": f.JSONValue(),
})
}
// RecordNumberField is a record number.
type RecordNumberField string
func (f RecordNumberField) JSONValue() interface{} {
return string(f)
}
func (f RecordNumberField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_RECNUM,
"value": f.JSONValue(),
})
}
// CreatorField is a user who created a record.
type CreatorField User
func (f CreatorField) JSONValue() interface{} {
return User(f)
}
func (f CreatorField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_CREATOR,
"value": f.JSONValue(),
})
}
// CreationTimeField is the time when a record is created.
type CreationTimeField time.Time
func (t CreationTimeField) JSONValue() interface{} {
return time.Time(t).Format(time.RFC3339)
}
func (t CreationTimeField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_CTIME,
"value": t.JSONValue(),
})
}
// ModifierField is a user who modified a record last.
type ModifierField User
func (f ModifierField) JSONValue() interface{} {
return User(f)
}
func (f ModifierField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_MODIFIER,
"value": f.JSONValue(),
})
}
// ModificationTimeField is the time when a record is last modified.
type ModificationTimeField time.Time
func (t ModificationTimeField) JSONValue() interface{} {
return time.Time(t).Format(time.RFC3339)
}
func (t ModificationTimeField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_MTIME,
"value": t.JSONValue(),
})
}
// SubTableEntry is a type for an entry in a subtable.
type SubTableEntry struct {
Id string `json:"id"` // The entry ID
Value map[string]interface{} `json:"value"` // Subtable data fields.
}
// SubTableField is a list of subtable entries.
type SubTableField []*Record
func (f SubTableField) JSONValue() interface{} {
type sub_record struct {
Record *Record `json:"value"`
}
type sub_record_with_id struct {
Id uint64 `json:"id,string"`
Record *Record `json:"value"`
}
recs := make([]interface{}, 0, len(f))
for _, rec := range f {
if rec.id == 0 {
recs = append(recs, sub_record{rec})
} else {
recs = append(recs, sub_record_with_id{rec.id, rec})
}
}
return recs
}
func (f SubTableField) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": FT_SUBTABLE,
"value": f.JSONValue(),
})
}
// IsBuiltinField returns true if the field is a built-in field.
func IsBuiltinField(o interface{}) bool {
switch o.(type) {
case CalcField:
return true
case CategoryField:
return true
case StatusField:
return true
case AssigneeField:
return true
case RecordNumberField:
return true
case CreatorField:
return true
case CreationTimeField:
return true
case ModifierField:
return true
case ModificationTimeField:
return true
}
return false
}