Mercurial > gemma
annotate pkg/common/attributes.go @ 5311:72d3b8321ed0 zpg-exception
Fixed broken migration which erased the style of the modified GeoServer view.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 02 Jun 2021 16:55:00 +0200 |
parents | 8c5df0f3562e |
children | 5f47eeea988d |
rev | line source |
---|---|
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
2563
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
7 // Copyright (C) 2018, 2019 by via donau |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
2563
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
13 // * Tom Gottfried <tom.gottfried@intevation.de> |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 package common |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 import ( |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "log" |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "strconv" |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "strings" |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "time" |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 ) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
24 type ( |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
25 // Attributes is a map of optional key/value attributes |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
26 // of a configuration. |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
27 Attributes map[string]string |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
28 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
29 // AttributesMarshaler is an interface to implement |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
30 // custom marshalling for the implementing type. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
31 AttributesMarshaler interface { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
32 MarshalAttributes(Attributes) error |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
33 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
34 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
35 // AttributesUnmarshaler is and interface to implement |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
36 // custom unmarshalling for the implementing type. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
37 AttributesUnmarshaler interface { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
38 UnmarshalAttributes(Attributes) error |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
39 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
40 ) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
41 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
42 // Marshal fills src into ca. |
2051
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
43 func (ca Attributes) Marshal(src interface{}) error { |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
44 if ca == nil { |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
45 return nil |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
46 } |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
47 var err error |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
48 if m, ok := src.(AttributesMarshaler); ok { |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
49 err = m.MarshalAttributes(ca) |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
50 } |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
51 return err |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
52 } |
7d627258e045
Imports: Re-enabled /imports/config POST.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2047
diff
changeset
|
53 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
54 // Unmarshal stored ca into dst. |
2047
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
55 func (ca Attributes) Unmarshal(dst interface{}) error { |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
56 if ca == nil { |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
57 return nil |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
58 } |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
59 var err error |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
60 if um, ok := dst.(AttributesUnmarshaler); ok { |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
61 err = um.UnmarshalAttributes(ca) |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
62 } |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
63 return err |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
64 } |
78002c5e838c
Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2036
diff
changeset
|
65 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
66 // Delete removes a key and its associated value from these |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
67 // attriibutes. Return true if the key/value pair was deleted. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
68 func (ca Attributes) Delete(key string) bool { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
69 if ca == nil { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
70 return false |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
71 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
72 if _, found := ca[key]; !found { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
73 return false |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
74 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
75 delete(ca, key) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
76 return true |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
77 } |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 // Get fetches a value for given key out of the configuration. |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 // If the key was not found the bool component of the return value |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 // return false. |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 func (ca Attributes) Get(key string) (string, bool) { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 if ca == nil { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 return "", false |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 value, found := ca[key] |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 return value, found |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
90 // Set stores a key/value pair in the attributes. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
91 // Returns true if the storing succeeded. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
92 func (ca Attributes) Set(key, value string) bool { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
93 if ca == nil { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
94 return false |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
95 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
96 ca[key] = value |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
97 return true |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
98 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
99 |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 // Bool returns a bool value for a given key. |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 func (ca Attributes) Bool(key string) bool { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 s, found := ca.Get(key) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 return found && strings.ToLower(s) == "true" |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
106 // SetBool stores a bool value with a given key in |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
107 // this attributes. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
108 // Returns true if the storing succeeded. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
109 func (ca Attributes) SetBool(key string, value bool) bool { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
110 var v string |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
111 if value { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
112 v = "true" |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
113 } else { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
114 v = "false" |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
115 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
116 return ca.Set(key, v) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
117 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
118 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
119 // Date returns a time.Time for a given key. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
120 // Returns true if the key was found and the |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
121 // represents a date. |
2059
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
122 func (ca Attributes) Date(key string) (time.Time, bool) { |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
123 s, found := ca.Get(key) |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
124 if !found { |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
125 return time.Time{}, false |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
126 } |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
127 d, err := time.Parse(DateFormat, s) |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
128 if err != nil { |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
129 log.Printf("error: %v\n", err) |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
130 return time.Time{}, false |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
131 } |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
132 return d, true |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
133 } |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
134 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
135 // SetDate stores a date with a given key in the attributes. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
136 // Returns true if the storage succeeded. |
2059
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
137 func (ca Attributes) SetDate(key string, date time.Time) bool { |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
138 s := date.Format(DateFormat) |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
139 return ca.Set(key, s) |
ae0021feaac8
Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2051
diff
changeset
|
140 } |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
141 |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 // Time gives a time.Time for a given key. |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 func (ca Attributes) Time(key string) (time.Time, bool) { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 s, found := ca.Get(key) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 if !found { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 return time.Time{}, false |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 } |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
148 t, err := time.Parse(TimeFormat, s) |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 if err != nil { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 log.Printf("error: %v\n", err) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 return time.Time{}, false |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 return t, true |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
154 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
155 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
156 // SetTime stores a time with a given key in the attributes. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
157 // Returns true if the storage succeeded. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
158 func (ca Attributes) SetTime(key string, t time.Time) bool { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
159 value := t.Format(TimeFormat) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
160 return ca.Set(key, value) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
161 |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
162 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
163 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
164 // Int returns an int for a given key. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
165 // Returns true if the key was found and the value |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
166 // is an integer. |
1708
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
167 func (ca Attributes) Int(key string) (int, bool) { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
168 s, found := ca.Get(key) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
169 if !found { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 return 0, false |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 i, err := strconv.Atoi(s) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 if err != nil { |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 log.Printf("error: %v\n", err) |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
175 return 0, false |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
176 } |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 return i, true |
49e047c2106e
Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 } |
1985
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
179 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
180 // SetInt stores an int with a given key in the attributes. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
181 // Returns true if the storage succeeded. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
182 func (ca Attributes) SetInt(key string, value int) bool { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
183 v := strconv.Itoa(value) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
184 return ca.Set(key, v) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
185 } |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
186 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
187 // Float returns a float64 for a given key. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
188 // Returns true if the key was found and the value |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
189 // is a floating point value. |
2563
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
190 func (ca Attributes) Float(key string) (float64, bool) { |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
191 s, found := ca.Get(key) |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
192 if !found { |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
193 return 0, false |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
194 } |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
195 f, err := strconv.ParseFloat(s, 64) |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
196 if err != nil { |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
197 log.Printf("error: %v\n", err) |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
198 return 0, false |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
199 } |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
200 return f, true |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
201 } |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
202 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
203 // SetFloat stores an float64 with a given key in the attributes. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
204 // Returns true if the storage succeeded. |
2563
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
205 func (ca Attributes) SetFloat(key string, value float64) bool { |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
206 v := strconv.FormatFloat(value, 'e', -1, 64) |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
207 return ca.Set(key, v) |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
208 } |
dc4fae4bdb8f
Expose axis snapping tolerance to users
Tom Gottfried <tom@intevation.de>
parents:
2150
diff
changeset
|
209 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
210 // Duration returns a duration for a given key. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
211 // Returns true if the key was found and the value |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
212 // is a duration. |
1985
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
213 func (ca Attributes) Duration(key string) (time.Duration, bool) { |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
214 s, found := ca.Get(key) |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
215 if !found { |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
216 return 0, false |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
217 } |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
218 d, err := time.ParseDuration(s) |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
219 if err != nil { |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
220 log.Printf("error: %v\n", err) |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
221 return 0, false |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
222 } |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
223 return d, true |
8eeb0b5eb340
Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1708
diff
changeset
|
224 } |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
225 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
226 // SetDuration stores a duration with a given key in the attributes. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2563
diff
changeset
|
227 // Returns true if the storage succeeded. |
2036
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
228 func (ca Attributes) SetDuration(key string, value time.Duration) bool { |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
229 v := value.String() |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
230 return ca.Set(key, v) |
74e24ae3205a
Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1985
diff
changeset
|
231 } |