-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.go
47 lines (42 loc) · 1.03 KB
/
api.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
package generator
import (
"bytes"
"go/format"
"io/ioutil"
"path/filepath"
"strings"
)
func GenerateBaseAPI(pkg, path, basePath string) error {
for _, file := range []string{"client.go"} {
b, err := content.ReadFile("api/" + file)
if err != nil {
return err
}
s := strings.Replace(string(b), "package api", "package "+pkg, 1)
s = strings.Replace(s,
"\"github.com/llonchj/godoo/types\"",
"\""+filepath.Join(path, pkg, "types")+"\"", 1)
if err := ioutil.WriteFile(filepath.Join(basePath, file), []byte(s), 0644); err != nil {
return err
}
}
return nil
}
func GenerateAPI(pkg, path, basePath, model string) error {
snakeModel := strings.Replace(model, ".", "_", -1)
var outTpl bytes.Buffer
args := map[string]string{
"Package": pkg,
"Path": path,
"Model": snakeModel,
}
err := apiTmpl.Execute(&outTpl, args)
if err != nil {
return err
}
b, err := format.Source(outTpl.Bytes())
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(basePath, snakeModel+"_gen.go"), b, 0644)
}