Mercurial > gemma
view pkg/common/attributes.go @ 3530:e98ea184538e
Improve warning message
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Wed, 29 May 2019 16:56:56 +0200 |
parents | dc4fae4bdb8f |
children | 8c5df0f3562e |
line wrap: on
line source
// 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, 2019 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH // // Author(s): // * Sascha L. Teichmann <sascha.teichmann@intevation.de> // * Tom Gottfried <tom.gottfried@intevation.de> package common import ( "log" "strconv" "strings" "time" ) type ( // Attributes is a map of optional key/value attributes // of a configuration. Attributes map[string]string AttributesMarshaler interface { MarshalAttributes(Attributes) error } AttributesUnmarshaler interface { UnmarshalAttributes(Attributes) error } ) func (ca Attributes) Marshal(src interface{}) error { if ca == nil { return nil } var err error if m, ok := src.(AttributesMarshaler); ok { err = m.MarshalAttributes(ca) } return err } func (ca Attributes) Unmarshal(dst interface{}) error { if ca == nil { return nil } var err error if um, ok := dst.(AttributesUnmarshaler); ok { err = um.UnmarshalAttributes(ca) } return err } func (ca Attributes) Delete(key string) bool { if ca == nil { return false } if _, found := ca[key]; !found { return false } delete(ca, key) return true } // 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 } func (ca Attributes) Set(key, value string) bool { if ca == nil { return false } ca[key] = value return true } // 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" } func (ca Attributes) SetBool(key string, value bool) bool { var v string if value { v = "true" } else { v = "false" } return ca.Set(key, v) } func (ca Attributes) Date(key string) (time.Time, bool) { s, found := ca.Get(key) if !found { return time.Time{}, false } d, err := time.Parse(DateFormat, s) if err != nil { log.Printf("error: %v\n", err) return time.Time{}, false } return d, true } func (ca Attributes) SetDate(key string, date time.Time) bool { s := date.Format(DateFormat) return ca.Set(key, s) } // 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(TimeFormat, s) if err != nil { log.Printf("error: %v\n", err) return time.Time{}, false } return t, true } func (ca Attributes) SetTime(key string, t time.Time) bool { value := t.Format(TimeFormat) return ca.Set(key, value) } 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 } func (ca Attributes) SetInt(key string, value int) bool { v := strconv.Itoa(value) return ca.Set(key, v) } func (ca Attributes) Float(key string) (float64, bool) { s, found := ca.Get(key) if !found { return 0, false } f, err := strconv.ParseFloat(s, 64) if err != nil { log.Printf("error: %v\n", err) return 0, false } return f, true } func (ca Attributes) SetFloat(key string, value float64) bool { v := strconv.FormatFloat(value, 'e', -1, 64) return ca.Set(key, v) } func (ca Attributes) Duration(key string) (time.Duration, bool) { s, found := ca.Get(key) if !found { return 0, false } d, err := time.ParseDuration(s) if err != nil { log.Printf("error: %v\n", err) return 0, false } return d, true } func (ca Attributes) SetDuration(key string, value time.Duration) bool { v := value.String() return ca.Set(key, v) }