hero: optimize *binding'String

For faster execution,
use strconv.Itoa rather than fmt.Sprintf to convert an integer to string,
remove if statement in loop.
This commit is contained in:
Jes Cok 2023-06-15 07:34:37 +08:00
parent 31163ed4db
commit 7fb00911d4

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"sort" "sort"
"strconv"
"github.com/kataras/iris/v12/context" "github.com/kataras/iris/v12/context"
) )
@ -43,15 +44,14 @@ func newStructFieldInput(f reflect.StructField) *Input {
// String returns the string representation of a binding. // String returns the string representation of a binding.
func (b *binding) String() string { func (b *binding) String() string {
index := fmt.Sprintf("%d", b.Input.Index) var index string
if len(b.Input.StructFieldIndex) > 0 { if len(b.Input.StructFieldIndex) > 0 {
for j, i := range b.Input.StructFieldIndex { index = strconv.Itoa(b.Input.StructFieldIndex[0])
if j == 0 { for _, i := range b.Input.StructFieldIndex[1:] {
index = fmt.Sprintf("%d", i)
continue
}
index += fmt.Sprintf(".%d", i) index += fmt.Sprintf(".%d", i)
} }
} else {
index = strconv.Itoa(b.Input.Index)
} }
return fmt.Sprintf("[%s:%s] maps to [%s]", index, b.Input.Type.String(), b.Dependency) return fmt.Sprintf("[%s:%s] maps to [%s]", index, b.Input.Type.String(), b.Dependency)