This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
/
entities.go
182 lines (172 loc) · 5.76 KB
/
entities.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
package internal
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/qlik-oss/enigma-go"
"gopkg.in/yaml.v2"
)
// EntitiesConfigFile defines a file that contains the different entities yaml string arrays.
type EntitiesConfigFile struct {
Measures []string
Dimensions []string
Objects []string
}
type genericEntity struct {
Info *enigma.NxInfo `json:"qInfo,omitempty"`
Property *enigma.GenericObjectProperties `json:"qProperty,omitempty"`
}
// ReadEntitiesFile reads the entity config file from the supplied path.
func ReadEntitiesFile(path string) EntitiesConfigFile {
var config EntitiesConfigFile
source, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("Could not find entities file:", path)
os.Exit(1)
}
err = yaml.Unmarshal(source, &config)
if err != nil {
FatalError(err)
}
return config
}
// SetupEntities reads all generic entities of the specified type from both the project file path and the config file path and updates
// the list of entities in the app.
func SetupEntities(ctx context.Context, doc *enigma.Doc, projectFile string, entitiesPathsOnCommandLine string, entityType string) {
entitiesOnCommandLine, err := filepath.Glob(entitiesPathsOnCommandLine)
if err != nil {
FatalError(err)
}
for _, relativeEntityPath := range entitiesOnCommandLine {
setupEntity(ctx, doc, relativeEntityPath, entityType)
}
if projectFile != "" {
configFileContents := ReadEntitiesFile(projectFile)
currentWorkingDir, _ := os.Getwd()
defer os.Chdir(currentWorkingDir)
os.Chdir(filepath.Dir(projectFile))
var entities []string
switch entityType {
case "dimension":
entities = configFileContents.Dimensions
case "measure":
entities = configFileContents.Measures
case "object":
entities = configFileContents.Objects
default:
FatalError("Unknown type: " + entityType)
}
for _, entityGlobPatternInConfigFile := range entities {
entitiesInConfigLine, err := filepath.Glob(entityGlobPatternInConfigFile)
if err != nil {
FatalError(err)
}
for _, relativeEntityPath := range entitiesInConfigLine {
setupEntity(ctx, doc, relativeEntityPath, entityType)
}
}
}
}
func setupEntity(ctx context.Context, doc *enigma.Doc, entityPath string, entityType string) {
entityFileContents, err := ioutil.ReadFile(entityPath)
if err != nil {
FatalError("Could not open "+entityType+" file", err)
}
var entity genericEntity
err = json.Unmarshal(entityFileContents, &entity)
validateEntity(entity, entityPath, err)
//I do not know how to to this nicer, with less duplication.
switch entityType {
case "dimension":
dimension, err := doc.GetDimension(ctx, entity.Info.Id)
if err == nil && dimension.Handle != 0 {
LogVerbose("Updating dimension " + entity.Info.Id)
err = dimension.SetPropertiesRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to update dimension "+entity.Info.Id, err)
}
} else {
LogVerbose("Creating dimension " + entity.Info.Id)
_, err = doc.CreateDimensionRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to create dimension "+entity.Info.Id, err)
}
}
case "measure":
measure, err := doc.GetMeasure(ctx, entity.Info.Id)
if err == nil && measure.Handle != 0 {
LogVerbose("Updating measure " + entity.Info.Id)
err = measure.SetPropertiesRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to update measure "+entity.Info.Id, err)
}
} else {
LogVerbose("Creating measure " + entity.Info.Id)
_, err = doc.CreateMeasureRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to create measure "+entity.Info.Id, err)
}
}
case "object":
var objectID string
isGenericObjectEntry := false
if entity.Info != nil {
objectID = entity.Info.Id
} else {
objectID = entity.Property.Info.Id
isGenericObjectEntry = true
}
object, err := doc.GetObject(ctx, objectID)
if err == nil && object.Handle != 0 {
if isGenericObjectEntry {
LogVerbose("Updating object " + objectID + " using SetFullPropertyTree")
err = object.SetFullPropertyTreeRaw(ctx, entityFileContents)
} else {
LogVerbose("Updating object " + objectID + " using SetProperties")
err = object.SetPropertiesRaw(ctx, entityFileContents)
}
if err != nil {
FatalError("Failed to update object "+objectID, err)
}
} else {
LogVerbose("Creating object " + objectID)
if isGenericObjectEntry {
var createdObject *enigma.GenericObject
createdObject, err = doc.CreateObject(ctx, &enigma.GenericObjectProperties{Info: &enigma.NxInfo{Id: objectID, Type: entity.Property.Info.Type}})
LogVerbose("Setting object " + objectID + " using SetFullPropertyTree")
err = createdObject.SetFullPropertyTreeRaw(ctx, entityFileContents)
} else {
_, err = doc.CreateObjectRaw(ctx, entityFileContents)
}
if err != nil {
FatalError("Failed to create object "+objectID, err)
}
}
}
}
func validateEntity(entity genericEntity, entityPath string, err error) {
if err != nil {
FatalError("Invalid json", err)
}
if entity.Info == nil && entity.Property == nil {
FatalError("Missing qInfo attribute or qProperty attribute", entityPath)
}
if entity.Info != nil && entity.Info.Id == "" {
FatalError("Missing qInfo qId attribute", entityPath)
}
if entity.Info != nil && entity.Info.Type == "" {
FatalError("Missing qInfo qType attribute", entityPath)
}
if entity.Property != nil && entity.Property.Info == nil {
FatalError("Missing qInfo attribute inside the qProperty", entityPath)
}
if entity.Property != nil && entity.Property.Info.Id == "" {
FatalError("Missing qInfo qId attribute inside qProperty", entityPath)
}
if entity.Property != nil && entity.Property.Info.Type == "" {
FatalError("Missing qInfo qType attribute inside qProperty", entityPath)
}
}