Remove switches in Geometry type dispatch
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 30/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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())
}
}
- Lingua principale
- Go
- Stelle
- 176
- Fork
- 27
- Merge medio
- 1g 14h
- PR unite (30g)
- 4
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Nessuna guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di peterstace/simplefeatures
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
peterstace/simplefeatures#726 ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
peterstace/simplefeatures#725 ·
-
bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
peterstace/simplefeatures#724 ·
-
cleanup
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
peterstace/simplefeatures#722 ·
-
bug
Difficoltà 3/5 Mezza giornata Idoneità per principianti 45/100
peterstace/simplefeatures#721 ·
Tutte le issue di peterstace/simplefeatures
Issue simili
-
【文档错误】创建镜像空间步骤中的截图标注与文字描述不一致Apertaenhancement
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 62/100
DaoCloud/DaoCloud-docs#7839 ·
I maintainer di solito rispondono entro 1 giorno
-
Type: Enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
projectdiscovery/urlfinder#167 ·
I maintainer di solito rispondono entro 1 giorno
-
area: import bug go good first issue priority: low
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
fukubie/StockLlama#69 ·
-
enhancement help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
cloverworks-hq/rumpty-cli#95 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
I maintainer di solito rispondono entro 10 giorni