+
Skip to content
This repository was archived by the owner on Sep 28, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/corectl_set_objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Sets or updates the objects in the current app

### Synopsis

Sets or updates the objects in the current app Example corectl set objects ./my-objects-glob-path.json
Sets or updates the objects in the current app Example corectl set objects ./my-objects-glob-path.json.
The JSON objects can be in either the GenericObjectProperties format or the GenericObjectEntry format

```
corectl set objects <glob-pattern-path-to-objects-files.json [flags]
Expand Down
82 changes: 57 additions & 25 deletions internal/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type EntitiesConfigFile struct {
Objects []string
}
type genericEntity struct {
Info *enigma.NxInfo `json:"qInfo"`
Info *enigma.NxInfo `json:"qInfo,omitempty"`
Property *enigma.GenericObjectProperties `json:"qProperty,omitempty"`
}

// ReadEntitiesFile reads the entity config file from the supplied path.
Expand Down Expand Up @@ -83,55 +84,74 @@ func setupEntity(ctx context.Context, doc *enigma.Doc, entityPath string, entity
if err != nil {
FatalError("Could not open "+entityType+" file", err)
}
var props genericEntity
err = json.Unmarshal(entityFileContents, &props)
validateEntity(props, entityPath, 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, props.Info.Id)
dimension, err := doc.GetDimension(ctx, entity.Info.Id)
if err == nil && dimension.Handle != 0 {
LogVerbose("Updating dimension " + props.Info.Id)
LogVerbose("Updating dimension " + entity.Info.Id)
err = dimension.SetPropertiesRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to update dimension "+props.Info.Id, err)
FatalError("Failed to update dimension "+entity.Info.Id, err)
}
} else {
LogVerbose("Creating dimension " + props.Info.Id)
LogVerbose("Creating dimension " + entity.Info.Id)
_, err = doc.CreateDimensionRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to create dimension "+props.Info.Id, err)
FatalError("Failed to create dimension "+entity.Info.Id, err)
}
}
case "measure":
measure, err := doc.GetMeasure(ctx, props.Info.Id)
measure, err := doc.GetMeasure(ctx, entity.Info.Id)
if err == nil && measure.Handle != 0 {
LogVerbose("Updating measure " + props.Info.Id)
LogVerbose("Updating measure " + entity.Info.Id)
err = measure.SetPropertiesRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to update measure "+props.Info.Id, err)
FatalError("Failed to update measure "+entity.Info.Id, err)
}
} else {
LogVerbose("Creating measure " + props.Info.Id)
LogVerbose("Creating measure " + entity.Info.Id)
_, err = doc.CreateMeasureRaw(ctx, entityFileContents)
if err != nil {
FatalError("Failed to create measure "+props.Info.Id, err)
FatalError("Failed to create measure "+entity.Info.Id, err)
}
}
case "object":
object, err := doc.GetObject(ctx, props.Info.Id)
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 {
LogVerbose("Updating object " + props.Info.Id)
err = object.SetPropertiesRaw(ctx, entityFileContents)
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 "+props.Info.Id, err)
FatalError("Failed to update object "+objectID, err)
}
} else {
LogVerbose("Creating object " + props.Info.Id)
_, err = doc.CreateObjectRaw(ctx, entityFileContents)
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 "+props.Info.Id, err)
FatalError("Failed to create object "+objectID, err)
}
}
}
Expand All @@ -141,10 +161,22 @@ func validateEntity(entity genericEntity, entityPath string, err error) {
if err != nil {
FatalError("Invalid json", err)
}
if entity.Info.Id == "" {
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.Type == "" {
FatalError("Missing qInfo type 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)
}
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,8 @@ corectl eval by "Region" // Returns the values for dimension "Region"`,
setObjectsCmd = &cobra.Command{
Use: "objects <glob-pattern-path-to-objects-files.json",
Short: "Sets or updates the objects in the current app",
Long: "Sets or updates the objects in the current app Example corectl set objects ./my-objects-glob-path.json",
Long: `Sets or updates the objects in the current app Example corectl set objects ./my-objects-glob-path.json.
The JSON objects can be in either the GenericObjectProperties format or the GenericObjectEntry format`,

PersistentPreRun: func(ccmd *cobra.Command, args []string) {
setCmd.PersistentPreRun(setCmd, args)
Expand Down
60 changes: 49 additions & 11 deletions test/corectl_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,54 @@ func (tf *testFile) load() string {
return string(content)
}

func TestConnections(t *testing.T) {
connectToEngine := "--engine=" + *engineIP
cmd := exec.Command(binaryPath, []string{connectToEngine, "--config=test/project2/corectl.yml", "build", "--connections=test/project2/connections.yml"}...)
func setupEntities(connectToEngine string, configPath string, entityType string, entityPath string) []byte {
cmd := exec.Command(binaryPath, []string{connectToEngine, configPath, "build", entityPath}...)
cmd.Run()
cmd = exec.Command(binaryPath, []string{connectToEngine, "--config=test/project2/corectl.yml", "get", "connections", "--json"}...)
cmd = exec.Command(binaryPath, []string{connectToEngine, configPath, "get", entityType, "--json"}...)
output, _ := cmd.CombinedOutput()
return output
}

func removeEntities(t *testing.T, connectToEngine string, configPath string, entityType string, entityId string) {
cmd := exec.Command(binaryPath, []string{connectToEngine, configPath, "remove", entityType, entityId}...)
output, _ := cmd.CombinedOutput()
assert.Equal(t, "Saving...Done\n\n", string(output))
}

func verifyNoEntities(t *testing.T, connectToEngine string, configPath string, entityType string) {
cmd := exec.Command(binaryPath, []string{connectToEngine, configPath, "get", entityType, "--json"}...)
output, _ := cmd.CombinedOutput()
assert.Equal(t, "[]\n", string(output))
}

func TestNestedObjectSupport(t *testing.T) {
connectToEngine := "--engine=" + *engineIP
//create the nested objects
output := setupEntities(connectToEngine, "--config=test/project2/corectl.yml", "objects", "--objects=test/project2/sheet.json")

//verify that the objects are created
var objects []*enigma.NxInfo
err := json.Unmarshal(output, &objects)
assert.NoError(t, err)
assert.NotNil(t, objects[0])
assert.NotNil(t, objects[0].Id)
assert.Equal(t, "a699ee97-152d-4470-9655-ae7c82d71491", objects[0].Id)
assert.Len(t, objects, 3)

//verify that removing the objects works
removeEntities(t, connectToEngine, "--config=test/project2/corectl.yml", "objects", objects[0].Id)

//verify that there is no objects in the app anymore.
verifyNoEntities(t, connectToEngine, "--config=test/project2/corectl.yml", "objects")

//remove the app as clean-up (Otherwise we might share sessions when we use that app again.)
_ = exec.Command(binaryPath, []string{connectToEngine, "--config=test/project2/corectl.yml", "remove", "app", "project2.qvf"}...)
}

func TestConnections(t *testing.T) {
//create the connection
connectToEngine := "--engine=" + *engineIP
output := setupEntities(connectToEngine, "--config=test/project2/corectl.yml", "connections", "--connections=test/project2/connections.yml")

//verify that the connection was created
var connections []*enigma.Connection
Expand All @@ -95,17 +137,13 @@ func TestConnections(t *testing.T) {
assert.NotNil(t, connections[0].Id)

//verify that removing the connection works
cmd = exec.Command(binaryPath, []string{connectToEngine, "--config=test/project2/corectl.yml", "remove", "connection", connections[0].Id}...)
output, _ = cmd.CombinedOutput()
assert.Equal(t, "Saving...Done\n\n", string(output))
removeEntities(t, connectToEngine, "--config=test/project2/corectl.yml", "connection", connections[0].Id)

//verify that there is no connections in the app anymore.
cmd = exec.Command(binaryPath, []string{connectToEngine, "--config=test/project2/corectl.yml", "get", "connections", "--json"}...)
output, _ = cmd.CombinedOutput()
assert.Equal(t, "[]\n", string(output))
verifyNoEntities(t, connectToEngine, "--config=test/project2/corectl.yml", "connections")

//remove the app as clean-up (Otherwise we might share sessions when we use that app again.)
_ = exec.Command(binaryPath, []string{connectToEngine, "--config=test/project2/corectl.yml", "remove", "app", "project1.qvf"}...)
_ = exec.Command(binaryPath, []string{connectToEngine, "--config=test/project2/corectl.yml", "remove", "app", "project2.qvf"}...)
}

func setupTest(t *testing.T, tt test) func(t *testing.T, tt test) {
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载