1 Star 0 Fork 0

非一般/framework-iris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
seeker_handler.go 3.47 KB
一键复制 编辑 原始数据 按行查看 历史
非一般 提交于 2024-08-21 15:12 . new version
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-08-20
package gen_annotation
import (
_ "embed"
"fmt"
)
//go:embed template_handler.tpl
var templateHandler string
// SeekerHandler
// is a component for handler files manager.
type SeekerHandler struct {
Seeker *Seeker
result *ResultHandler
target string
}
// Add
// adds a handler file resource to manager.
func (o *SeekerHandler) Add(resource *Resource) (err error) {
return resource.Reader(o.Seeker.Provider, o)
}
// Do
// generate and save to target file.
func (o *SeekerHandler) Do() (err error) {
err = o.Seeker.Save(o.result.Ready(), o.target, templateHandler)
return
}
// Match
// matches the line text of a resource file.
func (o *SeekerHandler) Match(resource *Resource, comment *Comment, line int, text string) (err error) {
var (
entity *EntityHandler
match []string
)
// Match
// text string and return if not match on handler.
if match = ResourceRegexMatchHandler.FindStringSubmatch(text); len(match) == 0 {
return
}
// Creates
// a handler entity.
entity = NewEntityHandler(match[1])
entity.Import = resource.Import
entity.Package = resource.Package
entity.File = fmt.Sprintf(`%s/%s`, resource.Path, resource.Src)
entity.Line = line
// Middleware for handler.
for anno, list := range comment.GetAnnotations() {
switch anno {
case AnnotationAny, AnnotationGet, AnnotationPost, AnnotationDelete, AnnotationHead, AnnotationOptions, AnnotationPatch, AnnotationPut:
// Route for handler that defined with `@Any("uri")`,
// `@Get("uri")`, `@Post("uri")`, `@Delete("uri")`,
// `@Head("uri")`, `@Options("uri")`, `@Patch("uri")`,
// `@Put("uri")`.
for _, item := range list {
if s := item.First(); s != "" {
entity.Uri = s
entity.Method = anno.ToUpper()
break
}
}
case AnnotationMiddleware:
// Middleware for handler that defined with `@Middleware("name")`.
for _, item := range list {
for _, s := range item.Values() {
if s != "" {
entity.Middlewares = append(entity.Middlewares, s)
}
}
}
}
}
// Parse succeed.
o.result.AddEntity(entity)
o.Seeker.Container.GetOutput().Info(` - found: source="%s/%s:%d", handler="%s", request="%s %s"`, resource.Path, resource.Src, line, entity.Func, entity.Method, entity.Uri)
return
}
// Target
// returns a file path for generated content.
func (o *SeekerHandler) Target() string { return o.target }
// +---------------------------------------------------------------------------+
// | Access methods |
// +---------------------------------------------------------------------------+
func (o *SeekerHandler) init() *SeekerHandler {
o.result = NewResultHandler(o.Seeker.Command, o.Seeker.Provider.AnnotationPkg)
o.target = fmt.Sprintf(`%s/%s/annotation_handlers.go`, o.Seeker.Provider.WorkingDir, o.Seeker.Provider.AnnotationPath)
o.Seeker.Container.GetOutput().Info(`+ [scanner:handler]`)
return o
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/go-wares/framework-iris.git
git@gitee.com:go-wares/framework-iris.git
go-wares
framework-iris
framework-iris
v1.1.3

搜索帮助