annotate pkg/common/attributes.go @ 2047:78002c5e838c unify_imports

Imports: Code simplification.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 25 Jan 2019 18:24:14 +0100
parents 74e24ae3205a
children 7d627258e045
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 //
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
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>
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package common
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "log"
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "strconv"
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "strings"
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "time"
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 )
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
23 type (
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
24
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
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
29 AttributesMarshaler interface {
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
30 MarshalAttributes(Attributes) error
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
31 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
32
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
33 AttributesUnmarshaler interface {
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
34 UnmarshalAttributes(Attributes) error
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
35 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
36 )
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
37
2047
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
38 func (ca Attributes) Unmarshal(dst interface{}) error {
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
39 if ca == nil {
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
40 return nil
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
41 }
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
42 var err error
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
43 if um, ok := dst.(AttributesUnmarshaler); ok {
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
44 err = um.UnmarshalAttributes(ca)
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
45 }
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
46 return err
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
47 }
78002c5e838c Imports: Code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2036
diff changeset
48
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
49 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
50 if ca == nil {
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
51 return false
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
52 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
53 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
54 return false
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
55 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
56 delete(ca, key)
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
57 return true
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
58 }
1708
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 // 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
61 // 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
62 // return false.
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 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
64 if ca == nil {
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 return "", false
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 }
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 value, found := ca[key]
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 return value, found
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 }
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
71 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
72 if ca == nil {
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 ca[key] = value
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 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
78
1708
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 // 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
80 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
81 s, found := ca.Get(key)
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 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
83 }
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
85 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
86 var v string
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
87 if value {
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
88 v = "true"
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
89 } else {
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
90 v = "false"
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
91 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
92 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
93 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
94
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
95 const TimeFormat = "2006-01-02T15:04:05"
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
96
1708
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 // 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
98 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
99 s, found := ca.Get(key)
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 if !found {
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 return time.Time{}, false
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 }
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
103 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
104 if err != nil {
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 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
106 return time.Time{}, false
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 }
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 return t, true
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 }
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
111 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
112 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
113 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
114
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
1708
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 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
118 s, found := ca.Get(key)
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 if !found {
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 return 0, false
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 }
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 i, err := strconv.Atoi(s)
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 if err != nil {
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 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
125 return 0, false
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 }
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 return i, true
49e047c2106e Imports: Made imports re-runnable if they fail.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 }
1985
8eeb0b5eb340 Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1708
diff changeset
129
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
130 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
131 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
132 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
133 }
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
134
1985
8eeb0b5eb340 Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1708
diff changeset
135 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
136 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
137 if !found {
8eeb0b5eb340 Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1708
diff changeset
138 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
139 }
8eeb0b5eb340 Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1708
diff changeset
140 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
141 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
142 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
143 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
144 }
8eeb0b5eb340 Imports: Made retries and the waiting between the attempts configurable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1708
diff changeset
145 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
146 }
2036
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
147
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
148 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
149 v := value.String()
74e24ae3205a Imports: Added a persistent model for the extra attributes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1985
diff changeset
150 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
151 }