utils.go: toInt() -> ToInt(); toBool() -> ToBool()
This commit is contained in:
parent
0bb4c96481
commit
9745a5d909
@ -66,11 +66,11 @@ func subStrFunc(ctx ExprContext, name string, args []any) (result any, err error
|
||||
return nil, errWrongParamType(name, ParamSource, TypeString, args[0])
|
||||
}
|
||||
|
||||
if start, err = toInt(args[1], name+"()"); err != nil {
|
||||
if start, err = ToInt(args[1], name+"()"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if count, err = toInt(args[2], name+"()"); err != nil {
|
||||
if count, err = ToInt(args[2], name+"()"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -26,21 +26,21 @@ func NewListIterator(list *ListType, args []any) (it *ListIterator) {
|
||||
}
|
||||
it = &ListIterator{a: list, count: 0, index: -1, start: 0, stop: listLen - 1, step: 1}
|
||||
if argc >= 1 {
|
||||
if i, err := toInt(args[0], "start index"); err == nil {
|
||||
if i, err := ToInt(args[0], "start index"); err == nil {
|
||||
if i < 0 {
|
||||
i = listLen + i
|
||||
}
|
||||
it.start = i
|
||||
}
|
||||
if argc >= 2 {
|
||||
if i, err := toInt(args[1], "stop index"); err == nil {
|
||||
if i, err := ToInt(args[1], "stop index"); err == nil {
|
||||
if i < 0 {
|
||||
i = listLen + i
|
||||
}
|
||||
it.stop = i
|
||||
}
|
||||
if argc >= 3 {
|
||||
if i, err := toInt(args[2], "step"); err == nil {
|
||||
if i, err := ToInt(args[2], "step"); err == nil {
|
||||
if i < 0 {
|
||||
i = -i
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ func evalNot(ctx ExprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if b, ok := toBool(rightValue); ok {
|
||||
if b, ok := ToBool(rightValue); ok {
|
||||
v = !b
|
||||
} else {
|
||||
err = self.errIncompatibleType(rightValue)
|
||||
@ -65,8 +65,8 @@ func evalAndWithoutShortcut(ctx ExprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
leftBool, lok = toBool(leftValue)
|
||||
rightBool, rok = toBool(rightValue)
|
||||
leftBool, lok = ToBool(leftValue)
|
||||
rightBool, rok = ToBool(rightValue)
|
||||
|
||||
if lok && rok {
|
||||
v = leftBool && rightBool
|
||||
@ -87,13 +87,13 @@ func evalAndWithShortcut(ctx ExprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if leftBool, lok := toBool(leftValue); !lok {
|
||||
if leftBool, lok := ToBool(leftValue); !lok {
|
||||
err = fmt.Errorf("got %T as left operand type of 'and' operator, it must be bool", leftBool)
|
||||
return
|
||||
} else if !leftBool {
|
||||
v = false
|
||||
} else if rightValue, err = self.children[1].compute(ctx); err == nil {
|
||||
if rightBool, rok := toBool(rightValue); rok {
|
||||
if rightBool, rok := ToBool(rightValue); rok {
|
||||
v = rightBool
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
@ -134,8 +134,8 @@ func evalOrWithoutShortcut(ctx ExprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
leftBool, lok = toBool(leftValue)
|
||||
rightBool, rok = toBool(rightValue)
|
||||
leftBool, lok = ToBool(leftValue)
|
||||
rightBool, rok = ToBool(rightValue)
|
||||
|
||||
if lok && rok {
|
||||
v = leftBool || rightBool
|
||||
@ -156,13 +156,13 @@ func evalOrWithShortcut(ctx ExprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if leftBool, lok := toBool(leftValue); !lok {
|
||||
if leftBool, lok := ToBool(leftValue); !lok {
|
||||
err = fmt.Errorf("got %T as left operand type of 'or' operator, it must be bool", leftBool)
|
||||
return
|
||||
} else if leftBool {
|
||||
v = true
|
||||
} else if rightValue, err = self.children[1].compute(ctx); err == nil {
|
||||
if rightBool, rok := toBool(rightValue); rok {
|
||||
if rightBool, rok := ToBool(rightValue); rok {
|
||||
v = rightBool
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
|
@ -23,7 +23,7 @@ func verifyKey(indexTerm *term, indexList *ListType) (index any, err error) {
|
||||
func verifyIndex(indexTerm *term, indexList *ListType, maxValue int) (index int, err error) {
|
||||
var v int
|
||||
|
||||
if v, err = toInt((*indexList)[0], "index expression"); err == nil {
|
||||
if v, err = ToInt((*indexList)[0], "index expression"); err == nil {
|
||||
if v < 0 && v >= -maxValue {
|
||||
v = maxValue + v
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ func evalLength(ctx ExprContext, self *term) (v any, err error) {
|
||||
} else if it, ok := childValue.(Iterator); ok {
|
||||
if extIt, ok := childValue.(ExtIterator); ok && extIt.HasOperation(countName) {
|
||||
count, _ := extIt.CallOperation(countName, nil)
|
||||
v, _ = toInt(count, "")
|
||||
v, _ = ToInt(count, "")
|
||||
} else {
|
||||
v = int64(it.Index() + 1)
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func newNotEqualTerm(tk *Token) (inst *term) {
|
||||
|
||||
func evalNotEqual(ctx ExprContext, self *term) (v any, err error) {
|
||||
if v, err = evalEqual(ctx, self); err == nil {
|
||||
b, _ := toBool(v)
|
||||
b, _ := ToBool(v)
|
||||
v = !b
|
||||
}
|
||||
return
|
||||
|
@ -63,7 +63,7 @@ func TestIsString(t *testing.T) {
|
||||
}
|
||||
|
||||
count++
|
||||
b, ok := toBool(true)
|
||||
b, ok := ToBool(true)
|
||||
if !(ok && b) {
|
||||
t.Errorf("%d: toBool(true) b=%v, ok=%v -> result = false, want true", count, b, ok)
|
||||
failed++
|
||||
@ -72,7 +72,7 @@ func TestIsString(t *testing.T) {
|
||||
}
|
||||
|
||||
count++
|
||||
b, ok = toBool("abc")
|
||||
b, ok = ToBool("abc")
|
||||
if !(ok && b) {
|
||||
t.Errorf("%d: toBool(\"abc\") b=%v, ok=%v -> result = false, want true", count, b, ok)
|
||||
failed++
|
||||
@ -81,7 +81,7 @@ func TestIsString(t *testing.T) {
|
||||
}
|
||||
|
||||
count++
|
||||
b, ok = toBool([]int{})
|
||||
b, ok = ToBool([]int{})
|
||||
if ok {
|
||||
t.Errorf("%d: toBool([]) b=%v, ok=%v -> result = true, want false", count, b, ok)
|
||||
failed++
|
||||
@ -97,7 +97,7 @@ func TestToIntOk(t *testing.T) {
|
||||
wantValue := int(64)
|
||||
wantErr := error(nil)
|
||||
|
||||
gotValue, gotErr := toInt(source, "test")
|
||||
gotValue, gotErr := ToInt(source, "test")
|
||||
|
||||
if gotErr != nil || gotValue != wantValue {
|
||||
t.Errorf("toInt(%v, \"test\") gotValue=%v, gotErr=%v -> wantValue=%v, wantErr=%v",
|
||||
@ -110,7 +110,7 @@ func TestToIntErr(t *testing.T) {
|
||||
wantValue := 0
|
||||
wantErr := errors.New(`test expected integer, got uint64 (64)`)
|
||||
|
||||
gotValue, gotErr := toInt(source, "test")
|
||||
gotValue, gotErr := ToInt(source, "test")
|
||||
|
||||
if gotErr.Error() != wantErr.Error() || gotValue != wantValue {
|
||||
t.Errorf("toInt(%v, \"test\") gotValue=%v, gotErr=%v -> wantValue=%v, wantErr=%v",
|
||||
|
4
utils.go
4
utils.go
@ -86,7 +86,7 @@ func numAsFloat(v any) (f float64) {
|
||||
return
|
||||
}
|
||||
|
||||
func toBool(v any) (b bool, ok bool) {
|
||||
func ToBool(v any) (b bool, ok bool) {
|
||||
ok = true
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
@ -201,7 +201,7 @@ func CloneFilteredMap[K comparable, V any](source map[K]V, filter func(key K) (a
|
||||
return CopyFilteredMap(dest, source, filter)
|
||||
}
|
||||
|
||||
func toInt(value any, description string) (i int, err error) {
|
||||
func ToInt(value any, description string) (i int, err error) {
|
||||
if valueInt64, ok := value.(int64); ok {
|
||||
i = int(valueInt64)
|
||||
} else if valueInt, ok := value.(int); ok {
|
||||
|
Loading…
Reference in New Issue
Block a user