Mercurial > gemma
diff pkg/common/attributes.go @ 1708:49e047c2106e
Imports: Made imports re-runnable if they fail.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 08 Jan 2019 13:35:44 +0100 |
parents | |
children | 8eeb0b5eb340 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/common/attributes.go Tue Jan 08 13:35:44 2019 +0100 @@ -0,0 +1,69 @@ +// This is Free Software under GNU Affero General Public License v >= 3.0 +// without warranty, see README.md and license for details. +// +// SPDX-License-Identifier: AGPL-3.0-or-later +// License-Filename: LICENSES/AGPL-3.0.txt +// +// Copyright (C) 2018 by via donau +// – Österreichische Wasserstraßen-Gesellschaft mbH +// Software engineering by Intevation GmbH +// +// Author(s): +// * Sascha L. Teichmann <sascha.teichmann@intevation.de> + +package common + +import ( + "log" + "strconv" + "strings" + "time" +) + +// Attributes is a map of optional key/value attributes +// of a configuration. +type Attributes map[string]string + +// Get fetches a value for given key out of the configuration. +// If the key was not found the bool component of the return value +// return false. +func (ca Attributes) Get(key string) (string, bool) { + if ca == nil { + return "", false + } + value, found := ca[key] + return value, found +} + +// Bool returns a bool value for a given key. +func (ca Attributes) Bool(key string) bool { + s, found := ca.Get(key) + return found && strings.ToLower(s) == "true" +} + +// Time gives a time.Time for a given key. +func (ca Attributes) Time(key string) (time.Time, bool) { + s, found := ca.Get(key) + if !found { + return time.Time{}, false + } + t, err := time.Parse("2006-01-02T15:04:05", s) + if err != nil { + log.Printf("error: %v\n", err) + return time.Time{}, false + } + return t, true +} + +func (ca Attributes) Int(key string) (int, bool) { + s, found := ca.Get(key) + if !found { + return 0, false + } + i, err := strconv.Atoi(s) + if err != nil { + log.Printf("error: %v\n", err) + return 0, false + } + return i, true +}