1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
inject_processor.go 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/**
*@describe:
*@author wfl19/Kristas
*@date 2021/04/10
*/
package datasource_processor
import (
"database/sql"
"gitee.com/kristas/booting-go/framework/common/util/reflectx"
"gitee.com/kristas/booting-go/framework/core/bean"
"gitee.com/kristas/booting-go/framework/core/component"
"gitee.com/kristas/booting-go/framework/data/wrapper_manager"
"gitee.com/kristas/booting-go/framework/logging"
"reflect"
)
type DatasourceInjectProcessor struct {
Log logging.Logger `bean:"log"`
}
func (d *DatasourceInjectProcessor) Filter(bean bean.Bean) bool {
_, ok := bean.(component.Repository)
return ok
}
func (d *DatasourceInjectProcessor) Do(bean bean.Bean) {
dbType := reflect.TypeOf(new(sql.DB))
reflectx.WalkStructField(bean, func(field reflect.StructField, value reflect.Value) {
if database, ok := field.Tag.Lookup("db"); ok && field.Type == dbType {
wrapper := wrapper_manager.Get(database)
if wrapper == nil {
d.Log.Panicf("database [%s] in TAG not found", database)
}
value.Set(reflect.ValueOf(wrapper.DB()))
}
}, true)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.3.2

搜索帮助