-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (69 loc) · 1.58 KB
/
main.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
package main
import (
"GenMapping/generator"
"fmt"
"go/format"
"os"
"path/filepath"
)
func main() {
args := os.Args[1:]
fmt.Println(args)
if len(args) != 1 {
fmt.Println("Missing args")
return
}
GenerateMappers(args[0])
return
}
func GenerateMappers(mainPath string) {
project, err := generator.ParseProject(mainPath)
if err != nil {
return
}
type Output struct {
name string
text string
}
result := make(map[string]*Output)
for _, mapper := range project.MapperInterfaces {
fmt.Printf("Mapper: %s\n", mapper.Name)
for _, method := range mapper.Methods {
fmt.Printf("- Mapping Method: %s\n", method.Name)
fmt.Printf("-- From: %v\n", method.Params)
fmt.Printf("-- To: %v\n", method.Target)
}
output := result[mapper.OutputPath(project)]
if output == nil {
output = new(Output)
result[mapper.OutputPath(project)] = output
}
output.text += generator.GenerateMapper(mapper, project).Methods
if output.name != "" {
output.name += "_"
}
output.name += mapper.Name
}
for mapperPath, mapperOutput := range result {
finalOutput := generator.ProcessImports(mapperOutput.text)
finalOutput = fmt.Sprintf("package %s\n", mapperOutput.name) + finalOutput
err := os.MkdirAll(filepath.Dir(mapperPath), 0)
if err != nil {
panic(err)
}
f, err := os.Create(mapperPath)
if err != nil {
panic(err)
}
formattedOutput, err := format.Source([]byte(finalOutput))
if err != nil {
fmt.Println(err)
formattedOutput = []byte(finalOutput)
}
_, err = f.Write(formattedOutput)
if err != nil {
panic(err)
}
}
return
}