From afa57abf8511fa64c789099dd27a98ca522fcc12 Mon Sep 17 00:00:00 2001 From: chenqi99 Date: Sun, 1 Sep 2024 11:28:29 +0800 Subject: [PATCH] fix issue of parsing datapool js expression --- internal/agent/exec/datapool.go | 25 ++++---------- internal/agent/exec/goja.go | 5 +++ internal/agent/exec/variable-replace.go | 45 +++++++------------------ 3 files changed, 25 insertions(+), 50 deletions(-) diff --git a/internal/agent/exec/datapool.go b/internal/agent/exec/datapool.go index af0a1cf7e..ad798443d 100644 --- a/internal/agent/exec/datapool.go +++ b/internal/agent/exec/datapool.go @@ -2,33 +2,20 @@ package agentExec import ( "fmt" - "github.com/aaronchen2k/deeptest/internal/pkg/consts" "github.com/aaronchen2k/deeptest/internal/pkg/domain" + _stringUtils "github.com/aaronchen2k/deeptest/pkg/lib/string" "math/rand" - "regexp" "strconv" - "strings" "time" ) -func getDatapoolValue(placeholder string, session *ExecSession) (ret string) { +func getDatapoolValue(dpName, dpCol string, dpSeq interface{}, session *ExecSession) (ret string) { execScene := session.ExecScene // _dp(name, col, 1 | seq | rand >) - regex := regexp.MustCompile(fmt.Sprintf("(?Ui)%s\\((.+),(.+),(.+)\\)", consts.PlaceholderPrefixDatapool)) - arrs := regex.FindAllStringSubmatch(placeholder, -1) - - if !(len(arrs) == 1 && len(arrs[0]) == 4) { - return - } - - dpName := strings.TrimSpace(arrs[0][1]) - dpCol := strings.TrimSpace(arrs[0][2]) - dpSeq := strings.TrimSpace(arrs[0][3]) - dp := execScene.Datapools[dpName] if dp == nil { - ret = fmt.Sprintf("${%s}", placeholder) + ret = fmt.Sprintf("datapoll ${%s} no found", dpName) return } @@ -49,7 +36,7 @@ func getDatapoolValue(placeholder string, session *ExecSession) (ret string) { return } -func getDatapoolRow(dpName, seq string, datapools domain.Datapools, datapoolCursor map[string]int) (ret int) { +func getDatapoolRow(dpName string, seq interface{}, datapools domain.Datapools, datapoolCursor map[string]int) (ret int) { dp := datapools[dpName] if dp == nil { return @@ -57,6 +44,8 @@ func getDatapoolRow(dpName, seq string, datapools domain.Datapools, datapoolCurs total := len(dp) + seqStr := _stringUtils.InterfToStr(seq) + if seq == "seq" { ret = datapoolCursor[dpName] % total datapoolCursor[dpName]++ @@ -66,7 +55,7 @@ func getDatapoolRow(dpName, seq string, datapools domain.Datapools, datapoolCurs ret = rand.Intn(total) } else { - seqInt, _ := strconv.Atoi(seq) + seqInt, _ := strconv.Atoi(seqStr) ret = seqInt - 1 ret = ret % total diff --git a/internal/agent/exec/goja.go b/internal/agent/exec/goja.go index cbd82ac5b..a7e6b6ebf 100644 --- a/internal/agent/exec/goja.go +++ b/internal/agent/exec/goja.go @@ -107,6 +107,11 @@ func defineJsFuncs(runtime *goja.Runtime, require *require.RequireModule, sessio return }) + runtime.Set("_dp", func(dp string, col string, seq interface{}) (ret interface{}) { + ret = getDatapoolValue(dp, col, seq, session) + return + }) + if isSimple { return } diff --git a/internal/agent/exec/variable-replace.go b/internal/agent/exec/variable-replace.go index 52190d231..58e2ee5ea 100644 --- a/internal/agent/exec/variable-replace.go +++ b/internal/agent/exec/variable-replace.go @@ -3,7 +3,6 @@ package agentExec import ( "github.com/aaronchen2k/deeptest/internal/pkg/consts" mockData "github.com/aaronchen2k/deeptest/internal/pkg/helper/openapi-mock/openapi/generator/data" - commUtils "github.com/aaronchen2k/deeptest/internal/pkg/utils" _commUtils "github.com/aaronchen2k/deeptest/pkg/lib/comm" _stringUtils "github.com/aaronchen2k/deeptest/pkg/lib/string" "regexp" @@ -52,24 +51,6 @@ func ReplaceVariableValueInBody(value string, session *ExecSession) (ret string) data = execJsFuncSimple(data, session) ret = _commUtils.JsonEncode(data) return - /* - variablePlaceholders := commUtils.GetVariablesInExpressionPlaceholder(value) - ret = value - - for _, placeholder := range variablePlaceholders { - oldVal := fmt.Sprintf("${%s}", placeholder) - if strings.Index(placeholder, "+") == 0 { // replace it with a number, if has prefix + - oldVal = "\"" + oldVal + "\"" - } - - placeholderWithoutPlus := strings.TrimLeft(placeholder, "+") - newVal := getPlaceholderVariableValue(placeholderWithoutPlus, session) - - ret = strings.ReplaceAll(ret, oldVal, newVal) - } - */ - - return } func execJsFuncSimple(data interface{}, session *ExecSession) interface{} { @@ -193,19 +174,19 @@ func parseStatement(statement string) (ret []Placeholder) { return } -func getPlaceholderVariableValue(name string, session *ExecSession) (ret string) { - typ := getPlaceholderType(name) - - if typ == consts.PlaceholderTypeVariable { - variable, _ := GetVariable(name, session.GetCurrScenarioProcessorId(), session) - ret, _ = commUtils.ConvertValueForPersistence(variable.Value) - - } else if typ == consts.PlaceholderTypeDatapool { - ret = getDatapoolValue(name, session) - } - - return -} +//func getPlaceholderVariableValue(name string, session *ExecSession) (ret string) { +// typ := getPlaceholderType(name) +// +// if typ == consts.PlaceholderTypeVariable { +// variable, _ := GetVariable(name, session.GetCurrScenarioProcessorId(), session) +// ret, _ = commUtils.ConvertValueForPersistence(variable.Value) +// +// } else if typ == consts.PlaceholderTypeDatapool { +// ret = getDatapoolValue(name, session) +// } +// +// return +//} func getPlaceholderType(placeholder string) (ret consts.PlaceholderType) { if strings.HasPrefix(placeholder, consts.PlaceholderPrefixDatapool.String()) {