github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/others/func/proto/test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"net/url"
     9  	"os"
    10  	"path"
    11  	"strings"
    12  
    13  	"github.com/easysoft/zendata/cmd/test/others/func/proto/defaults"
    14  	"github.com/easysoft/zendata/cmd/test/others/func/proto/dist"
    15  	"github.com/easysoft/zendata/internal/pkg/model"
    16  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    17  	httpUtils "github.com/easysoft/zendata/pkg/utils/http"
    18  	stringUtils "github.com/easysoft/zendata/pkg/utils/string"
    19  	"github.com/easysoft/zendata/pkg/utils/vari"
    20  	"gopkg.in/yaml.v3"
    21  )
    22  
    23  var (
    24  	flagSet   *flag.FlagSet
    25  	protoFile string
    26  )
    27  
    28  func main() {
    29  	flagSet = flag.NewFlagSet("zd", flag.ContinueOnError)
    30  	flagSet.StringVar(&protoFile, "f", "", "")
    31  	flagSet.BoolVar(&vari.Verbose, "verbose", false, "")
    32  	flagSet.Parse(os.Args[1:])
    33  
    34  	dir := fileUtils.GetAbsDir(protoFile)
    35  	//yamlPath := getYamlFile(protoFile)
    36  	//request(yamlPath, 8)
    37  
    38  	fld := model.DefField{}
    39  	person := new(dist.Person)
    40  
    41  	defaults.Set(person, &fld)
    42  	def := convertFieldToDef(fld)
    43  
    44  	bytes, _ := yaml.Marshal(def)
    45  	str := stringUtils.ConvertYamlStringToMapFormat(bytes)
    46  	fileUtils.WriteFile(path.Join(dir, "dist/person.yaml"), str)
    47  
    48  	bytes, _ = json.Marshal(person)
    49  	fileUtils.WriteFile(path.Join(dir, "dist/person.json"), string(bytes))
    50  }
    51  
    52  func convertFieldToDef(fld model.DefField) (def model.DefData) {
    53  	def.Version = "1.0"
    54  	def.Title = fld.Field
    55  	def.Author = "ProtoBuf"
    56  
    57  	def.Fields = fld.Fields
    58  
    59  	return
    60  }
    61  
    62  func getYamlFile(protoFile string) string {
    63  	dir := fileUtils.GetAbsDir(protoFile)
    64  	base := path.Base(protoFile)
    65  	yamlPath := path.Join(dir, strings.Replace(base, ".proto", ".yaml", 1))
    66  
    67  	return yamlPath
    68  }
    69  
    70  func request(config string, count int) {
    71  	requestWithParams("127.0.0.1", 8848, "", count, "", config)
    72  }
    73  func requestWithParams(host string, port int, fields string, count int, defaultt, config string) {
    74  	urlStr := httpUtils.GenUrl(host, port, fmt.Sprintf("?F=%s&lines=%d", fields, count))
    75  	data := url.Values{}
    76  
    77  	defaultContent := fileUtils.ReadFile(defaultt)
    78  	configContent := fileUtils.ReadFile(config)
    79  
    80  	data.Add("default", defaultContent)
    81  	data.Add("config", configContent)
    82  	data.Add("lines", "8")
    83  
    84  	resp, _ := httpUtils.PostForm(urlStr, data)
    85  	log.Println(resp.([]byte))
    86  }