-
Notifications
You must be signed in to change notification settings - Fork 3
/
enigmaobject.go
346 lines (306 loc) · 8.08 KB
/
enigmaobject.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
package enigmahandlers
import (
"fmt"
"sync"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/qlik-oss/enigma-go/v4"
"github.com/qlik-oss/gopherciser/helpers"
)
type (
// ObjectType type of enigma object
ObjectType int
// HyperCube wrapping enigma hypercube with additional if binned or not
HyperCube struct {
*enigma.HyperCube
Binned bool
}
objectData struct {
properties *enigma.GenericObjectProperties
childlist *enigma.ChildList
children *[]ObjChild
listobject *enigma.ListObject
hypercube *HyperCube
treeDataPages []*enigma.NxTreeNode
}
// Object sense object handler
Object struct {
// Handle of sense object
Handle int
// ID of object
ID string
// Type of object
Type ObjectType
// EnigmaObject enigma object instance
EnigmaObject interface{}
closefuncs []func() error
data *objectData
lockData sync.Mutex
}
ObjChild struct {
ExternalReference struct {
MasterID string `json:"masterId"`
App string `json:"app"`
ViewID string `json:"viewId"`
} `json:"externalReference"`
Label string `json:"label"`
RefID string `json:"refId"`
Type string `json:"type"`
}
// ObjectNotFound error
ObjectNotFound int
// ObjectIDNotFound error
ObjectIDNotFound string
)
const (
// ObjTypeApp object is an app
ObjTypeApp ObjectType = iota
// ObjTypeSheet object is a sheet
ObjTypeSheet
// ObjTypeGenericObject object is sheet object
ObjTypeGenericObject
)
// SetProperties set/update properties of object
func (obj *Object) SetProperties(properties *enigma.GenericObjectProperties) {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
obj.data = &objectData{
properties: properties,
}
return
}
obj.data.properties = properties
}
// Properties get object properties
func (obj *Object) Properties() *enigma.GenericObjectProperties {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
return nil
}
return obj.data.properties
}
// SetChildList set object childlist
func (obj *Object) SetChildList(children *enigma.ChildList) {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
obj.data = &objectData{
childlist: children,
}
return
}
obj.data.childlist = children
}
// SetChildren set object children
func (obj *Object) SetChildren(children *[]ObjChild) {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
obj.data = &objectData{
children: children,
}
return
}
obj.data.children = children
}
// ChildList child objects of object
func (obj *Object) ChildList() *enigma.ChildList {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
return nil
}
return obj.data.childlist
}
// SetListObject of object
func (obj *Object) SetListObject(listobject *enigma.ListObject) {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
obj.data = &objectData{
listobject: listobject,
}
return
}
obj.data.listobject = listobject
}
// ListObject of object
func (obj *Object) ListObject() *enigma.ListObject {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
return nil
}
return obj.data.listobject
}
// SetHyperCube of object
func (obj *Object) SetHyperCube(hypercube *enigma.HyperCube) {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
obj.data = &objectData{
hypercube: &HyperCube{
hypercube,
false,
},
}
}
obj.data.hypercube = &HyperCube{hypercube, false}
}
// Get all apps from ExternalReference
func (obj *Object) ExternalReferenceApps() []string {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.children == nil {
return nil
}
apps := make([]string, 0, len(*obj.data.children))
for _, item := range *obj.data.children {
apps = append(apps, item.ExternalReference.App)
}
return apps
}
// HyperCube of object
func (obj *Object) HyperCube() *HyperCube {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
return nil
}
return obj.data.hypercube
}
// SetListObjectDataPages on object listobject
func (obj *Object) SetListObjectDataPages(datapages []*enigma.NxDataPage) error {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.listobject == nil {
return errors.Errorf("object<%s> has no listobject", obj.ID)
}
obj.data.listobject.DataPages = datapages
return nil
}
// ListObjectDataPages from object listobject
func (obj *Object) ListObjectDataPages() []*enigma.NxDataPage {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.listobject == nil {
return nil
}
return obj.data.listobject.DataPages
}
// SetHyperCubeDataPages on object hypercube
func (obj *Object) SetHyperCubeDataPages(datapages []*enigma.NxDataPage, binned bool) error {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.hypercube == nil {
return errors.Errorf("object<%s> has no hypercube", obj.ID)
}
obj.data.hypercube.DataPages = datapages
obj.data.hypercube.Binned = binned
return nil
}
// SetStackHyperCubePages on object hypercube
func (obj *Object) SetStackHyperCubePages(datapages []*enigma.NxStackPage) error {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.hypercube == nil {
return errors.Errorf("object<%s> has no hypercube", obj.ID)
}
obj.data.hypercube.StackedDataPages = datapages
return nil
}
// SetPivotHyperCubePages on object hypercube
func (obj *Object) SetPivotHyperCubePages(datapages []*enigma.NxPivotPage) error {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.hypercube == nil {
return errors.Errorf("object<%s> has no hypercube", obj.ID)
}
obj.data.hypercube.PivotDataPages = datapages
return nil
}
// SetTreeDataPages on object
func (obj *Object) SetTreeDataPages(treeDataPages []*enigma.NxTreeNode) error {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil {
return errors.Errorf("object<%s> has no data structure", obj.ID)
}
obj.data.treeDataPages = treeDataPages
return nil
}
// HyperCubeDataPages from object hypercube
func (obj *Object) HyperCubeDataPages() []*enigma.NxDataPage {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.hypercube == nil {
return nil
}
return obj.data.hypercube.DataPages
}
// HyperCubeStackPages from object hypercube
func (obj *Object) HyperCubeStackPages() []*enigma.NxStackPage {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.hypercube == nil {
return nil
}
return obj.data.hypercube.StackedDataPages
}
// HyperPivotPages from object hypercube
func (obj *Object) HyperPivotPages() []*enigma.NxPivotPage {
obj.lockData.Lock()
defer obj.lockData.Unlock()
if obj.data == nil || obj.data.hypercube == nil {
return nil
}
return obj.data.hypercube.PivotDataPages
}
// Close execute registered close functions
func (obj *Object) Close() error {
if obj == nil {
return nil
}
if obj.closefuncs == nil {
return nil
}
var mErr *multierror.Error
for _, f := range obj.closefuncs {
err := f()
if err != nil {
mErr = multierror.Append(mErr, err)
}
}
return errors.WithStack(helpers.FlattenMultiError(mErr))
}
// RegisterCloseFunc register function to be executed on object close
func (obj *Object) RegisterCloseFunc(f func() error) {
if obj.closefuncs == nil {
obj.closefuncs = make([]func() error, 1)
obj.closefuncs[0] = f
return
}
obj.closefuncs = append(obj.closefuncs, f)
}
// HasDims returns true if object has at least one dimension
func (obj *Object) HasDims() bool {
return obj.ListObject() != nil || (obj.HyperCube() != nil && len(obj.HyperCube().DimensionInfo) > 0)
}
// NewObject container for enigma object
func NewObject(handle int, t ObjectType, id string, enigmaobject interface{}) *Object {
obj := &Object{
Handle: handle,
Type: t,
ID: id,
EnigmaObject: enigmaobject,
}
return obj
}
func (onf ObjectNotFound) Error() string {
return fmt.Sprintf("handle<%d> not found in object list", int(onf))
}
func (onf ObjectIDNotFound) Error() string {
return fmt.Sprintf("Object<%s> not found in object list", string(onf))
}