feat: code finishing
Some checks are pending
Test and coverage / build (1.21.x) (push) Waiting to run
Test and coverage / build (1.22.x) (push) Waiting to run
Test and coverage / build (1.23.x) (push) Waiting to run

This commit is contained in:
dapeng 2024-11-25 10:15:44 +08:00
parent 8f332a94a2
commit 71cb1ef8b6
3 changed files with 40 additions and 34 deletions

View File

@ -246,8 +246,11 @@ func (c *cemetery) reviveByVampire2(goner Goner, extConfig string, v reflect.Val
return false, nil
}
func (c *cemetery) reviveFieldByType(field reflect.StructField, v reflect.Value, goneTypeName string) (deps []Tomb, suc bool) {
container := c.getGonerContainerByType(field.Type, fmt.Sprintf("%s.%s", goneTypeName, field.Name))
func (c *cemetery) reviveFieldByType(gonerType reflect.Type, field reflect.StructField, v reflect.Value) (deps []Tomb, suc bool) {
goneTypeName := getNameByType(gonerType)
filedName := fmt.Sprintf("%s.%s", goneTypeName, field.Name)
container := c.getGonerContainerByType(field.Type, filedName)
if container != nil {
setFieldValue(v, container.GetGoner())
suc = true
@ -256,6 +259,23 @@ func (c *cemetery) reviveFieldByType(field reflect.StructField, v reflect.Value,
return
}
func (c *cemetery) reviveFieldByProvider(tag string, field reflect.StructField, v reflect.Value) (deps []Tomb, suc bool, err error) {
if aTomb, ok := c.providerMap[field.Type]; ok {
err = c.checkRevive(aTomb)
if err != nil {
return
}
deps = append(deps, aTomb)
vampire2 := aTomb.GetGoner().(Vampire2)
_, extConfig := parseGoneTagId(tag)
err = vampire2.Suck(extConfig, v, field)
suc = err == nil
}
return
}
func (c *cemetery) reviveSpecialTypeFields(field reflect.StructField, v reflect.Value) (deps []Tomb, suc bool) {
t := field.Type
switch t.Kind() {
@ -327,10 +347,7 @@ func (c *cemetery) reviveOneAndItsDeps(tomb Tomb) (deps []Tomb, err error) {
return
}
func (c *cemetery) ReviveOne(goner any) (deps []Tomb, err error) {
gonerType := reflect.TypeOf(goner).Elem()
gonerValue := reflect.ValueOf(goner).Elem()
func getNameByType(gonerType reflect.Type) string {
goneTypeName := gonerType.PkgPath()
if goneTypeName == "" {
goneTypeName = gonerType.Name()
@ -341,6 +358,12 @@ func (c *cemetery) ReviveOne(goner any) (deps []Tomb, err error) {
if goneTypeName == "" {
goneTypeName = "[Anonymous Goner]"
}
return goneTypeName
}
func (c *cemetery) ReviveOne(goner any) (deps []Tomb, err error) {
gonerType := reflect.TypeOf(goner).Elem()
gonerValue := reflect.ValueOf(goner).Elem()
for i := 0; i < gonerValue.NumField(); i++ {
field := gonerType.Field(i)
@ -370,15 +393,15 @@ func (c *cemetery) ReviveOne(goner any) (deps []Tomb, err error) {
continue
}
if tmpDeps, suc, err = c.reviveFieldByProvider(tag, field, v, goneTypeName); err != nil {
return
} else if suc {
// inject by type
if tmpDeps, suc = c.reviveFieldByType(gonerType, field, v); suc {
deps = append(deps, tmpDeps...)
continue
}
// inject by type
if tmpDeps, suc = c.reviveFieldByType(field, v, goneTypeName); suc {
if tmpDeps, suc, err = c.reviveFieldByProvider(tag, field, v); err != nil {
return
} else if suc {
deps = append(deps, tmpDeps...)
continue
}
@ -394,23 +417,6 @@ func (c *cemetery) ReviveOne(goner any) (deps []Tomb, err error) {
return
}
func (c *cemetery) reviveFieldByProvider(tag string, field reflect.StructField, v reflect.Value, goneTypeName string) (deps []Tomb, suc bool, err error) {
if aTomb, ok := c.providerMap[field.Type]; ok {
err = c.checkRevive(aTomb)
if err != nil {
return
}
deps = append(deps, aTomb)
vampire2 := aTomb.GetGoner().(Vampire2)
_, extConfig := parseGoneTagId(tag)
err = vampire2.Suck(extConfig, v, field)
suc = err == nil
}
return
}
func (c *cemetery) reviveOneFromTomb(tomb Tomb) (deps []Tomb, err error) {
goner := tomb.GetGoner()
deps, err = c.ReviveOne(goner)

View File

@ -317,12 +317,6 @@ func (p *provider[T]) Suck(conf string, v reflect.Value, field reflect.StructFie
return nil
}
// Provider is a factory function template, which return `T` instance and goner framework will call this function to
// create a new `T` instance for Type `T` fields of a struct who need injected.
// The parameter `tagConf` is the tag string of the field, and the parameter `param` should be an anonymous struct which field can be tag by `gone` and injected by goner framework.
// The function should be used for NewProviderPriest to create a provider priest.
type Provider[P, T any] func(tagConf string, param P) (T, error)
// NewProviderPriest create a provider priest function for goner from a function like: `func(tagConf string, injectableStructParam struct{}) (provideType T, err error)`
// example:
// ```go

View File

@ -166,6 +166,12 @@ type Vampire2 interface {
Suck(conf string, v reflect.Value, field reflect.StructField) error
}
// Provider is a factory function template, which return `T` instance and goner framework will call this function to
// create a new `T` instance for Type `T` fields of a struct who need injected.
// The parameter `tagConf` is the tag string of the field, and the parameter `param` should be an anonymous struct which field can be tag by `gone` and injected by goner framework.
// The function should be used for NewProviderPriest to create a provider priest.
type Provider[P, T any] func(tagConf string, param P) (T, error)
/*
Three errors:
- Internal error, internal system error, which can only be repaired by system upgrade.