File tree Expand file tree Collapse file tree 4 files changed +815
-3
lines changed
Expand file tree Collapse file tree 4 files changed +815
-3
lines changed Original file line number Diff line number Diff line change 1+ package convert
2+
3+ import (
4+ "encoding/json"
5+ "reflect"
6+ "strings"
7+ )
8+
9+ func ToMap (in interface {}, ignoreFields ... string ) map [string ]interface {} {
10+ out := make (map [string ]interface {})
11+ v := reflect .ValueOf (in )
12+ if v .Kind () == reflect .Ptr {
13+ v = v .Elem ()
14+ }
15+ typ := v .Type ()
16+ for i := 0 ; i < v .NumField (); i ++ {
17+ f := v .Field (i )
18+ fv := f .Interface ()
19+ k := f .Kind ()
20+ if k == reflect .Ptr {
21+ if f .IsNil () {
22+ continue
23+ } else {
24+ fv = reflect .Indirect (reflect .ValueOf (fv )).Interface ()
25+ }
26+ } else if k == reflect .Slice {
27+ if f .IsNil () {
28+ continue
29+ }
30+ }
31+ n := getTag (typ .Field (i ), "json" )
32+ out [n ] = fv
33+ }
34+ for _ , v := range ignoreFields {
35+ if _ , ok := out [v ]; ok {
36+ delete (out , v )
37+ }
38+ }
39+ return out
40+ }
41+ func getTag (fi reflect.StructField , tag string ) string {
42+ if tagv := fi .Tag .Get (tag ); tagv != "" {
43+ arrValue := strings .Split (tagv , "," )
44+ if len (arrValue ) > 0 {
45+ return arrValue [0 ]
46+ } else {
47+ return tagv
48+ }
49+ }
50+ return fi .Name
51+ }
52+ func ToObject (ms map [string ]interface {}, result interface {}) error {
53+ bytes , err := json .Marshal (ms )
54+ if err != nil {
55+ return err
56+ }
57+ return json .Unmarshal (bytes , result )
58+ }
Original file line number Diff line number Diff line change @@ -11,9 +11,7 @@ import (
1111 "time"
1212)
1313
14- const (
15- DateLayout string = "2006-01-02 15:04:05 +0700 +07"
16- )
14+ const DateLayout string = "2006-01-02 15:04:05 +0700 +07"
1715
1816type DelimiterFormatter struct {
1917 Delimiter string
You can’t perform that action at this time.
0 commit comments