Skip to content

Commit

Permalink
added yaml unmarshal Position
Browse files Browse the repository at this point in the history
  • Loading branch information
WouterVisscher committed Aug 14, 2020
1 parent 1774faf commit d8d412d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
17 changes: 17 additions & 0 deletions pkg/ows/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,20 @@ func (c *CRS) parseString(s string) {
c.Code = i
}
}

func getPositionFromString(position string) []float64 {
regex := regexp.MustCompile(` `)
result := regex.Split(position, -1)
var ps []float64 //slice because length can be 2 or more

// check if 'strings' are parsable to float64
// if one is not return nothing
for _, fs := range result {
f, err := strconv.ParseFloat(fs, 64)
if err != nil {
return nil
}
ps = append(ps, f)
}
return ps
}
19 changes: 0 additions & 19 deletions pkg/ows/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package ows
import (
"encoding/xml"
"fmt"
"regexp"
"strconv"
)

// XMLAttribute wrapper around the array of xml.Attr
Expand Down Expand Up @@ -93,20 +91,3 @@ func (c *CRS) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
}
}
}

func getPositionFromString(position string) []float64 {
regex := regexp.MustCompile(` `)
result := regex.Split(position, -1)
var ps []float64 //slice because length can be 2 or more

// check if 'strings' are parsable to float64
// if one is not return nothing
for _, fs := range result {
f, err := strconv.ParseFloat(fs, 64)
if err != nil {
return nil
}
ps = append(ps, f)
}
return ps
}
13 changes: 13 additions & 0 deletions pkg/ows/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ func (c *CRS) UnmarshalYAML(unmarshal func(interface{}) error) error {

return nil
}

// UnmarshalYAML Position
func (p *Position) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}

position := getPositionFromString(s)
*p = Position{position[0], position[1]}

return nil
}
26 changes: 25 additions & 1 deletion pkg/ows/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,35 @@ othercrs:
var ftl FeatureType
err := yaml.Unmarshal(test.yaml, &ftl)
if err != nil {
t.Errorf("yaml.UnMarshal failed with '%s'\n", err)
t.Errorf("test: %d, yaml.UnMarshal failed with '%s'\n", k, err)
} else {
if ftl.DefaultCRS.Code != test.expectedCrs.Code || ftl.DefaultCRS.Namespace != test.expectedCrs.Namespace {
t.Errorf("test: %d, expected: %v+,\n got: %v+", k, test.expectedCrs, ftl.DefaultCRS)
}
}
}
}

func TestUnmarshalYAMLPosition(t *testing.T) {

var tests = []struct {
positionstring []byte
expectedposition Position
}{
0: {positionstring: []byte(`2.52712538742158 50.2128625669452`), expectedposition: Position{2.52712538742158, 50.2128625669452}},
1: {positionstring: []byte(`7.37402550506231 55.7211602557705`), expectedposition: Position{7.37402550506231, 55.7211602557705}},
2: {positionstring: []byte(`7.37402550506231 55.7211602557705 0 1 2 3`), expectedposition: Position{7.37402550506231, 55.7211602557705}},
}

for k, test := range tests {
var pos Position
err := yaml.Unmarshal(test.positionstring, &pos)
if err != nil {
t.Errorf("test: %d, yaml.UnMarshal failed with '%s'\n", k, err)
} else {
if pos != test.expectedposition {
t.Errorf("test: %d, expected: %v+,\n got: %v+", k, test.expectedposition, pos)
}
}
}
}

0 comments on commit d8d412d

Please sign in to comment.