In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to write an annotation-based static code enhancer / generator using golang". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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 11)
Write an enhancer for @ RestController
Enhancer/IEnhancer.go: define the intensifier interface
Enhanced implementation of enhancer/RestControllerEnhancer.go:REST Controller
Enhancer/IEnhancer.go: define the intensifier interface
Package enhancerimport "learning/gooop/spring/autogen/domain" / / IEnhancer clones the original file and appends enhanced codetype IEnhancer interface {Matches (file * domain.CodeFileInfo) bool Enhance (file * domain.CodeFileInfo) (error, * domain.CodeFileInfo)}
Enhanced implementation of enhancer/RestControllerEnhancer.go REST Controller
Package controllerimport ("errors"fmt"learning/gooop/spring/autogen/domain"path"strings") type RestControllerEnhancer inttype tMappingMethodInfo struct {httpMethod string httpPath string method * domain.MethodInfo} func (me * RestControllerEnhancer) Matches (file * domain.CodeFileInfo) bool {for _, it: = range file.Structs {if ok, _: = me.hasAnnotation (it.Annotations "RestController") Ok {return true}} return false} func (me * RestControllerEnhancer) Enhance (file * domain.CodeFileInfo) (error, * domain.CodeFileInfo) {/ / clone file file = file.Clone (). (* domain.CodeFileInfo) file.LocalFile = strings.Replace (file.LocalFile, ".go", "_ Enhanced.go" -1) / find structs with @ RestController changed: = false for _, it: = range file.Structs {if ok, a: = me.hasAnnotation (it.Annotations, "RestController") Ok {/ / enhance target struct e: = me.enhanceStruct (it, a) if e! = nil {return e Nil} changed = true}} if changed {return nil, file} else {return nil, nil}} func (me * RestControllerEnhancer) hasAnnotation (arr [] * domain.AnnotationInfo, name string) (bool, * domain.AnnotationInfo) {for _ It: = range arr {if it.Name = = name {return true, it}} return false, nil} func (me * RestControllerEnhancer) enhanceStruct (s * domain.StructInfo, sa * domain.AnnotationInfo) error {/ / update struct name s.Name = s.Name + "_ Enhanced" / / ensure imports me.ensureImport (s.CodeFile "github.com/gin-gonic/gin") me.ensureImport (s.CodeFile, "net/http") / / enhance GetMapping methods methods: = [] * tMappingMethodInfo {} for _, it: = range s.Methods {if ok, a: = me.hasAnnotation (it.Annotations, "GetMapping") Ok {e: = me.enhanceGetMapping (it A) if e! = nil {return e} info: = new (tMappingMethodInfo) info.httpMethod = "GET" info.httpPath = path.Join (sa.Get ("path") A.Get ("path") info.method = it methods = append (methods, info)}} / / enhance PostMapping methods for _, it: = range s.Methods {if ok, a: = me.hasAnnotation (it.Annotations, "PostMapping") Ok {e: = me.enhancePostMapping (it A) if e! = nil {return e} info: = new (tMappingMethodInfo) info.httpMethod = "POST" info.httpPath = path.Join (sa.Get ("path") A.Get ("path") info.method = it methods = append (methods, info)}} / / generate RegisterRestController () if len (methods)
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.