-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.go
More file actions
129 lines (115 loc) · 2.52 KB
/
misc.go
File metadata and controls
129 lines (115 loc) · 2.52 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
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
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
"github.com/dlepex/typeinst/internal/pan"
)
var bpan = pan.NewBounded()
var symCounter int64 = 9
func genSymbol(prefix string) string {
return fmt.Sprintf("%s_%d", prefix, atomic.AddInt64(&symCounter, 1))
}
func dictStr(d map[string]string) (keystr, str string) {
if len(d) == 0 {
return "", ""
}
keys := make([]string, 0, len(d))
for k := range d {
keys = append(keys, k)
}
sort.Strings(keys)
b := bytes.NewBuffer(make([]byte, 0, 64))
bk := bytes.NewBuffer(make([]byte, 0, 64))
for _, k := range keys {
b.WriteString(k)
b.WriteRune('=')
b.WriteString(d[k])
b.WriteRune(',')
bk.WriteString(k)
bk.WriteRune(',')
}
return bk.String(), b.String()
}
// TypeArgs is cached typevar bindings map, *TypeArgs is used as map key.
type TypeArgs struct {
Binds map[string]string // typevar -> replacement
Key string // unique string for targsCache
Shape string // unique string of Binds map keys
}
var targsCache = make(map[string]*TypeArgs)
var bindsCacheLock sync.Mutex
// TypeArgsOf returns cached result
func TypeArgsOf(m map[string]string) *TypeArgs {
if len(m) == 0 {
return nil
}
bindsCacheLock.Lock()
defer bindsCacheLock.Unlock()
shape, key := dictStr(m)
if b, ok := targsCache[key]; ok {
return b
}
b := &TypeArgs{m, key, shape}
targsCache[key] = b
return b
}
// Len returns number of bindings
func (b *TypeArgs) Len() int {
if b == nil {
return 0
}
return len(b.Binds)
}
func unquote(s string) string {
q := "\""
if strings.HasPrefix(s, q) {
s = s[1:]
}
if strings.HasSuffix(s, q) {
s = s[:len(s)-1]
}
return s
}
func packagePath(pkg string) string {
cmd := exec.Command("go", "list", "-m", "all")
b, err := cmd.CombinedOutput()
if err != nil {
//no go modules:
return packagePathGopath(pkg, "src")
}
sc := bufio.NewScanner(bytes.NewReader(b))
for sc.Scan() {
sp := strings.Split(sc.Text(), " ")
mod, ver := sp[0], ""
if len(sp) == 2 {
ver = sp[1]
}
if strings.HasPrefix(pkg, mod) {
suffix := pkg[len(mod):]
full := mod + "@" + ver + suffix
return packagePathGopath(full, "pkg/mod")
}
}
return ""
}
func packagePathGopath(pkg string, subdir string) string {
for _, dir := range filepath.SplitList(os.Getenv("GOPATH")) {
fullPath := filepath.Join(dir, subdir, pkg)
if pathExists(fullPath) {
return fullPath
}
}
return ""
}
func pathExists(fpath string) bool {
_, err := os.Stat(fpath)
return !os.IsNotExist(err)
}