comparison pkg/controllers/types.go @ 414:c1047fd04a3a

Moved project specific Go packages to new pkg folder.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 15 Aug 2018 17:30:50 +0200
parents controllers/types.go@0777aa6de45b
children c70ddc6eb168
comparison
equal deleted inserted replaced
413:a9440a4826aa 414:c1047fd04a3a
1 package controllers
2
3 import (
4 "database/sql/driver"
5 "encoding/json"
6 "errors"
7 "regexp"
8 "strings"
9 )
10
11 type (
12 Email string
13 Country string
14 Role string
15
16 BoundingBox struct {
17 X1 float64 `json:"x1"`
18 Y1 float64 `json:"y1"`
19 X2 float64 `json:"x2"`
20 Y2 float64 `json:"y2"`
21 }
22
23 User struct {
24 User string `json:"user"`
25 Role Role `json:"role"`
26 Password string `json:"password,omitempty"`
27 Email Email `json:"email"`
28 Country Country `json:"country"`
29 Extent *BoundingBox `json:"extent"`
30 }
31
32 PWResetUser struct {
33 User string `json:"user"`
34 }
35 )
36
37 var (
38 // https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression
39 emailRe = regexp.MustCompile(
40 `(?:[a-z0-9!#$%&'*+/=?^_` + "`" +
41 `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" +
42 `{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]` +
43 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")` +
44 `@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?` +
45 `|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}` +
46 `(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]` +
47 `:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]` +
48 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`)
49
50 errNoEmailAddress = errors.New("Not a valid email address")
51 errNoString = errors.New("Not a string")
52 )
53
54 func (e *Email) UnmarshalJSON(data []byte) error {
55 var s string
56 if err := json.Unmarshal(data, &s); err != nil {
57 return err
58 }
59 if !emailRe.MatchString(s) {
60 return errNoEmailAddress
61 }
62 *e = Email(s)
63 return nil
64 }
65
66 func (e Email) Value() (driver.Value, error) {
67 return string(e), nil
68 }
69
70 func (e *Email) Scan(src interface{}) (err error) {
71 if s, ok := src.(string); ok {
72 *e = Email(s)
73 } else {
74 err = errNoString
75 }
76 return
77 }
78
79 var (
80 validCountries = []string{
81 "AT", "BG", "DE", "HU", "HR",
82 "MD", "RO", "RS", "SK", "UA",
83 }
84 errNoValidCountry = errors.New("Not a valid country")
85 )
86
87 func (c *Country) UnmarshalJSON(data []byte) error {
88 var s string
89 if err := json.Unmarshal(data, &s); err != nil {
90 return err
91 }
92 s = strings.ToUpper(s)
93 for _, v := range validCountries {
94 if v == s {
95 *c = Country(v)
96 return nil
97 }
98 }
99 return errNoValidCountry
100 }
101
102 func (c Country) Value() (driver.Value, error) {
103 return string(c), nil
104 }
105
106 func (c *Country) Scan(src interface{}) (err error) {
107 if s, ok := src.(string); ok {
108 *c = Country(s)
109 } else {
110 err = errNoString
111 }
112 return
113 }
114
115 var (
116 validRoles = []string{
117 "waterway_user",
118 "waterway_admin",
119 "sys_admin",
120 }
121 errNoValidRole = errors.New("Not a valid role")
122 )
123
124 func (r Role) Value() (driver.Value, error) {
125 return string(r), nil
126 }
127
128 func (r *Role) Scan(src interface{}) (err error) {
129 if s, ok := src.(string); ok {
130 *r = Role(s)
131 } else {
132 err = errNoString
133 }
134 return
135 }
136
137 func (r *Role) UnmarshalJSON(data []byte) error {
138 var s string
139 if err := json.Unmarshal(data, &s); err != nil {
140 return err
141 }
142 s = strings.ToLower(s)
143 for _, v := range validRoles {
144 if v == s {
145 *r = Role(v)
146 return nil
147 }
148 }
149 return errNoValidRole
150 }