mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-27 18:41:33 +08:00
refactor: ConvertToFloat64Slice
This commit is contained in:
@@ -210,26 +210,13 @@ func Interface2Float64(i interface{}) (float64, error) {
|
||||
}
|
||||
|
||||
func TypeNormalization(raw interface{}) interface{} {
|
||||
rawValue := reflect.ValueOf(raw)
|
||||
switch rawValue.Kind() {
|
||||
case reflect.Int:
|
||||
return rawValue.Int()
|
||||
case reflect.Int8:
|
||||
return rawValue.Int()
|
||||
case reflect.Int16:
|
||||
return rawValue.Int()
|
||||
case reflect.Int32:
|
||||
return rawValue.Int()
|
||||
case reflect.Float32:
|
||||
return rawValue.Float()
|
||||
case reflect.Uint:
|
||||
return rawValue.Uint()
|
||||
case reflect.Uint8:
|
||||
return rawValue.Uint()
|
||||
case reflect.Uint16:
|
||||
return rawValue.Uint()
|
||||
case reflect.Uint32:
|
||||
return rawValue.Uint()
|
||||
switch v := raw.(type) {
|
||||
case int, int8, int16, int32, int64:
|
||||
return reflect.ValueOf(v).Int()
|
||||
case uint, uint8, uint16, uint32, uint64:
|
||||
return reflect.ValueOf(v).Uint()
|
||||
case float32, float64:
|
||||
return reflect.ValueOf(v).Float()
|
||||
default:
|
||||
return raw
|
||||
}
|
||||
@@ -396,19 +383,37 @@ func ConvertToFloat64(val interface{}) (float64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func ConvertToStringSlice(val interface{}) ([]string, error) {
|
||||
if valSlice, ok := val.([]interface{}); ok {
|
||||
var res []string
|
||||
for _, iVal := range valSlice {
|
||||
valString, ok := iVal.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid type for converting one of the elements to string: %T, value: %v", iVal, iVal)
|
||||
}
|
||||
res = append(res, valString)
|
||||
}
|
||||
return res, nil
|
||||
func ConvertToFloat64Slice(val interface{}) ([]float64, error) {
|
||||
paramsSlice, ok := val.([]interface{})
|
||||
if !ok {
|
||||
return nil, errors.New("val is not slice")
|
||||
}
|
||||
return nil, fmt.Errorf("invalid type for conversion to []string")
|
||||
|
||||
var err error
|
||||
float64Slice := make([]float64, len(paramsSlice))
|
||||
for i, v := range paramsSlice {
|
||||
float64Slice[i], err = ConvertToFloat64(v)
|
||||
if err != nil {
|
||||
return nil, errors.New("val is not float64 slice")
|
||||
}
|
||||
}
|
||||
return float64Slice, nil
|
||||
}
|
||||
|
||||
func ConvertToStringSlice(val interface{}) ([]string, error) {
|
||||
paramsSlice, ok := val.([]interface{})
|
||||
if !ok {
|
||||
return nil, errors.New("val is not slice")
|
||||
}
|
||||
|
||||
stringSlice := make([]string, len(paramsSlice))
|
||||
for i, v := range paramsSlice {
|
||||
stringSlice[i], ok = v.(string)
|
||||
if !ok {
|
||||
return nil, errors.New("val is not string slice")
|
||||
}
|
||||
}
|
||||
return stringSlice, nil
|
||||
}
|
||||
|
||||
func GetFreePort() (int, error) {
|
||||
@@ -455,6 +460,7 @@ func DownloadFile(filePath string, fileUrl string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: rename token
|
||||
eapiToken := os.Getenv("EAPI_TOKEN")
|
||||
if eapiToken != "" {
|
||||
if parsedURL.Host != "gtf-eapi-cn.bytedance.com" && parsedURL.Host != "gtf-eapi-cn.bytedance.net" {
|
||||
|
||||
@@ -1 +1 @@
|
||||
v5.0.0+2411152045
|
||||
v5.0.0+2411152251
|
||||
|
||||
@@ -109,10 +109,6 @@ func (p *Parser) Parse(raw interface{}, variablesMapping map[string]interface{})
|
||||
value = strings.TrimSpace(value)
|
||||
return p.ParseString(value, variablesMapping)
|
||||
case reflect.Slice:
|
||||
if rawValue.Type().Elem().Kind() == reflect.Float64 {
|
||||
// return float64, avoid convert to []interface{}
|
||||
return raw, nil
|
||||
}
|
||||
parsedSlice := make([]interface{}, rawValue.Len())
|
||||
for i := 0; i < rawValue.Len(); i++ {
|
||||
parsedValue, err := p.Parse(rawValue.Index(i).Interface(), variablesMapping)
|
||||
|
||||
@@ -664,24 +664,22 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
|
||||
return nil
|
||||
}
|
||||
case ACTION_TapXY:
|
||||
if location, ok := action.Params.([]interface{}); ok {
|
||||
if params, err := builtin.ConvertToFloat64Slice(action.Params); err == nil {
|
||||
// relative x,y of window size: [0.5, 0.5]
|
||||
if len(location) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", location)
|
||||
if len(params) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", params)
|
||||
}
|
||||
x, _ := location[0].(float64)
|
||||
y, _ := location[1].(float64)
|
||||
x, y := params[0], params[1]
|
||||
return dExt.TapXY(x, y, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_TapXY, action.Params)
|
||||
case ACTION_TapAbsXY:
|
||||
if location, ok := action.Params.([]interface{}); ok {
|
||||
if params, err := builtin.ConvertToFloat64Slice(action.Params); err == nil {
|
||||
// absolute coordinates x,y of window size: [100, 300]
|
||||
if len(location) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", location)
|
||||
if len(params) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", params)
|
||||
}
|
||||
x, _ := location[0].(float64)
|
||||
y, _ := location[1].(float64)
|
||||
x, y := params[0], params[1]
|
||||
return dExt.TapAbsXY(x, y, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_TapAbsXY, action.Params)
|
||||
@@ -702,13 +700,12 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_TapByCV, action.Params)
|
||||
case ACTION_DoubleTapXY:
|
||||
if location, ok := action.Params.([]interface{}); ok {
|
||||
if params, err := builtin.ConvertToFloat64Slice(action.Params); err == nil {
|
||||
// relative x,y of window size: [0.5, 0.5]
|
||||
if len(location) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", location)
|
||||
if len(params) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", params)
|
||||
}
|
||||
x, _ := location[0].(float64)
|
||||
y, _ := location[1].(float64)
|
||||
x, y := params[0], params[1]
|
||||
return dExt.DoubleTapXY(x, y)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_DoubleTapXY, action.Params)
|
||||
@@ -759,7 +756,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
|
||||
}
|
||||
return fmt.Errorf("invalid sleep ms params: %v(%T)", action.Params, action.Params)
|
||||
case ACTION_SleepRandom:
|
||||
if params, ok := action.Params.([]interface{}); ok {
|
||||
if params, err := builtin.ConvertToFloat64Slice(action.Params); err == nil {
|
||||
sleepStrict(time.Now(), getSimulationDuration(params))
|
||||
return nil
|
||||
}
|
||||
@@ -791,15 +788,10 @@ type SleepConfig struct {
|
||||
var errActionNotImplemented = errors.New("UI action not implemented")
|
||||
|
||||
// getSimulationDuration returns simulation duration by given params (in seconds)
|
||||
func getSimulationDuration(params []interface{}) (milliseconds int64) {
|
||||
func getSimulationDuration(params []float64) (milliseconds int64) {
|
||||
if len(params) == 1 {
|
||||
// given constant duration time
|
||||
seconds, err := builtin.ConvertToFloat64(params[0])
|
||||
if err != nil {
|
||||
log.Error().Err(err).Interface("params", params).Msg("invalid params")
|
||||
return 0
|
||||
}
|
||||
return int64(seconds * 1000)
|
||||
return int64(params[0] * 1000)
|
||||
}
|
||||
|
||||
if len(params) == 2 {
|
||||
@@ -813,21 +805,9 @@ func getSimulationDuration(params []interface{}) (milliseconds int64) {
|
||||
}
|
||||
totalProb := 0.0
|
||||
for i := 0; i+3 <= len(params); i += 3 {
|
||||
min, err := builtin.ConvertToFloat64(params[i])
|
||||
if err != nil {
|
||||
log.Error().Err(err).Interface("min", params[i]).Msg("invalid minimum time")
|
||||
return 0
|
||||
}
|
||||
max, err := builtin.ConvertToFloat64(params[i+1])
|
||||
if err != nil {
|
||||
log.Error().Err(err).Interface("max", params[i+1]).Msg("invalid maximum time")
|
||||
return 0
|
||||
}
|
||||
weight, err := builtin.ConvertToFloat64(params[i+2])
|
||||
if err != nil {
|
||||
log.Error().Err(err).Interface("weight", params[i+2]).Msg("invalid weight value")
|
||||
return 0
|
||||
}
|
||||
min := params[i]
|
||||
max := params[i+1]
|
||||
weight := params[i+2]
|
||||
totalProb += weight
|
||||
sections = append(sections,
|
||||
struct{ min, max, weight float64 }{min, max, weight},
|
||||
|
||||
@@ -16,19 +16,19 @@ func checkErr(t *testing.T, err error, msg ...string) {
|
||||
}
|
||||
|
||||
func TestGetSimulationDuration(t *testing.T) {
|
||||
params := []interface{}{1.23}
|
||||
params := []float64{1.23}
|
||||
duration := getSimulationDuration(params)
|
||||
if duration != 1230 {
|
||||
t.Fatal("getSimulationDuration failed")
|
||||
}
|
||||
|
||||
params = []interface{}{1, 2}
|
||||
params = []float64{1, 2}
|
||||
duration = getSimulationDuration(params)
|
||||
if duration < 1000 || duration > 2000 {
|
||||
t.Fatal("getSimulationDuration failed")
|
||||
}
|
||||
|
||||
params = []interface{}{1, 5, 0.7, 5, 10, 0.3}
|
||||
params = []float64{1, 5, 0.7, 5, 10, 0.3}
|
||||
duration = getSimulationDuration(params)
|
||||
if duration < 1000 || duration > 10000 {
|
||||
t.Fatal("getSimulationDuration failed")
|
||||
|
||||
Reference in New Issue
Block a user