-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
47 lines (40 loc) · 976 Bytes
/
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
package typegen
import (
"fmt"
"regexp"
"strings"
)
func filterMapFunc[M ~map[K]V, K comparable, V any](m M, match func(K, V) bool) M {
f := make(M, len(m))
for k, v := range m {
if !match(k, v) {
continue
}
f[k] = v
}
return f
}
// resetDebugString provides a stub for replacement by debug.go for when the
// `debug` tag is used.
var resetDebugString = func(any) {}
var iFaceRE = regexp.MustCompile(`^(\W*)interface \{}`)
func replaceInterfaceWithAny(name string) string {
return iFaceRE.ReplaceAllString(name, "${1}any")
}
// maybeStripPackage will remove `foo.` from `foo.Bar`, *foo.Bar`, []foo.Bar` and so on.
func maybeStripPackage(name, omitPkg string) string {
var pkgStripRE *regexp.Regexp
if name == "&" {
goto end
}
if len(name) == 0 {
goto end
}
if !strings.Contains(name, ".") {
goto end
}
pkgStripRE = regexp.MustCompile(fmt.Sprintf(`^(\W*)%s\.`, omitPkg))
name = pkgStripRE.ReplaceAllString(name, "$1")
end:
return name
}