forked from contentful-labs/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_field.go
159 lines (123 loc) · 3.35 KB
/
entry_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
package contentful
import "reflect"
// EntryField model
type EntryField struct {
value interface{}
dataType string
}
// String converts interface to string
func (ef *EntryField) String() string {
return ef.value.(string)
}
//LString returns the given locale
func (ef *EntryField) LString(locale string) string {
m := ef.value.(map[string]interface{})
if val, ok := m[locale]; ok {
return val.(string)
}
panic("no such a locale")
}
//Integer converts interface to integer
func (ef *EntryField) Integer() int {
return int(ef.value.(float64))
}
//LInteger converts interface to integer
func (ef *EntryField) LInteger(locale string) int {
m := ef.value.(map[string]interface{})
if val, ok := m[locale]; ok {
return int(val.(float64))
}
panic("no such a locale")
}
//Array converts interface to slice
func (ef *EntryField) Array() []string {
res := []string{}
switch reflect.TypeOf(ef.value).Kind() {
case reflect.Slice:
s := reflect.ValueOf(ef.value)
for i := 0; i < s.Len(); i++ {
res = append(res, s.Index(i).Interface().(string))
}
}
return res
}
//LArray converts interface to slice
func (ef *EntryField) LArray(locale string) []string {
m := ef.value.(map[string]interface{})
if val, ok := m[locale]; ok {
res := []string{}
switch reflect.TypeOf(val).Kind() {
case reflect.Slice:
s := reflect.ValueOf(val)
for i := 0; i < s.Len(); i++ {
res = append(res, s.Index(i).Interface().(string))
}
}
return res
}
panic("no such a locale")
}
//LinkID returns link model
func (ef *EntryField) LinkID() string {
m := ef.value.(map[string]interface{})
sys := m["sys"].(map[string]interface{})
return sys["id"].(string)
}
//LLinkID returns link model
func (ef *EntryField) LLinkID(locale string) string {
m := ef.value.(map[string]interface{})
if val, ok := m[locale]; ok {
m := val.(map[string]interface{})
sys := m["sys"].(map[string]interface{})
return sys["id"].(string)
}
panic("no such a locale")
}
//LinkType returns link model
func (ef *EntryField) LinkType() string {
m := ef.value.(map[string]interface{})
sys := m["sys"].(map[string]interface{})
return sys["linkType"].(string)
}
//LLinkType returns link model
func (ef *EntryField) LLinkType(locale string) string {
m := ef.value.(map[string]interface{})
if val, ok := m[locale]; ok {
m := val.(map[string]interface{})
sys := m["sys"].(map[string]interface{})
return sys["linkType"].(string)
}
panic("no such a locale")
}
//Asset returns the linked asset
func (ef *EntryField) Asset() *Asset {
if ef.LinkType() != "Asset" {
panic("you can only convert asset types")
}
// asset, _ := ef.space.GetAsset(ef.LinkID())
return &Asset{}
}
//LAsset returns the linked asset
func (ef *EntryField) LAsset(locale string) *Asset {
if ef.LLinkType(locale) != "Asset" {
panic("you can only convert asset types")
}
// asset, _ := ef.space.GetAsset(ef.LLinkID(locale))
return &Asset{}
}
//Entry returns the linked entry
func (ef *EntryField) Entry() *Entry {
if ef.LinkType() != "Entry" {
panic("you can only convert entry types")
}
// entry, _ := ef.space.GetEntries().Get(ef.LinkID())
return &Entry{}
}
//LEntry returns the linked entry
func (ef *EntryField) LEntry(locale string) *Entry {
if ef.LLinkType(locale) != "Entry" {
panic("you can only convert entry types")
}
// entry, _ := ef.space.GetEntries().Get(ef.LLinkID(locale))
return &Entry{}
}