add Create function for gvar; add Uints/SliceUint function for gconv

This commit is contained in:
John
2019-10-01 10:42:34 +08:00
parent eba97277b2
commit c8cf46a5a7
7 changed files with 426 additions and 163 deletions

View File

@ -24,11 +24,19 @@ type Var struct {
safe bool // Concurrent safe or not.
}
// New returns a new Var with given <value>.
// The parameter <safe> used to specify whether using Var in concurrent-safety,
// New creates and returns a new *Var with given <value>.
// The optional parameter <safe> specifies whether Var is used in concurrent-safety,
// which is false in default.
func New(value interface{}, safe ...bool) *Var {
v := &Var{}
v := Create(value, safe...)
return &v
}
// Create creates and returns a new Var with given <value>.
// The optional parameter <safe> specifies whether Var is used in concurrent-safety,
// which is false in default.
func Create(value interface{}, safe ...bool) Var {
v := Var{}
if len(safe) > 0 && !safe[0] {
v.safe = true
v.value = gtype.NewInterface(value)
@ -95,6 +103,11 @@ func (v *Var) Int() int {
return gconv.Int(v.Val())
}
// Ints converts and returns <v> as []int.
func (v *Var) Ints() []int {
return gconv.Ints(v.Val())
}
// Int8 converts and returns <v> as int8.
func (v *Var) Int8() int8 {
return gconv.Int8(v.Val())
@ -120,6 +133,11 @@ func (v *Var) Uint() uint {
return gconv.Uint(v.Val())
}
// Uints converts and returns <v> as []uint.
func (v *Var) Uints() []uint {
return gconv.Uints(v.Val())
}
// Uint8 converts and returns <v> as uint8.
func (v *Var) Uint8() uint8 {
return gconv.Uint8(v.Val())
@ -150,11 +168,6 @@ func (v *Var) Float64() float64 {
return gconv.Float64(v.Val())
}
// Ints converts and returns <v> as []int.
func (v *Var) Ints() []int {
return gconv.Ints(v.Val())
}
// Floats converts and returns <v> as []float64.
func (v *Var) Floats() []float64 {
return gconv.Floats(v.Val())

View File

@ -0,0 +1,145 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gvar
import "testing"
var varObj = Create(nil)
func Benchmark_Obj_Set(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Set(i)
}
}
func Benchmark_Obj_Val(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Val()
}
}
func Benchmark_Obj_IsNil(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.IsNil()
}
}
func Benchmark_Obj_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Bytes()
}
}
func Benchmark_Obj_String(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.String()
}
}
func Benchmark_Obj_Bool(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Bool()
}
}
func Benchmark_Obj_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int()
}
}
func Benchmark_Obj_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int8()
}
}
func Benchmark_Obj_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int16()
}
}
func Benchmark_Obj_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int32()
}
}
func Benchmark_Obj_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int64()
}
}
func Benchmark_Obj_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint()
}
}
func Benchmark_Obj_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint8()
}
}
func Benchmark_Obj_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint16()
}
}
func Benchmark_Obj_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint32()
}
}
func Benchmark_Obj_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint64()
}
}
func Benchmark_Obj_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Float32()
}
}
func Benchmark_Obj_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Float64()
}
}
func Benchmark_Obj_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Ints()
}
}
func Benchmark_Obj_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Strings()
}
}
func Benchmark_Obj_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Floats()
}
}
func Benchmark_Obj_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Interfaces()
}
}

View File

@ -0,0 +1,145 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gvar
import "testing"
var varPtr = New(nil)
func Benchmark_Ptr_Set(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Set(i)
}
}
func Benchmark_Ptr_Val(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Val()
}
}
func Benchmark_Ptr_IsNil(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.IsNil()
}
}
func Benchmark_Ptr_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Bytes()
}
}
func Benchmark_Ptr_String(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.String()
}
}
func Benchmark_Ptr_Bool(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Bool()
}
}
func Benchmark_Ptr_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Int()
}
}
func Benchmark_Ptr_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Int8()
}
}
func Benchmark_Ptr_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Int16()
}
}
func Benchmark_Ptr_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Int32()
}
}
func Benchmark_Ptr_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Int64()
}
}
func Benchmark_Ptr_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Uint()
}
}
func Benchmark_Ptr_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Uint8()
}
}
func Benchmark_Ptr_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Uint16()
}
}
func Benchmark_Ptr_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Uint32()
}
}
func Benchmark_Ptr_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Uint64()
}
}
func Benchmark_Ptr_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Float32()
}
}
func Benchmark_Ptr_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Float64()
}
}
func Benchmark_Ptr_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Ints()
}
}
func Benchmark_Ptr_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Strings()
}
}
func Benchmark_Ptr_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Floats()
}
}
func Benchmark_Ptr_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
varPtr.Interfaces()
}
}

View File

@ -1,145 +0,0 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gvar
import "testing"
var vn = New(nil)
func Benchmark_Set(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Set(i)
}
}
func Benchmark_Val(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Val()
}
}
func Benchmark_IsNil(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.IsNil()
}
}
func Benchmark_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Bytes()
}
}
func Benchmark_String(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.String()
}
}
func Benchmark_Bool(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Bool()
}
}
func Benchmark_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Int()
}
}
func Benchmark_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Int8()
}
}
func Benchmark_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Int16()
}
}
func Benchmark_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Int32()
}
}
func Benchmark_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Int64()
}
}
func Benchmark_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Uint()
}
}
func Benchmark_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Uint8()
}
}
func Benchmark_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Uint16()
}
}
func Benchmark_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Uint32()
}
}
func Benchmark_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Uint64()
}
}
func Benchmark_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Float32()
}
}
func Benchmark_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Float64()
}
}
func Benchmark_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Ints()
}
}
func Benchmark_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Strings()
}
}
func Benchmark_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Floats()
}
}
func Benchmark_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
vn.Interfaces()
}
}

View File

@ -21,6 +21,20 @@ import (
)
func Test_Set(t *testing.T) {
gtest.Case(t, func() {
var v gvar.Var
v.Set(123.456)
gtest.Assert(v.Val(), 123.456)
})
gtest.Case(t, func() {
var v gvar.Var
v.Set(123.456)
gtest.Assert(v.Val(), 123.456)
})
gtest.Case(t, func() {
v := gvar.Create(123.456)
gtest.Assert(v.Val(), 123.456)
})
gtest.Case(t, func() {
objOne := gvar.New("old", true)
objOneOld, _ := objOne.Set("new").(string)

View File

@ -18,6 +18,11 @@ func NewVar(i interface{}, safe ...bool) *Var {
return gvar.New(i, safe...)
}
// CreateVar returns a gvar.Var.
func CreateVar(i interface{}, safe ...bool) Var {
return gvar.Create(i, safe...)
}
// Wait blocks until all the web servers shutdown.
func Wait() {
ghttp.Wait()

View File

@ -19,6 +19,11 @@ func SliceInt(i interface{}) []int {
return Ints(i)
}
// SliceUint is alias of Uints.
func SliceUint(i interface{}) []uint {
return Uints(i)
}
// SliceStr is alias of Strings.
func SliceStr(i interface{}) []string {
return Strings(i)
@ -70,43 +75,47 @@ func Ints(i interface{}) []int {
}
case []int8:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []int16:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []int32:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []int64:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []uint:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []uint8:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []uint16:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []uint32:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []uint64:
for _, v := range value {
array = append(array, Int(v))
array = append(array, int(v))
}
case []bool:
for _, v := range value {
array = append(array, Int(v))
if v {
array = append(array, 1)
} else {
array = append(array, 0)
}
}
case []float32:
for _, v := range value {
@ -131,6 +140,83 @@ func Ints(i interface{}) []int {
}
}
// Uints converts <i> to []uint.
func Uints(i interface{}) []uint {
if i == nil {
return nil
}
if r, ok := i.([]uint); ok {
return r
} else {
array := make([]uint, 0)
switch value := i.(type) {
case []string:
for _, v := range value {
array = append(array, Uint(v))
}
case []int8:
for _, v := range value {
array = append(array, uint(v))
}
case []int16:
for _, v := range value {
array = append(array, uint(v))
}
case []int32:
for _, v := range value {
array = append(array, uint(v))
}
case []int64:
for _, v := range value {
array = append(array, uint(v))
}
case []uint8:
for _, v := range value {
array = append(array, uint(v))
}
case []uint16:
for _, v := range value {
array = append(array, uint(v))
}
case []uint32:
for _, v := range value {
array = append(array, uint(v))
}
case []uint64:
for _, v := range value {
array = append(array, uint(v))
}
case []bool:
for _, v := range value {
if v {
array = append(array, 1)
} else {
array = append(array, 0)
}
}
case []float32:
for _, v := range value {
array = append(array, Uint(v))
}
case []float64:
for _, v := range value {
array = append(array, Uint(v))
}
case []interface{}:
for _, v := range value {
array = append(array, Uint(v))
}
case [][]byte:
for _, v := range value {
array = append(array, Uint(v))
}
default:
return []uint{Uint(i)}
}
return array
}
}
// Strings converts <i> to []string.
func Strings(i interface{}) []string {
if i == nil {