-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
287 lines (255 loc) · 5.61 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package mgoc
import (
"encoding/hex"
"fmt"
"github.com/civet148/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
bson2 "gopkg.in/mgo.v2/bson"
"time"
"regexp"
"strings"
)
const (
radianBase = float64(6359.0)
OfficalObjectIdSize = 24
MgoV2ObjectIdSize = 48
)
func NewObjectID() ObjectID {
return primitive.NewObjectID()
}
func NewObjectIDFromString(v string) (ObjectID, error) {
oid, err := primitive.ObjectIDFromHex(v)
if err != nil {
return oid, log.Errorf("new object id from string %s error [%s]", v, err)
}
return oid, nil
}
func NewObjectIDFromTimestamp(t time.Time) ObjectID {
return primitive.NewObjectIDFromTimestamp(t)
}
func NewDecimal128(high, low uint64) Decimal128 {
return primitive.NewDecimal128(high, low)
}
func NewDateTimeFromTime(t time.Time) DateTime {
return primitive.NewDateTimeFromTime(t)
}
func ConvertValue(column string, value interface{}) (v interface{}) {
if column == defaultPrimaryKeyName {
v = MakeObjectID(value)
} else {
v = value
}
return v
}
func MakeObjectID(v interface{}) (id interface{}) {
//log.Debugf("value type [%v]", reflect.TypeOf(v))
switch v.(type) {
case string:
{
id = Str2ObjectID(v.(string))
}
case primitive.ObjectID:
{
id = v
}
case bson2.ObjectId:
{
vid := v.(bson2.ObjectId)
hexid := vid.Hex()
if len(hexid) == 0 {
return nil
}
id = Str2ObjectID(vid.Hex())
}
default:
id = v
}
return
}
func Str2ObjectID(strId string) (id interface{}) {
//log.Debugf("id [%v]", strId)
if len(strId) == 0 {
return nil
}
if len(strId) == OfficalObjectIdSize { //mongo-driver MakeObjectID
oid, err := primitive.ObjectIDFromHex(strId)
if err != nil {
return strId
}
//log.Debugf("mongo-driver id [%v]", oid)
return oid
} else if len(strId) == MgoV2ObjectIdSize { //mgo.v2 ObjectId
hexid, err := hex.DecodeString(strId)
if err != nil {
return strId
}
oid, err := primitive.ObjectIDFromHex(string(hexid))
if err != nil {
return strId
}
//log.Debugf("mgo.v2 id [%v]", oid)
return oid
}
return strId
}
func All(expr interface{}) bson.M {
return bson.M{
KeyAll: expr,
}
}
func Sum(expr interface{}) bson.M {
return bson.M{
KeySum: expr,
}
}
func ToBool(expr interface{}) bson.M {
return bson.M{
toBool: expr,
}
}
func ToDecimal(expr interface{}) bson.M {
return bson.M{
toDecimal: expr,
}
}
func ToDouble(expr interface{}) bson.M {
return bson.M{
toDouble: expr,
}
}
func ToInt(expr interface{}) bson.M {
return bson.M{
toInt: expr,
}
}
func ToLong(expr interface{}) bson.M {
return bson.M{
toLong: expr,
}
}
func ToDate(expr interface{}) bson.M {
return bson.M{
toDate: expr,
}
}
func ToString(expr interface{}) bson.M {
return bson.M{
toString: expr,
}
}
func ToObjectId(expr interface{}) bson.M {
return bson.M{
toObjectId: expr,
}
}
func ToLower(expr interface{}) bson.M {
return bson.M{
toLower: expr,
}
}
func ToUpper(expr interface{}) bson.M {
return bson.M{
toUpper: expr,
}
}
func RoundColumn(strColumn string, place int) bson.M {
return bson.M{
KeyRound: bson.A{
fmt.Sprintf("$%s", strColumn),
place,
},
}
}
//计算弧度
func Radian(meters uint64) float64 {
r := float64(meters) / 1000
return r / radianBase
}
func NewGeoPoint(coord Coordinate) *GeoPoint {
return &GeoPoint{
Type: GeoTypePoint,
Coordinates: []float64{coord.X, coord.Y},
}
}
func NewGeoMultiPoint(coords []Coordinate) *GeoMultiPoint {
var coordinates FloatArray2
for _, coord := range coords {
coordinates = append(coordinates, FloatArray{coord.X, coord.Y})
}
return &GeoMultiPoint{
Type: GeoTypeMultiPoint,
Coordinates: coordinates,
}
}
func NewGeoLineString(coords []Coordinate) *GeoLineString {
var coordinates FloatArray2
for _, coord := range coords {
coordinates = append(coordinates, FloatArray{coord.X, coord.Y})
}
return &GeoLineString{
Type: GeoTypeLineString,
Coordinates: coordinates,
}
}
func NewGeoMultiLineString(coords [][]Coordinate) *GeoMultiLineString {
var coordinates FloatArray3
for _, coord := range coords {
var cs FloatArray2
for _, v := range coord {
cs = append(cs, FloatArray{v.X, v.Y})
}
coordinates = append(coordinates, cs)
}
return &GeoMultiLineString{
Type: GeoTypeMultiLineString,
Coordinates: coordinates,
}
}
func NewGeoPolygon(coords [][]Coordinate) *GeoPolygon {
var coordinates FloatArray3
for _, coord := range coords {
var cs FloatArray2
for _, v := range coord {
cs = append(cs, FloatArray{v.X, v.Y})
}
coordinates = append(coordinates, cs)
}
return &GeoPolygon{
Type: GeoTypePolygon,
Coordinates: coordinates,
}
}
func NewGeoMultiPolygon(coords [][][]Coordinate) *GeoMultiPolygon {
var coordinates FloatArray4
for _, coord := range coords {
var cs FloatArray3
for _, v := range coord {
var cs2 FloatArray2
for _, v2 := range v {
cs2 = append(cs2, FloatArray{v2.X, v2.Y})
}
cs = append(cs, cs2)
}
coordinates = append(coordinates, cs)
}
return &GeoMultiPolygon{
Type: GeoTypeMultiPolygon,
Coordinates: coordinates,
}
}
func NewGeoMetry(typ GeoType, coordinates interface{}) *Geometry {
return &Geometry{
Type: typ,
Coordinates: coordinates,
}
}
// convertCamelToSnake converts a CamelCase string to snake_case
func convertCamelToSnake(s string) string {
// 使用正则表达式匹配大写字母前的所有字符
re := regexp.MustCompile("([A-Z])")
// 将匹配到的大写字母替换为下划线加小写字母
snake := re.ReplaceAllString(s, "_$1")
// 将字符串转换为小写并去掉开头的下划线
return strings.ToLower(strings.TrimPrefix(snake, "_"))
}