Skip to content

Commit cfaa0c7

Browse files
committed
Move geo into geo directory
1 parent aa10674 commit cfaa0c7

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
lines changed

geo.go renamed to geo/geo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package mongo
1+
package geo
22

3-
type GeoJSON struct {
3+
type JSON struct {
44
Type string `json:"type,omitempty" bson:"type,omitempty"`
55
Coordinates []float64 `json:"coordinates,omitempty" bson:"coordinates,omitempty"`
66
}

point_mapper.go renamed to geo/point_mapper.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mongo
1+
package geo
22

33
import (
44
"context"
@@ -16,6 +16,51 @@ type PointMapper struct {
1616
bsonName string
1717
}
1818

19+
//For Get By Id
20+
func findFieldIndex(modelType reflect.Type, fieldName string) int {
21+
numField := modelType.NumField()
22+
for i := 0; i < numField; i++ {
23+
field := modelType.Field(i)
24+
if field.Name == fieldName {
25+
return i
26+
}
27+
}
28+
return -1
29+
}
30+
func getBsonName(modelType reflect.Type, fieldName string) string {
31+
field, found := modelType.FieldByName(fieldName)
32+
if !found {
33+
return fieldName
34+
}
35+
if tag, ok := field.Tag.Lookup("bson"); ok {
36+
return strings.Split(tag, ",")[0]
37+
}
38+
return fieldName
39+
}
40+
func getJsonByIndex(modelType reflect.Type, fieldIndex int) string {
41+
if tag, ok := modelType.Field(fieldIndex).Tag.Lookup("json"); ok {
42+
return strings.Split(tag, ",")[0]
43+
}
44+
return ""
45+
}
46+
func getBsonNameByIndex(modelType reflect.Type, fieldIndex int) string {
47+
if tag, ok := modelType.Field(fieldIndex).Tag.Lookup("bson"); ok {
48+
return strings.Split(tag, ",")[0]
49+
}
50+
return ""
51+
}
52+
func FindGeoIndex(modelType reflect.Type) int {
53+
numField := modelType.NumField()
54+
k := JSON{}
55+
for i := 0; i < numField; i++ {
56+
t := modelType.Field(i).Type
57+
if t == reflect.TypeOf(&k) || t == reflect.TypeOf(k) {
58+
return i
59+
}
60+
}
61+
return -1
62+
}
63+
1964
func NewMapper(modelType reflect.Type, options ...string) *PointMapper {
2065
var bsonName, latitudeName, longitudeName string
2166
if len(options) >= 1 && len(options[0]) > 0 {
@@ -31,11 +76,11 @@ func NewMapper(modelType reflect.Type, options ...string) *PointMapper {
3176
} else {
3277
longitudeName = "Longitude"
3378
}
34-
latitudeIndex := FindFieldIndex(modelType, latitudeName)
35-
longitudeIndex := FindFieldIndex(modelType, longitudeName)
79+
latitudeIndex := findFieldIndex(modelType, latitudeName)
80+
longitudeIndex := findFieldIndex(modelType, longitudeName)
3681
var bsonIndex int
3782
if len(bsonName) > 0 {
38-
bsonIndex = FindFieldIndex(modelType, bsonName)
83+
bsonIndex = findFieldIndex(modelType, bsonName)
3984
} else {
4085
bsonIndex = FindGeoIndex(modelType)
4186
}
@@ -80,9 +125,9 @@ func (s *PointMapper) DbToModels(ctx context.Context, model interface{}) (interf
80125
func (s *PointMapper) ModelToDb(ctx context.Context, model interface{}) (interface{}, error) {
81126
m, ok := model.(map[string]interface{})
82127
if ok {
83-
latJson := GetJsonByIndex(s.modelType, s.latitudeIndex)
84-
logJson := GetJsonByIndex(s.modelType, s.longitudeIndex)
85-
bs := GetBsonNameByIndex(s.modelType, s.bsonIndex)
128+
latJson := getJsonByIndex(s.modelType, s.latitudeIndex)
129+
logJson := getJsonByIndex(s.modelType, s.longitudeIndex)
130+
bs := getBsonNameByIndex(s.modelType, s.bsonIndex)
86131
m2 := FromPointMap(m, bs, latJson, logJson)
87132
return m2, nil
88133
}
@@ -174,15 +219,15 @@ func (s *PointMapper) bsonToPoint(value reflect.Value, bsonIndex int, latitudeIn
174219

175220
if value.Kind() == reflect.Map {
176221
var arrLatLongTag, latitudeTag, longitudeTag string
177-
if arrLatLongTag = GetBsonName(s.modelType, s.bsonName); arrLatLongTag == "" || arrLatLongTag == "-" {
222+
if arrLatLongTag = getBsonName(s.modelType, s.bsonName); arrLatLongTag == "" || arrLatLongTag == "-" {
178223
arrLatLongTag = getJsonName(s.modelType, s.bsonName)
179224
}
180225

181-
if latitudeTag = GetBsonName(s.modelType, s.latitudeName); latitudeTag == "" || latitudeTag == "-" {
226+
if latitudeTag = getBsonName(s.modelType, s.latitudeName); latitudeTag == "" || latitudeTag == "-" {
182227
latitudeTag = getJsonName(s.modelType, s.latitudeName)
183228
}
184229

185-
if longitudeTag = GetBsonName(s.modelType, s.longitudeName); longitudeTag == "" || longitudeTag == "-" {
230+
if longitudeTag = getBsonName(s.modelType, s.longitudeName); longitudeTag == "" || longitudeTag == "-" {
186231
longitudeTag = getJsonName(s.modelType, s.longitudeName)
187232
}
188233

@@ -206,7 +251,7 @@ func FromPointMap(m map[string]interface{}, bsonName string, latitudeJson string
206251
if ok3 && ok4 {
207252
var arr []float64
208253
arr = append(arr, la, lo)
209-
ml := GeoJSON{Type: "Point", Coordinates: arr}
254+
ml := JSON{Type: "Point", Coordinates: arr}
210255
m2 := make(map[string]interface{})
211256
m2[bsonName] = ml
212257
for key := range m {
@@ -247,7 +292,7 @@ func FromPoint(value reflect.Value, bsonIndex int, latitudeIndex int, longitudeI
247292
arr = append(arr, la, lo)
248293
coordinatesField := v.Field(bsonIndex)
249294
if coordinatesField.Kind() == reflect.Ptr {
250-
m := &GeoJSON{Type: "Point", Coordinates: arr}
295+
m := &JSON{Type: "Point", Coordinates: arr}
251296
coordinatesField.Set(reflect.ValueOf(m))
252297
} else {
253298
x := coordinatesField.FieldByName("Type")

mongo.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -792,18 +792,6 @@ func UpsertMaps(ctx context.Context, collection *mongo.Collection, maps []map[st
792792
return res, err
793793
}
794794

795-
func FindGeoIndex(modelType reflect.Type) int {
796-
numField := modelType.NumField()
797-
k := GeoJSON{}
798-
for i := 0; i < numField; i++ {
799-
t := modelType.Field(i).Type
800-
if t == reflect.TypeOf(&k) || t == reflect.TypeOf(k) {
801-
return i
802-
}
803-
}
804-
return -1
805-
}
806-
807795
//For Get By Id
808796
func FindFieldIndex(modelType reflect.Type, fieldName string) int {
809797
numField := modelType.NumField()

0 commit comments

Comments
 (0)