Skip to content

Commit

Permalink
Improvements to layout container handling (#440)
Browse files Browse the repository at this point in the history
* Add handling for show conditions in layout container
* Add handling for master objects in layout container
  • Loading branch information
DnlLrssn authored Dec 11, 2023
1 parent e3180ff commit d2a88ad
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 99 deletions.
64 changes: 64 additions & 0 deletions helpers/fuzzybool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package helpers

import (
"github.com/goccy/go-json"
"github.com/pkg/errors"
)

type (
// FuzzyBool resolves boolean sent as strings, integers, float64 or boolean, json unmarshal defaults to true
FuzzyBool bool
)

// UnmarshalJSON FuzzyBool
func (sb *FuzzyBool) UnmarshalJSON(arg []byte) error {
if sb == nil {
return nil
}

if len(arg) < 1 {
*sb = true
return nil
}

var val interface{}
if err := json.Unmarshal(arg, &val); err != nil {
return errors.Wrapf(err, "Failed to unmarshal byte array<%v> as bool", arg)
}

switch val := val.(type) {
case int:
switch val {
case 0:
*sb = false
default:
*sb = true
}
case float64:
*sb = !FuzzyBool(NearlyEqual(val, 0.0))
case string:
switch val {
case "false", "0":
*sb = false
default:
*sb = true
}
case bool:
*sb = FuzzyBool(val)
case nil:
*sb = true
default:
return errors.Errorf("Failed to unmarshal byte array<%v> as bool", arg)
}

return nil
}

// AsBool returns bool representation of StringBool
func (sb *FuzzyBool) AsBool() bool {
if sb == nil {
return false
}

return bool(*sb)
}
114 changes: 114 additions & 0 deletions helpers/fuzzybool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package helpers_test

import (
"fmt"
"testing"

"github.com/qlik-oss/gopherciser/helpers"
)

func TestFuzzyBool_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
args []byte
wantErr bool
wantSb helpers.FuzzyBool
}{
{
name: "Test empty",
args: []byte(``),
wantErr: false,
wantSb: true,
},
{
name: "Test null",
args: []byte(`null`),
wantErr: false,
wantSb: true,
},
{
name: "Test bool false",
args: []byte(`false`),
wantErr: false,
wantSb: false,
},
{
name: "Test bool true",
args: []byte(`true`),
wantErr: false,
wantSb: true,
},
{
name: "Test string false",
args: []byte(`"false"`),
wantErr: false,
wantSb: false,
},
{
name: "Test string NaN",
args: []byte(`"NaN"`),
wantErr: false,
wantSb: true,
},
{
name: "Test string 0",
args: []byte(`"0"`),
wantErr: false,
wantSb: false,
},
{
name: "Test int 0",
args: []byte(`0`),
wantErr: false,
wantSb: false,
},
{
name: "Test int 1",
args: []byte(`1`),
wantErr: false,
wantSb: true,
},
{
name: "Test int -1",
args: []byte(`-1`),
wantErr: false,
wantSb: true,
},
{
name: "Test float 0.0",
args: []byte(`0.0`),
wantErr: false,
wantSb: false,
},
{
name: fmt.Sprintf("Test float %f", helpers.DefaultEpsilon),
args: []byte(fmt.Sprintf("%f", helpers.DefaultEpsilon)),
wantErr: false,
wantSb: false,
},
{
name: "Test float 1.0",
args: []byte(`1.0`),
wantErr: false,
wantSb: true,
},
{
name: "Test array",
args: []byte(`[1]`),
wantErr: true,
wantSb: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sb := helpers.FuzzyBool(false)
if err := (&sb).UnmarshalJSON(tt.args); (err != nil) != tt.wantErr {
t.Errorf("FuzzyBool.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if sb != tt.wantSb {
t.Errorf("FuzzyBool.UnmarshalJSON() sb value = %v, want %v", sb, tt.wantSb)
}
})
}
}
30 changes: 0 additions & 30 deletions helpers/stringbool.go

This file was deleted.

16 changes: 8 additions & 8 deletions senseobjects/sheetlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ type (
Rowspan int `json:"rowspan"`
Bounds SheetBounds `json:"bounds,omitempty"`
} `json:"cells,omitempty"`
Columns interface{} `json:"columns"`
Rows interface{} `json:"rows"`
Title string `json:"title"`
LabelExpression string `json:"labelExpression"`
Description string `json:"description"`
DescriptionExpression string `json:"descriptionExpression"`
Rank interface{} `json:"rank"`
ShowCondition helpers.StringBool `json:"showCondition"`
Columns interface{} `json:"columns"`
Rows interface{} `json:"rows"`
Title string `json:"title"`
LabelExpression string `json:"labelExpression"`
Description string `json:"description"`
DescriptionExpression string `json:"descriptionExpression"`
Rank interface{} `json:"rank"`
ShowCondition helpers.FuzzyBool `json:"showCondition"`
}

// SheetListPropertiesData properties of sheetlist
Expand Down
12 changes: 6 additions & 6 deletions session/containerhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ type (
}

ContainerChild struct {
RefID string `json:"refId"`
Label string `json:"label"`
IsMaster bool `json:"isMaster"`
ExternalReference *ContainerExternal `json:"externalReference"`
Type string `json:"type"`
Condition *helpers.StringBool `json:"condition"`
RefID string `json:"refId"`
Label string `json:"label"`
IsMaster bool `json:"isMaster"`
ExternalReference *ContainerExternal `json:"externalReference"`
Type string `json:"type"`
Condition *helpers.FuzzyBool `json:"condition"`
}

ContainerChildItemData struct {
Expand Down
41 changes: 1 addition & 40 deletions session/defaulthandler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package session

import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/qlik-oss/enigma-go/v4"
"github.com/qlik-oss/gopherciser/action"
Expand All @@ -26,43 +23,7 @@ func (handler *DefaultHandler) Instance(id string) ObjectHandlerInstance {

// GetObject implement ObjectHandler interface
func (instance *DefaultHandlerInstance) SetObjectAndEvents(sessionState *State, actionState *action.State, obj *enigmahandlers.Object, genObj *enigma.GenericObject) {
SetObjectDataAndEvents(sessionState, actionState, obj, genObj)

children := obj.ChildList()
childListItems := make(map[string]interface{})
if children != nil && children.Items != nil {
sessionState.LogEntry.LogDebugf("object<%s> type<%s> has children", genObj.GenericId, genObj.GenericType)
for _, child := range children.Items {
sessionState.LogEntry.LogDebug(fmt.Sprintf("obj<%s> child<%s> found in ChildList", obj.ID, child.Info.Id))
childListItems[child.Info.Id] = nil
GetAndAddObjectAsync(sessionState, actionState, child.Info.Id)
}
}

if genObj.GenericType == "sheet" {
sessionState.QueueRequest(func(ctx context.Context) error {
sheetList, err := sessionState.Connection.Sense().CurrentApp.GetSheetList(sessionState, actionState)
if err != nil {
return errors.WithStack(err)
}
if sheetList != nil {
entry, err := sheetList.GetSheetEntry(genObj.GenericId)
if err != nil {
return errors.WithStack(err)
}
if entry != nil && entry.Data != nil {
for _, cell := range entry.Data.Cells {
if _, ok := childListItems[cell.Name]; !ok {
// Todo should this be a warning?
sessionState.LogEntry.LogDebug(fmt.Sprintf("cell<%s> missing from sheet<%s> childlist", cell.Name, genObj.GenericId))
GetAndAddObjectAsync(sessionState, actionState, cell.Name)
}
}
}
}
return nil
}, actionState, true, "")
}
DefaultSetObjectDataAndEvents(sessionState, actionState, obj, genObj, nil)
}

func (instance *DefaultHandlerInstance) GetObjectDefinition(objectType string) (string, senseobjdef.SelectType, senseobjdef.DataDefType, error) {
Expand Down
Loading

0 comments on commit d2a88ad

Please sign in to comment.