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

gen def yaml from DB

上级 1854a924
......@@ -30,8 +30,8 @@ type ResData struct {
type Model struct {
ID uint `gorm:"column:id;primary_key" json:"id"`
CreatedAt time.Time `gorm:"column:createTime" json:"createTime" yaml:"-"`
UpdatedAt time.Time `gorm:"column:updateTime" json:"updateTime" yaml:"-"`
CreatedAt time.Time `gorm:"column:createdAt" json:"createdAt" yaml:"-"`
UpdatedAt time.Time `gorm:"column:updatedAt" json:"updatedAt" yaml:"-"`
Disabled bool `gorm:"column:disabled;default:false" json:"disabled" yaml:"-"`
Deleted bool `gorm:"column:deleted;default:false" json:"deleted" yaml:"-"`
......
......@@ -11,7 +11,7 @@ type ConfigRepo struct {
}
func (r *ConfigRepo) ListAll() (models []*model.ZdConfig) {
r.db.Find(&models)
r.db.Select("id,title,folder,path,updatedAt").Find(&models)
return
}
......
......@@ -11,7 +11,7 @@ type DefRepo struct {
}
func (r *DefRepo) ListAll() (models []*model.ZdDef) {
r.db.Find(&models)
r.db.Select("id,title,folder,path,updatedAt").Find(&models)
return
}
......
......@@ -11,7 +11,7 @@ type ExcelRepo struct {
}
func (r *ExcelRepo) ListAll() (models []*model.ZdExcel) {
r.db.Find(&models)
r.db.Select("id,title,folder,path,updatedAt").Find(&models)
return
}
......
......@@ -11,7 +11,7 @@ type InstancesRepo struct {
}
func (r *InstancesRepo) ListAll() (models []*model.ZdInstances) {
r.db.Find(&models)
r.db.Select("id,title,folder,path,updatedAt").Find(&models)
return
}
......
......@@ -11,7 +11,7 @@ type RangesRepo struct {
}
func (r *RangesRepo) ListAll() (models []*model.ZdRanges) {
r.db.Find(&models)
r.db.Select("id,title,folder,path,updatedAt").Find(&models)
return
}
......
......@@ -11,7 +11,7 @@ type TextRepo struct {
}
func (r *TextRepo) ListAll() (models []*model.ZdText) {
r.db.Find(&models)
r.db.Select("id,title,folder,path,updatedAt").Find(&models)
return
}
......
......@@ -21,12 +21,7 @@ type ConfigService struct {
}
func (s *ConfigService) List(keywords string, page int) (list []*model.ZdConfig, total int) {
config := s.resService.LoadRes("config")
list, _, _ = s.configRepo.List("", -1)
s.importResToDB(config, list)
list, total, _ = s.configRepo.List(strings.TrimSpace(keywords), page)
return
}
......
......@@ -52,9 +52,8 @@ func (s *DefService) Create(def *model.ZdDef) (err error) {
rootField, err := s.fieldRepo.CreateTreeNode(def.ID, 0, "字段", "root")
s.referRepo.CreateDefault(rootField.ID, constant.ResTypeDef)
s.dataToYaml(def)
err = s.defRepo.Update(def)
s.updateYaml(def.ID)
return
}
......@@ -69,8 +68,8 @@ func (s *DefService) Update(def *model.ZdDef) (err error) {
fileUtils.RemoveExist(old.Path)
}
s.dataToYaml(def)
err = s.defRepo.Update(def)
s.updateYaml(def.ID)
return
}
......@@ -87,17 +86,18 @@ func (s *DefService) Remove(id int) (err error) {
return
}
func (s *DefService) UpdateYaml(defId uint) (err error) {
func (s *DefService) updateYaml(defId uint) (err error) {
var def model.ZdDef
def, _ = s.defRepo.Get(defId)
s.dataToYaml(&def)
s.genYaml(&def)
err = s.defRepo.UpdateYaml(def)
fileUtils.WriteFile(def.Path, def.Yaml)
return
}
func (s *DefService) dataToYaml(def *model.ZdDef) (str string) {
func (s *DefService) genYaml(def *model.ZdDef) (str string) {
root, err := s.fieldRepo.GetDefFieldTree(def.ID)
if err != nil {
return
......@@ -114,7 +114,7 @@ func (s *DefService) dataToYaml(def *model.ZdDef) (str string) {
}
bytes, err := yaml.Marshal(defData)
def.Yaml = string(bytes)
def.Yaml = stringUtils.ConvertYamlStringToMapFormat(bytes)
return
}
......@@ -129,11 +129,13 @@ func (s *DefService) Sync(files []model.ResFile) (err error) {
for _, fi := range files {
_, found := defMap[fi.Path]
if found {
if !found { // no record
s.SyncToDB(fi)
} else if fi.UpdatedAt.Unix() > defMap[fi.Path].UpdatedAt.Unix() { // db is old
s.defRepo.Remove(defMap[fi.Path].ID)
}
if !found || fi.UpdatedAt.Unix() > defMap[fi.Path].UpdatedAt.Unix() {
s.SyncToDB(fi)
} else { // db is new
}
}
......@@ -172,10 +174,6 @@ func (s *DefService) SyncToDB(fi model.ResFile) (err error) {
return
}
func (s *DefService) SyncFromDB(files []model.ResFile) (err error) {
return
}
func NewDefService(defRepo *serverRepo.DefRepo, fieldRepo *serverRepo.FieldRepo,
referRepo *serverRepo.ReferRepo) *DefService {
......
......@@ -19,12 +19,7 @@ type ExcelService struct {
}
func (s *ExcelService) List(keywords string, page int) (list []*model.ZdExcel, total int) {
excel := s.resService.LoadRes("excel")
list, _, _ = s.excelRepo.List("", -1)
s.importResToDB(excel, list)
list, total, _ = s.excelRepo.List(strings.TrimSpace(keywords), page)
return
}
......
......@@ -22,12 +22,7 @@ type InstancesService struct {
}
func (s *InstancesService) List(keywords string, page int) (list []*model.ZdInstances, total int) {
instances := s.resService.LoadRes("instances")
list, _, _ = s.instancesRepo.List("", -1)
s.importResToDB(instances, list)
list, total, _ = s.instancesRepo.List(strings.TrimSpace(keywords), page)
return
}
......
......@@ -21,12 +21,7 @@ type RangesService struct {
}
func (s *RangesService) List(keywords string, page int) (list []*model.ZdRanges, total int) {
ranges := s.resService.LoadRes("ranges")
list, _, _ = s.rangesRepo.List("", -1)
s.importResToDB(ranges, list)
list, total, _ = s.rangesRepo.List(strings.TrimSpace(keywords), page)
return
}
......
......@@ -26,7 +26,6 @@ func (s *SyncService) SyncData(mode string) { // TODO: overwrite or not
}
defs := fileMap["yaml"]
s.defService.Sync(defs)
}
......
......@@ -19,10 +19,6 @@ type TextService struct {
}
func (s *TextService) List(keywords string, page int) (list []*model.ZdText, total int) {
texts := s.resService.LoadRes("text")
list, _, _ = s.textRepo.List("", -1)
s.importResToDB(texts, list)
list, total, _ = s.textRepo.List(strings.TrimSpace(keywords), page)
return
......
......@@ -5,6 +5,7 @@ import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
stringUtils "github.com/easysoft/zendata/src/utils/string"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v3"
"path"
......@@ -76,13 +77,7 @@ func convertSentYaml(filePath, dist string) (yamlPaths []string) {
}
bytes, _ := yaml.Marshal(&conf)
content := string(bytes)
// convert yaml format by using a map
m := make(map[string]interface{})
yaml.Unmarshal([]byte(content), &m)
bytes, _ = yaml.Marshal(&m)
content = string(bytes)
content := stringUtils.ConvertYamlStringToMapFormat(bytes)
content = strings.Replace(content, "xfields", "\nfields", -1)
yamlPath := fileUtils.AddSepIfNeeded(dist) +
......
......@@ -149,7 +149,7 @@ func GetFilesAndDirs(pth, typ string, res *map[string][]model.ResFile) {
continue
}
file := model.ResFile{Path: pth + constant.PthSep + name}
file := model.ResFile{Path: pth + constant.PthSep + name, UpdatedAt: fi.ModTime()}
(*res)[typ] = append((*res)[typ], file)
}
}
......
......@@ -12,6 +12,7 @@ import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
"github.com/mattn/go-runewidth"
"gopkg.in/yaml.v2"
"strconv"
"strings"
"unicode"
......@@ -268,3 +269,12 @@ func ReplaceSpecialChars(bytes []byte) []byte {
return []byte(ret)
}
func ConvertYamlStringToMapFormat(bytes []byte) (ret string) {
m := yaml.MapSlice{}
yaml.Unmarshal(bytes, &m)
bytesReturn, _ := yaml.Marshal(&m)
ret = string(bytesReturn)
return
}
......@@ -15,7 +15,7 @@ h1, h2, h3, h4, h5, h6 {
.head {
display: flex;
.title {
width: 200px;
width: 300px;
margin-bottom: 15px;
padding: 3px 5px;
line-height: 32px;
......
title: 数据测试
desc:
author: zentao
version: 1.0
fields:
- field: field_with_children # 字段多层嵌套
fields:
- field: child1
range: a-z
prefix: part1_
postfix: '|'
- field: child2
range: A-Z
prefix: part2_
postfix: '|'
- field: child_with_child
prefix: part3_
postfix:
fields:
- field: field_grandson
prefix: int_
range: 10-20
postfix:
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册