Mercurial > gemma
comparison pkg/models/common.go @ 1905:4f58bada50b8 dev-pdf-generation
merging in default branch
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Fri, 18 Jan 2019 17:10:16 +0100 |
parents | 71b722809b2b |
children | 0bc0312105e4 |
comparison
equal
deleted
inserted
replaced
1903:7247ac316cb7 | 1905:4f58bada50b8 |
---|---|
11 // Author(s): | 11 // Author(s): |
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> | 12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
13 | 13 |
14 package models | 14 package models |
15 | 15 |
16 import "errors" | 16 import ( |
17 "database/sql/driver" | |
18 "encoding/json" | |
19 "errors" | |
20 "fmt" | |
21 "strings" | |
22 "time" | |
23 ) | |
17 | 24 |
18 var ( | 25 var ( |
19 errNoString = errors.New("Not a string") | 26 errNoString = errors.New("Not a string") |
20 errNoByteSlice = errors.New("Not a byte slice") | 27 errNoByteSlice = errors.New("Not a byte slice") |
21 ) | 28 ) |
22 | 29 |
23 // WGS84 is the EPSG of the World Geodetic System 1984. | 30 // WGS84 is the EPSG of the World Geodetic System 1984. |
24 const WGS84 = 4326 | 31 const WGS84 = 4326 |
32 | |
33 const DateFormat = "2006-01-02" | |
34 | |
35 type ( | |
36 Date struct{ time.Time } | |
37 // Country is a valid country 2 letter code. | |
38 Country string | |
39 // UniqueCountries is a list of unique countries. | |
40 UniqueCountries []Country | |
41 ) | |
42 | |
43 func (srd Date) MarshalJSON() ([]byte, error) { | |
44 return json.Marshal(srd.Format(DateFormat)) | |
45 } | |
46 | |
47 func (srd *Date) UnmarshalJSON(data []byte) error { | |
48 var s string | |
49 if err := json.Unmarshal(data, &s); err != nil { | |
50 return err | |
51 } | |
52 d, err := time.Parse(DateFormat, s) | |
53 if err == nil { | |
54 *srd = Date{d} | |
55 } | |
56 return err | |
57 } | |
58 | |
59 var ( | |
60 validCountries = []string{ | |
61 "AT", "BG", "DE", "HU", "HR", | |
62 "MD", "RO", "RS", "SK", "UA", | |
63 } | |
64 errNoValidCountry = errors.New("Not a valid country") | |
65 ) | |
66 | |
67 // UnmarshalJSON ensures that the given string forms a valid | |
68 // two letter country code. | |
69 func (c *Country) UnmarshalJSON(data []byte) error { | |
70 var s string | |
71 if err := json.Unmarshal(data, &s); err != nil { | |
72 return err | |
73 } | |
74 s = strings.ToUpper(s) | |
75 for _, v := range validCountries { | |
76 if v == s { | |
77 *c = Country(v) | |
78 return nil | |
79 } | |
80 } | |
81 return errNoValidCountry | |
82 } | |
83 | |
84 // Value implements the driver.Valuer interface. | |
85 func (c Country) Value() (driver.Value, error) { | |
86 return string(c), nil | |
87 } | |
88 | |
89 // Scan implements the sql.Scanner interfaces. | |
90 func (c *Country) Scan(src interface{}) (err error) { | |
91 if s, ok := src.(string); ok { | |
92 *c = Country(s) | |
93 } else { | |
94 err = errNoString | |
95 } | |
96 return | |
97 } | |
98 | |
99 func (uc *UniqueCountries) UnmarshalJSON(data []byte) error { | |
100 var countries []Country | |
101 if err := json.Unmarshal(data, &countries); err != nil { | |
102 return err | |
103 } | |
104 unique := map[Country]struct{}{} | |
105 for _, c := range countries { | |
106 if _, found := unique[c]; found { | |
107 return fmt.Errorf("country '%s' is not unique", string(c)) | |
108 } | |
109 unique[c] = struct{}{} | |
110 } | |
111 *uc = countries | |
112 return nil | |
113 } |