手?jǐn)]golang 仿spring ioc/aop 之12 增強(qiáng)3

手?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語句的生成器
    • 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語句的生成器

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ù))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容