-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutil.go
149 lines (141 loc) · 3.93 KB
/
gutil.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
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gutil provides utility functions.
package gutil
import (
"fmt"
"github.com/gogf/gf/internal/empty"
"github.com/gogf/gf/util/gconv"
"reflect"
)
// Throw throws out an exception, which can be caught be TryCatch or recover.
func Throw(exception interface{}) {
panic(exception)
}
// Try implements try... logistics using internal panic...recover.
// It returns error if any exception occurs, or else it returns nil.
func Try(try func()) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf(`%v`, e)
}
}()
try()
return
}
// TryCatch implements try...catch... logistics using internal panic...recover.
// It automatically calls function <catch> if any exception occurs ans passes the exception as an error.
func TryCatch(try func(), catch ...func(exception error)) {
defer func() {
if exception := recover(); exception != nil && len(catch) > 0 {
if err, ok := exception.(error); ok {
catch[0](err)
} else {
catch[0](fmt.Errorf(`%v`, exception))
}
}
}()
try()
}
// IsEmpty checks given <value> empty or not.
// It returns false if <value> is: integer(0), bool(false), slice/map(len=0), nil;
// or else returns true.
func IsEmpty(value interface{}) bool {
return empty.IsEmpty(value)
}
// Keys retrieves and returns the keys from given map or struct.
func Keys(mapOrStruct interface{}) (keysOrAttrs []string) {
keysOrAttrs = make([]string, 0)
if m, ok := mapOrStruct.(map[string]interface{}); ok {
for k, _ := range m {
keysOrAttrs = append(keysOrAttrs, k)
}
return
}
var (
reflectValue reflect.Value
reflectKind reflect.Kind
)
if v, ok := mapOrStruct.(reflect.Value); ok {
reflectValue = v
} else {
reflectValue = reflect.ValueOf(mapOrStruct)
}
reflectKind = reflectValue.Kind()
for reflectKind == reflect.Ptr {
if !reflectValue.IsValid() || reflectValue.IsNil() {
reflectValue = reflect.New(reflectValue.Type().Elem()).Elem()
reflectKind = reflectValue.Kind()
} else {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
}
switch reflectKind {
case reflect.Map:
for _, k := range reflectValue.MapKeys() {
keysOrAttrs = append(keysOrAttrs, gconv.String(k.Interface()))
}
case reflect.Struct:
var (
fieldType reflect.StructField
reflectType = reflectValue.Type()
)
for i := 0; i < reflectValue.NumField(); i++ {
fieldType = reflectType.Field(i)
if fieldType.Anonymous {
keysOrAttrs = append(keysOrAttrs, Keys(reflectValue.Field(i))...)
} else {
keysOrAttrs = append(keysOrAttrs, fieldType.Name)
}
}
}
return
}
// Values retrieves and returns the values from given map or struct.
func Values(mapOrStruct interface{}) (values []interface{}) {
values = make([]interface{}, 0)
if m, ok := mapOrStruct.(map[string]interface{}); ok {
for _, v := range m {
values = append(values, v)
}
return
}
var (
reflectValue reflect.Value
reflectKind reflect.Kind
)
if v, ok := mapOrStruct.(reflect.Value); ok {
reflectValue = v
} else {
reflectValue = reflect.ValueOf(mapOrStruct)
}
reflectKind = reflectValue.Kind()
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch reflectKind {
case reflect.Map:
for _, k := range reflectValue.MapKeys() {
values = append(values, reflectValue.MapIndex(k).Interface())
}
case reflect.Struct:
var (
fieldType reflect.StructField
reflectType = reflectValue.Type()
)
for i := 0; i < reflectValue.NumField(); i++ {
fieldType = reflectType.Field(i)
if fieldType.Anonymous {
values = append(values, Values(reflectValue.Field(i))...)
} else {
values = append(values, reflectValue.Field(i).Interface())
}
}
}
return
}