-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
129 lines (120 loc) · 3.72 KB
/
util.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
/*
* Copyright (c) 2021-2023 boot-go
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package boot
import (
"io"
"log"
"os"
"reflect"
gort "runtime"
"strings"
)
// QualifiedName returns the full name of a struct, function or a simple name of a primitive.
func QualifiedName(v any) string {
t := reflect.TypeOf(v)
if t != nil {
switch t.Kind() { //nolint:exhaustive,nolintlint
case reflect.Ptr:
return t.Elem().PkgPath() + "/" + t.Elem().Name()
case reflect.Func:
return gort.FuncForPC(reflect.ValueOf(v).Pointer()).Name()
default:
pkg := t.PkgPath()
if pkg != "" {
pkg += "/"
}
return pkg + reflect.TypeOf(v).Name()
}
} else {
return "nil"
}
}
// Split returns the tokens separated by sep and ignores content in the quotes. If the parsing is okay, the bool return value will be true.
// Unfortunately, the regex can't be used to split the string, because Go has limitations.
func Split(s, sep, quote string) ([]string, bool) {
var result []string
tokens := strings.Split(s, sep)
summarizedToken := ""
summarizing := false
for _, token := range tokens {
count := len(strings.Split(token, quote)) - 1
if count == 1 || count%2 != 0 {
if summarizing {
// summarizing completed
summarizing = false
summarizedToken += sep + token
} else {
// summarizing started
summarizing = true
summarizedToken = token
}
} else {
if summarizing {
// summarizing ongoing
summarizedToken += sep + token
} else {
// no summarizing at all
summarizedToken = token
}
}
if !summarizing {
result = append(result, summarizedToken)
summarizedToken = ""
}
}
if summarizedToken == "" {
return result, true
} else {
return nil, false
}
}
// logger contains multiple loggers which can be seen as different
// log levels
type logger struct {
Debug *log.Logger
Info *log.Logger
Warn *log.Logger
Error *log.Logger
}
// Mute will mute the provided logger.
func (l logger) Mute(logger *log.Logger) {
logger.SetOutput(io.Discard)
}
// Unmute will unmute the provided logger.
func (l logger) Unmute(logger *log.Logger) {
logger.SetOutput(os.Stdout)
}
var (
// Logger contains a debug, info, warning and error logger, which is used for fine-grained log
// output. Every logger can be muted or unmuted separately.
// e.g. Logger.Unmute(Logger.Debug)
Logger logger
)
func init() {
Logger.Debug = log.New(os.Stdout, "boot.debug ", log.LstdFlags|log.Lmsgprefix)
Logger.Info = log.New(os.Stdout, "boot.info ", log.LstdFlags|log.Lmsgprefix)
Logger.Warn = log.New(os.Stdout, "boot.warn ", log.LstdFlags|log.Lmsgprefix)
Logger.Error = log.New(os.Stdout, "boot.error ", log.LstdFlags|log.Lmsgprefix)
// default is to mute the debug logger
Logger.Mute(Logger.Debug)
}