Mercurial > gemma
changeset 4162:8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 02 Aug 2019 17:53:18 +0200 |
parents | 64cd18281c76 |
children | 51cfa54fc5b1 |
files | pkg/common/attributes.go pkg/common/nashsutcliffe.go pkg/common/random.go pkg/common/time.go |
diffstat | 4 files changed, 75 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/common/attributes.go Fri Aug 02 17:14:13 2019 +0200 +++ b/pkg/common/attributes.go Fri Aug 02 17:53:18 2019 +0200 @@ -26,15 +26,20 @@ // of a configuration. Attributes map[string]string + // AttributesMarshaler is an interface to implement + // custom marshalling for the implementing type. AttributesMarshaler interface { MarshalAttributes(Attributes) error } + // AttributesUnmarshaler is and interface to implement + // custom unmarshalling for the implementing type. AttributesUnmarshaler interface { UnmarshalAttributes(Attributes) error } ) +// Marshal fills src into ca. func (ca Attributes) Marshal(src interface{}) error { if ca == nil { return nil @@ -46,6 +51,7 @@ return err } +// Unmarshal stored ca into dst. func (ca Attributes) Unmarshal(dst interface{}) error { if ca == nil { return nil @@ -57,6 +63,8 @@ return err } +// Delete removes a key and its associated value from these +// attriibutes. Return true if the key/value pair was deleted. func (ca Attributes) Delete(key string) bool { if ca == nil { return false @@ -79,6 +87,8 @@ return value, found } +// Set stores a key/value pair in the attributes. +// Returns true if the storing succeeded. func (ca Attributes) Set(key, value string) bool { if ca == nil { return false @@ -93,6 +103,9 @@ return found && strings.ToLower(s) == "true" } +// SetBool stores a bool value with a given key in +// this attributes. +// Returns true if the storing succeeded. func (ca Attributes) SetBool(key string, value bool) bool { var v string if value { @@ -103,6 +116,9 @@ return ca.Set(key, v) } +// Date returns a time.Time for a given key. +// Returns true if the key was found and the +// represents a date. func (ca Attributes) Date(key string) (time.Time, bool) { s, found := ca.Get(key) if !found { @@ -116,6 +132,8 @@ return d, true } +// SetDate stores a date with a given key in the attributes. +// Returns true if the storage succeeded. func (ca Attributes) SetDate(key string, date time.Time) bool { s := date.Format(DateFormat) return ca.Set(key, s) @@ -135,12 +153,17 @@ return t, true } +// SetTime stores a time with a given key in the attributes. +// Returns true if the storage succeeded. func (ca Attributes) SetTime(key string, t time.Time) bool { value := t.Format(TimeFormat) return ca.Set(key, value) } +// Int returns an int for a given key. +// Returns true if the key was found and the value +// is an integer. func (ca Attributes) Int(key string) (int, bool) { s, found := ca.Get(key) if !found { @@ -154,11 +177,16 @@ return i, true } +// SetInt stores an int with a given key in the attributes. +// Returns true if the storage succeeded. func (ca Attributes) SetInt(key string, value int) bool { v := strconv.Itoa(value) return ca.Set(key, v) } +// Float returns a float64 for a given key. +// Returns true if the key was found and the value +// is a floating point value. func (ca Attributes) Float(key string) (float64, bool) { s, found := ca.Get(key) if !found { @@ -172,11 +200,16 @@ return f, true } +// SetFloat stores an float64 with a given key in the attributes. +// Returns true if the storage succeeded. func (ca Attributes) SetFloat(key string, value float64) bool { v := strconv.FormatFloat(value, 'e', -1, 64) return ca.Set(key, v) } +// Duration returns a duration for a given key. +// Returns true if the key was found and the value +// is a duration. func (ca Attributes) Duration(key string) (time.Duration, bool) { s, found := ca.Get(key) if !found { @@ -190,6 +223,8 @@ return d, true } +// SetDuration stores a duration with a given key in the attributes. +// Returns true if the storage succeeded. func (ca Attributes) SetDuration(key string, value time.Duration) bool { v := value.String() return ca.Set(key, v)
--- a/pkg/common/nashsutcliffe.go Fri Aug 02 17:14:13 2019 +0200 +++ b/pkg/common/nashsutcliffe.go Fri Aug 02 17:53:18 2019 +0200 @@ -18,18 +18,26 @@ "time" ) -type TimedValue struct { - When time.Time - Value float64 -} +type ( + // TimedValue is a tuple of a point in time and an associated value. + TimedValue struct { + When time.Time + Value float64 + } -type TimedValues []TimedValue + // TimedValues is a slice of TimedValue tuples. + TimedValues []TimedValue +) func epsEquals(a, b time.Time) bool { d := a.Sub(b) return -10*time.Millisecond < d && d < 10*time.Millisecond } +// Find scans to the tuples and compares the time with an +// epsilon of ten micro seconds. If they are equals the associated +// value is returned. The return bool flags indicated if the +// search was successful. func (mvs TimedValues) Find(when time.Time) (float64, bool) { for i := range mvs { if epsEquals(when, mvs[i].When) { @@ -39,6 +47,12 @@ return 0, false } +// NashSutcliffe calculates the Nash-Sutcliffe coefficent for +// given predicted and observed values. +// See +// https://en.wikipedia.org/wiki/Nash%E2%80%93Sutcliffe_model_efficiency_coefficient +// for details. +// The function panics if predicted and observed are of different lengths. func NashSutcliffe(predicted, observed []float64) float64 { if len(predicted) != len(observed) {
--- a/pkg/common/random.go Fri Aug 02 17:14:13 2019 +0200 +++ b/pkg/common/random.go Fri Aug 02 17:53:18 2019 +0200 @@ -68,6 +68,8 @@ return string(out) } +// Random returns a function which generates pseudo random +// values in the range between low and high. func Random(low, high float64) func() float64 { if low > high { low, high = high, low
--- a/pkg/common/time.go Fri Aug 02 17:14:13 2019 +0200 +++ b/pkg/common/time.go Fri Aug 02 17:53:18 2019 +0200 @@ -23,15 +23,25 @@ // time.RFC3339 equals "simplified ISO format as defined by ECMA-262" // https://tc39.github.io/ecma262/#sec-date-time-string-format // and "SHOULD be used in new protocols on the Internet." (RFC section 5.6) - TimeFormat = time.RFC3339 - TimeFormatMicro = "2006-01-02T15:04:05.999Z07:00" + + // TimeFormat is the preferred time format represention in gemma. + TimeFormat = time.RFC3339 + // TimeFormatMicro is the same as TimeFormat but also + // contains 3 digits for micro seconds. + // This is useful for e.g. times in imports and the + // respective logs. + TimeFormatMicro = "2006-01-02T15:04:05.999Z07:00" + // TimeFormatMicroLocal is the same as TimeFormatMicro but + // w/o the timezone. Only used for tolerant parsing. TimeFormatMicroLocal = "2006-01-02T15:04:05.000" - DateFormat = "2006-01-02" + // DateFormat represents the preferred date format in Gemma. + DateFormat = "2006-01-02" ) // TimeParser is a list of time formats. type TimeParser []string +// ParseTime parses the time formats known to Gemma. var ParseTime = TimeParser{ TimeFormat, TimeFormatMicro, @@ -56,6 +66,12 @@ return t, err } +// InterpolateTime returns a function that linearly interpolates the +// time between t1 and t2 for given value m1 to m2. +// If m1 is given to the returned function t1 is returned. +// If m2 is given to the returned function t2 is returned. +// Values between m1 and m2 will result in the proportional +// time between t1 and t2. func InterpolateTime(t1 time.Time, m1 float64, t2 time.Time, m2 float64) func(float64) time.Time { // f(m1) = t1