手?jǐn)]golang 仿spring ioc/aop 之12 增強(qiáng)3
緣起
最近閱讀 [Spring Boot技術(shù)內(nèi)幕: 架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理] (朱智勝 , 2020.6)
本系列筆記擬采用golang練習(xí)之
Talk is cheap, show me the code.
Spring
Spring的主要特性:
1. 控制反轉(zhuǎn)(Inversion of Control, IoC)
2. 面向容器
3. 面向切面(AspectOriented Programming, AOP)
源碼gitee地址:
https://gitee.com/ioly/learning.gooop
原文鏈接:
https://my.oschina.net/ioly
目標(biāo)
- 參考spring boot常用注解,使用golang編寫“基于注解的靜態(tài)代碼增強(qiáng)器/生成器”
子目標(biāo)(Day 12)
- 昨天的增強(qiáng)代碼太不oo了息尺,今天添加接口封裝:
- domain/ICodeGenerator: 定義代碼生成器接口
- generator/ImportGenerator: import語(yǔ)句的生成器
- generator/ParamDTOGenerator: get/post參數(shù)DTO結(jié)構(gòu)體的生成器
- generator/MappingMethodGenerator: 增強(qiáng)GetMapping/PostMapping方法的生成器
domain/CodeFileInfo
代碼文件模型
package domain
type CodeFileInfo struct {
Package *PackageInfo `json:"-"`
LocalFile string
RawLines []string
CleanLines []string
Imports []*ImportInfo
Structs []*StructInfo
AdditionalLines []ICodeGenerator
}
domain/ICodeGenerator.go
定義代碼生成器接口
package domain
import (
"fmt"
)
type ICodeGenerator interface {
fmt.Stringer
Section() CodeSection
}
generator/ImportGenerator.go
import語(yǔ)句的生成器
package generator
import (
"fmt"
"learning/gooop/spring/autogen/domain"
)
type ImportGenerator struct {
Package string
Alias string
}
func (me *ImportGenerator) String() string {
if me.Alias != "" {
return fmt.Sprintf(`import %s %s`, me.Alias, me.Package)
} else {
return fmt.Sprintf(`import %s`, me.Package)
}
}
func (me *ImportGenerator) Section() domain.CodeSection {
return domain.ImportSection
}
generator/ParamDTOGenerator.go
get/post參數(shù)DTO結(jié)構(gòu)體的生成器
package generator
import (
"fmt"
"learning/gooop/spring/autogen/domain"
"strings"
)
type ParamsDTOGenerator struct {
Method *domain.MethodInfo
}
func NewParamsDTOGenerator(m *domain.MethodInfo) domain.ICodeGenerator {
it := new(ParamsDTOGenerator)
it.Method = m
return it
}
func (me *ParamsDTOGenerator) Section() domain.CodeSection {
return domain.ImportSection
}
func (me *ParamsDTOGenerator) String() string {
lines := []string{}
fnAddLinef := func(f string, args... interface{}) {
lines = append(lines, fmt.Sprintf(f, args...))
}
fnAddLinef(``)
// declare
name := fmt.Sprintf(`%s_%s_ParamsDTO`, me.Method.Struct.Name, me.Method.Name)
fnAddLinef(`// %s is auto generated struct for wrapping parameters of PagedQuery`, name)
fnAddLinef(`type %s struct {`, name)
// fields
for _,a := range me.Method.Arguments {
fnAddLinef(` %s %s`, strings.Title(a.Name), a.DataType)
}
// end
fnAddLinef("}")
// return
return strings.Join(lines, "\n")
}
generator/MappingMethodGenerator.go
增強(qiáng)GetMapping/PostMapping方法的生成器
package generator
import (
"fmt"
"learning/gooop/spring/autogen/domain"
)
type MappingMethodGenerator struct {
Method *domain.MethodInfo
}
func (me *MappingMethodGenerator) Section() domain.CodeSection {
return domain.AnywhereSection
}
func (me *MappingMethodGenerator) String() string {
lines := []string{}
fnAddLinef := func(f string, args... interface{}) {
lines = append(lines, fmt.Sprintf(f, args...))
}
fnAddLinef(``)
fnAddLinef(`// %s_Enhanced is the enhanced version of PagedQuery`, me.Method.Name)
fnAddLinef(`func (me *%s) %s_Enhanced(c *gin.Context) {`, me.Method.Struct.Name, me.Method.Name)
// todo: fixme
//r := new(OrderController_Query_ParamsDTO)
//e := c.Bind(r)
//if e != nil {
// c.JSON(http.StatusBadRequest, gin.H{"ok": false, "error": e.Error()})
// return
//}
//
//e, d := me.Query(r.CustomerID, r.StatusFlag, r.DateFrom, r.DateTO, r.PageNO, r.PageSize)
//if e != nil {
// c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()})
// return
//}
//
//c.JSON(http.StatusOK, gin.H{"ok": true, "data": d})
//}
return ""
}
(未完待續(xù))