1
- package mongo
1
+ package geo
2
2
3
3
import (
4
4
"context"
@@ -16,6 +16,51 @@ type PointMapper struct {
16
16
bsonName string
17
17
}
18
18
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
+
19
64
func NewMapper (modelType reflect.Type , options ... string ) * PointMapper {
20
65
var bsonName , latitudeName , longitudeName string
21
66
if len (options ) >= 1 && len (options [0 ]) > 0 {
@@ -31,11 +76,11 @@ func NewMapper(modelType reflect.Type, options ...string) *PointMapper {
31
76
} else {
32
77
longitudeName = "Longitude"
33
78
}
34
- latitudeIndex := FindFieldIndex (modelType , latitudeName )
35
- longitudeIndex := FindFieldIndex (modelType , longitudeName )
79
+ latitudeIndex := findFieldIndex (modelType , latitudeName )
80
+ longitudeIndex := findFieldIndex (modelType , longitudeName )
36
81
var bsonIndex int
37
82
if len (bsonName ) > 0 {
38
- bsonIndex = FindFieldIndex (modelType , bsonName )
83
+ bsonIndex = findFieldIndex (modelType , bsonName )
39
84
} else {
40
85
bsonIndex = FindGeoIndex (modelType )
41
86
}
@@ -80,9 +125,9 @@ func (s *PointMapper) DbToModels(ctx context.Context, model interface{}) (interf
80
125
func (s * PointMapper ) ModelToDb (ctx context.Context , model interface {}) (interface {}, error ) {
81
126
m , ok := model .(map [string ]interface {})
82
127
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 )
86
131
m2 := FromPointMap (m , bs , latJson , logJson )
87
132
return m2 , nil
88
133
}
@@ -174,15 +219,15 @@ func (s *PointMapper) bsonToPoint(value reflect.Value, bsonIndex int, latitudeIn
174
219
175
220
if value .Kind () == reflect .Map {
176
221
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 == "-" {
178
223
arrLatLongTag = getJsonName (s .modelType , s .bsonName )
179
224
}
180
225
181
- if latitudeTag = GetBsonName (s .modelType , s .latitudeName ); latitudeTag == "" || latitudeTag == "-" {
226
+ if latitudeTag = getBsonName (s .modelType , s .latitudeName ); latitudeTag == "" || latitudeTag == "-" {
182
227
latitudeTag = getJsonName (s .modelType , s .latitudeName )
183
228
}
184
229
185
- if longitudeTag = GetBsonName (s .modelType , s .longitudeName ); longitudeTag == "" || longitudeTag == "-" {
230
+ if longitudeTag = getBsonName (s .modelType , s .longitudeName ); longitudeTag == "" || longitudeTag == "-" {
186
231
longitudeTag = getJsonName (s .modelType , s .longitudeName )
187
232
}
188
233
@@ -206,7 +251,7 @@ func FromPointMap(m map[string]interface{}, bsonName string, latitudeJson string
206
251
if ok3 && ok4 {
207
252
var arr []float64
208
253
arr = append (arr , la , lo )
209
- ml := GeoJSON {Type : "Point" , Coordinates : arr }
254
+ ml := JSON {Type : "Point" , Coordinates : arr }
210
255
m2 := make (map [string ]interface {})
211
256
m2 [bsonName ] = ml
212
257
for key := range m {
@@ -247,7 +292,7 @@ func FromPoint(value reflect.Value, bsonIndex int, latitudeIndex int, longitudeI
247
292
arr = append (arr , la , lo )
248
293
coordinatesField := v .Field (bsonIndex )
249
294
if coordinatesField .Kind () == reflect .Ptr {
250
- m := & GeoJSON {Type : "Point" , Coordinates : arr }
295
+ m := & JSON {Type : "Point" , Coordinates : arr }
251
296
coordinatesField .Set (reflect .ValueOf (m ))
252
297
} else {
253
298
x := coordinatesField .FieldByName ("Type" )
0 commit comments