FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
GoMessagingTestFrameWork/generateMapFuncCode.go at master · botaydotcom/GoMessagingTestFrameWork · GitHub
botaydotcom
/
GoMessagingTestFrameWork
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
GoMessagingTestFrameWork
/
generateMapFuncCode.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
200 lines (170 loc) · 5.03 KB
Breadcrumbs
GoMessagingTestFrameWork
/
generateMapFuncCode.go
Copy path
File metadata and controls
200 lines (170 loc) · 5.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
main
import
(
"bufio"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path"
"path/filepath"
"strings"
"text/template"
)
const
(
PATH_SEP
=
"
\\
/"
DEFAULT_EXTENSION
=
".func.go"
SRC_DIR
=
"src"
templateCode
=
`
package {{.GeneratedPackage}}
import (
"reflect"
{{range $path, $value:= .ImportPath}}
"{{$path}}"
{{end}}
)
// exported FuncMap
var FuncMap map[string] reflect.Value
func init() {
FuncMap = make(map[string] reflect.Value)
{{range $index, $dataType:= .Types}}
FuncMap["{{$dataType.PackageName}}_{{$dataType.TypeName}}"] = reflect.ValueOf({{$dataType.PackageName}}.{{$dataType.TypeName}})
{{end}}
}
`
)
type
DataType
struct
{
TypeName
string
PackageName
string
}
type
FileDataType
struct
{
ImportPath
string
PackageName
string
Types
map
[
string
]
DataType
}
type
OutputData
struct
{
GeneratedPackage
string
ImportPath
map
[
string
]
bool
Types
map
[
string
]
DataType
}
var
PackageNameMap
map
[
string
]
bool
func
isPathSep
(
r
rune
)
bool
{
for
_
,
pS
:=
range
PATH_SEP
{
if
r
==
pS
{
return
true
}
}
return
false
}
// Adding the package name to import path if it does not contain
// normalize the import path according to go convention
func
cleanImportPath
(
importPath
string
)
string
{
importPath
=
strings
.
Replace
(
importPath
,
"
\\
"
,
"/"
,
-
1
)
return
strings
.
TrimFunc
(
importPath
,
isPathSep
)
}
// parse Protobuf file and extract all data type from that file
// inputFile: input file to parse
// import path: import path to this file.
// output: list of all data type from input file, parsed into a FileDataType type
func
parseProtobufFile
(
inputFilePath
string
,
importPath
string
)
FileDataType
{
fset
:=
token
.
NewFileSet
()
// positions are relative to fset
// Parse this file
parsedFile
,
err
:=
parser
.
ParseFile
(
fset
,
inputFilePath
,
nil
,
0
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
FileDataType
{}
}
fmt
.
Println
(
"----Parsing file: "
,
inputFilePath
,
" import path: "
,
path
.
Dir
(
inputFilePath
))
var
data
FileDataType
fmt
.
Println
(
parsedFile
.
Name
)
data
.
PackageName
=
parsedFile
.
Name
.
Name
data
.
ImportPath
=
cleanImportPath
(
importPath
)
fmt
.
Println
(
"---Import Path: "
,
data
.
ImportPath
)
data
.
Types
=
make
(
map
[
string
]
DataType
)
for
typeName
,
objType
:=
range
parsedFile
.
Scope
.
Objects
{
if
objType
.
Kind
==
ast
.
Fun
&&
(
strings
.
ToUpper
(
typeName
[:
1
])
==
typeName
[:
1
]) {
key
:=
data
.
PackageName
+
"_"
+
typeName
fmt
.
Println
(
key
)
newType
:=
DataType
{
TypeName
:
typeName
,
PackageName
:
data
.
PackageName
}
data
.
Types
[
key
]
=
newType
}
}
return
data
}
// visit a file path
// if the file is a .func.go (function helper file), call the parse function and add result to output
func
visit
(
currentPath
string
,
f
os.
FileInfo
,
err
error
)
error
{
fmt
.
Printf
(
"Visited: %s
\n
"
,
currentPath
)
if
!
f
.
IsDir
() {
var
fileName
=
f
.
Name
()
var
strLen
=
len
(
fileName
)
if
strLen
>=
len
(
DEFAULT_EXTENSION
)
&&
fileName
[
strLen
-
len
(
DEFAULT_EXTENSION
):
strLen
]
==
DEFAULT_EXTENSION
{
absPath
,
err1
:=
filepath
.
Abs
(
currentPath
)
if
err1
==
nil
{
importPath
:=
filepath
.
Dir
(
absPath
)
if
importPath
[
0
:
len
(
goPath
)]
==
goPath
{
importPath
=
importPath
[
len
(
goPath
):
len
(
importPath
)]
}
srcIndex
:=
strings
.
Index
(
importPath
,
SRC_DIR
)
importPath
=
importPath
[
srcIndex
+
len
(
SRC_DIR
) :
len
(
importPath
)]
fmt
.
Printf
(
"file path from go path: %d %s
\n
"
,
srcIndex
,
importPath
)
result
:=
parseProtobufFile
(
absPath
,
importPath
)
if
!
output
.
ImportPath
[
result
.
ImportPath
] {
// add import path to output
output
.
ImportPath
[
result
.
ImportPath
]
=
true
}
for
key
,
dataType
:=
range
result
.
Types
{
if
_
,
ok
:=
output
.
Types
[
key
];
!
ok
{
output
.
Types
[
key
]
=
dataType
}
}
}
}
}
return
err
}
var
goPath
string
var
output
OutputData
func
main
() {
var
generatedPackage
string
var
inputDir
string
var
outputFile
string
// get go path for later use
goPath
=
os
.
Getenv
(
"GOPATH"
)
flag
.
StringVar
(
&
inputDir
,
"inputDir"
,
"."
,
"The inputDir to be parsed"
)
flag
.
StringVar
(
&
outputFile
,
"output"
,
"GeneratedDataStructure/generatedDataFuncStructure.go"
,
"The output file"
)
flag
.
Parse
()
generatedPackage
=
path
.
Base
(
path
.
Dir
(
outputFile
))
fmt
.
Println
(
"Input directory: "
,
inputDir
)
output
.
GeneratedPackage
=
generatedPackage
output
.
ImportPath
=
make
(
map
[
string
]
bool
)
output
.
Types
=
make
(
map
[
string
]
DataType
)
fmt
.
Println
(
"Start parsing now ..."
)
fmt
.
Println
(
"Parsing directory: "
,
inputDir
,
""
)
root
:=
inputDir
err
:=
filepath
.
Walk
(
root
,
visit
)
fmt
.
Println
(
"End of parsing"
)
fmt
.
Println
(
"Output file: "
,
outputFile
)
fmt
.
Println
(
"Generated Package: "
,
generatedPackage
)
fmt
.
Println
(
"Writing output file ..."
)
t
:=
template
.
Must
(
template
.
New
(
"DataStructure"
).
Parse
(
templateCode
))
err
=
t
.
Execute
(
os
.
Stdout
,
output
)
if
err
!=
nil
{
log
.
Println
(
"Try Executing template, get error:"
,
err
)
}
fo
,
err
:=
os
.
Create
(
outputFile
)
if
err
!=
nil
{
panic
(
err
)
}
defer
fo
.
Close
()
w
:=
bufio
.
NewWriter
(
fo
)
err
=
t
.
Execute
(
w
,
output
)
if
err
!=
nil
{
log
.
Println
(
"Try executing template and write to file, get error:"
,
err
)
}
if
err
=
w
.
Flush
();
err
!=
nil
{
panic
(
err
)
}
}
Back
|
FazBrowse Home
|
New Git URL