forked from dglo/java2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
233 lines (196 loc) · 5.01 KB
/
config.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package parser
import (
"bufio"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
)
// configuration file
type Config struct {
interfaceMap map[string]string
interfaceList []string
packageMap map[string]string
packageList []string
receiverMap map[string]string
receiverList []string
}
// keyword for defining Java interfaces
const typeInterface = "INTERFACE"
// keyword for mapping Java package names to Go names
const typePackage = "PACKAGE"
// keyword for declaring receiver names for a class
const typeReceiver = "RECEIVER"
func addEntry(entryMap map[string]string, typeName string, key string,
val string) map[string]string {
if entryMap == nil {
entryMap = make(map[string]string)
}
if _, ok := entryMap[key]; ok {
log.Printf("Overwriting %s entry %s value %s with %s\n", typeName,
key, entryMap[key], val)
}
entryMap[key] = val
return entryMap
}
func (cfg *Config) addInterface(name string) {
cfg.interfaceMap = addEntry(cfg.interfaceMap, typeInterface, name, name)
cfg.interfaceList = nil
}
func (cfg *Config) addPackage(name string, value string) {
cfg.packageMap = addEntry(cfg.packageMap, typePackage, name, value)
cfg.packageList = nil
}
func (cfg *Config) addReceiver(name string, value string) {
cfg.receiverMap = addEntry(cfg.receiverMap, typeReceiver, name, value)
cfg.receiverList = nil
}
func getValue(entryMap map[string]string, key string) string {
if entryMap != nil {
if val, ok := entryMap[key]; ok {
return val
}
}
return ""
}
// read the configuration file
func ReadConfig(name string) *Config {
fd, err := os.Open(name)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot open config file %s\n", name)
return nil
}
cfg := &Config{}
scan := bufio.NewScanner(bufio.NewReader(fd))
for scan.Scan() {
flds := strings.Fields(scan.Text())
if len(flds) == 0 || strings.Index(flds[0], "#") == 0 {
// ignore empty lines and comments
continue
}
switch strings.ToUpper(flds[0]) {
case typeInterface:
if len(flds) != 2 {
log.Printf("Bad config line: %s\n", scan.Text())
} else {
cfg.addInterface(flds[1])
}
case typePackage:
if len(flds) != 4 {
log.Printf("Bad config line: %s\n", scan.Text())
} else {
cfg.addPackage(flds[1], flds[3])
}
case typeReceiver:
if len(flds) != 4 {
log.Printf("Bad config line: %s\n", scan.Text())
} else {
cfg.addReceiver(flds[1], flds[3])
}
}
}
return cfg
}
// print the configuration data to the output
func (cfg *Config) Dump(out io.Writer) {
need_nl := false
if len(cfg.packageMap) > 0 {
if need_nl { fmt.Fprintln(out) }
fmt.Fprintln(out, "# map Java packages to Go packages")
for _, k := range cfg.packageKeys() {
fmt.Fprintf(out, "%v %v -> %v\n", typePackage, k,
cfg.packageName(k))
}
need_nl = true
}
if len(cfg.interfaceMap) > 0 {
if need_nl { fmt.Fprintln(out) }
fmt.Fprintln(out, "# names which should be treated as interfaces" +
" rather than structs")
for _, k := range cfg.interfaces() {
fmt.Fprintf(out, "%v %v\n", typeInterface, k)
}
need_nl = true
}
if len(cfg.receiverMap) > 0 {
if need_nl { fmt.Fprintln(out) }
fmt.Fprintln(out, "# receiver name to use (other than 'rcvr')")
for _, k := range cfg.receiverKeys() {
fmt.Fprintf(out, "%v %v -> %v\n", typeReceiver, k, cfg.receiver(k))
}
need_nl = true
}
}
func (cfg *Config) findPackage(str string) string {
var pstr string
var pval string
var pextra string
for k, v := range cfg.packageMap {
idx := strings.Index(str, k)
if idx == 0 {
if len(k) > len(pstr) {
pstr = k
pval = v
if len(str) == len(pstr) {
pextra = ""
break
}
pextra = str[len(pstr):]
}
}
}
return pval + pextra
}
func (cfg *Config) interfaces() []string {
if cfg.interfaceList == nil {
cfg.interfaceList = make([]string, len(cfg.interfaceMap))
var i int
for k := range cfg.interfaceMap {
cfg.interfaceList[i] = k
i += 1
}
sort.Sort(sort.StringSlice(cfg.interfaceList))
}
return cfg.interfaceList
}
func (cfg *Config) isInterface(name string) bool {
_, ok := cfg.interfaceMap[name]
return ok
}
func (cfg *Config) packageName(key string) string {
return getValue(cfg.packageMap, key)
}
func (cfg *Config) packageKeys() []string {
if cfg.packageList == nil {
cfg.packageList = make([]string, len(cfg.packageMap))
var i int
for k := range cfg.packageMap {
cfg.packageList[i] = k
i += 1
}
sort.Sort(sort.StringSlice(cfg.packageList))
}
return cfg.packageList
}
func (cfg *Config) receiver(key string) string {
return getValue(cfg.receiverMap, key)
}
func (cfg *Config) receiverKeys() []string {
if cfg.receiverList == nil {
cfg.receiverList = make([]string, len(cfg.receiverMap))
var i int
for k := range cfg.receiverMap {
cfg.receiverList[i] = k
i += 1
}
sort.Sort(sort.StringSlice(cfg.receiverList))
}
return cfg.receiverList
}
// print a minimal description of the configuration rules
func (cfg *Config) String() string {
return fmt.Sprintf("Config[%d ifaces, %d pkgs, %d rcvrs]",
len(cfg.interfaceMap), len(cfg.packageMap), len(cfg.receiverMap))
}