github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/service/placeholder.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/easysoft/zendata/internal/pkg/helper"
    10  	commonUtils "github.com/easysoft/zendata/pkg/utils/common"
    11  	"github.com/easysoft/zendata/pkg/utils/vari"
    12  )
    13  
    14  type PlaceholderService struct {
    15  	FixService *FixService `inject:""`
    16  }
    17  
    18  func (s *PlaceholderService) ReplacePlaceholder(val string) (ret interface{}) {
    19  	ret = val
    20  
    21  	re := regexp.MustCompile("(?siU)\\${(.*)}")
    22  	matchResultArr := re.FindAllStringSubmatch(fmt.Sprintf("%v", ret), -1)
    23  	matchTimes := len(matchResultArr)
    24  
    25  	for _, childArr := range matchResultArr {
    26  		placeholderStr := childArr[1]
    27  		values := s.getValForPlaceholder(placeholderStr, matchTimes)
    28  
    29  		for _, val := range values {
    30  			key, _ := strconv.Atoi(placeholderStr)
    31  			temp := s.PlaceholderStr(key)
    32  
    33  			str := fmt.Sprintf("%v", ret)
    34  			isNotStr := matchTimes == 1 && commonUtils.GetType(val) != "string" &&
    35  				strings.Index(str, "${") == 0 && str[len(str)-1] == '}'
    36  
    37  			if isNotStr {
    38  				ret = val
    39  			} else {
    40  				ret = strings.Replace(fmt.Sprintf("%v", ret), temp, fmt.Sprintf("%v", val), 1)
    41  				ret = s.FixService.TrimIfFormatIsNotText(fmt.Sprintf("%v", ret))
    42  			}
    43  		}
    44  	}
    45  
    46  	return
    47  }
    48  
    49  func (s *PlaceholderService) getValForPlaceholder(placeholderStr string, count int) (ret []interface{}) {
    50  	placeholderNo, _ := strconv.Atoi(placeholderStr)
    51  	mp := vari.GlobalVars.RandFieldSectionPathToValuesMap[placeholderNo]
    52  
    53  	tp := mp["type"].(string)
    54  	repeatObj := mp["repeat"]
    55  
    56  	repeat := 1
    57  	if repeatObj != nil {
    58  		repeat = repeatObj.(int)
    59  	}
    60  
    61  	repeatTag := mp["repeatTag"].(string)
    62  	if tp == "int" {
    63  		start := mp["start"].(string)
    64  		end := mp["end"].(string)
    65  		precision := mp["precision"].(string)
    66  		format := mp["format"].(string)
    67  
    68  		ret = helper.GetRandValuesFromRange("int", start, end, "1", repeat, repeatTag, precision, format, count)
    69  
    70  	} else if tp == "float" {
    71  		start := mp["start"].(string)
    72  		end := mp["end"].(string)
    73  		stepStr := fmt.Sprintf("%v", mp["step"])
    74  
    75  		precision := mp["precision"].(string)
    76  		format := mp["format"].(string)
    77  
    78  		ret = helper.GetRandValuesFromRange("float", start, end, stepStr,
    79  			repeat, repeatTag, precision, format, count)
    80  
    81  	} else if tp == "char" {
    82  		start := mp["start"].(string)
    83  		end := mp["end"].(string)
    84  		precision := mp["precision"].(string)
    85  		format := mp["format"].(string)
    86  
    87  		ret = helper.GetRandValuesFromRange("char", start, end, "1",
    88  			repeat, repeatTag, precision, format, count)
    89  
    90  	} else if tp == "list" {
    91  		list := mp["list"].([]string)
    92  		ret = helper.GetRandFromList(list, repeat, count)
    93  
    94  	}
    95  
    96  	ret = ret[:count]
    97  
    98  	return
    99  }
   100  
   101  func (s *PlaceholderService) PlaceholderStr(key int) string {
   102  	return fmt.Sprintf("${%d}", key)
   103  }
   104  
   105  func (s *PlaceholderService) PlaceholderMapForRandValues(tp string, list []string, start, end, step, precision, format string,
   106  	repeat int, repeatTag string) map[string]interface{} {
   107  	ret := map[string]interface{}{}
   108  
   109  	ret["type"] = tp
   110  
   111  	// for literal values
   112  	ret["list"] = list
   113  
   114  	// for interval values
   115  	ret["start"] = start
   116  	ret["end"] = end
   117  	ret["step"] = step
   118  	ret["precision"] = precision
   119  	ret["format"] = format
   120  
   121  	ret["repeat"] = repeat
   122  	ret["repeatTag"] = repeatTag
   123  
   124  	return ret
   125  }
   126  
   127  func (s *PlaceholderService) GetRandFieldSectionKey(pth string) (key int) {
   128  	max := 0
   129  
   130  	for k, v := range vari.GlobalVars.RandFieldSectionShortKeysToPathMap {
   131  		if pth == v {
   132  			key = k
   133  			return
   134  		}
   135  
   136  		if k > max {
   137  			max = k
   138  		}
   139  	}
   140  
   141  	if key == 0 {
   142  		key = max + 1
   143  	}
   144  
   145  	return
   146  }