In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to write an annotation-based static code enhancer / generator with golang". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to write an annotation-based static code enhancer / generator in golang.
The main features of SpringSpring: 1. Control inversion (Inversion of Control, IoC) 2. Facing Container 3. Aspect-oriented (AspectOriented Programming, AOP) 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 boot and write "Annotation-based static Code Enhancer / Generator" in golang.
Subgoal (Day 7)
Continue the scan of the hard part:struct/field/method elements today
Common/Tokens.go: lexical parsing support for adding data types
Scanner/IStructScanner.go: interface and implementation of structure Scanner
Common/Tokens.go
Add lexical parsing support for data types:
Parsing basic types / custom types / pointer types / array types / map types respectively
Custom types need to be careful to exclude the 'map'' keyword
Pointers, arrays and map types are all compound types and need to be parsed recursively
Package commonimport ("regexp"strings"sync") type tTokens struct {cache map [string] * regexp.Regexp rwmutex * sync.RWMutex} var Tokens = newTokensLib () func newTokensLib () * tTokens {it: = new (tTokens) it.init () return it} func (me * tTokens) init () {me.cache = make (map [string] * regexp) .Regexp) me.rwmutex = new (sync.RWMutex)} func (me * tTokens) MatchString (s string P string) bool {return strings.HasPrefix (s, p)} func (me * tTokens) MatchRegexp (s string, p string) (bool, string) {me.rwmutex.RLock () r, ok: = me.cache [p] me.rwmutex.RUnlock () if! ok {me.rwmutex.Lock () if r, ok = me.cache [p] ! ok {r, _ = regexp.Compile (p)} me.rwmutex.Unlock ()} if r = = nil {return false, ""} if! r.MatchString (s) {return false, "} return true R.FindString (s)} func (me * tTokens) MatchIdentifier (s string) (bool, string) {return me.MatchRegexp (s, "^ [_ a-zA-Z]\\ w {0pime99}")} func (me * tTokens) MatchSpaces (s string) (bool, string) {return me.MatchRegexp (s, "^\\ s +")} func (me * tTokens) MatchDir (s string) (bool, string) {b, s: = me.MatchRegexp (s) "^ ([a-zA-Z]\:)? ([\ /] [^\] [^\] +) + [\\ /]?") If b {return b, s} b, s = me.MatchRegexp (s, "^\" ([a-zA-Z]\:)? ([\ /] [^ / *? |\\ "\\] +) + [\\]?\\") if b {return b, s} b, s = me.MatchRegexp (s "^'([a-zA-Z]\:)? ([\ /] [^'/: *? |\\"\] +) + [\ /]?'") If b {return b, s} return false, ""} func (me * tTokens) MatchDataType (s string) (bool, string) {if ok,t: = me.MatchBasicType (s); ok {return true, t} if ok,t: = me.MatchCustomType (s) Ok {return true, t} if ok,t: = me.MatchPointerType (s); ok {return true, t} if ok,t: = me.MatchArrayType (s); ok {return true, t} if ok,t: = me.MatchMapType (s) Ok {return true, t} return false, ""} func (me * tTokens) MatchBasicType (s string) (bool, string) {list: = [] string {"int", "string", "bool", "byte", "int32", "int64" "uint32", "uint64", "float32", "float64", "int8", "uint8", "int16", "uint16", "time.Time",} for _ It: = range list {if me.MatchString (s, it) {return true, it}} return false, ""} func (me * tTokens) MatchCustomType (s string) (bool, string) {t: = s b1, S1: = me.MatchRegexp (t `^\ w+\ .`) if b1 {t = t [len (S1):]} b2, S2: = me.MatchRegexp (t, `^\ w+ `) if! b2 {return false, ""} if S2 = = "map" {/ / map is reserved word return false "} return true, S1 + S2} func (me * tTokens) MatchPointerType (s string) (bool, string) {t: = s if t [0]! ='*'{return false,"} t = t [1:] b, s: = me.MatchDataType (t) if! b {return false "} return true," * "+ s} func (me * tTokens) MatchArrayType (s string) (bool, string) {t: = s b1, S1: = me.MatchRegexp (s, `^\ [\ s*\ d *\ s*\]\ s*`) if! b1 {return false,"} t = t [len (S1):] b2 S2: = me.MatchDataType (t) if! b2 {return false, "} return true, S1 + S2} func (me * tTokens) MatchMapType (s string) (bool, string) {t: = s S1: =" map "if! me.MatchString (t, S1) {return false ""} t = t [len (S1):] b2, S2: = me.MatchRegexp (t, `^\ s*\ [\ s* `) if! b2 {return false, ""} t = t [len (S2):] b3jue S3: = me.MatchDataType (t) if! b3 {return false ""} t = t [len (S3):] b4, s4: = me.MatchRegexp (t, `^\ s*\]\ s* `) if! b4 {return false, ""} t = t [len (S4):] b5, S5: = me.MatchDataType (t) if! b5 {return false ""} return true, S1 + S2 + S3 + S4 + S5} scanner/IStructScanner.go
Interface and implementation of structure Scanner
Package scannerimport ("errors"learning/gooop/spring/autogen/common"learning/gooop/spring/autogen/domain"regexp"strings") type IStructScanner interface {ScanStruct (file * domain.CodeFileInfo)} type tStructScanner intfunc (me * tStructScanner) ScanStruct (file * domain.CodeFileInfo) {bInStruct: = false var stru * domain.StructInfo for lineNO Line: = range file.CleanLines {if bInStruct {/ / end? If gStructEndRegexp.MatchString (line) {bInStruct = false me.scanMethod (stru, lineNO + 1) stru = nil continue}} / / start? If gStructStartRegexp.MatchString (line) {bInStruct = true ss: = gStructStartRegexp.FindAllString (line -1) stru: = domain.NewStructInfo () stru.LineNO = lineNO stru.CodeFile = file stru.Name = ss [1] continue} / / in struct block ok,fname Ftype: = me.scanField (line) if ok {stru.AppendField (lineNO, fname, ftype)}} func (me * tStructScanner) scanField (line string) (ok bool, fldName string, fldType string) {if! gFieldStartRegexp.MatchString (line) {return false, "" ""} fldName = strings.TrimSpace (gFieldStartRegexp.FindString (line)) fldType = strings.TrimSpace (line [len (fldName):]) return true, fldName, fldType} func (me * tStructScanner) scanMethod (stru * domain.StructInfo, fromLineNO int) {for iMax. = fromLineNO, len (stru.CodeFile.CleanLines) I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.