diff pkg/models/imports.go @ 2059:ae0021feaac8 unify_imports

Imports: Made stretch import marshable (even if it is not needed).
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 29 Jan 2019 08:28:50 +0100
parents 74e24ae3205a
children 46343e3f7a3e
line wrap: on
line diff
--- a/pkg/models/imports.go	Mon Jan 28 20:35:14 2019 +0100
+++ b/pkg/models/imports.go	Tue Jan 29 08:28:50 2019 +0100
@@ -14,6 +14,7 @@
 
 import (
 	"errors"
+	"strings"
 
 	"gemma.intevation.de/gemma/pkg/common"
 )
@@ -179,3 +180,85 @@
 	fdi.SourceOrganization = source
 	return nil
 }
+
+func (sti *StretchImport) MarshalAttributes(attrs common.Attributes) error {
+	if err := sti.EmailType.MarshalAttributes(attrs); err != nil {
+		return err
+	}
+	attrs.Set("name", sti.Name)
+	attrs.Set("from", sti.From.String())
+	attrs.Set("to", sti.To.String())
+	attrs.Set("objnam", sti.ObjNam)
+	if sti.NObjNam != nil {
+		attrs.Set("nobjnam", *sti.NObjNam)
+	}
+	attrs.Set("source-organization", sti.Source)
+	attrs.SetDate("date-info", sti.Date.Time)
+	if len(sti.Countries) > 0 {
+		countries := make([]string, len(sti.Countries))
+		for i, c := range sti.Countries {
+			countries[i] = string(c)
+		}
+		attrs.Set("countries", strings.Join(countries, ","))
+	}
+
+	return nil
+}
+
+func (sti *StretchImport) UnmarshalAttributes(attrs common.Attributes) error {
+	if err := sti.EmailType.UnmarshalAttributes(attrs); err != nil {
+		return err
+	}
+	name, found := attrs.Get("name")
+	if !found {
+		return errors.New("missing 'name' attribute")
+	}
+	sti.Name = name
+	from, found := attrs.Get("from")
+	if !found {
+		return errors.New("missing 'from' attribute")
+	}
+	f, err := IsrsFromString(from)
+	if err != nil {
+		return err
+	}
+	sti.From = *f
+	to, found := attrs.Get("from")
+	if !found {
+		return errors.New("missing 'to' attribute")
+	}
+	t, err := IsrsFromString(to)
+	if err != nil {
+		return err
+	}
+	sti.To = *t
+	objnam, found := attrs.Get("objnam")
+	if !found {
+		return errors.New("missing 'objnam' attribute")
+	}
+	sti.ObjNam = objnam
+	nobjnam, found := attrs.Get("nobjnam")
+	if found {
+		sti.NObjNam = &nobjnam
+	}
+	source, found := attrs.Get("source-organization")
+	if !found {
+		return errors.New("missing 'source' attribute")
+	}
+	sti.Source = source
+	date, found := attrs.Date("date-info")
+	if !found {
+		return errors.New("missing 'date-info' attribute")
+	}
+	sti.Date = Date{date}
+	countries, found := attrs.Get("countries")
+	if found {
+		csp := strings.Split(countries, ",")
+		cs := make(UniqueCountries, len(csp))
+		for i, c := range csp {
+			cs[i] = Country(c)
+		}
+		sti.Countries = cs
+	}
+	return nil
+}