-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcustomfield.go
250 lines (201 loc) · 5.45 KB
/
customfield.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
package yext
import "fmt"
type CustomFieldType string
const (
CUSTOMFIELDTYPE_YESNO = "BOOLEAN"
CUSTOMFIELDTYPE_SINGLELINETEXT = "TEXT"
CUSTOMFIELDTYPE_MULTILINETEXT = "MULTILINE_TEXT"
CUSTOMFIELDTYPE_SINGLEOPTION = "SINGLE_OPTION"
CUSTOMFIELDTYPE_URL = "URL"
CUSTOMFIELDTYPE_DATE = "DATE"
CUSTOMFIELDTYPE_NUMBER = "NUMBER"
CUSTOMFIELDTYPE_MULTIOPTION = "MULTI_OPTION"
CUSTOMFIELDTYPE_TEXTLIST = "TEXT_LIST"
CUSTOMFIELDTYPE_PHOTO = "PHOTO"
CUSTOMFIELDTYPE_GALLERY = "GALLERY"
CUSTOMFIELDTYPE_VIDEO = "VIDEO"
CUSTOMFIELDTYPE_HOURS = "HOURS"
CUSTOMFIELDTYPE_DAILYTIMES = "DAILY_TIMES"
CUSTOMFIELDTYPE_LOCATIONLIST = "LOCATION_LIST"
// not sure what to do with "DAILYTIMES", omitting
)
var (
UnsetPhotoValue = (*Photo)(nil)
)
type CustomFieldOption struct {
Key string `json:"key,omitempty"`
Value string `json:"value"`
}
// CustomField is the representation of a Custom Field definition in Yext Location Manager.
// For details see https://www.yext.com/support/platform-api/#Administration_API/Custom_Fields.htm
type CustomField struct {
Id *string `json:"id,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Options []CustomFieldOption `json:"options,omitempty"` // Only present for multi-option custom fields
Group string `json:"group"`
Description string `json:"description"`
AlternateLanguageBehaviour string `json:"alternateLanguageBehavior"`
}
func (c CustomField) GetId() string {
if c.Id == nil {
return ""
}
return *c.Id
}
type CustomFieldValue interface {
CustomFieldTag() string
}
type YesNo bool
func (y YesNo) CustomFieldTag() string {
return CUSTOMFIELDTYPE_YESNO
}
type SingleLineText string
func (s SingleLineText) CustomFieldTag() string {
return CUSTOMFIELDTYPE_SINGLELINETEXT
}
type MultiLineText string
func (m MultiLineText) CustomFieldTag() string {
return CUSTOMFIELDTYPE_MULTILINETEXT
}
type Url string
func (u Url) CustomFieldTag() string {
return CUSTOMFIELDTYPE_URL
}
type Date string
func (d Date) CustomFieldTag() string {
return CUSTOMFIELDTYPE_DATE
}
type Number string
func (n Number) CustomFieldTag() string {
return CUSTOMFIELDTYPE_NUMBER
}
type OptionField interface {
CustomFieldValue
SetOptionId(id string)
UnsetOptionId(id string)
IsOptionIdSet(id string) bool
}
type SingleOption string
func (s SingleOption) CustomFieldTag() string {
return CUSTOMFIELDTYPE_SINGLEOPTION
}
func (s *SingleOption) SetOptionId(id string) {
*s = SingleOption(id)
}
func (s *SingleOption) UnsetOptionId(id string) {
if string(*s) == id {
*s = SingleOption("")
}
}
func (s *SingleOption) IsOptionIdSet(id string) bool {
return *s == SingleOption(id)
}
type MultiOption UnorderedStrings
func (m MultiOption) CustomFieldTag() string {
return CUSTOMFIELDTYPE_MULTIOPTION
}
func (m MultiOption) Equal(c Comparable) bool {
var n MultiOption
switch v := c.(type) {
case MultiOption:
n = v
case *MultiOption:
n = *v
default:
panic(fmt.Errorf("%v is not a MultiOption is %T", c, c))
}
if len(m) != len(n) {
return false
}
a := UnorderedStrings(m)
b := UnorderedStrings(n)
return (&a).Equal(&b)
}
func (m *MultiOption) SetOptionId(id string) {
if !m.IsOptionIdSet(id) {
*m = append(*m, id)
}
}
func (m *MultiOption) UnsetOptionId(id string) {
if m.IsOptionIdSet(id) {
t := []string(*m)
indexOfTarget := -1
for i := 0; i < len(*m); i++ {
if t[i] == id {
indexOfTarget = i
}
}
if indexOfTarget >= 0 {
*m = append(t[:indexOfTarget], t[indexOfTarget+1:]...)
}
}
}
func (m *MultiOption) IsOptionIdSet(id string) bool {
for _, option := range *m {
if option == id {
return true
}
}
return false
}
type TextList []string
func (t TextList) CustomFieldTag() string {
return CUSTOMFIELDTYPE_TEXTLIST
}
type LocationList UnorderedStrings
func (l LocationList) CustomFieldTag() string {
return CUSTOMFIELDTYPE_LOCATIONLIST
}
func (m LocationList) Equal(c Comparable) bool {
var n LocationList
switch v := c.(type) {
case LocationList:
n = v
case *LocationList:
n = *v
default:
panic(fmt.Errorf("%v is not a LocationList is %T", c, c))
}
if len(m) != len(n) {
return false
}
a := UnorderedStrings(m)
b := UnorderedStrings(n)
return (&a).Equal(&b)
}
type Photo struct {
Url string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
Details string `json:"details,omitempty"`
ClickThroughURL string `json:"clickthroughUrl,omitempty"`
}
func (p *Photo) CustomFieldTag() string {
return CUSTOMFIELDTYPE_PHOTO
}
type Gallery []*Photo
func (g *Gallery) CustomFieldTag() string {
return CUSTOMFIELDTYPE_GALLERY
}
type Video struct {
Description string `json:"description"`
Url string `json:"url"`
}
type VideoGallery []Video
func (v *VideoGallery) CustomFieldTag() string {
return CUSTOMFIELDTYPE_VIDEO
}
type Hours struct {
AdditionalText string `json:"additionalHoursText,omitempty"`
Hours string `json:"hours,omitempty"`
HolidayHours []HolidayHours `json:"holidayHours,omitempty"`
}
func (h Hours) CustomFieldTag() string {
return CUSTOMFIELDTYPE_HOURS
}
type DailyTimes struct {
DailyTimes string `json:"dailyTimes,omitempty"`
}
func (d DailyTimes) CustomFieldTag() string {
return CUSTOMFIELDTYPE_DAILYTIMES
}