-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
245 lines (212 loc) · 4.97 KB
/
state.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package forms
import (
"fmt"
"strings"
)
type Identity struct {
ID string
FullName string
}
type Field struct {
Identity
RawFormValue string
RawFormValues []string
RawFormValuePresent bool
}
func (field *Field) EnumFields(f func(name string, field *Field)) {
f("", field)
}
type Subst struct {
For string
Use string
}
type Value struct {
Key string
Value any
}
type Style struct {
Templates []Subst
Classes map[string]string
// Values []Value
}
type TemplateStyle struct {
Classes map[string]string
}
func (ts *TemplateStyle) TemplateStylePtr() *TemplateStyle {
return ts
}
type State struct {
Data *FormData
path []string
errSites []ErrorSite
fields map[string]*Field
templKeys []string
templValues []string
// values map[string]any
// valueKeys []string
// valueValues []any
classes []map[string]string
classesCopied []bool
}
func (st *State) PushName(name string) {
if name != "" {
st.path = append(st.path, name)
}
}
func (st *State) PopName() {
st.path = st.path[:len(st.path)-1]
}
func (st *State) WithName(name string, f func()) {
st.PushName(name)
defer st.PopName()
f()
}
func (st *State) PushTemplate(forTempl, useTempl string) {
st.templKeys = append(st.templKeys, forTempl)
st.templValues = append(st.templValues, useTempl)
}
// func (st *State) PushValue(key string, value any) {
// st.valueKeys = append(st.valueKeys, key)
// st.valueValues = append(st.valueValues, st.values[key])
// st.values[key] = value
// }
func (st *State) PushTemplates(substs []Subst) {
for _, s := range substs {
st.PushTemplate(s.For, s.Use)
}
}
func (st *State) ensureClassesCopied() map[string]string {
classes := st.classes[len(st.classes)-1]
if cn := len(st.classesCopied); !st.classesCopied[cn-1] {
st.classesCopied[cn-1] = true
newClasses := make(map[string]string, len(classes)+10)
for k, v := range classes {
newClasses[k] = v
}
classes = newClasses
st.classes = append(st.classes, classes)
}
return classes
}
func (st *State) PushClass(key string, value string) {
classes := st.ensureClassesCopied()
classes[key] = joinClasses(classes[key], value)
}
func (st *State) PushClasses(newClasses map[string]string) {
classes := st.ensureClassesCopied()
for key, value := range newClasses {
classes[key] = joinClasses(classes[key], value)
}
}
func (st *State) Classes() map[string]string {
return st.classes[len(st.classes)-1]
}
func (st *State) TemplateStyle() TemplateStyle {
return TemplateStyle{
Classes: st.Classes(),
}
}
func (st *State) PushStyle(s *Style) {
st.PushTemplates(s.Templates)
st.PushClasses(s.Classes)
}
func (st *State) PushStyles(styles []*Style) {
for _, s := range styles {
st.PushStyle(s)
}
}
func (st *State) LookupTemplate(templ string) string {
keys := st.templKeys
for i := len(keys) - 1; i >= 0; i-- {
if keys[i] == templ {
return st.templValues[i]
}
}
return templ
}
func (st *State) ErrorSite() ErrorSite {
n := len(st.errSites)
if n == 0 {
return nil
}
return st.errSites[n-1]
}
func (st *State) PushErrorSite(errs ErrorSite) {
if errs != nil {
errs.Init(st.ErrorSite())
st.errSites = append(st.errSites, errs)
}
}
func (st *State) Fin() {
if len(st.path) != 0 {
panic(fmt.Errorf("ended with path = %v", st.path))
}
}
func (st *State) AddField(name string, field *Field) {
if name != "" {
st.PushName(name)
}
st.AssignIdentity(&field.Identity)
st.fields[field.FullName] = field
if name != "" {
st.PopName()
}
}
func (st *State) AssignSubidentity(name string, ident *Identity) {
st.PushName(name)
st.AssignIdentity(ident)
st.PopName()
}
func (st *State) AssignIdentity(ident *Identity) {
if ident.ID == "" {
ident.ID = strings.Join(st.path, "_")
}
if ident.FullName == "" {
ident.FullName = JoinNames(st.path...)
}
}
func (st *State) finalizeTree(c Child, flags ChildFlags) {
if c == nil {
return
}
pathLen := len(st.path)
errSitesLen := len(st.errSites)
templLen := len(st.templKeys)
classesLen := len(st.classes)
st.classesCopied = append(st.classesCopied, false)
c.Finalize(st)
if p, ok := c.(Processor); ok {
p.EnumFields(st.AddField)
p.EnumBindings(func(b AnyBinding) {
b.Init(st.ErrorSite())
})
}
if t, ok := c.(interface{ TemplateStylePtr() *TemplateStyle }); ok {
stylePtr := t.TemplateStylePtr()
if stylePtr != nil {
*stylePtr = st.TemplateStyle()
}
}
if cont, ok := c.(Container); ok {
cont.EnumChildren(st.finalizeTree)
}
st.path = st.path[:pathLen]
st.errSites = st.errSites[:errSitesLen]
st.templKeys = st.templKeys[:templLen]
st.templValues = st.templValues[:templLen]
st.classes = st.classes[:classesLen]
st.classesCopied = st.classesCopied[:len(st.classesCopied)-1]
}
func joinClasses(prev, next string) string {
prev = strings.TrimSpace(prev)
next = strings.TrimSpace(next)
if next, ok := strings.CutPrefix(next, "+ "); ok {
next = strings.TrimSpace(next)
if prev != "" && next != "" {
return prev + " " + next
} else {
return prev + next
}
}
return next
}