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

     1  package service
     2  
     3  import (
     4  	"strings"
     5  
     6  	consts "github.com/easysoft/zendata/internal/pkg/const"
     7  	"github.com/easysoft/zendata/internal/pkg/domain"
     8  	commonUtils "github.com/easysoft/zendata/pkg/utils/common"
     9  )
    10  
    11  type ListService struct {
    12  	TextService  *TextService  `inject:""`
    13  	RangeService *RangeService `inject:""`
    14  }
    15  
    16  func (s *ListService) CreateListField(field *domain.DefField) {
    17  	if len(field.Fields) > 0 {
    18  		for _, child := range field.Fields {
    19  			s.CreateListField(&child)
    20  		}
    21  	} else {
    22  		s.CreateListFieldValues(field)
    23  	}
    24  }
    25  
    26  func (s *ListService) CreateListFieldValues(field *domain.DefField) {
    27  	if strings.Index(field.Range, ".txt") > -1 {
    28  		s.TextService.CreateFieldValuesFromText(field)
    29  	} else {
    30  		s.RangeService.CreateFieldValuesFromRange(field)
    31  	}
    32  }
    33  
    34  func (s *ListService) AppendValues(items *[]interface{}, val string, repeat int, total int) int {
    35  	for round := 0; round < repeat; round++ {
    36  		*items = append(*items, val)
    37  
    38  		total++
    39  		if total > consts.MaxNumb {
    40  			break
    41  		}
    42  	}
    43  
    44  	return total
    45  }
    46  
    47  func (s *ListService) AppendArrItems(items *[]interface{}, arr []string, total int, isRand bool) int {
    48  	for i := 0; i < len(arr); i++ {
    49  		idx := i
    50  		if isRand {
    51  			idx = commonUtils.RandNum(len(arr)) // should set random here too
    52  		}
    53  
    54  		*items = append(*items, arr[idx])
    55  
    56  		total++
    57  		if total > consts.MaxNumb {
    58  			break
    59  		}
    60  	}
    61  
    62  	return total
    63  }