Mercurial > gemma
annotate pkg/models/common.go @ 5710:37c8feeecb4d
Merged branch sr-v2 into default.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 20 Feb 2024 21:28:56 +0100 |
parents | 1222b777f51f |
children | 6270951dda28 |
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 ( |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
37 // Date is the common on-wire date represention. |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
38 Date struct{ time.Time } |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
39 // Time is the common on-wire time represention. |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
40 Time struct{ time.Time } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
41 |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
42 // Country is a valid country 2 letter code. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
43 Country string |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
44 // UniqueCountries is a list of unique countries. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
45 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
|
46 |
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
|
47 // 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
|
48 SafePath string |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
49 ) |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
50 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
51 // MarshalJSON implements the json.Marshaler interface. |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
52 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
|
53 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
|
54 } |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
55 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
56 // UnmarshalJSON implements the json.Unmarshaler interface. |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 } |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
62 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
|
63 if err == nil { |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
64 *d = Date{d2} |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
65 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
66 return err |
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 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
69 // MarshalJSON implements the json.Marshaler interface. |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
70 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
|
71 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
|
72 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
73 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
74 // UnmarshalJSON implements the json.Unmarshaler interface. |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
75 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
|
76 var s string |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
77 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
|
78 return err |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
79 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
80 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
|
81 if err == nil { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2059
diff
changeset
|
82 *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
|
83 } |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
84 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
|
85 } |
1901
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 var ( |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
88 validCountries = []string{ |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
89 "AT", "BG", "DE", "HU", "HR", |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
90 "MD", "RO", "RS", "SK", "UA", |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
91 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
92 ) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
93 |
4332
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
94 // 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
|
95 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
|
96 for _, v := range validCountries { |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
97 if string(c) == v { |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
98 return true |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
99 } |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
100 } |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
101 return false |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
102 } |
8080007d3c06
shape upload stretch import: Finished implementation (staging, summary).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2231
diff
changeset
|
103 |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
104 // 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
|
105 // two letter country code. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
106 func (c *Country) UnmarshalJSON(data []byte) error { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
107 var s string |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
108 if err := json.Unmarshal(data, &s); err != nil { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
109 return err |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
110 } |
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
|
111 u := strings.ToUpper(s) |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
112 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
|
113 if v == u { |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
114 *c = Country(v) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
115 return nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
116 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
117 } |
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
|
118 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
|
119 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
120 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
121 // Value implements the driver.Valuer interface. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
122 func (c Country) Value() (driver.Value, error) { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
123 return string(c), nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
124 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
125 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
126 // Scan implements the sql.Scanner interfaces. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
127 func (c *Country) Scan(src interface{}) (err error) { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
128 if s, ok := src.(string); ok { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
129 *c = Country(s) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
130 } else { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
131 err = errNoString |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
132 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
133 return |
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 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
136 // UnmarshalJSON implements the json.Unmarshaler interface. |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
137 func (uc *UniqueCountries) UnmarshalJSON(data []byte) error { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
138 var countries []Country |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
139 if err := json.Unmarshal(data, &countries); err != nil { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
140 return err |
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 unique := map[Country]struct{}{} |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
143 for _, c := range countries { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
144 if _, found := unique[c]; found { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
145 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
|
146 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
147 unique[c] = struct{}{} |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
148 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
149 *uc = countries |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
150 return nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
151 } |
2231
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
152 |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 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
|
157 b.WriteString(", ") |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
158 } |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
159 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
|
160 } |
97bba8b51b9c
Stretch import: Be more verbose about range before inserting data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
161 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
|
162 } |
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
|
163 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
164 // SafePathExp is used to check if a path consists only |
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
165 // of harmless runes. |
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
|
166 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
|
167 |
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 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
|
169 |
5601
1222b777f51f
Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5377
diff
changeset
|
170 // Valid checks is the given path is safe. |
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
|
171 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
|
172 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
|
173 } |
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 |
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 // 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
|
176 // 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 } |
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
|
182 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
|
183 *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
|
184 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
|
185 } |
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
|
186 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
|
187 } |