-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
77 lines (66 loc) · 1.56 KB
/
generate.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
package generate
import (
"fmt"
"go/types"
"io"
"os"
"os/exec"
"text/template"
)
func GetAllStructsToConvert([][2]types.Var) []*types.Struct {
return []*types.Struct{}
}
func Create(fileName string) {
f, err := os.Open("./templ/out.tmpl")
if err != nil {
panic(err)
}
b, err := io.ReadAll(f)
if err != nil {
panic(err)
}
templ := template.Must(template.New("out").Parse(string(b)))
templ.Execute(createFile(fileName), TemplVar{
PkgName: "convert",
SrcTypeName: "blah.Foo",
DestTypeName: "boo.Foo",
Imports: []string{
"github.com/janshercs/structconv/pkg/match/testfiles/blah",
"github.com/janshercs/structconv/pkg/match/testfiles/boo",
},
BasicFields: []string{"Foo", "Bar", "This", "That"},
StructFields: []string{"Amount"},
})
f.Close()
}
func CreateAndFormat(fileName string) {
Create(fileName)
format(fileName)
}
func createFile(fileName string) *os.File {
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
return file
}
type TemplVar struct {
PkgName string
SrcTypeName string
DestTypeName string
Imports []string
BasicFields []string
StructFields []string
}
func format(fileName string) {
cmd := exec.Command("goimports", "-w", fileName)
if errOut, err := cmd.CombinedOutput(); err != nil {
panic(fmt.Errorf("failed to run %v with error: %s, %s", cmd, err, errOut))
}
}
// TODO: How to recursively convert structs?
// TODO: How to collect all the struct defs?
// TODO: How to do struct pointers easily?
// TODO: Add struct tags
// OUTOFSCOPE: Anonymous structs
// OUTOFSCOPE: Anonymous structs