github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/server/utils/common.go (about) 1 package serverUtils 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "regexp" 7 "strings" 8 9 consts "github.com/easysoft/zendata/internal/pkg/const" 10 "github.com/easysoft/zendata/internal/pkg/domain" 11 "github.com/easysoft/zendata/internal/pkg/model" 12 fileUtils "github.com/easysoft/zendata/pkg/utils/file" 13 "github.com/easysoft/zendata/pkg/utils/vari" 14 ) 15 16 func ConvertDef(data interface{}) (def model.ZdDef) { 17 bytes, _ := json.Marshal(data) 18 json.Unmarshal(bytes, &def) 19 20 return 21 } 22 func ConvertField(data interface{}) (field model.ZdField) { 23 bytes, _ := json.Marshal(data) 24 json.Unmarshal(bytes, &field) 25 26 return 27 } 28 29 func ConvertSection(data interface{}) (section model.ZdSection) { 30 bytes, _ := json.Marshal(data) 31 json.Unmarshal(bytes, §ion) 32 33 return 34 } 35 36 func ConvertRefer(data interface{}) (refer model.ZdRefer) { 37 bytes, _ := json.Marshal(data) 38 json.Unmarshal(bytes, &refer) 39 40 return 41 } 42 43 func ConvertRanges(data interface{}) (ranges model.ZdRanges) { 44 bytes, _ := json.Marshal(data) 45 json.Unmarshal(bytes, &ranges) 46 47 return 48 } 49 func ConvertRangesItem(data interface{}) (item model.ZdRangesItem) { 50 bytes, _ := json.Marshal(data) 51 json.Unmarshal(bytes, &item) 52 53 return 54 } 55 func ConvertInstances(data interface{}) (ranges model.ZdInstances) { 56 bytes, _ := json.Marshal(data) 57 json.Unmarshal(bytes, &ranges) 58 59 return 60 } 61 func ConvertInstancesItem(data interface{}) (item model.ZdInstancesItem) { 62 bytes, _ := json.Marshal(data) 63 json.Unmarshal(bytes, &item) 64 65 return 66 } 67 func ConvertExcel(data interface{}) (ranges model.ZdExcel) { 68 bytes, _ := json.Marshal(data) 69 json.Unmarshal(bytes, &ranges) 70 71 return 72 } 73 func ConvertConfig(data interface{}) (ranges model.ZdConfig) { 74 bytes, _ := json.Marshal(data) 75 json.Unmarshal(bytes, &ranges) 76 77 return 78 } 79 func ConvertText(data interface{}) (ranges model.ZdText) { 80 bytes, _ := json.Marshal(data) 81 json.Unmarshal(bytes, &ranges) 82 83 return 84 } 85 86 func ConvertParams(data interface{}) (mp map[string]string) { 87 bytes, _ := json.Marshal(data) 88 json.Unmarshal(bytes, &mp) 89 90 return 91 } 92 93 func GetRelativePath(pth string) string { 94 idx := strings.LastIndex(pth, consts.PthSep) 95 folder := pth[:idx+1] 96 folder = strings.Replace(folder, vari.WorkDir, "", 1) 97 98 return folder 99 } 100 101 func DealWithPathSepRight(pth string) string { 102 pth = fileUtils.RemovePathSepLeftIfNeeded(pth) 103 pth = fileUtils.AddPathSepRightIfNeeded(pth) 104 105 return pth 106 } 107 108 func AddExt(pth, ext string) string { 109 if strings.LastIndex(pth, ext) != len(pth)-4 { 110 pth += ext 111 } 112 113 return pth 114 } 115 116 func GetDirs(dir string, dirs *[]domain.Dir) { 117 isArticleFiles, _ := regexp.MatchString("yaml.article", dir) 118 if isArticleFiles { 119 return 120 } 121 122 folder := fileUtils.AddSepIfNeeded(dir) 123 *dirs = append(*dirs, domain.Dir{Name: folder}) 124 125 files, _ := ioutil.ReadDir(folder) 126 for _, fi := range files { 127 name := fi.Name() 128 if !fi.IsDir() || strings.Index(name, ".") == 0 { 129 continue 130 } 131 132 childFolder := folder + name 133 GetDirs(childFolder, dirs) 134 } 135 136 return 137 }