Mercurial > gemma
annotate pkg/models/common.go @ 1921:0059aa870a39 v2
Hotfixed stretch import.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Fri, 18 Jan 2019 20:26:21 +0100 |
parents | 71b722809b2b |
children | 0bc0312105e4 |
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" |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
21 "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
|
22 "time" |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
23 ) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
25 var ( |
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
26 errNoString = errors.New("Not a string") |
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
27 errNoByteSlice = errors.New("Not a byte slice") |
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
28 ) |
1373
84e78d2e2d95
Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
29 |
84e78d2e2d95
Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
30 // 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
|
31 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
|
32 |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
33 const DateFormat = "2006-01-02" |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
34 |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
35 type ( |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
36 Date struct{ time.Time } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
37 // Country is a valid country 2 letter code. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
38 Country string |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
39 // UniqueCountries is a list of unique countries. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
40 UniqueCountries []Country |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
41 ) |
1900
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
42 |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
43 func (srd Date) MarshalJSON() ([]byte, error) { |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
44 return json.Marshal(srd.Format(DateFormat)) |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
45 } |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
46 |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
47 func (srd *Date) UnmarshalJSON(data []byte) error { |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
48 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
|
49 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
|
50 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
|
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 d, err := time.Parse(DateFormat, s) |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
53 if 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
|
54 *srd = Date{d} |
6a67cd819e93
To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1373
diff
changeset
|
55 } |
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 } |
1901
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
58 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
59 var ( |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
60 validCountries = []string{ |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
61 "AT", "BG", "DE", "HU", "HR", |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
62 "MD", "RO", "RS", "SK", "UA", |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
63 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
64 errNoValidCountry = errors.New("Not a valid country") |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
65 ) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
66 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
67 // 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
|
68 // two letter country code. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
69 func (c *Country) UnmarshalJSON(data []byte) error { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
70 var s string |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
71 if err := json.Unmarshal(data, &s); err != nil { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
72 return err |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
73 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
74 s = strings.ToUpper(s) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
75 for _, v := range validCountries { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
76 if v == s { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
77 *c = Country(v) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
78 return nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
79 } |
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 return errNoValidCountry |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
82 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
83 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
84 // Value implements the driver.Valuer interface. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
85 func (c Country) Value() (driver.Value, error) { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
86 return string(c), nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
87 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
88 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
89 // Scan implements the sql.Scanner interfaces. |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
90 func (c *Country) Scan(src interface{}) (err error) { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
91 if s, ok := src.(string); ok { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
92 *c = Country(s) |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
93 } else { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
94 err = errNoString |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
95 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
96 return |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
97 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
98 |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
99 func (uc *UniqueCountries) UnmarshalJSON(data []byte) error { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
100 var countries []Country |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
101 if err := json.Unmarshal(data, &countries); err != nil { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
102 return err |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
103 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
104 unique := map[Country]struct{}{} |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
105 for _, c := range countries { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
106 if _, found := unique[c]; found { |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
107 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
|
108 } |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
109 unique[c] = struct{}{} |
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 *uc = countries |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
112 return nil |
71b722809b2b
Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1900
diff
changeset
|
113 } |