Mercurial > gemma
view pkg/models/importbase.go @ 5637:7f9600ab5461
merge
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Wed, 14 Jun 2023 08:45:11 +0200 |
parents | 1222b777f51f |
children |
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 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH // // Author(s): // * Sascha L. Teichmann <sascha.teichmann@intevation.de> package models import ( "encoding/json" "errors" "time" "gemma.intevation.de/gemma/pkg/common" cron "gopkg.in/robfig/cron.v1" ) type ( // CronSpec is a string containing a cron line. CronSpec string // ConfigTime represents a configuration time. ConfigTime struct{ time.Time } // ConfigDuration represents a configuration duration. ConfigDuration struct{ time.Duration } // EmailType is a type to contain an email address. EmailType struct { Email bool `json:"send-email,omitempty"` } // QueueConfigurationType is the common base of // many configurations. QueueConfigurationType struct { EmailType Trys *int `json:"trys,omitempty"` WaitRetry *ConfigDuration `json:"wait-retry,omitempty"` Due *ConfigTime `json:"due,omitempty"` Cron *CronSpec `json:"cron,omitempty"` } // URLType is the common base of configurations // containing a password protected URL. URLType struct { URL string `json:"url"` Insecure bool `json:"insecure,omitempty"` User *string `json:"user,omitempty"` Password *string `json:"password,omitempty"` } // EmailTypeGetter tells which email type should be used. EmailTypeGetter interface { GetEmailType() *EmailType } // QueueConfigurationGetter tells which configuration type // should be used. QueueConfigurationGetter interface { GetQueueConfiguration() *QueueConfigurationType } // URLTypeGetter tells which URL type should be used, URLTypeGetter interface { GetURLType() *URLType } ) // GetQueueConfiguration implements QueueConfigurationGetter interface. func (qct *QueueConfigurationType) GetQueueConfiguration() *QueueConfigurationType { return qct } // GetURLType implements the URLTypeGetter interface. func (ut *URLType) GetURLType() *URLType { return ut } // GetEmailType implements the EmailTypeGetter interface. func (et *EmailType) GetEmailType() *EmailType { return et } // UnmarshalJSON implements the json.Unmarshaler interface. func (cd *ConfigDuration) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { return err } dur, err := time.ParseDuration(s) if err != nil { return err } if dur < 0 { return errors.New("duration has to be none negative") } *cd = ConfigDuration{dur} return nil } // MarshalJSON implements the json.Marshaler interface. func (cd *ConfigDuration) MarshalJSON() ([]byte, error) { return json.Marshal(cd.Duration.String()) } // UnmarshalJSON implements the json.Unmarshaler interface. func (ct *ConfigTime) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { return err } t, err := time.Parse(common.TimeFormat, s) if err != nil { return err } *ct = ConfigTime{t} return nil } // MarshalJSON implements json.Marshaler interface. func (ct *ConfigTime) MarshalJSON() ([]byte, error) { s := ct.Time.Format(common.TimeFormat) return json.Marshal([]byte(s)) } // UnmarshalJSON checks if the incoming string is a valid cron line. func (cs *CronSpec) UnmarshalJSON(data []byte) error { var spec string if err := json.Unmarshal(data, &spec); err != nil { return err } if _, err := cron.Parse(spec); err != nil { return err } *cs = CronSpec(spec) return nil } // MarshalAttributes stores a configuration. func (et *EmailType) MarshalAttributes(attrs common.Attributes) error { if et.Email { attrs.SetBool("email", et.Email) } return nil } // UnmarshalAttributes restores a configuration. func (et *EmailType) UnmarshalAttributes(attrs common.Attributes) error { et.Email = attrs.Bool("email") return nil } // MarshalAttributes stores a configuration. func (qct *QueueConfigurationType) MarshalAttributes(attrs common.Attributes) error { if err := qct.EmailType.MarshalAttributes(attrs); err != nil { return err } if qct.Trys != nil { attrs.SetInt("trys", *qct.Trys) } if qct.WaitRetry != nil { attrs.SetDuration("wait-retry", qct.WaitRetry.Duration) } if qct.Due != nil { attrs.SetTime("due", qct.Due.Time) } if qct.Cron != nil { attrs.Set("cron", string(*qct.Cron)) } return nil } // UnmarshalAttributes restores a configuration. func (qct *QueueConfigurationType) UnmarshalAttributes(attrs common.Attributes) error { if err := qct.EmailType.UnmarshalAttributes(attrs); err != nil { return err } if trys, found := attrs.Int("trys"); found { qct.Trys = &trys } if duration, found := attrs.Duration("wait-retry"); found { qct.WaitRetry = &ConfigDuration{duration} } if due, found := attrs.Time("due"); found { qct.Due = &ConfigTime{due} } if cron, found := attrs.Get("cron"); found { cs := CronSpec(cron) qct.Cron = &cs } return nil } // MarshalAttributes stores a configuration. func (ut *URLType) MarshalAttributes(attrs common.Attributes) error { attrs.Set("url", ut.URL) if ut.Insecure { attrs.SetBool("insecure", ut.Insecure) } if ut.User != nil { attrs.Set("user", *ut.User) } if ut.Password != nil { attrs.Set("password", *ut.Password) } return nil } // UnmarshalAttributes restores a configuration. func (ut *URLType) UnmarshalAttributes(attrs common.Attributes) error { url, found := attrs.Get("url") if !found { return errors.New("missing 'url' attribute") } ut.URL = url if insecure := attrs.Bool("insecure"); insecure { ut.Insecure = insecure } if user, found := attrs.Get("user"); found { ut.User = &user } if password, found := attrs.Get("password"); found { ut.Password = &password } return nil }