changeset 1900:6a67cd819e93

To prepare stretch import made some model data types re-usable.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 18 Jan 2019 15:04:53 +0100
parents 272133cd65da
children 71b722809b2b
files pkg/controllers/srimports.go pkg/imports/sr.go pkg/models/common.go pkg/models/gauge.go pkg/models/isrs.go pkg/models/sr.go
diffstat 6 files changed, 109 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/srimports.go	Fri Jan 18 14:58:34 2019 +0100
+++ b/pkg/controllers/srimports.go	Fri Jan 18 15:04:53 2019 +0100
@@ -116,11 +116,11 @@
 	}
 
 	if v := req.FormValue("date"); v != "" {
-		date, err := time.Parse(models.SoundingResultDateFormat, v)
+		date, err := time.Parse(models.DateFormat, v)
 		if err != nil {
 			return err
 		}
-		sr.Date = &models.SoundingResultDate{Time: date}
+		sr.Date = &models.Date{Time: date}
 	}
 
 	if v := req.FormValue("depth-reference"); v != "" {
--- a/pkg/imports/sr.go	Fri Jan 18 14:58:34 2019 +0100
+++ b/pkg/imports/sr.go	Fri Jan 18 15:04:53 2019 +0100
@@ -48,7 +48,7 @@
 
 	// Override data
 	// Date if given overrides the date value from the meta.json.
-	Date *models.SoundingResultDate `json:"date,omitempty"`
+	Date *models.Date `json:"date,omitempty"`
 	// Date if given overrides the name of the bottleneck from the meta.json.
 	Bottleneck *string `json:"bottleneck,omitempty"`
 	// EPSG if given overrides the EPSG code from the meta.json.
@@ -311,10 +311,10 @@
 	}
 
 	summary := struct {
-		Bottleneck string                    `json:"bottleneck"`
-		Date       models.SoundingResultDate `json:"date"`
-		Lat        float64                   `json:"lat"`
-		Lon        float64                   `json:"lon"`
+		Bottleneck string      `json:"bottleneck"`
+		Date       models.Date `json:"date"`
+		Lat        float64     `json:"lat"`
+		Lon        float64     `json:"lon"`
 	}{
 		Bottleneck: m.Bottleneck,
 		Date:       m.Date,
--- a/pkg/models/common.go	Fri Jan 18 14:58:34 2019 +0100
+++ b/pkg/models/common.go	Fri Jan 18 15:04:53 2019 +0100
@@ -13,7 +13,11 @@
 
 package models
 
-import "errors"
+import (
+	"encoding/json"
+	"errors"
+	"time"
+)
 
 var (
 	errNoString    = errors.New("Not a string")
@@ -22,3 +26,23 @@
 
 // WGS84 is the EPSG of the World Geodetic System 1984.
 const WGS84 = 4326
+
+const DateFormat = "2006-01-02"
+
+type Date struct{ time.Time }
+
+func (srd Date) MarshalJSON() ([]byte, error) {
+	return json.Marshal(srd.Format(DateFormat))
+}
+
+func (srd *Date) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+	d, err := time.Parse(DateFormat, s)
+	if err == nil {
+		*srd = Date{d}
+	}
+	return err
+}
--- a/pkg/models/gauge.go	Fri Jan 18 14:58:34 2019 +0100
+++ b/pkg/models/gauge.go	Fri Jan 18 15:04:53 2019 +0100
@@ -14,9 +14,6 @@
 package models
 
 import (
-	"errors"
-	"fmt"
-	"strconv"
 	"time"
 
 	"gemma.intevation.de/gemma/pkg/common"
@@ -36,41 +33,3 @@
 	Gauge           Isrs
 	LatestDateIssue time.Time
 }
-
-// Isrs represents the gauge identification data structure
-type Isrs struct {
-	CountryCode    string
-	LoCode         string
-	FairwaySection string
-	Orc            string
-	Hectometre     int
-}
-
-// IsrsFromString converts string representation of isrs code to type Isrs
-func IsrsFromString(isrsCode string) (*Isrs, error) {
-	if len(isrsCode) < 20 {
-		return nil, errors.New("ISRS code too short")
-	}
-	hm, err := strconv.Atoi(isrsCode[15:20])
-	if err != nil {
-		return nil, err
-	}
-	isrs := Isrs{
-		CountryCode:    isrsCode[0:2],
-		LoCode:         isrsCode[2:5],
-		FairwaySection: isrsCode[5:10],
-		Orc:            isrsCode[10:15],
-		Hectometre:     hm,
-	}
-	return &isrs, nil
-}
-
-// String creates a isrs code string from Isrs
-func (isrs *Isrs) String() string {
-	return fmt.Sprintf("%s%s%s%s%05d",
-		isrs.CountryCode,
-		isrs.LoCode,
-		isrs.FairwaySection,
-		isrs.Orc,
-		isrs.Hectometre)
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/models/isrs.go	Fri Jan 18 15:04:53 2019 +0100
@@ -0,0 +1,73 @@
+// 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):
+//  * Raimund Renkert <raimund.renkert@intevation.de>
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package models
+
+import (
+	"encoding/json"
+	"errors"
+	"fmt"
+	"strconv"
+)
+
+// Isrs represents the gauge identification data structure
+type Isrs struct {
+	CountryCode    string
+	LoCode         string
+	FairwaySection string
+	Orc            string
+	Hectometre     int
+}
+
+func (isrs *Isrs) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+	in, err := IsrsFromString(s)
+	if err != nil {
+		return err
+	}
+	*isrs = *in
+	return nil
+}
+
+// IsrsFromString converts string representation of isrs code to type Isrs
+func IsrsFromString(isrsCode string) (*Isrs, error) {
+	if len(isrsCode) < 20 {
+		return nil, errors.New("ISRS code too short")
+	}
+	hm, err := strconv.Atoi(isrsCode[15:20])
+	if err != nil {
+		return nil, err
+	}
+	isrs := Isrs{
+		CountryCode:    isrsCode[0:2],
+		LoCode:         isrsCode[2:5],
+		FairwaySection: isrsCode[5:10],
+		Orc:            isrsCode[10:15],
+		Hectometre:     hm,
+	}
+	return &isrs, nil
+}
+
+// String creates a isrs code string from Isrs
+func (isrs *Isrs) String() string {
+	return fmt.Sprintf("%s%s%s%s%05d",
+		isrs.CountryCode,
+		isrs.LoCode,
+		isrs.FairwaySection,
+		isrs.Orc,
+		isrs.Hectometre)
+}
--- a/pkg/models/sr.go	Fri Jan 18 14:58:34 2019 +0100
+++ b/pkg/models/sr.go	Fri Jan 18 15:04:53 2019 +0100
@@ -21,19 +21,14 @@
 	"errors"
 	"fmt"
 	"io"
-	"time"
 )
 
-const SoundingResultDateFormat = "2006-01-02"
-
 type (
-	SoundingResultDate struct{ time.Time }
-
 	SoundingResultMeta struct {
-		Date           SoundingResultDate `json:"date"`
-		Bottleneck     string             `json:"bottleneck"`
-		EPSG           uint               `json:"epsg"`
-		DepthReference string             `json:"depth-reference"`
+		Date           Date   `json:"date"`
+		Bottleneck     string `json:"bottleneck"`
+		EPSG           uint   `json:"epsg"`
+		DepthReference string `json:"depth-reference"`
 	}
 )
 
@@ -50,22 +45,6 @@
 WHERE bn.objnam = $1 AND sr.date_info = $2`
 )
 
-func (srd SoundingResultDate) MarshalJSON() ([]byte, error) {
-	return json.Marshal(srd.Format(SoundingResultDateFormat))
-}
-
-func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error {
-	var s string
-	if err := json.Unmarshal(data, &s); err != nil {
-		return err
-	}
-	d, err := time.Parse(SoundingResultDateFormat, s)
-	if err == nil {
-		*srd = SoundingResultDate{d}
-	}
-	return err
-}
-
 func (m *SoundingResultMeta) Decode(r io.Reader) error {
 	err := json.NewDecoder(r).Decode(m)
 	if err == nil && m.EPSG == 0 {