Remove switches in Geometry type dispatch
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
Start by reading type_geometry.go and tracing the geometry type dispatch used by Geometry.AsText; the issue also sketches a reflection based cast helper as one possible approach. Compare the other repeated switches and consider the tradeoffs before choosing a design. Done means the repeated dispatch is addressed consistently across the geometry methods, with tests covering every geometry type.
Written by the indexing model from the issue text.
Description
In type_geometry.go, there are lots of places where we perform a switch statement on the geometry type, grab out the concrete geometry, and then perform some action on it. Usually, the action is the same for each of the types. For example:
// AsText returns the WKT (Well Known Text) representation of this geometry.
func (g Geometry) AsText() string {
switch g.gtype {
case TypeGeometryCollection:
return g.AsGeometryCollection().AsText()
case TypePoint:
return g.AsPoint().AsText()
case TypeLineString:
return g.AsLineString().AsText()
case TypePolygon:
return g.AsPolygon().AsText()
case TypeMultiPoint:
return g.AsMultiPoint().AsText()
case TypeMultiLineString:
return g.AsMultiLineString().AsText()
case TypeMultiPolygon:
return g.AsMultiPolygon().AsText()
default:
panic("unknown geometry: " + g.gtype.String())
}
}
Having this switch statement everywhere isn't ideal:
- It's error prone. If we forget a case, then it will panic for the missing type. We currently rely on unit testing to prevent this.
- It's fairly verbose, and just "one more thing" that's needed to be coded up when implementing a new method.
- It's a fairly major code smell.
I'm not too sure what the ideal solution should be... Here's one idea though: We can create a helper that tries to wrap a single interface around all geometry types. The helper then does the switch for us. While the switch statement is still there, it would be shared across all instances of the switch pattern (so we'd only have a single switch, instead of over a dozen). The downside of this approach is that it's much less explicit, and a little bit more magical. It would look something like:
// AsText returns the WKT (Well Known Text) representation of this geometry.
func (g Geometry) AsText() string {
var iface interface {
AsText() string
}
g.cast(&iface)
return iface.AsText()
}
// cast tries to store the geometry into a particular interface. The iface
// parameter should be a pointer to the interface value that is attempting to
// be casted to. It panics if it's unable to assign the concrete geometry type
// to the interface.
func (g Geometry) cast(ptrToInterface interface{}) {
ptrVal := reflect.ValueOf(ptrToInterface)
interfaceVal := ptrVal.Elem()
switch g.gtype {
case TypeGeometryCollection:
interfaceVal.Set(reflect.ValueOf(g.AsGeometryCollection()))
case TypePoint:
interfaceVal.Set(reflect.ValueOf(g.AsPoint()))
case TypeLineString:
interfaceVal.Set(reflect.ValueOf(g.AsLineString()))
case TypePolygon:
interfaceVal.Set(reflect.ValueOf(g.AsPolygon()))
case TypeMultiPoint:
interfaceVal.Set(reflect.ValueOf(g.AsMultiPoint()))
case TypeMultiLineString:
interfaceVal.Set(reflect.ValueOf(g.AsMultiLineString()))
case TypeMultiPolygon:
interfaceVal.Set(reflect.ValueOf(g.AsMultiPolygon()))
default:
panic("unknown geometry: " + g.gtype.String())
}
}
- Dominant language
- Go
- Stars
- 176
- Forks
- 27
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 6
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from peterstace/simplefeatures
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
peterstace/simplefeatures#726 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
peterstace/simplefeatures#725 ·
-
cleanup
Difficulty 3/5 Half a day Newbie friendliness 72/100
peterstace/simplefeatures#729 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 58/100
peterstace/simplefeatures#724 ·
-
cleanup
Difficulty 3/5 1-2 days Newbie friendliness 35/100
peterstace/simplefeatures#722 ·
All issues in peterstace/simplefeatures
Similar issues
-
bug raised-by:reviewer
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
medici-finance/assay#7655 ·
Maintainers usually reply within 1 day
-
helm chart: make DaemonSet pod affinity configurable via values.yamlPossibly taken @Young-Zen claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
flannel-io/flannel#2632 ·
Maintainers usually reply within 6 days
-
go-vulnerability security-scan
Difficulty 1/5 Under an hour Newbie friendliness 72/100
teslashibe/agent-go#36 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nwverse/persona#15194 · 1 comment ·
Maintainers usually reply within 1 day
-
bug flaky-test
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
rosskukulinski/tailworks#411 ·
Maintainers usually reply within 1 day