Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ cmd/mapbox/
- Testing: standard `testing` + `github.com/google/go-cmp/cmp` only. No
Testify or other frameworks.
- Linting: GolangCI-Lint v2, configured in `.golangci.yml`.
- Commit subject: max 60 characters (enforced by commitlint in CI). Conventional commit prefix counts toward the limit.
- Commit subject must not be sentence-case: first word after the prefix must be lowercase (`fix(x): add foo`, not `fix(x): Add foo`).

## Retry

Expand Down
8 changes: 7 additions & 1 deletion geocoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ type FeatureCollection struct {
Features []Feature `json:"features"`
}

// PointGeometry is a GeoJSON Point geometry. Coordinates holds [longitude, latitude].
type PointGeometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
}

// Feature is a GeoJSON Feature with Mapbox v6 geocoding properties.
type Feature struct {
Type string `json:"type"`
ID string `json:"id"`
Geometry GeoJSONGeometry `json:"geometry"`
Geometry PointGeometry `json:"geometry"`
Properties FeatureProperties `json:"properties"`
}

Expand Down
43 changes: 43 additions & 0 deletions geocoding_reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,49 @@ func TestReverseGeocode_ContextParsing(t *testing.T) {
}
}

func TestReverseGeocode_PointGeometry(t *testing.T) {
// Mapbox returns Point geometry with flat [lon, lat] coordinates.
// Regression: GeoJSONGeometry used [][]float64 (LineString), causing decode to fail.
const body = `{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [24.9384, 60.1699]},
"properties": {
"mapbox_id": "addr.1",
"feature_type": "address",
"full_address": "Mannerheimintie 1, 00100 Helsinki, Finland"
}
}]
}`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte(body)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

client := mapbox.NewClient(mapbox.WithAccessToken("t"), mapbox.WithBaseURL(srv.URL))
result, err := client.ReverseGeocode(context.Background(), &mapbox.ReverseGeocodeRequest{
Longitude: 24.9384,
Latitude: 60.1699,
})
if err != nil {
t.Fatalf("ReverseGeocode error: %v", err)
}
if len(result.Features) != 1 {
t.Fatalf("len(Features) = %d, want 1", len(result.Features))
}
f := result.Features[0]
if f.Geometry.Type != "Point" {
t.Errorf("Geometry.Type = %q, want Point", f.Geometry.Type)
}
if len(f.Geometry.Coordinates) != 2 {
t.Errorf("Geometry.Coordinates len = %d, want 2", len(f.Geometry.Coordinates))
}
}

func TestReverseGeocode_HTTPError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"message":"Not Authorized - Invalid Token"}`, http.StatusUnauthorized)
Expand Down
2 changes: 1 addition & 1 deletion searchbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Suggestion struct {
// RetrieveFeature is a GeoJSON Feature returned by /retrieve.
type RetrieveFeature struct {
Type string `json:"type"`
Geometry GeoJSONGeometry `json:"geometry"`
Geometry PointGeometry `json:"geometry"`
Properties RetrieveFeatureProperties `json:"properties"`
}

Expand Down