手撸golang 仿spring ioc/aop 之12 增强3
缘起
最近阅读 [Spring Boot技术内幕: 架构设计与实现原理] (朱智胜 , 2020.6)
本系列笔记拟采用golang练习之
Talk is cheap, show me the code.
Spring
Spring的主要特性:
1. 控制反转(Inversion of Control, IoC)
2. 面向容器
3. 面向切面(AspectOriented Programming, AOP)
源码gitee地址:
https://gitee.com/ioly/learning.gooop
原文链接:
https://my.oschina.net/ioly
目标
- 参考spring boot常用注解,使用golang编写“基于注解的静态代码增强器/生成器”
子目标(Day 12)
- 昨天的增强代码太不oo了,今天添加接口封装:
- domain/ICodeGenerator: 定义代码生成器接口
- generator/ImportGenerator: import语句的生成器
- generator/ParamDTOGenerator: get/post参数DTO结构体的生成器
- generator/MappingMethodGenerator: 增强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语句的生成器
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参数DTO结构体的生成器
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
增强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 ""
}
(未完待续)