-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from cho4036/dev_project
Project RBAC 구현을 위한 공통 코드 개발
- Loading branch information
Showing
20 changed files
with
2,309 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
//func main() { | ||
// fset := token.NewFileSet() | ||
// | ||
// node, err := parser.ParseFile(fset, "./internal/delivery/api/endpoint.go", nil, parser.ParseComments) | ||
// if err != nil { | ||
// log.Fatalf("파싱 오류: %v", err) | ||
// } | ||
// | ||
// ast.Print(fset, node) | ||
// | ||
// // write ast to file | ||
// f, err := os.Create("ast.txt") | ||
// if err != nil { | ||
// log.Fatalf("파일 생성 오류: %v", err) | ||
// } | ||
// defer f.Close() | ||
// ast.Fprint(f, fset, node, nil) | ||
// | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"html/template" | ||
"io/ioutil" | ||
"log" | ||
"strings" | ||
) | ||
|
||
const endpointFilePath = "./internal/delivery/api/endpoint.go" | ||
|
||
type endpointDecl struct { | ||
Name string | ||
Group string | ||
} | ||
|
||
//const indexTemplateStr = ` // This is generated code. DO NOT EDIT. | ||
// | ||
//package api | ||
// | ||
//type Endpoint int | ||
//type EndpointInfo struct { | ||
// Name string | ||
// Group string | ||
//} | ||
//` | ||
|
||
//const endpointTemplateStr = `// Comment below is special purpose for code generation. | ||
//// Do not edit this comment. | ||
//// Endpoint for Code Generation | ||
//const ( | ||
//{{- range .}} | ||
// {{.Name}} Endpoint = iota | ||
//{{- end}} | ||
//) | ||
//` | ||
|
||
const apiMapTemplateStr = `var ApiMap = map[Endpoint]EndpointInfo{ | ||
{{- range .}} | ||
{{.Name}}: { | ||
Name: "{{.Name}}", | ||
Group: "{{.Group}}", | ||
}, | ||
{{- end}} | ||
} | ||
` | ||
|
||
const stringFunctionTemplateStr = `func (e Endpoint) String() string { | ||
switch e { | ||
{{- range .}} | ||
case {{.Name}}: | ||
return "{{.Name}}" | ||
{{- end}} | ||
default: | ||
return "" | ||
} | ||
} | ||
` | ||
|
||
const getEndpointFunctionTemplateStr = `func GetEndpoint(name string) Endpoint { | ||
switch name { | ||
{{- range .}} | ||
case "{{.Name}}": | ||
return {{.Name}} | ||
{{- end}} | ||
default: | ||
return -1 | ||
} | ||
} | ||
` | ||
|
||
func main() { | ||
fset := token.NewFileSet() | ||
node, err := parser.ParseFile(fset, endpointFilePath, nil, parser.ParseComments) | ||
if err != nil { | ||
log.Fatalf("failed to parse file: %v", err) | ||
} | ||
|
||
var endpoints []endpointDecl | ||
var currentGroup string | ||
|
||
// AST를 탐색합니다. | ||
ast.Inspect(node, func(n ast.Node) bool { | ||
switch x := n.(type) { | ||
case *ast.GenDecl: | ||
if x.Tok == token.CONST { | ||
if x.Doc != nil { | ||
for _, comment := range x.Doc.List { | ||
if strings.Contains(comment.Text, "Endpoint for Code Generation") { | ||
continue | ||
} | ||
if strings.HasPrefix(comment.Text, "//") { | ||
currentGroup = strings.TrimSpace(strings.TrimPrefix(comment.Text, "//")) | ||
} | ||
} | ||
} | ||
for _, spec := range x.Specs { | ||
vs, ok := spec.(*ast.ValueSpec) | ||
if !ok { | ||
continue | ||
} | ||
if vs.Doc != nil { | ||
for _, comment := range vs.Doc.List { | ||
if strings.HasPrefix(comment.Text, "//") { | ||
currentGroup = strings.TrimSpace(strings.TrimPrefix(comment.Text, "//")) | ||
} | ||
} | ||
} | ||
|
||
for _, name := range vs.Names { | ||
endpoints = append(endpoints, endpointDecl{ | ||
Name: name.Name, | ||
Group: currentGroup, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
for _, ep := range endpoints { | ||
log.Printf("Endpoint: %s, Group: %s\n", ep.Name, ep.Group) | ||
} | ||
|
||
//// contents for index | ||
//indexTemplate := template.New("index") | ||
//indexTemplate, err = indexTemplate.Parse(indexTemplateStr) | ||
//if err != nil { | ||
// log.Fatalf("failed to parse template: %v", err) | ||
//} | ||
//var indexCode bytes.Buffer | ||
//if err := indexTemplate.Execute(&indexCode, endpoints); err != nil { | ||
// log.Fatalf("failed to execute template: %v", err) | ||
//} | ||
|
||
//// contents for endpoint | ||
//endpointTemplate := template.New("endpoint") | ||
//endpointTemplate, err = endpointTemplate.Parse(endpointTemplateStr) | ||
//if err != nil { | ||
// log.Fatalf("failed to parse template: %v", err) | ||
//} | ||
//var endpointCode bytes.Buffer | ||
//if err := endpointTemplate.Execute(&endpointCode, endpoints); err != nil { | ||
// log.Fatalf("failed to execute template: %v", err) | ||
//} | ||
|
||
// contents for apiMap | ||
apiMapTemplate := template.New("apiMap") | ||
apiMapTemplate, err = apiMapTemplate.Parse(apiMapTemplateStr) | ||
if err != nil { | ||
log.Fatalf("failed to parse template: %v", err) | ||
} | ||
var apiMapCode bytes.Buffer | ||
if err := apiMapTemplate.Execute(&apiMapCode, endpoints); err != nil { | ||
log.Fatalf("failed to execute template: %v", err) | ||
} | ||
|
||
// contents for stringFunction | ||
stringFunctionTemplate := template.New("stringFunction") | ||
stringFunctionTemplate, err = stringFunctionTemplate.Parse(stringFunctionTemplateStr) | ||
if err != nil { | ||
log.Fatalf("failed to parse template: %v", err) | ||
} | ||
var stringFunctionCode bytes.Buffer | ||
if err := stringFunctionTemplate.Execute(&stringFunctionCode, endpoints); err != nil { | ||
log.Fatalf("failed to execute template: %v", err) | ||
} | ||
|
||
// contents for getEndpointFunction | ||
getEndpointFunctionTemplate := template.New("getEndpointFunction") | ||
getEndpointFunctionTemplate, err = getEndpointFunctionTemplate.Parse(getEndpointFunctionTemplateStr) | ||
if err != nil { | ||
log.Fatalf("failed to parse template: %v", err) | ||
} | ||
var getEndpointFunctionCode bytes.Buffer | ||
if err := getEndpointFunctionTemplate.Execute(&getEndpointFunctionCode, endpoints); err != nil { | ||
log.Fatalf("failed to execute template: %v", err) | ||
} | ||
|
||
// replace original file(endpointFilePath) with new contents | ||
//contents := indexCode.String() + endpointCode.String() + apiMapCode.String() + stringFunctionCode.String() + getEndpointFunctionCode.String() | ||
contents := apiMapCode.String() + stringFunctionCode.String() + getEndpointFunctionCode.String() | ||
newFilePath := strings.Replace(endpointFilePath, "endpoint", "generated_endpoints.go", 1) | ||
|
||
if err := ioutil.WriteFile(newFilePath, []byte(contents), 0644); err != nil { | ||
log.Fatalf("failed to write file: %v", err) | ||
} | ||
|
||
log.Println("Code generation is done.") | ||
} |
Oops, something went wrong.