-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from PDOK/PDOK-13218-validate-xsd
Fixes for validation errors.
- Loading branch information
Showing
11 changed files
with
204 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package wfs200 | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
// | ||
const ( | ||
codeSpace = `urn:ogc:def:crs:EPSG:` | ||
) | ||
|
||
// CRS struct with namespace/authority/registry and code | ||
type CRS struct { | ||
Namespace string //TODO maybe AuthorityType is a better name...? | ||
Code int | ||
} | ||
|
||
// String of the EPSGCode | ||
func (c *CRS) String() string { | ||
return c.Namespace + `:` + strconv.Itoa(c.Code) | ||
} | ||
|
||
// Identifier returns the EPSG | ||
func (c *CRS) Identifier() string { | ||
return codeSpace + strconv.Itoa(c.Code) | ||
} | ||
|
||
// ParseString build CRS struct from input string | ||
func (c *CRS) ParseString(s string) { | ||
c.parseString(s) | ||
} | ||
|
||
func (c *CRS) parseString(s string) { | ||
regex := regexp.MustCompile(`(^.*):([0-9]+)`) | ||
code := regex.FindStringSubmatch(s) | ||
if len(code) == 3 { // code[0] is the full match, the other the parts | ||
c.Namespace = codeSpace | ||
|
||
// the regex already checks if it [0-9] | ||
i, _ := strconv.Atoi(code[2]) | ||
c.Code = i | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package wfs200 | ||
|
||
import ( | ||
"testing" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestCRSParseString(t *testing.T) { | ||
var tests = []struct { | ||
input string | ||
expectedCRS CRS | ||
}{ | ||
0: {}, // Empty input == empty struct | ||
1: {input: `urn:ogc:def:crs:EPSG::4326`, expectedCRS: CRS{Code: 4326, Namespace: `urn:ogc:def:crs:EPSG:`}}, | ||
2: {input: `EPSG:4326`, expectedCRS: CRS{Code: 4326, Namespace: `urn:ogc:def:crs:EPSG:`}}, | ||
} | ||
|
||
for k, test := range tests { | ||
var crs CRS | ||
crs.parseString(test.input) | ||
|
||
if crs.Code != test.expectedCRS.Code || crs.Namespace != test.expectedCRS.Namespace { | ||
t.Errorf("test: %d, expected: %v,\n got: %v", k, test.expectedCRS, crs) | ||
} | ||
} | ||
} | ||
|
||
func TestUnmarshalYAMLCrs(t *testing.T) { | ||
|
||
var stringYAML = `defaultcrs: urn:ogc:def:crs:EPSG::4326 | ||
othercrs: | ||
- EPSG:4258 | ||
- urn:ogc:def:crs:EPSG::3857` | ||
|
||
type FeatureType struct { | ||
DefaultCRS CRS `yaml:"defaultcrs"` | ||
OtherCRS []CRS `yaml:"othercrs"` | ||
} | ||
|
||
var tests = []struct { | ||
yaml []byte | ||
expectedcrs CRS | ||
}{ | ||
0: {yaml: []byte(stringYAML), expectedcrs: CRS{Code: 4326, Namespace: codeSpace}}, | ||
1: {yaml: []byte(`defaultcrs: urn:ogc:def:crs:EPSG::4326`), expectedcrs: CRS{Code: 4326, Namespace: codeSpace}}, | ||
2: {yaml: []byte(`defaultcrs: EPSG:4326`), expectedcrs: CRS{Code: 4326, Namespace: codeSpace}}, | ||
} | ||
for k, test := range tests { | ||
var ftl FeatureType | ||
err := yaml.Unmarshal(test.yaml, &ftl) | ||
if err != nil { | ||
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package wfs200 | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
) | ||
|
||
// MarshalXML Position | ||
func (c *CRS) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | ||
var s = `` | ||
if c.Namespace != `` { | ||
s = fmt.Sprintf("%s:%d", c.Namespace, c.Code) | ||
} | ||
|
||
return e.EncodeElement(s, start) | ||
} | ||
|
||
// UnmarshalXML Position | ||
func (c *CRS) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { | ||
var crs CRS | ||
for { | ||
token, err := d.Token() | ||
if err != nil { | ||
return err | ||
} | ||
switch el := token.(type) { | ||
case xml.CharData: | ||
crs.parseString(string([]byte(el))) | ||
case xml.EndElement: | ||
if el == start.End() { | ||
*c = crs | ||
return nil | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package wfs200 | ||
|
||
// UnmarshalYAML CRS | ||
func (c *CRS) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var s string | ||
if err := unmarshal(&s); err != nil { | ||
return err | ||
} | ||
|
||
var crs CRS | ||
crs.parseString(s) | ||
|
||
*c = crs | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters