Skip to content

Commit ae1630b

Browse files
committed
docs: add comment and example for Geometry.String;
refactor: rename var for Geometry methods
1 parent 27b0204 commit ae1630b

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

sql.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type (
3636
func New[T geom.T](geom T) Geometry[T] { return Geometry[T]{geom} }
3737

3838
// Scan impl sql.Scanner
39-
func (p *Geometry[T]) Scan(value interface{}) (err error) {
39+
func (g *Geometry[T]) Scan(value interface{}) (err error) {
4040
var (
4141
wkb []byte
4242
ok bool
@@ -60,7 +60,7 @@ func (p *Geometry[T]) Scan(value interface{}) (err error) {
6060
return err
6161
}
6262

63-
p.Geom, ok = geometryT.(T)
63+
g.Geom, ok = geometryT.(T)
6464
if !ok {
6565
return ErrUnexpectedValueType
6666
}
@@ -69,24 +69,24 @@ func (p *Geometry[T]) Scan(value interface{}) (err error) {
6969
}
7070

7171
// Value impl driver.Valuer
72-
func (p Geometry[T]) Value() (driver.Value, error) {
73-
if geom.T(p.Geom) == nil {
72+
func (g Geometry[T]) Value() (driver.Value, error) {
73+
if geom.T(g.Geom) == nil {
7474
return nil, nil
7575
}
7676

7777
sb := &bytes.Buffer{}
78-
if err := ewkb.Write(sb, binary.LittleEndian, p.Geom); err != nil {
78+
if err := ewkb.Write(sb, binary.LittleEndian, g.Geom); err != nil {
7979
return nil, err
8080
}
8181

8282
return hex.EncodeToString(sb.Bytes()), nil
8383
}
8484

8585
// GormDataType impl schema.GormDataTypeInterface
86-
func (p Geometry[T]) GormDataType() string {
86+
func (g Geometry[T]) GormDataType() string {
8787
srid := strconv.Itoa(SRID)
8888

89-
switch any(p.Geom).(type) {
89+
switch any(g.Geom).(type) {
9090
case *geom.Point:
9191
return "Geometry(Point, " + srid + ")"
9292
case *geom.LineString:
@@ -106,10 +106,11 @@ func (p Geometry[T]) GormDataType() string {
106106
}
107107
}
108108

109-
func (p Geometry[T]) String() string {
110-
if geomWkt, err := wkt.Marshal(p.Geom); err == nil {
109+
// String returns geometry formatted using WKT format
110+
func (g Geometry[T]) String() string {
111+
if geomWkt, err := wkt.Marshal(g.Geom); err == nil {
111112
return geomWkt
112113
}
113114

114-
return fmt.Sprintf("cannot marshal geometry: %T", p.Geom)
115+
return fmt.Sprintf("cannot marshal geometry: %T", g.Geom)
115116
}

sql_test.go

+11-16
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,20 @@ import (
1212
"github.com/twpayne/go-geom/encoding/wkbcommon"
1313
)
1414

15-
func ExamplePolygon_String() {
16-
fmt.Println(Polygon{
17-
Geom: geom.NewPolygon(geom.XY).MustSetCoords(
18-
[][]geom.Coord{
19-
{{42, 42}, {1, 1}, {2, 2}, {42, 42}},
20-
}),
21-
}.String())
22-
23-
// Output: POLYGON ((42 42, 1 1, 2 2, 42 42))
24-
}
15+
func ExampleGeometry_String() {
16+
point := Geometry[geom.T]{
17+
Geom: geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{42, 42}),
18+
}
2519

26-
func ExamplePoint_String() {
27-
fmt.Println(Point{
28-
Geom: geom.NewPoint(geom.XY).MustSetCoords(
29-
geom.Coord{42, 42},
30-
).SetSRID(4326),
31-
}.String())
20+
polygon := Geometry[geom.T]{
21+
Geom: geom.NewPolygon(geom.XY).MustSetCoords(
22+
[][]geom.Coord{{{42, 42}, {1, 1}, {2, 2}, {42, 42}}}),
23+
}
3224

25+
fmt.Println(point.String())
26+
fmt.Println(polygon.String())
3327
// Output: POINT (42 42)
28+
// POLYGON ((42 42, 1 1, 2 2, 42 42))
3429
}
3530

3631
func TestGeometryStringExpectCannotMarshal(t *testing.T) {

0 commit comments

Comments
 (0)