-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
255 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
//Annotation 注解封装 | ||
type Annotation interface { | ||
SetTag(tag reflect.StructTag) | ||
String() string | ||
} | ||
|
||
var AnnotationList []Annotation | ||
|
||
func IsAnnotation(t reflect.Type) bool { | ||
for _, item := range AnnotationList { | ||
if reflect.TypeOf(item) == t { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func init() { | ||
AnnotationList = append(AnnotationList, new(Value)) | ||
} | ||
|
||
//Value tag结构封装 | ||
type Value struct { | ||
tag reflect.StructTag | ||
BeanFactory *BeanFactory | ||
} | ||
|
||
func (v *Value) SetTag(tag reflect.StructTag) { | ||
v.tag = tag | ||
} | ||
|
||
func (v *Value) String() string { | ||
prefix := v.tag.Get("prefix") | ||
if prefix == "" { | ||
return "" | ||
} | ||
if config := v.BeanFactory.GetBean(new(SysConfig)); config != nil { | ||
rV := GetConfigValue(config.(*SysConfig).Config, strings.Split(prefix, "."), 0) | ||
if rV != nil { | ||
return fmt.Sprintf("%v", rV) | ||
} | ||
return "" | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package internal | ||
|
||
import "reflect" | ||
|
||
//BeanFactory 依赖注入工厂 | ||
type BeanFactory struct { | ||
beans []interface{} | ||
} | ||
|
||
func NewBeanFactory() *BeanFactory { | ||
bf := &BeanFactory{beans: make([]interface{}, 0)} | ||
bf.beans = append(bf.beans, bf) | ||
return bf | ||
} | ||
|
||
func (b *BeanFactory) setBean(beans ...interface{}) { | ||
b.beans = append(b.beans, beans...) | ||
} | ||
|
||
func (b *BeanFactory) GetBean(bean interface{}) interface{} { | ||
return b.getBean(reflect.TypeOf(bean)) | ||
} | ||
|
||
func (b *BeanFactory) getBean(r reflect.Type) interface{} { | ||
for _, p := range b.beans { | ||
if r == reflect.TypeOf(p) { | ||
return p | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (t *BeanFactory) Inject(v IClass) { | ||
vRef := reflect.ValueOf(v).Elem() | ||
vT := reflect.TypeOf(v).Elem() | ||
for i := 0; i < vRef.NumField(); i++ { | ||
f := vRef.Field(i) | ||
if !f.IsNil() || f.Kind() != reflect.Ptr { | ||
continue | ||
} | ||
if IsAnnotation(f.Type()) { | ||
f.Set(reflect.New(f.Type().Elem())) | ||
f.Interface().(Annotation).SetTag(vT.Field(i).Tag) | ||
t.inject(f.Interface()) | ||
continue | ||
} | ||
|
||
if p := t.getBean(f.Type()); p != nil { | ||
// vRef.Field(0).Type() --> 指针 *GormAdapter | ||
// vRef.Field(0).Type().Elem() -->指针指向的对象 GormAdapter | ||
f.Set(reflect.New(f.Type().Elem())) | ||
f.Elem().Set(reflect.ValueOf(p).Elem()) | ||
} | ||
} | ||
} | ||
|
||
func (b *BeanFactory) inject(object interface{}) { | ||
rV := reflect.ValueOf(object) | ||
if rV.Kind() == reflect.Ptr { | ||
rV = rV.Elem() | ||
} | ||
for i := 0; i < rV.NumField(); i++ { | ||
f := rV.Field(i) | ||
if f.Kind() != reflect.Ptr || !f.IsNil() { | ||
continue | ||
} | ||
if p := b.getBean(f.Type()); p != nil && f.CanInterface() { | ||
f.Set(reflect.New(f.Type().Elem())) | ||
f.Elem().Set(reflect.ValueOf(p).Elem()) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ func init() { | |
} | ||
} | ||
|
||
//Responder 响应封装 | ||
type Responder interface { | ||
RespondTo() gin.HandlerFunc | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/robfig/cron" | ||
"sync" | ||
) | ||
|
||
type TaskFunc func(params ...interface{}) | ||
|
||
var ( | ||
taskList chan *TaskExecutor | ||
once sync.Once | ||
onceCron sync.Once | ||
taskCron *cron.Cron | ||
) | ||
|
||
//TaskExecutor 任务组件 | ||
type TaskExecutor struct { | ||
f TaskFunc | ||
p []interface{} | ||
callback func() | ||
} | ||
|
||
func getTaskList() chan *TaskExecutor { | ||
once.Do(func() { | ||
taskList = make(chan *TaskExecutor) | ||
}) | ||
return taskList | ||
} | ||
|
||
func init() { | ||
ch := getTaskList() | ||
go func() { | ||
for t := range ch { | ||
doTask(t) | ||
} | ||
}() | ||
} | ||
|
||
func doTask(t *TaskExecutor) { | ||
go func() { | ||
defer func() { | ||
if t.callback != nil { | ||
t.callback() | ||
} | ||
}() | ||
t.Exec() | ||
}() | ||
} | ||
func NewTaskExecutor(f TaskFunc, p []interface{}, callback func()) *TaskExecutor { | ||
return &TaskExecutor{f: f, p: p, callback: callback} | ||
} | ||
|
||
func (t *TaskExecutor) Exec() { | ||
t.f(t.p...) | ||
} | ||
|
||
func Task(f TaskFunc, callback func(), params ...interface{}) { | ||
if f == nil { | ||
return | ||
} | ||
go func() { | ||
getTaskList() <- NewTaskExecutor(f, params, callback) | ||
}() | ||
} | ||
|
||
func getCronTask() *cron.Cron { | ||
onceCron.Do(func() { | ||
taskCron = cron.New() | ||
}) | ||
return taskCron | ||
} |
Oops, something went wrong.