-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (67 loc) · 1.5 KB
/
main.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
// Copyright 2016 Horacio Duran.
// Licensed under the MIT, see LICENCE file for details.
package main
import (
"fmt"
"strconv"
)
type sample struct {
FirstName string
LastName string
Email string `html:"email"`
Website string `html:"url"`
Phone string `html:"telephone"`
Password string `html:"secret"`
Age int
}
// ProcessPost implements POSTFillable.
func (s *sample) ProcessPOST(post map[string]string) error {
if value, ok := post["FirstName"]; ok {
s.FirstName = value
}
if value, ok := post["LastName"]; ok {
s.LastName = value
}
if value, ok := post["Email"]; ok {
s.Email = value
}
if value, ok := post["Website"]; ok {
s.Website = value
}
if value, ok := post["Phone"]; ok {
s.Phone = value
}
if value, ok := post["Password"]; ok {
s.Password = value
}
if value, ok := post["Age"]; ok {
i, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return fmt.Errorf("unexpected value for Age %q", value)
}
s.Age = int(i)
}
return nil
}
func main() {
fields, err := getFields(&sample{})
if err != nil {
fmt.Printf("<h1>cannot obtain fields for type: %v</h1>", err)
return
}
html := doHTML(fields)
fmt.Println(html)
html, err = doFilledForm(&sample{
FirstName: "Horacio",
LastName: "Duran",
Email: "horacio.duran@gmail.com",
Website: "http://perri.to",
Phone: "+555555555",
Password: "a big secret",
Age: 32,
})
if err != nil {
fmt.Printf("<h1>cannot create a filled form: %v</h1>", err)
}
fmt.Println(html)
}