gen_annotation

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package gen_annotation parse annotation from source code file and recognize follows.

Comment @Handler("name", "pkg.ExampleHandler") to define handler and use @Middleware("name") to use it around Controller handler or route handler.

Index

Constants

View Source
const (
	Description = "Build annotation files for middleware, crontab, controller etc"
	Name        = "gen:annotation"

	OptAnnotationPath             = "annotation-path"
	OptAnnotationPathDesc         = "Generated files will be saved to"
	OptAnnotationPathShort        = 'a'
	OptAnnotationPathDefaultValue = "app/annotations"

	OptControllerPath             = "controller-path"
	OptControllerPathDesc         = "Controller files location"
	OptControllerPathShort        = 'c'
	OptControllerPathDefaultValue = "app/controllers"

	OptCrontabPath             = "crontab-path"
	OptCrontabPathDesc         = "Crontab files location"
	OptCrontabPathShort        = 'r'
	OptCrontabPathDefaultValue = "app/crontabs"

	OptHandlerPath             = "handler-path"
	OptHandlerPathDesc         = "Handler files location"
	OptHandlerPathShort        = 'h'
	OptHandlerPathDefaultValue = "app/handlers"

	OptMiddlewarePath             = "middleware-path"
	OptMiddlewarePathDesc         = "Middleware files location"
	OptMiddlewarePathShort        = 'm'
	OptMiddlewarePathDefaultValue = "app/middlewares"

	OptOverride     = "override"
	OptOverrideDesc = "Override existed file, Do nothing if not specified"

	OptWorkingDir             = "working-dir"
	OptWorkingDirShort        = 'w'
	OptWorkingDirDesc         = "Current working dir is"
	OptWorkingDirDefaultValue = "./"
)

Variables

View Source
var (
	RegexAnnotationAddQuotes        = regexp.MustCompile(`,([^"]*),`)
	RegexAnnotationRemoveCommaSpace = regexp.MustCompile(`\s*,\s*`)
)
View Source
var (
	ResourceRegexMatchAnnotation        = regexp.MustCompile(`^/+\s*@([_a-zA-Z0-9]+)\(?([^)]*)\)?`)
	ResourceRegexMatchComment           = regexp.MustCompile(`^/+\s?(.*)`)
	ResourceRegexMatchPackageFromImport = regexp.MustCompile(`([_a-zA-Z0-9]+)$`)

	// ResourceRegexMatchController
	// is a regular expression to match controller type.
	//
	//   func ExampleMiddleware(i iris.Context) {
	//       ...
	//   }
	ResourceRegexMatchController     = regexp.MustCompile(`^([_a-zA-Z0-9]*Controller)\s+struct\s*\{`)
	ResourceRegexMatchController2    = regexp.MustCompile(`^type\s+([_a-zA-Z0-9]*Controller)\s+struct\s*\{`)
	ResourceRegexMatchControllerName = regexp.MustCompile(`^[A-Z][_a-zA-Z0-9]*$`)

	// ResourceRegexMatchHandler
	// is a regular expression to match handler function.
	//
	//   func ExampleHandler(i iris.Context) {
	//       ...
	//   }
	ResourceRegexMatchHandler = regexp.MustCompile(`^func\s+([A-Z][_a-zA-Z0-9]*Handler)\s*\([_a-zA-Z0-9]*\s*iris\.Context\)\s+\{`)

	// ResourceRegexMatchCrontab
	// is a regular expression to match crontab type.
	//
	//   func ExampleMiddleware(i iris.Context) {
	//       ...
	//   }
	ResourceRegexMatchCrontab  = regexp.MustCompile(`^([A-Z][_a-zA-Z0-9]*Crontab)\s+struct\s*\{`)
	ResourceRegexMatchCrontab2 = regexp.MustCompile(`^type\s+([A-Z][_a-zA-Z0-9]*Crontab)\s+struct\s*\{`)

	// ResourceRegexMatchMiddleware
	// is a regular expression to match middleware function.
	//
	//   func ExampleMiddleware(i iris.Context) {
	//       ...
	//   }
	ResourceRegexMatchMiddleware = regexp.MustCompile(`^func\s+([A-Z][_a-zA-Z0-9]*Middleware)\s*\([_a-zA-Z0-9]*\s*iris\.Context\)\s+\{`)

	ResourceRegexRemoveFilename = regexp.MustCompile(`/([^/]+)$`)
)
View Source
var (
	SeekerRegexAllowedName  = regexp.MustCompile(`^[a-zA-Z]`)
	SeekerRegexFileIsGolang = regexp.MustCompile(`\.go$`)
)

Functions

func New

func New() *console.Command

New creates a command for console.

You can run `go run main.go gen:annotation` to run this command.

Types

type Annotation

type Annotation string

Annotation is a type name for @ definition.

const (
	AnnotationAny     Annotation = "any"
	AnnotationDelete  Annotation = "delete"
	AnnotationGet     Annotation = "get"
	AnnotationHead    Annotation = "head"
	AnnotationOptions Annotation = "options"
	AnnotationPatch   Annotation = "patch"
	AnnotationPost    Annotation = "post"
	AnnotationPut     Annotation = "put"

	AnnotationCron               Annotation = "cron"
	AnnotationCronGloballyUnique Annotation = "crongloballyunique"
	AnnotationCronRunOnStartup   Annotation = "cronrunonstartup"
	AnnotationCronUnique         Annotation = "cronunique"
	AnnotationMiddleware         Annotation = "middleware"
	AnnotationName               Annotation = "name"
	AnnotationRoutePrefix        Annotation = "routeprefix"
)

func (Annotation) String

func (o Annotation) String() string

func (Annotation) ToUpper

func (o Annotation) ToUpper() string

type AnnotationItem

type AnnotationItem struct {
	Annotation Annotation
	// contains filtered or unexported fields
}

func NewAnnotationItem

func NewAnnotationItem(anno Annotation, value string) *AnnotationItem

func (*AnnotationItem) First

func (o *AnnotationItem) First() (str string)

func (*AnnotationItem) FirstBool

func (o *AnnotationItem) FirstBool() bool

func (*AnnotationItem) Values

func (o *AnnotationItem) Values() []string

type Comment

type Comment struct {
	// contains filtered or unexported fields
}

Comment is a component that store code comments.

func NewComment

func NewComment() *Comment

func (*Comment) Add

func (o *Comment) Add(text string)

Add adds comment line to component.

func (*Comment) GetAnnotation

func (o *Comment) GetAnnotation(anno Annotation) []*AnnotationItem

func (*Comment) GetAnnotations

func (o *Comment) GetAnnotations() map[Annotation][]*AnnotationItem

type EntityController

type EntityController struct {
	Line        int      // e.g. 24
	File        string   // e.g. app/controllers/home_controller.go
	Import      string   // e.g. sketch/app/controllers
	Package     string   // e.g. a0
	Middlewares []string // e.g. []string{"example"}
	Prefix      string   // e.g. /
	Struct      string   // e.g. HomeController
}

EntityController is a component used to store seeker result for controller.

func NewEntityController

func NewEntityController(typ string) *EntityController

NewEntityController creates a new entity.

type EntityCrontab

type EntityCrontab struct {
	Line                       int    // e.g. 24
	File                       string // e.g. app/crontabs/example_crontab.go
	Import                     string // e.g. sketch/app/crontabs
	Package                    string // e.g. a0
	NodeUnique, GloballyUnique bool
	RunOnStartup               bool
	Strategy                   crontab.Strategy // e.g. 0 0 0 * * * *
	Struct                     string           // e.g. ExampleCrontab
}

EntityCrontab is a component used to store seeker result for crontab.

func NewEntityCrontab

func NewEntityCrontab(typ string) *EntityCrontab

NewEntityCrontab creates a new entity.

type EntityHandler

type EntityHandler struct {
	Line        int      // e.g. 24
	File        string   // e.g. app/handlers/ping_handler.go
	Import      string   // e.g. app/handlers
	Package     string   // e.g. a0
	Func        string   // e.g. PingHandler
	Method      string   // e.g. GET
	Middlewares []string // e.g. []string{"example"}
	Uri         string   // e.g. /ping
}

EntityHandler is a component used to store seeker result for handler.

func NewEntityHandler

func NewEntityHandler(handler string) *EntityHandler

NewEntityHandler creates a new entity handler with default.

type EntityMiddleware

type EntityMiddleware struct {
	Line    int    // e.g. 24
	File    string // e.g. app/middlewares/example_middleware.go
	Import  string // e.g. sketch/app/middlewares
	Package string // e.g. a0
	Name    string // e.g. example
	Func    string // e.g. ExampleMiddleware
}

EntityMiddleware is a component used to store seeker result for middleware.

func NewEntityMiddleware

func NewEntityMiddleware(handler string) *EntityMiddleware

NewEntityMiddleware creates a new entity.

type Provider

type Provider struct {
	commands.Common
	Module, ModuleFolder                                                     string
	AnnotationPath, ControllerPath, CrontabPath, HandlerPath, MiddlewarePath string
	AnnotationPkg                                                            string
	WorkingDir                                                               string
	Override                                                                 bool
}

Provider is a component used to parse source to and save to annotation file.

func (*Provider) Before

func (o *Provider) Before(_ context.Context, container *console.Container, command *console.Command) (err error)

Before validate command options and accesses.

func (*Provider) Run

func (o *Provider) Run(_ context.Context, container *console.Container, command *console.Command) (err error)

Run provider process to seek files and generate.

type Resource

type Resource struct {
	Folder string // folder
	Path   string // app/middlewares
	Src    string // example_middleware.go

	// Import
	// is an application import package for scanned source file.
	//
	//   gitee.com/go-wares/framework-iris/_examples/framework/app/crontabs
	//   gitee.com/go-wares/framework-iris/_examples/framework/app/middlewares
	Import string

	// Package
	// is end word of Import field.
	//
	//   crontabs
	//   middlewares
	Package string
}

Resource is a component used to translate source code file.

func NewResource

func NewResource(path, src string) *Resource

NewResource creates a resource with given path and source.

func (*Resource) FillSrc

func (o *Resource) FillSrc(folders ...string)

FillSrc fill prefix of src field.

func (*Resource) Reader

func (o *Resource) Reader(provider *Provider, scanner ResourceScanner) (err error)

Reader read content of the resource.

type ResourceScanner

type ResourceScanner interface {
	Match(resource *Resource, comment *Comment, line int, text string) (err error)
}

type ResultController

type ResultController struct {
	Controllers      []*ResultControllerItem
	Package          string
	PackageList      []*ResultControllerAlias
	Script, Datetime template.HTML
	// contains filtered or unexported fields
}

ResultController is a component for crontab annotation result.

func NewResultController

func NewResultController(command *console.Command, pkg string) *ResultController

NewResultController creates a crontab result component.

func (*ResultController) AddEntity

func (o *ResultController) AddEntity(entity *EntityController)

AddEntity add a crontab entity.

func (*ResultController) AddPackage

func (o *ResultController) AddPackage(pkg string, rename bool) (alias string)

AddPackage add a package import.

func (*ResultController) Ready

func (o *ResultController) Ready() *ResultController

Ready is a hook when result ready.

type ResultControllerAlias

type ResultControllerAlias struct {
	Alias string // a0
	Pkg   string // sketch/app/crontabs
}

ResultControllerAlias is a component for package imports.

type ResultControllerItem

type ResultControllerItem struct {
	Line        int      // e.g. 24
	File        string   // e.g. sketch/app/controllers/home_controller.go
	Pkg         string   // e.g. a0
	Struct      string   // e.g. ExampleCrontab
	Middlewares []string // e.g. []string{"example"}
	Prefix      string   // e.g. /
}

ResultControllerItem is a component for crontab executors.

type ResultCrontab

type ResultCrontab struct {
	Crontabs         []*ResultCrontabItem
	Package          string
	PackageList      []*ResultCrontabAlias
	Script, Datetime template.HTML
	// contains filtered or unexported fields
}

ResultCrontab is a component for crontab annotation result.

func NewResultCrontab

func NewResultCrontab(command *console.Command, pkg string) *ResultCrontab

NewResultCrontab creates a crontab result component.

func (*ResultCrontab) AddEntity

func (o *ResultCrontab) AddEntity(entity *EntityCrontab)

AddEntity add a crontab entity.

func (*ResultCrontab) AddPackage

func (o *ResultCrontab) AddPackage(pkg string, rename bool) (alias string)

AddPackage add a package import.

func (*ResultCrontab) Ready

func (o *ResultCrontab) Ready() *ResultCrontab

Ready is a hook when result ready.

type ResultCrontabAlias

type ResultCrontabAlias struct {
	Alias string // a0
	Pkg   string // sketch/app/crontabs
}

ResultCrontabAlias is a component for package imports.

type ResultCrontabItem

type ResultCrontabItem struct {
	Line   int    // 24
	File   string // app/crontabs/example_crontab.go
	Pkg    string // a0
	Struct string // ExampleCrontab

	Strategy                                 string
	NodeUnique, GloballyUnique, RunOnStartup string
}

ResultCrontabItem is a component for crontab executors.

type ResultHandler

type ResultHandler struct {
	Handlers         []*ResultHandlerHandler
	Package          string
	PackageList      []*ResultHandlerAlias
	Script, Datetime template.HTML
	// contains filtered or unexported fields
}

ResultHandler is a component for handler annotation result.

func NewResultHandler

func NewResultHandler(command *console.Command, pkg string) *ResultHandler

NewResultHandler creates a handler result component.

func (*ResultHandler) AddEntity

func (o *ResultHandler) AddEntity(entity *EntityHandler)

AddEntity add a handler entity.

func (*ResultHandler) AddPackage

func (o *ResultHandler) AddPackage(pkg string, rename bool) (alias string)

AddPackage add a package import.

func (*ResultHandler) Ready

func (o *ResultHandler) Ready() *ResultHandler

Ready is a hook when result ready.

type ResultHandlerAlias

type ResultHandlerAlias struct {
	Alias string // a0
	Pkg   string // sketch/app/handlers
}

ResultHandlerAlias is a component for package imports.

type ResultHandlerHandler

type ResultHandlerHandler struct {
	Line int    // 24
	File string // app/handlers/example_handler.go
	Func string // ExampleHandler
	Pkg  string // a0

	Method      string
	Middlewares []string
	Path        string
}

ResultHandlerHandler is a component for handler executors.

type ResultMiddleware

type ResultMiddleware struct {
	Handlers         []*ResultMiddlewareHandler
	Package          string
	PackageList      []*ResultMiddlewareAlias
	Script, Datetime template.HTML
	// contains filtered or unexported fields
}

ResultMiddleware is a component for middleware annotation result.

func NewResultMiddleware

func NewResultMiddleware(command *console.Command, pkg string) *ResultMiddleware

NewResultMiddleware creates a middleware result component.

func (*ResultMiddleware) AddEntity

func (o *ResultMiddleware) AddEntity(entity *EntityMiddleware)

AddEntity add a middleware entity.

func (*ResultMiddleware) AddPackage

func (o *ResultMiddleware) AddPackage(pkg string, rename bool) (alias string)

AddPackage add a package import.

func (*ResultMiddleware) Ready

func (o *ResultMiddleware) Ready() *ResultMiddleware

Ready is a hook when result ready.

type ResultMiddlewareAlias

type ResultMiddlewareAlias struct {
	Alias string
	Pkg   string
}

ResultMiddlewareAlias is a component for package imports.

type ResultMiddlewareHandler

type ResultMiddlewareHandler struct {
	Line int    // 24
	File string // app/middlewares/example_middleware.go
	Func string // ExampleMiddleware
	Name string // example
	Pkg  string // a0
}

ResultMiddlewareHandler is a component for handler executors.

var AnnotationMiddlewares = map[string]iris.Handler{
    "example": a0.ExampleMiddleware,
    "Example2Middleware": a1.Example2Middleware,
}

type Seeker

type Seeker struct {
	Command   *console.Command
	Container *console.Container
	Provider  *Provider

	SeekerController *SeekerController
	SeekerCrontab    *SeekerCrontab
	SeekerHandler    *SeekerHandler
	SeekerMiddleware *SeekerMiddleware
}

Seeker is a component for seeker manager.

func (*Seeker) Do

func (o *Seeker) Do() (err error)

func (*Seeker) DoController

func (o *Seeker) DoController(path string) error

func (*Seeker) DoCrontab

func (o *Seeker) DoCrontab(path string) error

func (*Seeker) DoHandler

func (o *Seeker) DoHandler(path string) error

func (*Seeker) DoMiddleware

func (o *Seeker) DoMiddleware(path string) error

func (*Seeker) Save

func (o *Seeker) Save(data any, target, text string) (err error)

type SeekerController

type SeekerController struct {
	Seeker *Seeker
	// contains filtered or unexported fields
}

SeekerController is a component for controller files manager.

func (*SeekerController) Add

func (o *SeekerController) Add(resource *Resource) (err error)

Add adds a controller file resource to manager.

func (*SeekerController) Do

func (o *SeekerController) Do() (err error)

Do generate and save to target file.

func (*SeekerController) Match

func (o *SeekerController) Match(resource *Resource, comment *Comment, line int, text string) (err error)

Match matches the line text of a resource file.

func (*SeekerController) Target

func (o *SeekerController) Target() string

Target returns a file path for generated content.

type SeekerCrontab

type SeekerCrontab struct {
	Seeker *Seeker
	// contains filtered or unexported fields
}

SeekerCrontab is a component for crontab files manager.

func (*SeekerCrontab) Add

func (o *SeekerCrontab) Add(resource *Resource) (err error)

Add adds a crontab file resource to manager.

func (*SeekerCrontab) Do

func (o *SeekerCrontab) Do() (err error)

Do generate and save to target file.

func (*SeekerCrontab) Match

func (o *SeekerCrontab) Match(resource *Resource, comment *Comment, line int, text string) (err error)

Match matches the line text of a resource file.

func (*SeekerCrontab) Target

func (o *SeekerCrontab) Target() string

Target returns a file path for generated content.

type SeekerDo

type SeekerDo func(path string) (err error)

SeekerDo is a hook for directory scanner.

type SeekerFound

type SeekerFound interface {
	// Add
	// adds a file to seeker manager.
	Add(resource *Resource) (err error)

	// Do
	// generate and save result to target file.
	Do() (err error)

	// Target
	// returns a file path for generated content.
	Target() string
}

SeekerFound is an interface that called when source file found in a directory.

type SeekerHandler

type SeekerHandler struct {
	Seeker *Seeker
	// contains filtered or unexported fields
}

SeekerHandler is a component for handler files manager.

func (*SeekerHandler) Add

func (o *SeekerHandler) Add(resource *Resource) (err error)

Add adds a handler file resource to manager.

func (*SeekerHandler) Do

func (o *SeekerHandler) Do() (err error)

Do generate and save to target file.

func (*SeekerHandler) Match

func (o *SeekerHandler) Match(resource *Resource, comment *Comment, line int, text string) (err error)

Match matches the line text of a resource file.

func (*SeekerHandler) Target

func (o *SeekerHandler) Target() string

Target returns a file path for generated content.

type SeekerMiddleware

type SeekerMiddleware struct {
	Seeker *Seeker
	// contains filtered or unexported fields
}

SeekerMiddleware is a component for middleware files manager.

func (*SeekerMiddleware) Add

func (o *SeekerMiddleware) Add(resource *Resource) (err error)

Add adds a middleware file resource to manager.

func (*SeekerMiddleware) Do

func (o *SeekerMiddleware) Do() (err error)

Do generate and save to target file.

func (*SeekerMiddleware) Match

func (o *SeekerMiddleware) Match(resource *Resource, comment *Comment, line int, text string) (err error)

Match matches the line text of a resource file.

func (*SeekerMiddleware) Target

func (o *SeekerMiddleware) Target() string

Target returns a file path for generated content.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL