From 2e9cf2def7484032e8e3f203639f7a6a1da2e915 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 4 Jan 2018 16:34:04 +0200 Subject: [PATCH] linting, 99% is not fine let's do it 100% Former-commit-id: 45d246e8d14deb7c238e01e6a2b1648a9ddf39c0 --- hero/di/di.go | 2 ++ hero/di/func.go | 2 ++ hero/di/reflect.go | 10 ++++++++++ hero/di/struct.go | 22 ++++++++++++++++++++++ websocket/config.go | 2 +- 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/hero/di/di.go b/hero/di/di.go index e4144793..3b8b9b7b 100644 --- a/hero/di/di.go +++ b/hero/di/di.go @@ -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" diff --git a/hero/di/func.go b/hero/di/func.go index fe69dfb0..da81d6f3 100644 --- a/hero/di/func.go +++ b/hero/di/func.go @@ -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 { diff --git a/hero/di/reflect.go b/hero/di/reflect.go index 9713ddb8..3b91ebf8 100644 --- a/hero/di/reflect.go +++ b/hero/di/reflect.go @@ -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: diff --git a/hero/di/struct.go b/hero/di/struct.go index cc48b1e4..b2779765 100644 --- a/hero/di/struct.go +++ b/hero/di/struct.go @@ -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 diff --git a/websocket/config.go b/websocket/config.go index 4280c8e9..cc0ac310 100644 --- a/websocket/config.go +++ b/websocket/config.go @@ -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