github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/service/decode.go (about) 1 package service 2 3 import ( 4 "strings" 5 6 "github.com/easysoft/zendata/internal/pkg/domain" 7 "github.com/easysoft/zendata/pkg/utils/vari" 8 "github.com/mattn/go-runewidth" 9 ) 10 11 type DecodeService struct { 12 DefService *DefService `inject:""` 13 FileService *FileService `inject:""` 14 ResService *ResService `inject:""` 15 } 16 17 //func (s *DecodeService) Decode(contents [][]byte, input string) { 18 // if vari.GlobalVars.Output != "" { 19 // fileUtils.MkDirIfNeeded(filepath.Dir(vari.GlobalVars.Output)) 20 // fileUtils.RemoveExist(vari.GlobalVars.Output) 21 // logUtils.OutputFileWriter, _ = os.OpenFile(vari.GlobalVars.Output, os.O_RDWR|os.O_CREATE, 0777) 22 // 23 // defer logUtils.OutputFileWriter.Close() 24 // } 25 // 26 // //vari.GlobalVars.ConfigFileDir = fileUtils.GetAbsDir(files[0]) 27 // 28 // vari.GlobalVars.Total = 10 29 // 30 // vari.GlobalVars.DefData = s.DefService.LoadDataContentDef(contents, &vari.GlobalVars.ExportFields) 31 // s.ResService.LoadResDef(vari.GlobalVars.ExportFields) 32 // 33 // data := fileUtils.ReadFile(input) 34 // 35 // var ret []map[string]interface{} 36 // s.linesToMap(data, vari.GlobalVars.ExportFields, &ret) 37 // jsonObj, _ := json.Marshal(ret) 38 // vari.JsonResp = string(jsonObj) 39 // 40 // //logUtils.PrintTo(i118Utils.I118Prt.Sprintf("analyse_success", output)) 41 // logUtils.PrintLine(vari.JsonResp) 42 //} 43 44 func (s *DecodeService) linesToMap(str string, fieldsToExport []string, ret *[]map[string]interface{}) { 45 start := 0 46 if vari.GlobalVars.Human { 47 start = 1 48 } 49 50 for index, line := range strings.Split(str, "\n") { 51 if index < start { 52 continue 53 } 54 55 rowMap := map[string]interface{}{} 56 s.decodeOneLevel(line, vari.GlobalVars.DefData.Fields, &rowMap) 57 *ret = append(*ret, rowMap) 58 } 59 return 60 } 61 62 func (s *DecodeService) decodeOneLevel(line string, fields []domain.DefField, rowMap *map[string]interface{}) { 63 left := []rune(line) 64 65 for j, field := range fields { 66 col := "" 67 68 if field.Length > 0 { 69 len := field.Length + runewidth.StringWidth(field.Prefix) + runewidth.StringWidth(field.Postfix) 70 71 col = string(left[:len]) 72 left = left[len:] 73 } else { 74 sepStr := "" 75 if j < len(fields)-1 { 76 sepStr = field.Postfix + fields[j+1].Prefix 77 } else { 78 sepStr = field.Postfix 79 } 80 sep := []rune(sepStr) 81 82 if len(sep) > 0 { 83 index := s.searchRune(left, sep) 84 if index > -1 { 85 col = string(left[:index+len(field.Postfix)]) 86 left = left[index+len(field.Postfix):] 87 } 88 } else if j == len(fields)-1 { 89 col = string(left) 90 left = []rune{} 91 } 92 } 93 94 if vari.GlobalVars.Trim { 95 col = strings.TrimLeft(col, field.Prefix) 96 col = strings.TrimRight(col, field.Postfix) 97 } 98 (*rowMap)[field.Field] = col 99 100 children := field.Fields 101 if len(children) > 0 { 102 rowMapChild := map[string]interface{}{} 103 s.decodeOneLevel(col, children, &rowMapChild) 104 105 (*rowMap)[field.Field+".fields"] = rowMapChild 106 } 107 } 108 109 return 110 } 111 112 func (s *DecodeService) searchRune(text []rune, what []rune) int { 113 for i := range text { 114 found := true 115 for j := range what { 116 if i+j < len(text) && text[i+j] != what[j] { 117 found = false 118 break 119 } 120 } 121 if found { 122 return i 123 } 124 } 125 return -1 126 }