How to write Annotation-based static Code Enhancer / Generator with golang imitating spring ioc/aop
This article mainly explains "how to write an annotation-based static code enhancer / generator with golang imitation spring ioc/aop". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write an annotation-based static code enhancer / generator with golang imitation spring ioc/aop".
SpringSpring implements a lightweight Java Web Service system application framework based on J2EE technology. It has many excellent features, and many companies choose Spring as the basic development architecture of their products or projects. The main features of Spring include: 1. Light weight 2. Control reversal (Inversion of Control, IoC) 3. Facing Container 4. Facing the section (AspectOriented Programming, AOP) 5. Framework flexible source gitee address: https://gitee.com/ioly/learning.gooop original text link: https://my.oschina.net/ioly target
Refer to the common notes in spring and write "Annotation-based static Code Enhancer / Generator" in golang.
Configuration: ComponentScan,Configuration, Bean
Bean statement: Component, Service, Controller
Bean injection: Autowried
AOP comments: Before, After, Around, PointCut
Subgoal (Day 3)
Add ProjectCmd to support project definition
Add auxiliary classes for text scanning: Chars.go, Tokens.go
Define the code generation service interface ICodingService and its supporting interface
Design
Command/ProjectCmd: project definition directive
Common/Chars: character recognition helper class
Common/Tokens: composite text recognition helper class
Service/ICodingService: code generation service interface
Service/iCodingContext: code generation context
Service/iCodingState: service status in status mode
Service/iCmdRunner: define the instruction executor interface to implement the proposed chain of responsibility mode for extension
Service/tInitialState: default service statu
Command/ProjectCmd.go
Project definition directive
Package project_cmdimport ("errors"fmt"learning/gooop/spring/autogen/command"learning/gooop/spring/autogen/common"os"strings") / / ProjectCmd defines a project with name and dirtype ProjectCmd struct {name string dir string} / / ProjectCmdBuilder parses cli input and creates a ProjectCmd instancetype ProjectCmdBuilder intconst gProjectCmdPrefix = "project" var gErrorInvalidProjectCmd = errors.New ("invalid project cmd") Func (me * ProjectCmd) String () string {return fmt.Sprintf ("project s" Me.name, me.dir)} func (me * ProjectCmd) Apply (ctx command.ICmdContext) error {panic ("implements me")} func (me * ProjectCmdBuilder) Build (line string) (error, command.ICmd) {if! common.Tokens.MatchString (line, gProjectCmdPrefix) {return nil, nil} line = strings.TrimSpace (linelen (gProjectCmdPrefix):]) b Name: = common.Tokens.MatchIdentifier (line) if! b {return gErrorInvalidProjectCmd, nil} line = line [len (name):] b, spaces: = common.Tokens.MatchSpaces (line) if! b {return gErrorInvalidProjectCmd, nil} line = line [len (spaces):] b Dir: = common.Tokens.MatchDir (line) if! b {return gErrorInvalidProjectCmd, nil} _, e: = os.Stat (dir) if e! = nil {return e, nil} return nil, & ProjectCmd {name, dir}} common/Chars.go
Character recognition helper class
Package commontype tChars intvar Chars = new (tChars) func (me * tChars) IsSpace (it rune) bool {switch it {case': return true case'\ t': return true case'\ r': return true case'\ n': return true} return false} func ( Me * tChars) Is09 (it rune) bool {return it > ='0' & & it ='1' & & it ='a'& & it ='A' & & it