forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatchfile.go
136 lines (90 loc) · 2.61 KB
/
patchfile.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
package main
import (
"github.com/davyxu/pbmeta"
pbprotos "github.com/davyxu/pbmeta/proto"
"github.com/davyxu/tabtoy/data"
)
type PatchFile struct {
pool *pbmeta.DescriptorPool
sheetDataArray []*sheetData
}
func (self *PatchFile) Load(filename string) bool {
self.sheetDataArray = exportSheetMsg(self.pool, filename)
if self.sheetDataArray == nil {
return false
}
return true
}
func (self *PatchFile) Patch(input []*sheetData) bool {
if self.sheetDataArray == nil {
return false
}
if len(self.sheetDataArray) != len(input) {
log.Errorf("patch failed, sheet count do not match")
return false
}
for index, sheetData := range self.sheetDataArray {
inputData := input[index]
if inputData.name != sheetData.name {
log.Errorf("patch failed, sheet name not match")
return false
}
if !patchMsg(inputData.msg, sheetData.msg, 0, inputData.name) {
return false
}
}
return true
}
func patchMsg(input, patch *data.DynamicMessage, indent int, sheetName string) bool {
return patch.IterateFieldDesc(func(fd *pbmeta.FieldDescriptor) bool {
if fd.Type() == pbprotos.FieldDescriptorProto_TYPE_MESSAGE {
if fd.IsRepeated() {
// 顶级的消息肯定是repeated的, 因此直接进入
if indent == 0 {
nextInputMsgArray := input.GetRepeatedMessage(fd)
nextPatchMsgArray := patch.GetRepeatedMessage(fd)
if len(nextInputMsgArray) != len(nextPatchMsgArray) {
log.Errorf("patch rows diff from input msg")
return false
}
// 相同的行数才可以进去继续迭代
for index, nextInputMsg := range nextInputMsgArray {
patchMsg(nextInputMsg, nextPatchMsgArray[index], indent+1, sheetName)
}
} else {
// 直接替换值数组
pathMsgArray := patch.GetRepeatedMessage(fd)
input.ClearFieldValue(fd)
for _, msg := range pathMsgArray {
input.AddRepeatedMessage(fd, msg)
}
}
} else {
nextInputMsg := input.GetMessage(fd)
nextPatchMsg := patch.GetMessage(fd)
patchMsg(nextInputMsg, nextPatchMsg, indent+1, sheetName)
}
} else {
if fd.IsRepeated() {
log.Infof("patch repeated field: '%s' = '%s'", fd.Name(), patch.GetRepeatedValue(fd))
// 直接用patch的覆盖多值
input.ClearFieldValue(fd)
for _, v := range patch.GetRepeatedValue(fd) {
input.AddRepeatedValue(fd, v)
}
} else {
v, _ := patch.GetValue(fd)
log.Infof("patch field: '%s' = '%s'", fd.Name(), v)
// 单值设置
input.SetValue(fd, v)
}
}
return true
})
return true
}
func NewPatchFile(pool *pbmeta.DescriptorPool) *PatchFile {
return &PatchFile{
pool: pool,
}
}