common.go
920 字节
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
package common
import (
"github.com/linmadan/egglib-go/utils/string_convert"
"reflect"
)
func SimpleStructToMap(toMapStruct interface{}) map[string]interface{} {
m := ObjectToMap(toMapStruct)
if pageNumber, ok := m["pageNumber"]; ok {
var pageSize int64
if _, ok := m["pageSize"]; ok {
pageSize = m["pageSize"].(int64)
} else {
pageSize = 20
}
m["offset"] = (pageNumber.(int64) - 1) * pageSize
m["limit"] = pageSize
}
return m
}
func ObjectToMap(o interface{}) map[string]interface{} {
if o == nil {
return nil
}
value := reflect.ValueOf(o)
if value.Kind() != reflect.Ptr {
return nil
}
elem := value.Elem()
relType := elem.Type()
m := make(map[string]interface{})
for i := 0; i < relType.NumField(); i++ {
field := relType.Field(i)
if elem.Field(i).IsZero() {
continue
}
m[string_convert.CamelCase(field.Name, false, false)] = elem.Field(i).Interface()
}
return m
}