提交 b64cc827 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

gen mock data

上级 b24c645c
......@@ -6,10 +6,12 @@ version: 1.0
fields:
- field: id
range: 1-10000
postfix: "\t"
- field: name
prefix: "product-"
range: 1-10000
postfix: "\t"
- field: name
- field: desc
range: "描述"
......@@ -218,7 +218,7 @@ type EndPoint struct {
Summary string `json:"summary"`
Config string `json:"config"`
Type string `json:"type"`
Lines string `json:"lines"`
Lines int `json:"lines"`
Fields string `json:"fields"`
}
......
......@@ -79,11 +79,11 @@ func injectModule(ws *WebServer) {
&inject.Object{Value: vari.DB},
&inject.Object{Value: indexModule},
); err != nil {
logUtils.PrintErrMsg(fmt.Sprintf("provide usecase objects to the Graph: %v", err))
panic(fmt.Sprintf("provide usecase objects to the Graph: %v", err))
}
err := g.Populate()
if err != nil {
logUtils.PrintErrMsg(fmt.Sprintf("populate the incomplete Objects: %v", err))
panic(fmt.Sprintf("populate the incomplete Objects: %v", err))
}
ws.AddModule(indexModule.Party(), indexModule.PartyData(), indexModule.PartyMock())
......
......@@ -10,8 +10,8 @@ import (
type IndexModule struct {
CommModule *index.CommModule `inject:""`
DefModule *index.DefModule `inject:""`
DataModule *index.DataModule `inject:""`
AdminModule *index.AdminModule `inject:""`
DataModule *index.DataModule `inject:""`
MockModule *index.MockModule `inject:""`
}
......
package serverService
import (
consts "github.com/easysoft/zendata/internal/pkg/const"
"github.com/easysoft/zendata/internal/pkg/model"
"github.com/easysoft/zendata/internal/pkg/service"
fileUtils "github.com/easysoft/zendata/pkg/utils/file"
"github.com/easysoft/zendata/pkg/utils/vari"
"gopkg.in/yaml.v2"
......@@ -11,7 +13,9 @@ import (
)
type MockService struct {
ResService *ResService `inject:""`
ResService *ResService `inject:""`
MainService *service.MainService `inject:""`
OutputService *service.OutputService `inject:""`
}
func (s *MockService) Init() (err error) {
......@@ -66,7 +70,7 @@ func (s *MockService) LoadDef(pth string, files *[]string, level int) (err error
}
func (s *MockService) GetResp(reqPath, reqMethod string) (ret interface{}, err error) {
reqPath = s.AddPrefixIfNeeded(reqPath)
reqPath = s.addPrefixIfNeeded(reqPath)
reqMethod = strings.ToLower(reqMethod)
if vari.GlobalVars.MockData.Paths[reqPath] == nil || // no such a path
......@@ -80,12 +84,35 @@ func (s *MockService) GetResp(reqPath, reqMethod string) (ret interface{}, err e
}
func (s *MockService) GenData(endpoint *model.EndPoint) (ret interface{}, err error) {
ret = endpoint
vari.GlobalVars.RunMode = consts.RunModeServerRequest
vari.GlobalVars.Total = endpoint.Lines
vari.GlobalVars.ExportFields = strings.Split(endpoint.Fields, ",")
dataType := endpoint.Type
if dataType == "item" {
vari.GlobalVars.Total = 1
}
configFile := filepath.Join(vari.ZdDir, endpoint.Config)
vari.GlobalVars.ConfigFileDir = fileUtils.GetAbsDir(configFile)
configContent := fileUtils.ReadFileBuf(configFile)
contents := [][]byte{configContent}
s.MainService.GenerateDataByContents(contents)
records := s.OutputService.GenRecords()
if dataType == "item" {
ret = records[0]
} else {
ret = records
}
return
}
func (s *MockService) AddPrefixIfNeeded(pth string) (ret string) {
func (s *MockService) addPrefixIfNeeded(pth string) (ret string) {
ret = "/" + strings.TrimPrefix(pth, "/")
return
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册