forked from glycerine/bambam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
175 lines (140 loc) · 3.42 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package bam
import (
"go/ast"
"io/ioutil"
"os"
"os/exec"
"unicode"
)
var capnpKeywords map[string]bool = map[string]bool{
"Void": true, "Bool": true, "Int8": true, "Int16": true, "Int32": true, "Int64": true, "UInt8": true, "UInt16": true, "UInt32": true, "UInt64": true, "Float32": true, "Float64": true, "Text": true, "Data": true, "List": true, "struct": true, "union": true, "group": true, "enum": true, "AnyPointer": true, "interface": true, "extends": true, "const": true, "using": true, "import": true, "annotation": true}
func isCapnpKeyword(w string) bool {
return capnpKeywords[w] // not found will return false, the zero value for bool.
}
// recursively extract the go type as a string
func GetTypeAsString(ty ast.Expr, sofar string, goTypeSeq []string) (string, string, []string) {
switch ty.(type) {
case (*ast.StarExpr):
return GetTypeAsString(ty.(*ast.StarExpr).X, sofar+"*", append(goTypeSeq, "*"))
case (*ast.Ident):
return sofar, ty.(*ast.Ident).Name, append(goTypeSeq, ty.(*ast.Ident).Name)
case (*ast.ArrayType):
// slice or array
return GetTypeAsString(ty.(*ast.ArrayType).Elt, sofar+"[]", append(goTypeSeq, "[]"))
}
return sofar, "", goTypeSeq
}
func underToCamelCase(s string) string {
ru := []rune(s)
n := len(ru)
last := n - 1
for i := 0; i < n; i++ {
if ru[i] == '_' && i < last {
if unicode.IsLower(ru[i+1]) {
ru[i+1] = unicode.ToUpper(ru[i+1])
}
copy(ru[i:], ru[i+1:])
ru = ru[:last]
last--
n--
i--
}
}
return string(ru)
}
type TempDir struct {
OrigDir string
DirPath string
Files map[string]*os.File
}
func NewTempDir() *TempDir {
dirname, err := ioutil.TempDir(".", "testdir_")
if err != nil {
panic(err)
}
origdir, err := os.Getwd()
if err != nil {
panic(err)
}
// add files needed for capnpc -ogo compilation
exec.Command("/bin/cp", "go.capnp", dirname).Run()
return &TempDir{
OrigDir: origdir,
DirPath: dirname,
Files: make(map[string]*os.File),
}
}
func (d *TempDir) MoveTo() {
err := os.Chdir(d.DirPath)
if err != nil {
panic(err)
}
}
func (d *TempDir) Close() {
for _, f := range d.Files {
f.Close()
}
}
func (d *TempDir) Cleanup() {
d.Close()
err := os.RemoveAll(d.DirPath)
if err != nil {
panic(err)
}
err = os.Chdir(d.OrigDir)
if err != nil {
panic(err)
}
}
func (d *TempDir) TempFile() *os.File {
f, err := ioutil.TempFile(d.DirPath, "testfile.")
if err != nil {
panic(err)
}
d.Files[f.Name()] = f
return f
}
type ByFinalOrder []*Field
func (s ByFinalOrder) Len() int {
return len(s)
}
func (s ByFinalOrder) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByFinalOrder) Less(i, j int) bool {
return s[i].finalOrder < s[j].finalOrder
}
type ByOrderOfAppearance []*Field
func (s ByOrderOfAppearance) Len() int {
return len(s)
}
func (s ByOrderOfAppearance) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByOrderOfAppearance) Less(i, j int) bool {
return s[i].orderOfAppearance < s[j].orderOfAppearance
}
type ByGoName []*Struct
func (s ByGoName) Len() int {
return len(s)
}
func (s ByGoName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByGoName) Less(i, j int) bool {
return s[i].goName < s[j].goName
}
type AlphaHelper struct {
Name string
Code []byte
}
type AlphaHelperSlice []AlphaHelper
func (s AlphaHelperSlice) Len() int {
return len(s)
}
func (s AlphaHelperSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s AlphaHelperSlice) Less(i, j int) bool {
return s[i].Name < s[j].Name
}