forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.go
494 lines (392 loc) · 11.1 KB
/
form.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package servermanager
import (
"bytes"
"fmt"
"html/template"
"net/http"
"reflect"
"strconv"
"strings"
"github.com/fatih/camelcase"
"github.com/sirupsen/logrus"
)
const (
formTypeTagName = "input"
formOptsTagName = "formopts"
formShowTagName = "show"
)
func NewForm(i interface{}, dropdownOpts map[string][]string, visibility string, forceShowAllOptions bool) *Form {
return &Form{
data: i,
dropdownOptions: dropdownOpts,
visibility: visibility,
forceShowAllOptions: forceShowAllOptions,
}
}
type Form struct {
// the data on which the form is based
data interface{}
dropdownOptions map[string][]string
// visibility is used to filter out fields by their 'show' tag.
visibility string
// force all options to show
forceShowAllOptions bool
}
func (f Form) Submit(r *http.Request) error {
if reflect.ValueOf(f.data).Kind() != reflect.Ptr {
panic("form data must be a pointer to a type")
}
if err := r.ParseForm(); err != nil {
return err
}
val := reflect.ValueOf(f.data).Elem()
for name, vals := range r.Form {
err := f.assignFieldValues(val, name, vals)
if err != nil {
return err
}
}
return nil
}
func (f Form) assignFieldValues(val reflect.Value, name string, vals []string) error {
parts := strings.Split(name, ".")
field := val.FieldByName(parts[0])
if field.IsValid() && field.CanSet() {
switch field.Kind() {
case reflect.Struct:
if len(parts) > 1 {
err := f.assignFieldValues(field, strings.Join(parts[1:], "."), vals)
if err != nil {
return err
}
}
case reflect.String:
field.SetString(strings.Join(vals, ";"))
case reflect.Int:
if vals[0] == "on" {
field.SetInt(1)
} else {
field.SetInt(int64(formValueAsInt(vals[0])))
}
case reflect.Float64:
f, err := strconv.ParseFloat(vals[0], 64)
if err != nil {
return err
}
field.SetFloat(f)
case reflect.Bool:
if vals[0] == "on" {
field.SetBool(true)
} else {
field.SetBool(formValueAsInt(vals[0]) == 1)
}
default:
panic("form submit - unknown type: " + field.Kind().String())
}
}
return nil
}
func (f Form) Fields() []FormElement {
if reflect.ValueOf(f.data).Kind() != reflect.Ptr {
panic("form data must be a pointer to a type")
}
val := reflect.ValueOf(f.data)
t := reflect.TypeOf(f.data)
opts := f.buildOpts(val.Elem(), t.Elem(), "", 0)
return opts
}
type FormHeader struct {
Name string
}
func (fh FormHeader) HTML() template.HTML {
return template.HTML("<h2>" + fh.Name + "</h2>")
}
type FormElement interface {
HTML() template.HTML
}
func (f Form) buildName(name string) string {
if name == "GUIDs" {
return name
}
return strings.Replace(strings.Join(camelcase.Split(name), " "), "ACSRAPI", "ACSR API", 1)
}
func (f Form) buildOpts(val reflect.Value, t reflect.Type, parentName string, recursionLevel int) []FormElement {
var opts []FormElement
for i := 0; i < t.NumField(); i++ {
typeField := t.Field(i)
valField := val.Field(i)
formShow := typeField.Tag.Get(formShowTagName)
// check to see if we should be showing this tag
if formShow == "-" || (formShow != "open" && f.visibility != "" && formShow != f.visibility) || formShow == "premium" && !Premium() {
continue
}
switch valField.Kind() {
case reflect.Struct:
if recursionLevel == 0 {
opts = append(opts, formCardOpen{name: f.buildName(typeField.Name)})
} else {
opts = append(opts, FormOption{Name: f.buildName(typeField.Name), Type: "heading", RecursionLevel: recursionLevel})
}
opts = append(opts, f.buildOpts(valField, typeField.Type, fmt.Sprintf("%s%s.", parentName, typeField.Name), recursionLevel+1)...)
if recursionLevel == 0 {
opts = append(opts, formCardClose{})
}
case reflect.Map:
for _, k := range valField.MapKeys() {
elem := valField.MapIndex(k)
opts = append(opts, FormHeader{Name: k.String()})
opts = append(opts, f.buildOpts(elem, elem.Type(), fmt.Sprintf("%s%s[%s].", parentName, typeField.Name, k.String()), recursionLevel+1)...)
}
case reflect.Slice, reflect.Array:
for i := 0; i < valField.Len(); i++ {
elem := valField.Index(i)
opts = append(opts, f.buildOpts(elem, elem.Type(), fmt.Sprintf("%s%s[%d].", parentName, typeField.Name, i), recursionLevel+1)...)
}
default:
// formType can be e.g. dropdown
formType := typeField.Tag.Get(formTypeTagName)
if formType == "" {
formType = typeField.Type.String()
}
formName := f.buildName(typeField.Name)
formOpt := FormOption{
Name: formName,
Key: parentName + typeField.Name,
Value: valField.Interface(),
HelpText: template.HTML(typeField.Tag.Get("help")),
Type: formType,
Opts: make(map[string]bool),
Hidden: formShow == "open" && IsHosted && !f.forceShowAllOptions,
RecursionLevel: recursionLevel,
}
if formType == "dropdown" || formType == "multiSelect" {
optsKey := typeField.Tag.Get(formOptsTagName)
if options, ok := f.dropdownOptions[optsKey]; ok {
for _, o := range options {
formOpt.Opts[o] = strings.Contains(formOpt.Value.(string), o)
}
} else {
logrus.Warnf("dropdown opts for field: %s specified, but none found", typeField.Name)
}
} else if formType == "int" || formType == "number" {
formOpt.Min, formOpt.Max = typeField.Tag.Get("min"), typeField.Tag.Get("max")
}
opts = append(opts, formOpt)
}
}
return opts
}
type FormOption struct {
Name string
Key string
Type string
HelpText template.HTML
Value interface{}
Min, Max string
Hidden bool
RecursionLevel int
Opts map[string]bool
}
func (f FormOption) render(templ string) (template.HTML, error) {
t, err := template.New(f.Name).Parse(templ)
if err != nil {
return "", err
}
out := new(bytes.Buffer)
err = t.Execute(out, f)
if err != nil {
return "", err
}
return template.HTML(out.String()), nil
}
func (f FormOption) renderDropdown() template.HTML {
const dropdownTemplate = `
{{ if not .Hidden }}
<div class="form-group row">
<label for="{{ .Key }}" class="col-sm-3 col-form-label">
{{ .Name }}
</label>
<div class="col-sm-9">
<select {{ if eq .Type "multiSelect" }} multiple {{ end }} class="form-control" name="{{ .Key }}" id="{{ .Key }}">
{{ range $opt, $selected := .Opts }}
<option {{ if $selected }} selected {{ end }} value="{{ $opt }}">{{ $opt }}</option>
{{ end }}
</select>
<small>{{ .HelpText }}</small>
</div>
</div>
{{ else }}
{{ range $opt, $selected := .Opts }}
{{ if $selected }}
<input type="hidden" name="{{ .Key }}" value="{{ $opt }}">
{{ end }}
{{ end }}
{{ end }}
`
tmpl, err := f.render(dropdownTemplate)
if err != nil {
return template.HTML(fmt.Sprintf("err: %s", err))
}
return tmpl
}
func (f FormOption) renderCheckbox() template.HTML {
if b, ok := f.Value.(bool); ok {
if b {
f.Value = 1
} else {
f.Value = 0
}
}
const checkboxTemplate = `
{{ if not .Hidden }}
<div class="form-group row">
<label for="{{ .Key }}" class="col-sm-3 col-form-label">{{ .Name }}</label>
<div class="col-sm-9">
<input type="checkbox" id="{{ .Key }}" name="{{ .Key }}" {{ if eq .Value 1 }}checked="checked"{{ end }}><br>
<small>{{ .HelpText }}</small>
</div>
</div>
{{ else }}
<input type="hidden" id="{{ .Key }}" name="{{ .Key }}" {{ if eq .Value 1 }}value="1"{{ else }}value="0"{{ end }}>
{{ end }}
`
tmpl, err := f.render(checkboxTemplate)
if err != nil {
return template.HTML(fmt.Sprintf("err: %s", err))
}
return tmpl
}
func (f FormOption) renderTextInput() template.HTML {
const inputTextTemplate = `
{{ if not .Hidden }}
<div class="form-group row">
<label for="{{ .Key }}" class="col-sm-3 col-form-label">{{ .Name }}</label>
<div class="col-sm-9">
<input type="{{ if eq .Type "password" }}password{{ else }}text{{ end }}"
id="{{ .Key }}" name="{{ .Key }}"
class="form-control"
value="{{ .Value }}"
{{ if eq .Type "password" }}
autocomplete="new-password"
{{ end }}
>
<small>{{ .HelpText }}</small>
</div>
</div>
{{ else }}
<input type="hidden" id="{{ .Key }}" name="{{ .Key }}" value="{{ .Value }}">
{{ end }}
`
tmpl, err := f.render(inputTextTemplate)
if err != nil {
return template.HTML(fmt.Sprintf("err: %s", err))
}
return tmpl
}
func (f FormOption) renderTextarea() template.HTML {
const textareaTemplate = `
<div class="form-group row" {{ if .Hidden }}style="display: none;"{{ end }}>
<label for="{{ .Key }}" class="col-sm-3 col-form-label">{{ .Name }}</label>
<div class="col-sm-9">
<textarea id="{{ .Key }}" name="{{ .Key }}" class="form-control text-monospace" rows="15">{{ .Value }}</textarea>
<small>{{ .HelpText }}</small>
</div>
</div>
`
tmpl, err := f.render(textareaTemplate)
if err != nil {
return template.HTML(fmt.Sprintf("err: %s", err))
}
return tmpl
}
func (f FormOption) renderNumberInput() template.HTML {
const numberInputTemplate = `
{{ if not .Hidden }}
<div class="form-group row">
<label for="{{ .Key }}" class="col-sm-3 col-form-label">{{ .Name }}</label>
<div class="col-sm-9">
<input
type="number"
id="{{ .Key }}"
name="{{ .Key }}"
class="form-control"
value="{{ .Value }}"
{{ with .Min }}min="{{ . }}"{{ end }}
{{ with .Max }}min="{{ . }}"{{ end }}
step="any"
>
<small>{{ .HelpText }}</small>
</div>
</div>
{{ else }}
<input type="hidden" id="{{ .Key }}" name="{{ .Key }}" value="{{ .Value }}">
{{ end }}
`
tmpl, err := f.render(numberInputTemplate)
if err != nil {
return template.HTML(fmt.Sprintf("err: %s", err))
}
return tmpl
}
type FormHeading string
func (f FormOption) renderHeading() template.HTML {
if f.Hidden {
return ""
}
headerType := "h2"
headingTemplate := ""
switch f.RecursionLevel {
case 0:
headingTemplate = `<hr class="mt-5">`
case 1:
headerType = "h3"
case 2:
headerType = "h4"
case 4:
headerType = "h5"
}
headingTemplate += fmt.Sprintf(`<%s class="mt-4 mb-4">{{ .Name }}</%s>`, headerType, headerType)
tmpl, err := f.render(headingTemplate)
if err != nil {
return template.HTML(fmt.Sprintf("err: %s", err))
}
return tmpl
}
func (f FormOption) HTML() template.HTML {
switch f.Type {
case "dropdown", "multiSelect":
return f.renderDropdown()
case "checkbox", "bool":
return f.renderCheckbox()
case "int", "float64":
if f.Value == nil {
f.Value = 0
}
return f.renderNumberInput()
case "textarea", reflect.TypeOf(NewLineSeparatedList("")).String():
return f.renderTextarea()
case "string", "password":
return f.renderTextInput()
case "heading":
return f.renderHeading()
default:
logrus.Errorf("Unknown type: %s", f.Type)
return ""
}
}
type formCardOpen struct {
name string
}
func (f formCardOpen) HTML() template.HTML {
return template.HTML(`
<div class="card mt-3 mb-3">
<div class="card-header">
<strong>` + f.name + `</strong>
</div>
<div class="card-body">
`)
}
type formCardClose struct{}
func (f formCardClose) HTML() template.HTML {
return `</div></div>`
}