-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfile.go
133 lines (113 loc) · 3.19 KB
/
file.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
package main
import (
"fmt"
"github.com/go-yaml/yaml"
"go/ast"
"go/parser"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"io/ioutil"
"path/filepath"
"strings"
)
type File struct {
Package string
Services Services
fset *token.FileSet
file *ast.File
}
func ParseYAMLFile(filepath string) (*File, error) {
f, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
var all *File
err = yaml.Unmarshal(f, &all)
if err != nil {
return nil, err
}
all.fset = token.NewFileSet()
return all, nil
}
func GenerateContainer(all *File, packageName string, outputFile string) (*File, error) {
var err error
packageLine := fmt.Sprintf("// Code generated by dingo; DO NOT EDIT\npackage %s", packageName)
all.file, err = parser.ParseFile(all.fset, outputFile, packageLine, parser.ParseComments)
if err != nil {
return nil, err
}
all.file.Decls = append(all.file.Decls,
all.Services.astContainerStruct(),
all.Services.astDefaultContainer(),
all.astNewContainerFunc())
for _, serviceName := range all.Services.ServiceNames() {
definition := all.Services[serviceName]
// Add imports for type, interface and explicit imports.
for packageName, shortName := range definition.Imports() {
astutil.AddNamedImport(all.fset, all.file, shortName, packageName)
}
all.file.Decls = append(all.file.Decls, &ast.FuncDecl{
Name: newIdent("Get" + serviceName),
Recv: &ast.FieldList{
List: []*ast.Field{
{
Names: []*ast.Ident{
newIdent("container"),
},
Type: newIdent("*Container"),
},
},
},
Type: &ast.FuncType{
Params: definition.astArguments(),
Results: newFieldList(definition.InterfaceOrLocalEntityType(all.Services, false)),
},
Body: definition.astFunctionBody(all, all.Services, serviceName, serviceName),
})
}
ast.SortImports(all.fset, all.file)
return all, nil
}
func (file *File) getPackageName(dingoYMLPath string) string {
abs, err := filepath.Abs(dingoYMLPath)
if err != nil {
panic(err)
}
// The directory name is not enough because it may contain a command
// (package main). Find the first non-test file to get the real package
// name.
dir := filepath.Dir(abs)
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
for _, fileInfo := range files {
if strings.HasSuffix(fileInfo.Name(), ".go") &&
!strings.HasSuffix(fileInfo.Name(), "_test.go") {
f, err := ioutil.ReadFile(dir + "/" + fileInfo.Name())
if err != nil {
panic(err)
}
parsedFile, err := parser.ParseFile(file.fset, fileInfo.Name(), f, 0)
if err != nil {
panic(err)
}
return parsedFile.Name.String()
}
}
// Couldn't find the package name. Assume command.
return "main"
}
func (file *File) astNewContainerFunc() *ast.FuncDecl {
fields := make(map[string]ast.Expr)
for _, serviceName := range file.Services.ServicesWithScope(ScopePrototype).ServiceNames() {
service := file.Services[serviceName]
fields[serviceName] = &ast.FuncLit{
Type: service.astFunctionPrototype(file.Services),
Body: service.astFunctionBody(file, file.Services, "", serviceName),
}
}
return newFunc("NewContainer", nil, []string{"*Container"}, newBlock(
newReturn(newCompositeLit("&Container", fields)),
))
}