/
repository_implementation.go
66 lines (53 loc) · 1.67 KB
/
repository_implementation.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
package repository
import (
_ "embed"
"fmt"
"go/format"
"io/ioutil"
"github.com/alenn-m/rgen/generator/parser"
"github.com/alenn-m/rgen/util/config"
"github.com/alenn-m/rgen/util/files"
"github.com/alenn-m/rgen/util/templates"
"github.com/jinzhu/inflection"
)
//go:embed "template_mysql.tmpl"
var TEMPLATE_MYSQL string
// RepositoryImplementation generator
type RepositoryImplementation struct {
parsedData parsedData
}
// Generate generates repository_imlementation
func (r *RepositoryImplementation) Generate(input *parser.Parser, conf *config.Config) error {
r.parsedData = parseData(input, conf)
contentString, err := templates.ParseTemplate(TEMPLATE_MYSQL, r.parsedData, map[string]interface{}{
"ActionUsed": templates.ActionUsed(r.parsedData.Actions),
"Pluralize": func(input string) string {
return inflection.Plural(input)
},
})
if err != nil {
return err
}
mysqlContent, err := format.Source([]byte(contentString))
if err != nil {
return err
}
r.parsedData.ImplContent = string(mysqlContent)
return nil
}
// Save saves generated repository_implementation to file
func (r *RepositoryImplementation) Save() error {
repositoriesPath := r.getServicePath()
err := files.MakeDirIfNotExist(repositoriesPath)
if err != nil {
return err
}
return ioutil.WriteFile(fmt.Sprintf("%s/%s.go", repositoriesPath, r.parsedData.Package), []byte(r.GetContent()), 0644)
}
// GetContent returns generated repository_implementation to file
func (r *RepositoryImplementation) GetContent() string {
return r.parsedData.ImplContent
}
func (r *RepositoryImplementation) getServicePath() string {
return fmt.Sprintf("%s/%s/repositories/mysql", dir, r.parsedData.Package)
}