Skip to content

Commit cd22e36

Browse files
authored
Merge pull request #46 from PDOK/jd/marshallers
Added missing marshallers
2 parents 3a6fd51 + 8c04024 commit cd22e36

File tree

6 files changed

+113
-4
lines changed

6 files changed

+113
-4
lines changed

pkg/wfs200/crs_yaml.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package wfs200
22

3-
import "strconv"
4-
53
// UnmarshalYAML CRS
64
func (c *CRS) UnmarshalYAML(unmarshal func(interface{}) error) error {
75
var s string
@@ -17,6 +15,6 @@ func (c *CRS) UnmarshalYAML(unmarshal func(interface{}) error) error {
1715
return nil
1816
}
1917

20-
func (c *CRS) MarshalYAML() (interface{}, error) {
21-
return codeSpace + ":" + strconv.Itoa(c.Code), nil
18+
func (c CRS) MarshalYAML() (interface{}, error) {
19+
return c.String(), nil
2220
}

pkg/wms130/boundingbox_yaml.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package wms130
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
// UnmarshalYAML Position
9+
func (p *Position) UnmarshalYAML(unmarshal func(interface{}) error) error {
10+
var s string
11+
if err := unmarshal(&s); err != nil {
12+
return err
13+
}
14+
15+
position := getPositionFromString(s)
16+
*p = Position{position[0], position[1]}
17+
18+
return nil
19+
}
20+
21+
// MarshalYAML Position
22+
func (p Position) MarshalYAML() (interface{}, error) {
23+
return fmt.Sprintf("%s %s", strconv.FormatFloat(p[0], 'f', -1, 64), strconv.FormatFloat(p[1], 'f', -1, 64)), nil
24+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package wms130
2+
3+
import (
4+
"testing"
5+
6+
"gopkg.in/yaml.v3"
7+
)
8+
9+
func TestUnmarshalYAMLPosition(t *testing.T) {
10+
11+
var tests = []struct {
12+
positionstring []byte
13+
expectedposition Position
14+
}{
15+
0: {positionstring: []byte(`2.52712538742158 50.2128625669452`), expectedposition: Position{2.52712538742158, 50.2128625669452}},
16+
1: {positionstring: []byte(`7.37402550506231 55.7211602557705`), expectedposition: Position{7.37402550506231, 55.7211602557705}},
17+
2: {positionstring: []byte(`7.37402550506231 55.7211602557705 0 1 2 3`), expectedposition: Position{7.37402550506231, 55.7211602557705}},
18+
}
19+
20+
for k, test := range tests {
21+
var pos Position
22+
err := yaml.Unmarshal(test.positionstring, &pos)
23+
if err != nil {
24+
t.Errorf("test: %d, yaml.UnMarshal failed with '%s'\n", k, err)
25+
} else {
26+
if pos != test.expectedposition {
27+
t.Errorf("test: %d, expected: %v+,\n got: %v+", k, test.expectedposition, pos)
28+
}
29+
}
30+
}
31+
}
32+
33+
func TestMarshalYAMLPosition(t *testing.T) {
34+
var tests = []struct {
35+
positionstring []byte
36+
position Position
37+
}{
38+
0: {positionstring: []byte("2.52712538742158 50.2128625669452\n"), position: Position{2.52712538742158, 50.2128625669452}},
39+
1: {positionstring: []byte("7.37402550506231 55.7211602557705\n"), position: Position{7.37402550506231, 55.7211602557705}},
40+
}
41+
42+
for k, test := range tests {
43+
pos, err := yaml.Marshal(test.position)
44+
if err != nil {
45+
t.Errorf("test: %d, yaml.Marshal failed with '%s'\n", k, err)
46+
} else {
47+
if string(pos) != string(test.positionstring) {
48+
t.Errorf("test: %d, expected: %s,\n got: %s", k, test.positionstring, pos)
49+
}
50+
}
51+
}
52+
}

pkg/wsc110/boundingbox_yaml.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package wsc110
22

3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
38
// UnmarshalYAML Position
49
func (p *Position) UnmarshalYAML(unmarshal func(interface{}) error) error {
510
var s string
@@ -12,3 +17,8 @@ func (p *Position) UnmarshalYAML(unmarshal func(interface{}) error) error {
1217

1318
return nil
1419
}
20+
21+
// MarshalYAML Position
22+
func (p Position) MarshalYAML() (interface{}, error) {
23+
return fmt.Sprintf("%s %s", strconv.FormatFloat(p[0], 'f', -1, 64), strconv.FormatFloat(p[1], 'f', -1, 64)), nil
24+
}

pkg/wsc110/boundingbox_yaml_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,24 @@ func TestUnmarshalYAMLPosition(t *testing.T) {
2929
}
3030
}
3131
}
32+
33+
func TestMarshalYAMLPosition(t *testing.T) {
34+
var tests = []struct {
35+
positionstring []byte
36+
position Position
37+
}{
38+
0: {positionstring: []byte("2.52712538742158 50.2128625669452\n"), position: Position{2.52712538742158, 50.2128625669452}},
39+
1: {positionstring: []byte("7.37402550506231 55.7211602557705\n"), position: Position{7.37402550506231, 55.7211602557705}},
40+
}
41+
42+
for k, test := range tests {
43+
pos, err := yaml.Marshal(test.position)
44+
if err != nil {
45+
t.Errorf("test: %d, yaml.Marshal failed with '%s'\n", k, err)
46+
} else {
47+
if string(pos) != string(test.positionstring) {
48+
t.Errorf("test: %d, expected: %s,\n got: %s", k, test.positionstring, pos)
49+
}
50+
}
51+
}
52+
}

pkg/wsc110/crs_yaml.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ func (c *CRS) UnmarshalYAML(unmarshal func(interface{}) error) error {
1414

1515
return nil
1616
}
17+
18+
func (c CRS) MarshalYAML() (interface{}, error) {
19+
return c.String(), nil
20+
}

0 commit comments

Comments
 (0)