-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to layout container handling (#440)
* Add handling for show conditions in layout container * Add handling for master objects in layout container
- Loading branch information
Showing
9 changed files
with
521 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.