-
Notifications
You must be signed in to change notification settings - Fork 7
/
customfield_service.go
797 lines (710 loc) · 21.4 KB
/
customfield_service.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
package yext
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
const customFieldPath = "customfields"
var CustomFieldListMaxLimit = 1000
type CustomFieldManager struct {
CustomFields []*CustomField
}
func (s *CustomFieldService) Create(cf *CustomField) (*Response, error) {
asJSON, err := json.Marshal(cf)
if err != nil {
return nil, err
}
var asMap map[string]interface{}
err = json.Unmarshal(asJSON, &asMap)
if err != nil {
return nil, err
}
delete(asMap, "id")
return s.client.DoRequestJSON("POST", customFieldPath, asMap, nil)
}
func (s *CustomFieldService) Edit(cf *CustomField) (*Response, error) {
asJSON, err := json.Marshal(cf)
if err != nil {
return nil, err
}
var asMap map[string]interface{}
err = json.Unmarshal(asJSON, &asMap)
if err != nil {
return nil, err
}
delete(asMap, "id")
delete(asMap, "type")
return s.client.DoRequestJSON("PUT", fmt.Sprintf("%s/%s", customFieldPath, cf.GetId()), asMap, nil)
}
func (s *CustomFieldService) Delete(customFieldId string) (*Response, error) {
return s.client.DoRequest("DELETE", fmt.Sprintf("%s/%s", customFieldPath, customFieldId), nil)
}
func (c *CustomFieldManager) Get(name string, loc *Location) (interface{}, error) {
if loc == nil || loc.CustomFields == nil {
return nil, nil
}
var (
field *CustomField
err error
)
if field, err = c.CustomField(name); err != nil {
return nil, err
}
return loc.CustomFields[field.GetId()], nil
}
func (c *CustomFieldManager) MustGet(name string, loc *Location) interface{} {
if ret, err := c.Get(name, loc); err != nil {
panic(err)
} else {
return ret
}
}
func (c *CustomFieldManager) IsOptionSet(fieldName string, optionName string, loc *Location) (bool, error) {
var (
field interface{}
err error
of OptionField
id string
)
if field, err = c.Get(fieldName, loc); err != nil {
return false, err
}
if field == nil {
return false, nil
}
switch field.(type) {
case nil:
return false, nil
case MultiOption:
mo := field.(MultiOption)
of = &mo
case *MultiOption:
of = field.(*MultiOption)
case SingleOption:
so := field.(SingleOption)
of = &so
case *SingleOption:
of = field.(*SingleOption)
default:
return false, fmt.Errorf("'%s' is not an OptionField custom field, is %T", fieldName, field)
}
if id, err = c.CustomFieldOptionId(fieldName, optionName); err != nil {
return false, err
}
return of.IsOptionIdSet(id), nil
}
func (c *CustomFieldManager) MustIsOptionSet(fieldName string, optionName string, loc *Location) bool {
if set, err := c.IsOptionSet(fieldName, optionName, loc); err != nil {
panic(err)
} else {
return set
}
}
func (c *CustomFieldManager) SetOption(fieldName string, optionName string, loc *Location) (*Location, error) {
var (
field interface{}
err error
of OptionField
ok bool
id string
)
if field, err = c.Get(fieldName, loc); err != nil {
return loc, err
}
if field == nil {
var cf *CustomField
if cf, err = c.CustomField(fieldName); err != nil {
return loc, fmt.Errorf("problem getting '%s': %v", fieldName, err)
}
switch cf.Type {
case CUSTOMFIELDTYPE_MULTIOPTION:
of = new(MultiOption)
case CUSTOMFIELDTYPE_SINGLEOPTION:
of = new(SingleOption)
default:
return loc, fmt.Errorf("'%s' is not an OptionField is '%s'", cf.Name, cf.Type)
}
} else if of, ok = field.(OptionField); !ok {
return loc, fmt.Errorf("'%s': %v is not an OptionField custom field is %T", fieldName, field, field)
}
if id, err = c.CustomFieldOptionId(fieldName, optionName); err != nil {
return loc, err
}
of.SetOptionId(id)
return c.Set(fieldName, of, loc)
}
func (c *CustomFieldManager) MustSetOption(fieldName string, optionName string, loc *Location) *Location {
if loc, err := c.SetOption(fieldName, optionName, loc); err != nil {
panic(err)
} else {
return loc
}
}
func (c *CustomFieldManager) UnsetOption(fieldName string, optionName string, loc *Location) (*Location, error) {
var (
field interface{}
err error
id string
)
if field, err = c.Get(fieldName, loc); err != nil {
return loc, err
}
if field == nil {
return loc, fmt.Errorf("'%s' is not currently set", fieldName)
}
option, ok := field.(OptionField)
if !ok {
return loc, fmt.Errorf("'%s' is not an OptionField custom field", fieldName)
}
if id, err = c.CustomFieldOptionId(fieldName, optionName); err != nil {
return loc, err
}
option.UnsetOptionId(id)
return c.Set(fieldName, option, loc)
}
func (c *CustomFieldManager) MustUnsetOption(fieldName string, optionName string, loc *Location) *Location {
if loc, err := c.UnsetOption(fieldName, optionName, loc); err != nil {
panic(err)
} else {
return loc
}
}
// TODO: Why does this return a location?
// TODO: Should we validate the the type we received matches the type of the field? Probably.
func (c *CustomFieldManager) Set(name string, value CustomFieldValue, loc *Location) (*Location, error) {
field, err := c.CustomField(name)
if err != nil {
return loc, err
}
loc.CustomFields[field.GetId()] = value
return loc, nil
}
func (c *CustomFieldManager) MustSet(name string, value CustomFieldValue, loc *Location) *Location {
if loc, err := c.Set(name, value, loc); err != nil {
panic(err)
} else {
return loc
}
}
func (c *CustomFieldManager) CustomField(name string) (*CustomField, error) {
names := []string{}
for _, cf := range c.CustomFields {
if name == cf.Name {
return cf, nil
}
names = append(names, cf.Name)
}
return nil, fmt.Errorf("Unable to find custom field with name %s, available fields: %v", name, names)
}
func (c *CustomFieldManager) MustCustomField(name string) *CustomField {
if cf, err := c.CustomField(name); err != nil {
panic(err)
} else {
return cf
}
}
func (c *CustomFieldManager) CustomFieldId(name string) (string, error) {
if cf, err := c.CustomField(name); err != nil {
return "", err
} else {
return cf.GetId(), nil
}
}
func (c *CustomFieldManager) MustCustomFieldId(name string) string {
if id, err := c.CustomFieldId(name); err != nil {
panic(err)
} else {
return id
}
}
func (c *CustomFieldManager) CustomFieldName(id string) (string, error) {
ids := []string{}
for _, cf := range c.CustomFields {
if id == cf.GetId() {
return cf.Name, nil
}
ids = append(ids, cf.GetId())
}
return "", fmt.Errorf("Unable to find custom field with Id %s, available Ids: %v", id, ids)
}
func (c *CustomFieldManager) MustCustomFieldName(id string) string {
if name, err := c.CustomFieldName(id); err != nil {
panic(err)
} else {
return name
}
}
func (c *CustomFieldManager) CustomFieldOptionId(fieldName, optionName string) (string, error) {
cf, err := c.CustomField(fieldName)
if err != nil {
return "", err
}
if cf.Options == nil {
return "", fmt.Errorf("Custom field %s doesn't have any options", fieldName)
}
for _, option := range cf.Options {
if option.Value == optionName {
return option.Key, nil
}
}
return "", fmt.Errorf("Unable to find custom field option with name %s", optionName)
}
func (c *CustomFieldManager) MustCustomFieldOptionId(fieldName, optionName string) string {
if id, err := c.CustomFieldOptionId(fieldName, optionName); err != nil {
panic(err)
} else {
return id
}
}
type CustomFieldService struct {
CustomFieldManager *CustomFieldManager
client *Client
}
type CustomFieldResponse struct {
Count int `json:"count"`
CustomFields []*CustomField `json:"customFields"`
}
func (s *CustomFieldService) ListAll() ([]*CustomField, error) {
var customFields []*CustomField
var lr listRetriever = func(opts *ListOptions) (int, int, error) {
cfr, _, err := s.List(opts)
if err != nil {
return 0, 0, err
}
customFields = append(customFields, cfr.CustomFields...)
return len(cfr.CustomFields), cfr.Count, err
}
if err := listHelper(lr, &ListOptions{Limit: CustomFieldListMaxLimit}); err != nil {
return nil, err
} else {
return customFields, nil
}
}
func (s *CustomFieldService) List(opts *ListOptions) (*CustomFieldResponse, *Response, error) {
requrl, err := addListOptions(customFieldPath, opts)
if err != nil {
return nil, nil, err
}
v := &CustomFieldResponse{}
r, err := s.client.DoRequest("GET", requrl, v)
if err != nil {
return nil, r, err
}
return v, r, nil
}
func (s *CustomFieldService) CacheCustomFields() ([]*CustomField, error) {
cfs, err := s.ListAll()
if err != nil {
return nil, err
}
s.CustomFieldManager = &CustomFieldManager{CustomFields: cfs}
return s.CustomFieldManager.CustomFields, nil
}
func (s *CustomFieldService) MustCacheCustomFields() []*CustomField {
slice, err := s.CacheCustomFields()
if err != nil {
panic(err)
}
return slice
}
func ParseCustomFields(cfraw map[string]interface{}, cfs []*CustomField) (map[string]interface{}, error) {
typefor := func(id string) string {
for _, cf := range cfs {
if cf.GetId() == id {
return cf.Type
}
}
return ""
}
parsed := map[string]interface{}{}
for k, v := range cfraw {
if _, ok := v.(CustomFieldValue); ok {
parsed[k] = v
continue
}
var newval interface{}
switch typefor(k) {
case CUSTOMFIELDTYPE_YESNO:
if typedVal, ok := v.(bool); ok {
newval = YesNo(typedVal)
} else if typedVal, ok := v.(string); ok {
b, err := strconv.ParseBool(typedVal)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as yes/no %v", v, err)
}
newval = YesNo(b)
} else {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as yes/no, expected bool got %T", v, v)
}
case CUSTOMFIELDTYPE_NUMBER:
if typedVal, ok := v.(string); ok {
newval = Number(typedVal)
} else {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as number, expected string got %T", v, v)
}
case CUSTOMFIELDTYPE_SINGLELINETEXT:
if typedVal, ok := v.(string); ok {
newval = SingleLineText(typedVal)
} else {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as single-line text, expected string got %T", v, v)
}
case CUSTOMFIELDTYPE_MULTILINETEXT:
if typedVal, ok := v.(string); ok {
newval = MultiLineText(typedVal)
} else {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as multi-line text, expected string got %T", v, v)
}
case CUSTOMFIELDTYPE_SINGLEOPTION:
if typedVal, ok := v.(string); ok {
newval = GetSingleOptionPointer(SingleOption(typedVal))
} else {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as single-option field, expected string got %T", v, v)
}
case CUSTOMFIELDTYPE_URL:
if typedVal, ok := v.(string); ok {
newval = Url(typedVal)
} else {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as url field, expected string got %T", v, v)
}
case CUSTOMFIELDTYPE_DATE:
if typedVal, ok := v.(string); ok {
newval = Date(typedVal)
} else {
return nil, fmt.Errorf("parse custom fields failure: could not parse '%v' as date field, expected string got %T", v, v)
}
case CUSTOMFIELDTYPE_TEXTLIST:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for Text List Field %v", v, err)
}
var cf TextList
err = json.Unmarshal(asJSON, &cf)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Text List Field %v", v, err)
}
newval = cf
case CUSTOMFIELDTYPE_MULTIOPTION:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for Multi-Option Field %v", v, err)
}
var cf MultiOption
err = json.Unmarshal(asJSON, &cf)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Multi-Option Field %v", v, err)
}
newval = cf
case CUSTOMFIELDTYPE_PHOTO:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for Photo Field %v", v, err)
}
var cfp *Photo
err = json.Unmarshal(asJSON, &cfp)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Photo Field %v", v, err)
}
newval = cfp
case CUSTOMFIELDTYPE_GALLERY:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for Photo Gallery Field %v", v, err)
}
var g Gallery
err = json.Unmarshal(asJSON, &g)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Photo Gallery Field %v", v, err)
}
newval = g
case CUSTOMFIELDTYPE_VIDEO:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for Video Field %v", v, err)
}
var cf Video
err = json.Unmarshal(asJSON, &cf)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Video Field %v", v, err)
}
newval = cf
case CUSTOMFIELDTYPE_HOURS:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for Hours Field %v", v, err)
}
var cf Hours
err = json.Unmarshal(asJSON, &cf)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Hours Field %v", v, err)
}
newval = cf
case CUSTOMFIELDTYPE_DAILYTIMES:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for DailyT imes Field %v", v, err)
}
var cf DailyTimes
err = json.Unmarshal(asJSON, &cf)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Daily Times Field %v", v, err)
}
newval = cf
case CUSTOMFIELDTYPE_LOCATIONLIST:
asJSON, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not re-marshal '%v' as json for Location List Field %v", v, err)
}
var cf LocationList
err = json.Unmarshal(asJSON, &cf)
if err != nil {
return nil, fmt.Errorf("parse custom fields failure: could not unmarshal '%v' into Location List Field %v", v, err)
}
newval = cf
default:
newval = v
}
parsed[k] = newval
}
return parsed, nil
}
func validateCustomFieldsKeys(cfs map[string]interface{}) error {
for k, _ := range cfs {
if !customFieldKeyRegex.MatchString(k) {
return errors.New(fmt.Sprintf("custom fields must be specified by their id, not name: %s", k))
}
}
return nil
}
func (c *CustomFieldManager) GetBool(name string, loc *Location) (bool, error) {
value, err := c.Get(name, loc)
if err != nil {
return false, err
}
if value == nil {
return false, nil
}
switch t := value.(type) {
case YesNo:
return bool(value.(YesNo)), nil
case *YesNo:
return bool(*value.(*YesNo)), nil
default:
return false, fmt.Errorf("GetBool failure: Field '%v' is not of a YesNo type", t)
}
}
func (c *CustomFieldManager) MustGetBool(name string, loc *Location) bool {
if ret, err := c.GetBool(name, loc); err != nil {
panic(err)
} else {
return ret
}
}
// GetStringAliasCustomField returns the string value from a string type alias
// custom field. It will return an error if the field is not a string type.
func (c *CustomFieldManager) GetString(name string, loc *Location) (string, error) {
fv, err := c.Get(name, loc)
if err != nil {
return "", err
}
if fv == nil {
return "", nil
}
switch fv.(type) {
case SingleLineText:
return string(fv.(SingleLineText)), nil
case *SingleLineText:
return string(*fv.(*SingleLineText)), nil
case MultiLineText:
return string(fv.(MultiLineText)), nil
case *MultiLineText:
return string(*fv.(*MultiLineText)), nil
case Url:
return string(fv.(Url)), nil
case *Url:
return string(*fv.(*Url)), nil
case Date:
return string(fv.(Date)), nil
case *Date:
return string(*fv.(*Date)), nil
case Number:
return string(fv.(Number)), nil
case *Number:
return string(*fv.(*Number)), nil
case SingleOption:
if string(fv.(SingleOption)) == "" {
return "", nil
}
return c.CustomFieldOptionName(name, string(fv.(SingleOption)))
case *SingleOption:
if string(*fv.(*SingleOption)) == "" {
return "", nil
}
return c.CustomFieldOptionName(name, string(*fv.(*SingleOption)))
default:
return "", fmt.Errorf("%s is not a string custom field type, is %T", name, fv)
}
}
func (c *CustomFieldManager) CustomFieldOptionName(cfName string, optionId string) (string, error) {
cf, err := c.CustomField(cfName)
if err != nil {
return "", err
}
for _, option := range cf.Options {
if option.Key == optionId {
return option.Value, nil
}
}
return "", fmt.Errorf("Unable to find option for key %s for custom field %s", optionId, cfName)
}
func (c *CustomFieldManager) MustCustomFieldOptionName(fieldName, optionId string) string {
if id, err := c.CustomFieldOptionName(fieldName, optionId); err != nil {
panic(err)
} else {
return id
}
}
func (c *CustomFieldManager) MustGetString(name string, loc *Location) string {
if ret, err := c.GetString(name, loc); err != nil {
panic(err)
} else {
return ret
}
}
// GetStringArrayAliasCustomField returns the string array value from a string array
// type alias custom field. It will return an error if the field is not a string
// array type.
func (c *CustomFieldManager) GetStringSlice(name string, loc *Location) ([]string, error) {
fv, err := c.Get(name, loc)
if err != nil {
return nil, err
}
if fv == nil {
return nil, nil
}
switch fv.(type) {
case UnorderedStrings:
return []string(fv.(UnorderedStrings)), nil
case *UnorderedStrings:
return []string(*fv.(*UnorderedStrings)), nil
case LocationList:
return []string(fv.(LocationList)), nil
case *LocationList:
return []string(*fv.(*LocationList)), nil
case TextList:
return []string(fv.(TextList)), nil
case *TextList:
return []string(*fv.(*TextList)), nil
case MultiOption:
return c.CustomFieldOptionNames(name, []string(fv.(MultiOption)))
case *MultiOption:
return c.CustomFieldOptionNames(name, []string(*fv.(*MultiOption)))
default:
return nil, fmt.Errorf("%s is not a string array custom field type, is %T", name, fv)
}
}
func (c *CustomFieldManager) CustomFieldOptionNames(cfName string, optionIds []string) ([]string, error) {
var optionNames = []string{}
for _, optionId := range optionIds {
optionName, err := c.CustomFieldOptionName(cfName, optionId)
if err != nil {
return nil, err
}
optionNames = append(optionNames, optionName)
}
return optionNames, nil
}
func (c *CustomFieldManager) MustGetStringSlice(name string, loc *Location) []string {
if ret, err := c.GetStringSlice(name, loc); err != nil {
panic(err)
} else {
return ret
}
}
func (c *CustomFieldManager) SetBool(name string, value bool, loc *Location) error {
field, err := c.CustomField(name)
if err != nil {
return err
}
if field.Type != CUSTOMFIELDTYPE_YESNO {
return fmt.Errorf("SetBool failure: custom field '%v' is of type '%v' and not boolean", name, field.Type)
}
loc.CustomFields[field.GetId()] = YesNo(value)
return nil
}
func (c *CustomFieldManager) MustSetBool(name string, value bool, loc *Location) {
if err := c.SetBool(name, value, loc); err != nil {
panic(err)
} else {
return
}
}
func (c *CustomFieldManager) SetStringSlice(name string, value []string, loc *Location) error {
field, err := c.CustomField(name)
if err != nil {
return err
}
switch field.Type {
case CUSTOMFIELDTYPE_MULTIOPTION:
for _, element := range value {
c.MustSetOption(name, element, loc)
}
return nil
case CUSTOMFIELDTYPE_TEXTLIST:
loc.CustomFields[field.GetId()] = TextList(value)
return nil
case CUSTOMFIELDTYPE_LOCATIONLIST:
loc.CustomFields[field.GetId()] = UnorderedStrings(value)
return nil
default:
return fmt.Errorf("SetStringSlice failure: custom field '%v' is of type '%v' and can not take a string slice", name, field.Type)
}
}
func (c *CustomFieldManager) MustSetStringSlice(name string, value []string, loc *Location) {
if err := c.SetStringSlice(name, value, loc); err != nil {
panic(err)
} else {
return
}
}
func (c *CustomFieldManager) SetString(name string, value string, loc *Location) error {
field, err := c.CustomField(name)
if err != nil {
return err
}
switch field.Type {
case CUSTOMFIELDTYPE_SINGLEOPTION:
c.MustSetOption(name, value, loc)
return nil
case CUSTOMFIELDTYPE_SINGLELINETEXT:
loc.CustomFields[field.GetId()] = SingleLineText(value)
return nil
case CUSTOMFIELDTYPE_MULTILINETEXT:
loc.CustomFields[field.GetId()] = MultiLineText(value)
return nil
case CUSTOMFIELDTYPE_URL:
loc.CustomFields[field.GetId()] = Url(value)
return nil
case CUSTOMFIELDTYPE_DATE:
loc.CustomFields[field.GetId()] = Date(value)
return nil
case CUSTOMFIELDTYPE_NUMBER:
loc.CustomFields[field.GetId()] = Number(value)
return nil
default:
return fmt.Errorf("SetString failure: custom field '%v' is of type '%v' and can not take a string", name, field.Type)
}
}
func (c *CustomFieldManager) MustSetString(name string, value string, loc *Location) {
Must(c.SetString(name, value, loc))
}
func (c *CustomFieldManager) SetPhoto(name string, v *Photo, loc *Location) error {
_, err := c.Set(name, v, loc)
return err
}
func (c *CustomFieldManager) UnsetPhoto(name string, loc *Location) error {
return c.SetPhoto(name, UnsetPhotoValue, loc)
}
func GetSingleOptionPointer(option SingleOption) *SingleOption {
return &option
}