view pkg/models/importbase.go @ 4969:5621586e7a54 fairway-marks-import

Avoid checking the same condition twice An entry should either be UPDATEd or INSERTed, if it's not already in the database. Thus simply INSERT if there was no UPDATE.
author Tom Gottfried <tom@intevation.de>
date Fri, 28 Feb 2020 17:55:55 +0100
parents 4847ac70103a
children 1222b777f51f
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 struct{ time.Time }

	ConfigDuration struct{ time.Duration }

	EmailType struct {
		Email bool `json:"send-email,omitempty"`
	}

	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 struct {
		URL      string  `json:"url"`
		Insecure bool    `json:"insecure,omitempty"`
		User     *string `json:"user,omitempty"`
		Password *string `json:"password,omitempty"`
	}

	EmailTypeGetter interface {
		GetEmailType() *EmailType
	}

	QueueConfigurationGetter interface {
		GetQueueConfiguration() *QueueConfigurationType
	}

	URLTypeGetter interface {
		GetURLType() *URLType
	}
)

func (qct *QueueConfigurationType) GetQueueConfiguration() *QueueConfigurationType {
	return qct
}

func (ut *URLType) GetURLType() *URLType {
	return ut
}

func (et *EmailType) GetEmailType() *EmailType {
	return et
}

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
}

func (cd *ConfigDuration) MarshalJSON() ([]byte, error) {
	return json.Marshal(cd.Duration.String())
}

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
}

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
}

func (et *EmailType) MarshalAttributes(attrs common.Attributes) error {
	if et.Email {
		attrs.SetBool("email", et.Email)
	}
	return nil
}

func (et *EmailType) UnmarshalAttributes(attrs common.Attributes) error {
	et.Email = attrs.Bool("email")
	return nil
}

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
}

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
}

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
}

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
}