This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
/
assert.go
105 lines (93 loc) · 2.37 KB
/
assert.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
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package util provides some utility tools for other components.
// Such as net-transporting, file-operating, rate-limiter.
package util
import (
"encoding/json"
"reflect"
"strconv"
"unicode"
)
// Max returns the larger of x or y.
func Max(x, y int32) int32 {
if x > y {
return x
}
return y
}
// Min returns the smaller of x or y.
func Min(x, y int32) int32 {
if x < y {
return x
}
return y
}
// IsEmptyStr returns whether the string s is empty.
func IsEmptyStr(s string) bool {
for _, v := range s {
if !unicode.IsSpace(v) {
return false
}
}
return true
}
// IsEmptySlice returns whether the slice values is empty.
func IsEmptySlice(values []string) bool {
return len(values) == 0
}
// IsNil returns whether the value is nil.
func IsNil(value interface{}) (result bool) {
if value == nil {
result = true
} else {
switch v := reflect.ValueOf(value); v.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
}
}
return
}
// IsTrue returns whether the value is true.
func IsTrue(value bool) bool {
return value == true
}
// IsPositive returns whether the value is a positive number.
func IsPositive(value int64) bool {
return value > 0
}
// IsNatural returns whether the value>=0.
func IsNatural(value string) bool {
if v, err := strconv.Atoi(value); err == nil {
return v >= 0
}
return false
}
// IsNumeric returns whether the value is a numeric.
// If the bitSize of value below 0 or above 64 an error is returned.
func IsNumeric(value string) bool {
if _, err := strconv.Atoi(value); err != nil {
return false
}
return true
}
// JSONString returns json string of the v.
func JSONString(v interface{}) string {
if str, e := json.Marshal(v); e == nil {
return string(str)
}
return ""
}