-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
224 lines (201 loc) · 4.9 KB
/
util.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package vpl
import (
"encoding/json"
"fmt"
"github.com/zbysir/vpl/internal/lib/log"
"github.com/zbysir/vpl/internal/parser"
"github.com/zbysir/vpl/internal/util"
"sort"
"strings"
)
// classProps: 支持 obj, array, string
func getClassFromProps(classProps interface{}) parser.Class {
if classProps == nil {
return nil
}
var cs []string
switch t := classProps.(type) {
case []string:
cs = t
case string:
cs = []string{t}
case map[string]interface{}:
c := make([]string, 0, len(t)/2)
for k, v := range t {
if util.InterfaceToBool(v) {
c = append(c, k)
}
}
sort.Strings(c)
cs = c
case []interface{}:
var c []string
for _, v := range t {
cc := getClassFromProps(v)
c = append(c, cc...)
}
cs = c
}
for i := range cs {
cs[i] = util.Escape(cs[i])
}
return cs
}
func writeClass(classProps interface{}, w *strings.Builder) {
if classProps == nil {
return
}
switch t := classProps.(type) {
case []string:
for _, c := range t {
if w.Len() != 0 {
w.WriteString(" ")
}
w.WriteString(util.Escape(c))
}
case string:
if w.Len() != 0 {
w.WriteString(" ")
}
w.WriteString(util.Escape(t))
case map[string]interface{}:
c := make([]string, 0, len(t)/2)
for k, v := range t {
if util.InterfaceToBool(v) {
c = append(c, k)
}
}
sort.Strings(c)
for _, c := range c {
if w.Len() != 0 {
w.WriteString(" ")
}
w.WriteString(util.Escape(c))
}
case []interface{}:
for _, v := range t {
writeClass(v, w)
}
}
}
// 将静态props生成attr字符串
// 用于预编译
func genAttrFromProps(props parser.Props) string {
s := strings.Builder{}
for _, a := range props {
if !a.IsStatic {
continue
}
if a.Key == "style" {
sty := getStyleFromProps(a.StaticVal)
if len(sty) != 0 {
if s.Len() != 0 {
s.WriteString(" ")
}
s.WriteString(`style="`)
s.WriteString(sty.ToAttr())
s.WriteString(`"`)
}
} else if a.Key == "class" {
cla := getClassFromProps(a.StaticVal)
if len(cla) != 0 {
if s.Len() != 0 {
s.WriteString(" ")
}
s.WriteString(`class="`)
s.WriteString(cla.ToAttr())
s.WriteString(`"`)
}
} else {
if s.Len() != 0 {
s.WriteString(" ")
}
v := a.StaticVal.(string)
if v != "" {
s.WriteString(fmt.Sprintf(`%s="%s"`, a.Key, v))
} else {
s.WriteString(fmt.Sprintf(`%s`, a.Key))
}
}
}
return s.String()
}
// 打印Statement, 方便调试
func NicePrintStatement(st Statement, lev int) string {
index := strings.Repeat(" ", lev*2)
s := ""
switch t := st.(type) {
case *EmptyStatement:
s += fmt.Sprintf("%sEmptyStatement\n", index)
case *StrStatement:
s += fmt.Sprintf("%s%s\n", index, t.Str)
case *groupStatement:
s = ""
for _, v := range t.s {
s += fmt.Sprintf("%s", NicePrintStatement(v, lev))
}
case *ComponentStatement:
s += fmt.Sprintf("%s<%s>\n", index, t.ComponentKey)
if t.ComponentStruct.Slots.Default != nil {
s += fmt.Sprintf("%s", NicePrintStatement(t.ComponentStruct.Slots.Default.Children, lev+1))
}
s += fmt.Sprintf("%s<%s/>\n", index, t.ComponentKey)
case *tagStatement:
s += fmt.Sprintf("%sTag(%s, %+v", index, t.tag, t.tagStruct.Props)
if t.tagStruct.VBind != nil {
if t.tagStruct.VBind.useProps {
s += fmt.Sprintf(", BindProps")
}
}
if len(t.tagStruct.Directives) != 0 {
s += fmt.Sprintf(",")
for _, v := range t.tagStruct.Directives {
s += fmt.Sprintf(" v-%s", v.Name)
}
}
s += fmt.Sprintf(")\n")
if t.tagStruct.Slots != nil && t.tagStruct.Slots.Default != nil {
s += fmt.Sprintf("%s", NicePrintStatement(t.tagStruct.Slots.Default.Children, lev+1))
}
case *ifStatement:
s += fmt.Sprintf("%sIf(%+v)\n", index, t.conditionCode)
s += fmt.Sprintf("%s", NicePrintStatement(t.ChildStatement, lev+1))
for _, ef := range t.ElseIf {
if ef.conditionCode != "" {
s += fmt.Sprintf("%sElseIf(%+v)\n", index, ef.conditionCode)
} else {
s += fmt.Sprintf("%sElse\n", index)
}
s += fmt.Sprintf("%s", NicePrintStatement(ef.ChildStatement, lev+1))
}
case *forStatement:
s += fmt.Sprintf("%sFor(%s in %s)\n", index, t.ItemKey, t.ArrayKey)
s += fmt.Sprintf("%s", NicePrintStatement(t.ChildChunks, lev+1))
case *mustacheStatement:
s += fmt.Sprintf("%s{{%s}}\n", index, t.exp)
case *rawHtmlStatement:
s += fmt.Sprintf("%s{{{%s}}}\n", index, t.exp)
default:
}
return s
}
// 不支持json序列化的map, 解决循环引用时Marshal报错的问题
type skipMarshalMap map[string]interface{}
func (s skipMarshalMap) MarshalJSON() ([]byte, error) {
return nil, nil
}
// Copy convert a complex structure to a structure containing only basic types.
// Please refer to README.md#Admonition
func Copy(i interface{}) (dst interface{}) {
bs, err := json.Marshal(i)
if err != nil {
log.Warningf("Copy error on Marshal: %v", err)
return nil
}
err = json.Unmarshal(bs, &dst)
if err != nil {
log.Warningf("Copy error on Unmarshal: %v", err)
return nil
}
return
}