/
struct.go
52 lines (38 loc) · 818 Bytes
/
struct.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
/**
* @program: lemo
*
* @description:
*
* @author: lemo
*
* @create: 2019-11-05 11:42
**/
package utils
import (
"reflect"
"github.com/mitchellh/mapstructure"
)
type structure int
const Structure structure = iota
func (d structure) StructToMap(input interface{}) map[string]interface{} {
var output = make(map[string]interface{})
if input == nil {
return output
}
var kf = reflect.TypeOf(input)
var vf = reflect.ValueOf(input)
if kf.Kind() == reflect.Ptr {
kf = kf.Elem()
vf = vf.Elem()
}
if kf.Kind() != reflect.Struct {
return nil
}
for i := 0; i < kf.NumField(); i++ {
output[kf.Field(i).Tag.Get("json")] = vf.Field(i).Interface()
}
return output
}
func (d structure) MapToStruct(input interface{}, output interface{}) error {
return mapstructure.WeakDecode(input, output)
}