added types.GeoPoint.AsMap method

This commit is contained in:
Gani Georgiev 2025-04-15 21:43:08 +03:00
parent c0fe600d14
commit 0876413d87
2 changed files with 43 additions and 0 deletions

View File

@ -22,6 +22,15 @@ func (p GeoPoint) String() string {
return string(raw)
}
// AsMap implements [core.mapExtractor] and returns a value suitable
// to be used in an API rule expression.
func (p GeoPoint) AsMap() map[string]any {
return map[string]any{
"lon": p.Lon,
"lat": p.Lat,
}
}
// Value implements the [driver.Valuer] interface.
func (p GeoPoint) Value() (driver.Value, error) {
data, err := json.Marshal(p)

View File

@ -7,6 +7,40 @@ import (
"github.com/pocketbase/pocketbase/tools/types"
)
func TestGeoPointAsMap(t *testing.T) {
t.Parallel()
scenarios := []struct {
name string
point types.GeoPoint
expected map[string]any
}{
{"zero", types.GeoPoint{}, map[string]any{"lon": 0.0, "lat": 0.0}},
{"non-zero", types.GeoPoint{Lon: -10, Lat: 20.123}, map[string]any{"lon": -10.0, "lat": 20.123}},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
result := s.point.AsMap()
if len(result) != len(s.expected) {
t.Fatalf("Expected %d keys, got %d: %v", len(s.expected), len(result), result)
}
for k, v := range s.expected {
found, ok := result[k]
if !ok {
t.Fatalf("Missing expected %q key: %v", k, result)
}
if found != v {
t.Fatalf("Expected %q key value %v, got %v", k, v, found)
}
}
})
}
}
func TestGeoPointStringAndValue(t *testing.T) {
t.Parallel()