mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
linting, 99% is not fine let's do it 100%
Former-commit-id: 45d246e8d14deb7c238e01e6a2b1648a9ddf39c0
This commit is contained in:
parent
d946d11dff
commit
2e9cf2def7
|
@ -1,3 +1,5 @@
|
|||
// Package di provides dependency injection for the Iris Hero and Iris MVC new features.
|
||||
// It's used internally by "hero" and "mvc" packages directly.
|
||||
package di
|
||||
|
||||
import "reflect"
|
||||
|
|
|
@ -145,6 +145,8 @@ func (s *FuncInjector) addValue(inputIndex int, value reflect.Value) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// Retry used to add missing dependencies, i.e path parameter built'n bindings if not already exists
|
||||
// in the `hero.Handler`, once, only for that func injector.
|
||||
func (s *FuncInjector) Retry(retryFn func(inIndex int, inTyp reflect.Type) (reflect.Value, bool)) bool {
|
||||
for _, missing := range s.lost {
|
||||
if missing.found {
|
||||
|
|
|
@ -58,10 +58,15 @@ func IsZero(v reflect.Value) bool {
|
|||
return v.Interface() == zero.Interface()
|
||||
}
|
||||
|
||||
// IndirectValue returns the reflect.Value that "v" points to.
|
||||
// If "v" is a nil pointer, Indirect returns a zero Value.
|
||||
// If "v" is not a pointer, Indirect returns v.
|
||||
func IndirectValue(v reflect.Value) reflect.Value {
|
||||
return reflect.Indirect(v)
|
||||
}
|
||||
|
||||
// ValueOf returns the reflect.Value of "o".
|
||||
// If "o" is already a reflect.Value returns "o".
|
||||
func ValueOf(o interface{}) reflect.Value {
|
||||
if v, ok := o.(reflect.Value); ok {
|
||||
return v
|
||||
|
@ -70,6 +75,8 @@ func ValueOf(o interface{}) reflect.Value {
|
|||
return reflect.ValueOf(o)
|
||||
}
|
||||
|
||||
// ValuesOf same as `ValueOf` but accepts a slice of
|
||||
// somethings and returns a slice of reflect.Value.
|
||||
func ValuesOf(valuesAsInterface []interface{}) (values []reflect.Value) {
|
||||
for _, v := range valuesAsInterface {
|
||||
values = append(values, ValueOf(v))
|
||||
|
@ -77,6 +84,9 @@ func ValuesOf(valuesAsInterface []interface{}) (values []reflect.Value) {
|
|||
return
|
||||
}
|
||||
|
||||
// IndirectType returns the value of a pointer-type "typ".
|
||||
// If "typ" is a pointer, array, chan, map or slice it returns its Elem,
|
||||
// otherwise returns the typ as it's.
|
||||
func IndirectType(typ reflect.Type) reflect.Type {
|
||||
switch typ.Kind() {
|
||||
case reflect.Ptr, reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
||||
|
|
|
@ -5,10 +5,18 @@ import (
|
|||
"reflect"
|
||||
)
|
||||
|
||||
// Scope is the struct injector's struct value scope/permant state.
|
||||
// See `Stateless` and `Singleton`.
|
||||
type Scope uint8
|
||||
|
||||
const (
|
||||
// Stateless is the scope that the struct should be different on each binding,
|
||||
// think it like `Request Scoped`, per-request struct for mvc.
|
||||
Stateless Scope = iota
|
||||
// Singleton is the scope that the struct is the same
|
||||
// between calls, it has no dynamic dependencies or
|
||||
// any unexported fields that is not seted on creation,
|
||||
// so it doesn't need to be created on each call/request.
|
||||
Singleton
|
||||
)
|
||||
|
||||
|
@ -169,6 +177,9 @@ func (s *StructInjector) String() (trace string) {
|
|||
return
|
||||
}
|
||||
|
||||
// Inject accepts a destination struct and any optional context value(s),
|
||||
// hero and mvc takes only one context value and this is the `context.Contex`.
|
||||
// It applies the bindings to the "dest" struct. It calls the InjectElem.
|
||||
func (s *StructInjector) Inject(dest interface{}, ctx ...reflect.Value) {
|
||||
if dest == nil {
|
||||
return
|
||||
|
@ -178,6 +189,7 @@ func (s *StructInjector) Inject(dest interface{}, ctx ...reflect.Value) {
|
|||
s.InjectElem(v, ctx...)
|
||||
}
|
||||
|
||||
// InjectElem same as `Inject` but accepts a reflect.Value and bind the necessary fields directly.
|
||||
func (s *StructInjector) InjectElem(destElem reflect.Value, ctx ...reflect.Value) {
|
||||
for _, f := range s.fields {
|
||||
f.Object.Assign(ctx, func(v reflect.Value) {
|
||||
|
@ -186,6 +198,12 @@ func (s *StructInjector) InjectElem(destElem reflect.Value, ctx ...reflect.Value
|
|||
}
|
||||
}
|
||||
|
||||
// Acquire returns a new value of the struct or
|
||||
// the same struct that is used for resolving the dependencies.
|
||||
// If the scope is marked as singleton then it returns the first instance,
|
||||
// otherwise it creates new and returns it.
|
||||
//
|
||||
// See `Singleton` and `Stateless` for more.
|
||||
func (s *StructInjector) Acquire() reflect.Value {
|
||||
if s.Scope == Singleton {
|
||||
return s.initRef
|
||||
|
@ -193,6 +211,10 @@ func (s *StructInjector) Acquire() reflect.Value {
|
|||
return reflect.New(s.elemType)
|
||||
}
|
||||
|
||||
// AcquireSlice same as `Acquire` but it returns a slice of
|
||||
// values structs, this can be used when a struct is passed as an input parameter
|
||||
// on a function, again if singleton then it returns a pre-created slice which contains
|
||||
// the first struct value given by the struct injector's user.
|
||||
func (s *StructInjector) AcquireSlice() []reflect.Value {
|
||||
if s.Scope == Singleton {
|
||||
return s.initRefAsSlice
|
||||
|
|
|
@ -81,7 +81,7 @@ type Config struct {
|
|||
// guarantee that compression will be supported. Currently only "no context
|
||||
// takeover" modes are supported.
|
||||
//
|
||||
// Defauls to false and it should be remain as it is, unless special requirements.
|
||||
// Defaults to false and it should be remain as it is, unless special requirements.
|
||||
EnableCompression bool
|
||||
|
||||
// Subprotocols specifies the server's supported protocols in order of
|
||||
|
|
Loading…
Reference in New Issue
Block a user