Skip to content

Commit

Permalink
Add AsFoo() (Foo, bool) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Oct 29, 2021
1 parent 8721852 commit b31e1d0
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
68 changes: 68 additions & 0 deletions geom/type_geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,74 @@ func (g Geometry) MustAsMultiPolygon() MultiPolygon {
return *(*MultiPolygon)(g.ptr)
}

// AsGeometryCollection checks if the geometry is a GeometryCollection, and
// returns it as a GeometryCollection if it is. The returned flag indicates if
// the conversion was successful.
func (g Geometry) AsGeometryCollection() (GeometryCollection, bool) {
if !g.IsGeometryCollection() {
return GeometryCollection{}, false
}
return g.MustAsGeometryCollection(), true
}

// AsPoint checks if the geometry is a Point, and returns it as a Point if it
// is. The returned flag indicates if the conversion was successful.
func (g Geometry) AsPoint() (Point, bool) {
if !g.IsPoint() {
return Point{}, false
}
return g.MustAsPoint(), true
}

// AsLineString checks if the geometry is a LineString, and returns it as a
// LineString if it is. The returned flag indicates if the conversion was
// successful.
func (g Geometry) AsLineString() (LineString, bool) {
if !g.IsLineString() {
return LineString{}, false
}
return g.MustAsLineString(), true
}

// AsPolygon checks if the geometry is a Polygon, and returns it as a Polygon
// if it is. The returned flag indicates if the conversion was successful.
func (g Geometry) AsPolygon() (Polygon, bool) {
if !g.IsPolygon() {
return Polygon{}, false
}
return g.MustAsPolygon(), true
}

// AsMultiPoint checks if the geometry is a MultiPoint, and returns it as a
// MultiPoint if it is. The returned flag indicates if the conversion was
// successful.
func (g Geometry) AsMultiPoint() (MultiPoint, bool) {
if !g.IsMultiPoint() {
return MultiPoint{}, false
}
return g.MustAsMultiPoint(), true
}

// AsMultiLineString checks if the geometry is a MultiLineString, and returns
// it as a MultiLineString if it is. The returned flag indicates if the
// conversion was successful.
func (g Geometry) AsMultiLineString() (MultiLineString, bool) {
if !g.IsMultiLineString() {
return MultiLineString{}, false
}
return g.MustAsMultiLineString(), true
}

// AsMultiPolygon checks if the geometry is a MultiPolygon, and returns it as a
// MultiPolygon if it is. The returned flag indicates if the conversion was
// successful.
func (g Geometry) AsMultiPolygon() (MultiPolygon, bool) {
if !g.IsMultiPolygon() {
return MultiPolygon{}, false
}
return g.MustAsMultiPolygon(), true
}

// AsText returns the WKT (Well Known Text) representation of this geometry.
func (g Geometry) AsText() string {
switch g.gtype {
Expand Down
82 changes: 82 additions & 0 deletions geom/type_geometry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func TestZeroGeometry(t *testing.T) {
expectBoolEq(t, z.IsGeometryCollection(), true)
z.MustAsGeometryCollection() // Doesn't crash.
expectStringEq(t, z.AsText(), "GEOMETRYCOLLECTION EMPTY")
gc, ok := z.AsGeometryCollection()
expectTrue(t, ok)
expectIntEq(t, gc.NumGeometries(), 0)

var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(z)
Expand Down Expand Up @@ -92,3 +95,82 @@ func TestGeometryTypeString(t *testing.T) {
})
}
}

func TestAsConcreteType(t *testing.T) {
for _, wkt := range []string{
"GEOMETRYCOLLECTION(POINT(1 2))",
"POINT(1 2)",
"LINESTRING(1 2,3 4)",
"POLYGON((0 0,0 1,1 0,0 0))",
"MULTIPOINT((1 2))",
"MULTILINESTRING((1 2,3 4))",
"MULTIPOLYGON(((0 0,0 1,1 0,0 0)))",
} {
t.Run(wkt, func(t *testing.T) {
g := geomFromWKT(t, wkt)

if g.IsGeometryCollection() {
concrete, ok := g.AsGeometryCollection()
expectTrue(t, ok)
expectFalse(t, concrete.IsEmpty())
} else {
_, ok := g.AsGeometryCollection()
expectFalse(t, ok)
}

if g.IsPoint() {
concrete, ok := g.AsPoint()
expectTrue(t, ok)
expectFalse(t, concrete.IsEmpty())
} else {
_, ok := g.AsPoint()
expectFalse(t, ok)
}

if g.IsLineString() {
concrete, ok := g.AsLineString()
expectTrue(t, ok)
expectFalse(t, concrete.IsEmpty())
} else {
_, ok := g.AsLineString()
expectFalse(t, ok)
}

if g.IsPolygon() {
concrete, ok := g.AsPolygon()
expectTrue(t, ok)
expectFalse(t, concrete.IsEmpty())
} else {
_, ok := g.AsPolygon()
expectFalse(t, ok)
}

if g.IsMultiPoint() {
concrete, ok := g.AsMultiPoint()
expectTrue(t, ok)
expectFalse(t, concrete.IsEmpty())
} else {
_, ok := g.AsMultiPoint()
expectFalse(t, ok)
}

if g.IsMultiLineString() {
concrete, ok := g.AsMultiLineString()
expectTrue(t, ok)
expectFalse(t, concrete.IsEmpty())
} else {
_, ok := g.AsMultiLineString()
expectFalse(t, ok)
}

if g.IsMultiPolygon() {
concrete, ok := g.AsMultiPolygon()
expectTrue(t, ok)
expectFalse(t, concrete.IsEmpty())
} else {
_, ok := g.AsMultiPolygon()
expectFalse(t, ok)
}
})
}
}

0 comments on commit b31e1d0

Please sign in to comment.