comparison pkg/models/isrs.go @ 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
children 6ffc372cde1e
comparison
equal deleted inserted replaced
1899:272133cd65da 1900:6a67cd819e93
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Raimund Renkert <raimund.renkert@intevation.de>
13 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
14
15 package models
16
17 import (
18 "encoding/json"
19 "errors"
20 "fmt"
21 "strconv"
22 )
23
24 // Isrs represents the gauge identification data structure
25 type Isrs struct {
26 CountryCode string
27 LoCode string
28 FairwaySection string
29 Orc string
30 Hectometre int
31 }
32
33 func (isrs *Isrs) UnmarshalJSON(data []byte) error {
34 var s string
35 if err := json.Unmarshal(data, &s); err != nil {
36 return err
37 }
38 in, err := IsrsFromString(s)
39 if err != nil {
40 return err
41 }
42 *isrs = *in
43 return nil
44 }
45
46 // IsrsFromString converts string representation of isrs code to type Isrs
47 func IsrsFromString(isrsCode string) (*Isrs, error) {
48 if len(isrsCode) < 20 {
49 return nil, errors.New("ISRS code too short")
50 }
51 hm, err := strconv.Atoi(isrsCode[15:20])
52 if err != nil {
53 return nil, err
54 }
55 isrs := Isrs{
56 CountryCode: isrsCode[0:2],
57 LoCode: isrsCode[2:5],
58 FairwaySection: isrsCode[5:10],
59 Orc: isrsCode[10:15],
60 Hectometre: hm,
61 }
62 return &isrs, nil
63 }
64
65 // String creates a isrs code string from Isrs
66 func (isrs *Isrs) String() string {
67 return fmt.Sprintf("%s%s%s%s%05d",
68 isrs.CountryCode,
69 isrs.LoCode,
70 isrs.FairwaySection,
71 isrs.Orc,
72 isrs.Hectometre)
73 }