Mercurial > gemma
annotate pkg/models/common.go @ 5591:0011f50cf216 surveysperbottleneckid
Removed no longer used alternative api for surveys/ endpoint.
As bottlenecks in the summary for SR imports are now identified by
their id and no longer by the (not guarantied to be unique!) name,
there is no longer the need to request survey data by the name+date
tuple (which isn't reliable anyway). So the workaround was now
reversed.
author | Sascha Wilde <wilde@sha-bang.de> |
---|---|
date | Wed, 06 Apr 2022 13:30:29 +0200 |
parents | d19fdf3d2099 |
children | 1222b777f51f |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
13 |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package models |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
16 import ( |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
17 "database/sql/driver" |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
18 "encoding/json" |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
19 "errors" |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
20 "fmt" |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
21 "regexp" |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
22 "strings" |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
23 "time" |
2059
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1979
diff
changeset
|
24 |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1979
diff
changeset
|
25 "gemma.intevation.de/gemma/pkg/common" |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
26 ) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
28 var ( |
4847
4847ac70103a
Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4332
diff
changeset
|
29 errNoString = errors.New("not a string") |
4847ac70103a
Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4332
diff
changeset
|
30 errNoByteSlice = errors.New("not a byte slice") |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
31 ) |
1373
84e78d2e2d95
Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
32 |
84e78d2e2d95
Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
33 // WGS84 is the EPSG of the World Geodetic System 1984. |
84e78d2e2d95
Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
34 const WGS84 = 4326 |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
35 |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
36 type ( |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
37 Date struct{ time.Time } |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
38 Time struct{ time.Time } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
39 |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
40 // Country is a valid country 2 letter code. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
41 Country string |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
42 // UniqueCountries is a list of unique countries. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
43 UniqueCountries []Country |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
44 |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
45 // SafePath should only contain chars that directory traversal safe. |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
46 SafePath string |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
47 ) |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
48 |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
49 func (d Date) MarshalJSON() ([]byte, error) { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
50 return json.Marshal(d.Format(common.DateFormat)) |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
51 } |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
52 |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
53 func (d *Date) UnmarshalJSON(data []byte) error { |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
54 var s string |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
55 if err := json.Unmarshal(data, &s); err != nil { |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
56 return err |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
57 } |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
58 d2, err := time.Parse(common.DateFormat, s) |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
59 if err == nil { |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
60 *d = Date{d2} |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
61 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
62 return err |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
63 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
64 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
65 func (t Time) MarshalJSON() ([]byte, error) { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
66 return json.Marshal(t.Format(common.TimeFormat)) |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
67 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
68 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
69 func (t *Time) UnmarshalJSON(data []byte) error { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
70 var s string |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
71 if err := json.Unmarshal(data, &s); err != nil { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
72 return err |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
73 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
74 t2, err := time.Parse(common.TimeFormat, s) |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
75 if err == nil { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
76 *t = Time{t2} |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
77 } |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
78 return err |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
79 } |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
80 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
81 var ( |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
82 validCountries = []string{ |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
83 "AT", "BG", "DE", "HU", "HR", |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
84 "MD", "RO", "RS", "SK", "UA", |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
85 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
86 ) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
87 |
4332
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
88 // Valid checks if the given country is a known one. |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
89 func (c Country) Valid() bool { |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
90 for _, v := range validCountries { |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
91 if string(c) == v { |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
92 return true |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
93 } |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
94 } |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
95 return false |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
96 } |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
97 |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
98 // UnmarshalJSON ensures that the given string forms a valid |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
99 // two letter country code. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
100 func (c *Country) UnmarshalJSON(data []byte) error { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
101 var s string |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
102 if err := json.Unmarshal(data, &s); err != nil { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
103 return err |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
104 } |
1979
0bc0312105e4
Improved the error message a bit if an invalid country name is passed via JSON api.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1901
diff
changeset
|
105 u := strings.ToUpper(s) |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
106 for _, v := range validCountries { |
1979
0bc0312105e4
Improved the error message a bit if an invalid country name is passed via JSON api.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1901
diff
changeset
|
107 if v == u { |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
108 *c = Country(v) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
109 return nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
110 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
111 } |
1979
0bc0312105e4
Improved the error message a bit if an invalid country name is passed via JSON api.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1901
diff
changeset
|
112 return fmt.Errorf("'%s' is not a valid country", s) |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
113 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
114 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
115 // Value implements the driver.Valuer interface. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
116 func (c Country) Value() (driver.Value, error) { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
117 return string(c), nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
118 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
119 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
120 // Scan implements the sql.Scanner interfaces. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
121 func (c *Country) Scan(src interface{}) (err error) { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
122 if s, ok := src.(string); ok { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
123 *c = Country(s) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
124 } else { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
125 err = errNoString |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
126 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
127 return |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
128 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
129 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
130 func (uc *UniqueCountries) UnmarshalJSON(data []byte) error { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
131 var countries []Country |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
132 if err := json.Unmarshal(data, &countries); err != nil { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
133 return err |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
134 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
135 unique := map[Country]struct{}{} |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
136 for _, c := range countries { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
137 if _, found := unique[c]; found { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
138 return fmt.Errorf("country '%s' is not unique", string(c)) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
139 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
140 unique[c] = struct{}{} |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
141 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
142 *uc = countries |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
143 return nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
144 } |
2231
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
145 |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
146 func (uc UniqueCountries) String() string { |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
147 var b strings.Builder |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
148 for i, c := range uc { |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
149 if i > 0 { |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
150 b.WriteString(", ") |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
151 } |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
152 b.WriteString(string(c)) |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
153 } |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
154 return b.String() |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
155 } |
5377
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
156 |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
157 const SafePathExp = "[a-zA-Z0-9_-]+" |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
158 |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
159 var safePathRegExp = regexp.MustCompile("^" + SafePathExp + "$") |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
160 |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
161 func (sp SafePath) Valid() bool { |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
162 return safePathRegExp.MatchString(string(sp)) |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
163 } |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
164 |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
165 // UnmarshalJSON ensures that the given string only consist |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
166 // of runes that are directory traversal safe. |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
167 func (sp *SafePath) UnmarshalJSON(data []byte) error { |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
168 var s string |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
169 if err := json.Unmarshal(data, &s); err != nil { |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
170 return err |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
171 } |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
172 if c := SafePath(s); c.Valid() { |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
173 *sp = c |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
174 return nil |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
175 } |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
176 return fmt.Errorf("'%s' is not a safe path", s) |
d19fdf3d2099
Add a string type that allows only runes that are safe of directory traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4847
diff
changeset
|
177 } |