-
Notifications
You must be signed in to change notification settings - Fork 3
/
unpack.go
175 lines (166 loc) · 4.18 KB
/
unpack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/spkg/bom"
)
func unpackJSON(filename string) {
bs, err := os.ReadFile(filename)
if err != nil {
fmt.Println(err)
return
}
bs = bom.Clean(bs)
test := testFormatStruct{}
if err := json.Unmarshal(bs, &test); err != nil {
fmt.Println("unmarshal file test:", err)
return
}
if test.len() == 1 && len(test.Name) > 0 {
importNew = true
file := dataFile{}
if err := json.Unmarshal(bs, &file); err != nil {
fmt.Println("unmarshal file error_2:", err)
return
}
if split {
splitDataFile(file)
return
} else {
unpackDataFile(file.Data)
}
} else {
file := importFile{}
if err := json.Unmarshal(bs, &file); err != nil {
fmt.Println("unmarshal file error_1:", err)
return
}
unpackStruct(file.Contracts, eSIM, dirCon)
unpackStruct(file.Menus, ePTL, dirMenu)
unpackStruct(file.Snippets, ePTL, dirSnippet)
unpackStruct(file.Pages, ePTL, dirPage)
unpackStruct(file.Tables, eJSON, dirTable)
unpackStruct(file.Parameters, eCSV, dirParam)
unpackStruct(file.Languages, eJSON, dirLang)
if len(file.Data) > 0 {
createDir(filepath.Join(outputName, dirData))
for _, item := range file.Data {
name := item.Table + eJSON
name = filepath.Join(dirData, name)
result, _ := json.MarshalIndent(item, "", " ")
writeFileString(name, string(result))
}
}
}
writeConfig(bs)
if abs, err := filepath.Abs(outputName); err == nil {
abspath := filepath.Join(abs, structFileName)
createGraph(abspath)
}
fmt.Println("unpack complete!\noutput dir:", outputName)
}
func unpackStruct(items []commonStruct, tail, dir string) {
if len(items) > 0 {
createDir(filepath.Join(outputName, dir))
for _, item := range items {
value := item.Value
fmt.Println(value)
switch dir {
case dirTable:
value = item.Columns
case dirLang:
value = item.Trans
}
name := item.Name
if len(item.Table) > 0 {
name = item.Table
}
fullName := name + tail
fullName = filepath.Join(dir, fullName)
writeFileString(fullName, value)
}
}
}
func unpackDataFile(items []importStruct) {
for _, item := range items {
createDir(filepath.Join(outputName, item.dir()))
value := item.Value
switch item.dir() {
case dirTable:
value = item.Columns
case dirLang:
value = item.Trans
case dirCon:
err := ParserGrammarBase(value)
if err != nil {
panic(err)
}
}
fullName := filepath.Join(item.dir(), item.fullName())
writeFileString(fullName, value)
}
}
func splitDataFile(items dataFile) {
segments := splitArray(items.Data, int64(number))
for i := 0; i < len(segments); i++ {
split := segments[i]
data := dataFile{}
data.Name = items.Name
data.Conditions = items.Conditions
data.Data = append(data.Data, split...)
result, _ := _JSONMarshal(data, true)
ext := filepath.Ext(outputName)
path := outputName[:len(outputName)-len(ext)] + strconv.Itoa(i+1) + ext
writeFileString2(path, string(result))
}
return
// sub dir split.
var tp = make(map[string][]importStruct)
for _, out := range items.Data {
var path string
ext := filepath.Ext(outputName)
path = outputName[:len(outputName)-len(ext)] + "." + out.dir() + ext
tp[path] = append(tp[path], out)
}
for path, out := range tp {
segments := splitArray(out, int64(number))
for i := 0; i < len(segments); i++ {
split := segments[i]
data := dataFile{}
data.Name = items.Name
data.Conditions = items.Conditions
data.Data = append(data.Data, split...)
result, _ := _JSONMarshal(data, true)
ext := filepath.Ext(path)
path := path[:len(path)-len(ext)] + strconv.Itoa(i+1) + ext
writeFileString2(path, string(result))
}
}
}
func splitArray(arr []importStruct, num int64) [][]importStruct {
max := int64(len(arr))
if max <= num || num <= 0 {
return [][]importStruct{arr}
}
var quantity int64
if max%num == 0 {
quantity = max / num
} else {
quantity = (max / num) + 1
}
var segments = make([][]importStruct, 0)
var start, end, i int64
for i = 1; i <= quantity; i++ {
end = i * num
if i != quantity {
segments = append(segments, arr[start:end])
} else {
segments = append(segments, arr[start:])
}
start = i * num
}
return segments
}