Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write an annotation-based static code enhancer / generator using golang

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use golang to write annotation-based static code enhancers / generators". In daily operations, I believe that many people have doubts about how to use golang to write annotation-based static code enhancers / generators. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "how to write an annotation-based static code enhancer / generator using golang"! Next, please follow the editor to study!

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

Struct parsing is clear, and then parsing annotations is easier.

Scanner/IStructScanner.go: fix the details of scanMethod () and scanAnnotation ()

Scanner/IAnnotationScanner.go: annotation scanning interface and default implementation. The properties of annotations support double quotation marks and accented strings.

Scanner/IAnnotationScanner_test.go: unit testing for annotated information

Scanner/IAnnotationScanner.go

Annotation scan interface and default implementation. The properties of annotations support double quotation marks and accented strings.

Package scannerimport ("errors"learning/gooop/spring/autogen/common"learning/gooop/spring/autogen/domain"regexp"strings") type IAnnotationScanner interface {ScanAnnotations (s * domain.StructInfo)} type tAnnotationScanner intfunc (me * tAnnotationScanner) ScanAnnotations (s * domain.StructInfo) {me.scanStructAnnotation (s) me.scanFieldAnnotation (s) me.scanMethodAnnotation (s) } func (me * tAnnotationScanner) scanStructAnnotation (s * domain.StructInfo) {for I: = s.LineNO-1 I > = 0 I if-{if! me.matchAnnotation (s, I) {break} code: = s.CodeFile.RawLines [I] e A: = me.parseAnnotation (code) if e! = nil {panic (e)} s.AppendAnnotation (a)}} func (me * tAnnotationScanner) scanFieldAnnotation (s * domain.StructInfo) {for _, fld: = range s.Fields {for I: = fld.LineNO-1 I > = 0 I if-{if! me.matchAnnotation (s, I) {break} code: = s.CodeFile.RawLines [I] e A: = me.parseAnnotation (code) if e! = nil {panic (e)} fld.AppendAnnotation (a)}} func (me * tAnnotationScanner) scanMethodAnnotation (s * domain.StructInfo) {for _ Method: = range s.Methods {for I: = method.LineNO-1 I > = 0 I if-{if! me.matchAnnotation (s, I) {break} code: = s.CodeFile.RawLines [I] e A: = me.parseAnnotation (code) if e! = nil {panic (e)} method.AppendAnnotation (a)}} func (me * tAnnotationScanner) matchAnnotation (s * domain.StructInfo LineNO int) bool {line: = s.CodeFile.RawLines [lineNO] return gAnnotationStartRegexp.MatchString (line)} func (me * tAnnotationScanner) parseAnnotation (line string) (error, * domain.AnnotationInfo) {ss: = gAnnotationStartRegexp.FindStringSubmatch (line) if len (ss)

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: 302

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report