-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcliutil.go
More file actions
71 lines (57 loc) · 1.67 KB
/
cliutil.go
File metadata and controls
71 lines (57 loc) · 1.67 KB
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
package cliutil
import (
"errors"
"go/types"
"strings"
"golang.org/x/tools/go/packages"
)
var (
// ErrNotFound indicates the object or the type could not be found
ErrNotFound = errors.New("not found")
)
// DefaultConfig is the default value of [Config].
var DefaultConfig = &Config{
Packages: &packages.Config{
Mode: packages.NeedTypes,
},
}
// TypeOf is wrapper of DefaultConfig.TypeOf(name).
func TypeOf(name string) (types.Type, error) {
return DefaultConfig.TypeOf(name)
}
// ObjectOf is wrapper of DefaultConfig.ObjectOf(name).
func ObjectOf(name string) (types.Object, error) {
return DefaultConfig.ObjectOf(name)
}
// CurrentPackage is wrapper of DefaultConfig.CurrentPackage().
func CurrentPackage() (*types.Package, error) {
return DefaultConfig.CurrentPackage()
}
// Split splits name into three sections.
// The first section means a package name or a pre-declared identifier.
// The second section means an object of package.
// The third section means a field or a method.
// The fourth return value indicates whether name has "*" prefix or not.
func Split(name string) (first, second, third string, ptr bool) {
slashed := strings.Split(name, "/")
prefix := strings.Join(slashed[:len(slashed)-1], "/")
splited := strings.Split(slashed[len(slashed)-1], ".")
if prefix != "" {
splited[0] = prefix + "/" + splited[0]
}
first = strings.TrimLeft(splited[0], "(")
first = strings.TrimRight(first, ")")
if strings.HasPrefix(first, "*") {
first = first[1:]
ptr = true
}
// builtin?
if len(splited) == 1 {
return first, "", "", ptr
}
second = strings.TrimRight(splited[1], ")")
if len(splited) >= 3 {
third = splited[2]
}
return first, second, third, ptr
}