Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a general Eval function for expressions in templates. #23927

Merged
merged 8 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
49 changes: 0 additions & 49 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,55 +141,6 @@ func FileSize(s int64) string {
return humanize.IBytes(uint64(s))
}

// Subtract deals with subtraction of all types of number.
func Subtract(left, right interface{}) interface{} {
var rleft, rright int64
var fleft, fright float64
isInt := true
switch v := left.(type) {
case int:
rleft = int64(v)
case int8:
rleft = int64(v)
case int16:
rleft = int64(v)
case int32:
rleft = int64(v)
case int64:
rleft = v
case float32:
fleft = float64(v)
isInt = false
case float64:
fleft = v
isInt = false
}

switch v := right.(type) {
case int:
rright = int64(v)
case int8:
rright = int64(v)
case int16:
rright = int64(v)
case int32:
rright = int64(v)
case int64:
rright = v
case float32:
fright = float64(v)
isInt = false
case float64:
fright = v
isInt = false
}

if isInt {
return rleft - rright
}
return fleft + float64(rleft) - (fright + float64(rright))
}

// EllipsisString returns a truncated short string,
// it appends '...' in the end of the length of string is too large.
func EllipsisString(str string, length int) string {
Expand Down
39 changes: 0 additions & 39 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,45 +114,6 @@ func TestFileSize(t *testing.T) {
assert.Equal(t, "2.0 EiB", FileSize(size))
}

func TestSubtract(t *testing.T) {
toFloat64 := func(n interface{}) float64 {
switch v := n.(type) {
case int:
return float64(v)
case int8:
return float64(v)
case int16:
return float64(v)
case int32:
return float64(v)
case int64:
return float64(v)
case float32:
return float64(v)
case float64:
return v
default:
return 0.0
}
}
values := []interface{}{
int(-3),
int8(14),
int16(81),
int32(-156),
int64(1528),
float32(3.5),
float64(-15.348),
}
for _, left := range values {
for _, right := range values {
expected := toFloat64(left) - toFloat64(right)
sub := Subtract(left, right)
assert.InDelta(t, expected, sub, 1e-3)
}
}
}

func TestEllipsisString(t *testing.T) {
assert.Equal(t, "...", EllipsisString("foobar", 0))
assert.Equal(t, "...", EllipsisString("foobar", 1))
Expand Down
Loading