# HG changeset patch # User Sascha L. Teichmann # Date 1536804696 -7200 # Node ID 7a867e48b3c245b5814a5e25ddeb33a609de56f8 # Parent 729c2692fea0c6a18defdde246f4135b63f76e82 Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings. diff -r 729c2692fea0 -r 7a867e48b3c2 pkg/models/cross.go --- a/pkg/models/cross.go Wed Sep 12 19:55:00 2018 +0200 +++ b/pkg/models/cross.go Thu Sep 13 04:11:36 2018 +0200 @@ -26,10 +26,12 @@ Date GeoJSONDate `json:"date"` } + GeoJSONLineCoordinates []GeoJSONCoordinate + CrossSectionInput struct { Type GeoJSONFeature `json:"type"` Geometry GeoJSONLineString `json:"geometry"` - Coordinates []GeoJSONCoordinate `json:"coordinates"` + Coordinates GeoJSONLineCoordinates `json:"coordinates"` Properties CrossSectionInputProperties `json:"properties"` } ) @@ -37,8 +39,22 @@ var ( errNoGeoJSONFeature = errors.New("Not a valid GeoJSON feature") errNoGeoJSONLineStringType = errors.New("Not a valid GeoJSON linestring type") + errTooLessComponents = errors.New("Too less components in coordinate") + errTooLessCoordinates = errors.New("Too less coordinates") ) +func (lc *GeoJSONLineCoordinates) UnmarshalJSON(data []byte) error { + var coords []GeoJSONCoordinate + if err := json.Unmarshal(data, &coords); err != nil { + return err + } + if len(coords) < 2 { + return errTooLessCoordinates + } + *lc = GeoJSONLineCoordinates(coords) + return nil +} + func (d *GeoJSONDate) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { @@ -53,10 +69,13 @@ } func (c *GeoJSONCoordinate) UnmarshalJSON(data []byte) error { - pos := make([]float64, 2) + var pos []float64 if err := json.Unmarshal(data, &pos); err != nil { return err } + if len(pos) < 2 { + return errTooLessComponents + } *c = GeoJSONCoordinate{Lon: pos[0], Lat: pos[1]} return nil }