Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 06:13:47 09月28日 Update

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)

Tags: Comments code generators static enhancers generation learning testing information units characters strings attributes quotes interfaces more targets accents problems help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker Linux Microsoft Redmi