Skip to content

Commit

Permalink
fix issue of parsing datapool js expression
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronchen2k committed Sep 1, 2024
1 parent b89010c commit afa57ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 50 deletions.
25 changes: 7 additions & 18 deletions internal/agent/exec/datapool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -49,14 +36,16 @@ 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
}

total := len(dp)

seqStr := _stringUtils.InterfToStr(seq)

if seq == "seq" {
ret = datapoolCursor[dpName] % total
datapoolCursor[dpName]++
Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions internal/agent/exec/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
45 changes: 13 additions & 32 deletions internal/agent/exec/variable-replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{} {
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit afa57ab

Please sign in to comment.