github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/gen/generator.go (about)

     1  package gen
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/easysoft/zendata/cmd/command/action"
     7  	"github.com/easysoft/zendata/cmd/test/consts"
     8  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
     9  	"github.com/easysoft/zendata/pkg/utils/vari"
    10  )
    11  
    12  type Generator struct {
    13  	Total        int
    14  	Configs      []string
    15  	ExportFields []string
    16  	Output       string
    17  	Trim         bool
    18  	Human        bool
    19  	Recursive    bool
    20  
    21  	DBType   string
    22  	Table    string
    23  	ProtoCls string
    24  }
    25  
    26  func New() (g *Generator) {
    27  	g = &Generator{}
    28  
    29  	return
    30  }
    31  
    32  func (s *Generator) Gen() (out string) {
    33  	if s.Total == 0 {
    34  		s.Total = 10
    35  	}
    36  
    37  	vari.GlobalVars.Total = s.Total
    38  	vari.GlobalVars.ExportFields = s.ExportFields
    39  	vari.GlobalVars.Output = s.Output
    40  	vari.GlobalVars.Trim = s.Trim
    41  	vari.GlobalVars.Human = s.Human
    42  	vari.GlobalVars.Recursive = s.Recursive
    43  
    44  	vari.GlobalVars.DBType = s.DBType
    45  	vari.GlobalVars.Table = s.Table
    46  	vari.ProtoCls = s.ProtoCls
    47  
    48  	action.GenData(s.Configs)
    49  
    50  	if len(s.Configs) > 0 && strings.HasSuffix(s.Configs[0], "proto") {
    51  		out = fileUtils.ReadFile(consts.CommandTestFileProtoOut)
    52  
    53  	} else if vari.GlobalVars.Output != "" && !strings.HasSuffix(vari.GlobalVars.Output, "xlsx") {
    54  		out = fileUtils.ReadFile(vari.GlobalVars.Output)
    55  
    56  	} else {
    57  		out = consts.Buf.String()
    58  	}
    59  
    60  	vari.GlobalVars.Trim = false
    61  
    62  	return
    63  }
    64  
    65  func (s *Generator) SetConfigs(configs []string) (r *Generator) {
    66  	s.Configs = configs
    67  
    68  	r = s
    69  	return r
    70  }
    71  
    72  func (s *Generator) SetTotal(total int) (r *Generator) {
    73  	s.Total = total
    74  
    75  	r = s
    76  	return r
    77  }
    78  
    79  func (s *Generator) SetFields(fields string) (r *Generator) {
    80  	if fields == "" {
    81  		r = s
    82  		return
    83  	}
    84  
    85  	arr := strings.Split(fields, ",")
    86  	s.ExportFields = arr
    87  
    88  	r = s
    89  	return r
    90  }
    91  
    92  func (s *Generator) SetOutput(pth string) (r *Generator) {
    93  	s.Output = pth
    94  
    95  	r = s
    96  	return r
    97  }
    98  
    99  func (s *Generator) SetDBTable(tp string) (r *Generator) {
   100  	s.DBType = tp
   101  
   102  	r = s
   103  	return r
   104  }
   105  func (s *Generator) SetTable(tbl string) (r *Generator) {
   106  	s.Table = tbl
   107  
   108  	r = s
   109  	return r
   110  }
   111  
   112  func (s *Generator) SetProtoCls(cls string) (r *Generator) {
   113  	s.ProtoCls = cls
   114  
   115  	r = s
   116  	return r
   117  }
   118  
   119  func (s *Generator) SetTrim(val bool) (r *Generator) {
   120  	s.Trim = val
   121  
   122  	r = s
   123  	return r
   124  }
   125  
   126  func (s *Generator) SetHuman(val bool) (r *Generator) {
   127  	s.Human = val
   128  
   129  	r = s
   130  	return r
   131  }
   132  
   133  func (s *Generator) SetRecursive(val bool) (r *Generator) {
   134  	s.Recursive = val
   135  
   136  	r = s
   137  	return r
   138  }