mirror of
https://gitee.com/johng/gf
synced 2026-07-06 21:45:34 +08:00
131
container/gvar/gvar_vars.go
Normal file
131
container/gvar/gvar_vars.go
Normal file
@ -0,0 +1,131 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). 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.
|
||||
|
||||
package gvar
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// Vars is a slice of *Var.
|
||||
type Vars []*Var
|
||||
|
||||
// Strings converts and returns `vs` as []string.
|
||||
func (vs Vars) Strings() (s []string) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.String())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Interfaces converts and returns `vs` as []interface{}.
|
||||
func (vs Vars) Interfaces() (s []interface{}) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Val())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Float32s converts and returns `vs` as []float32.
|
||||
func (vs Vars) Float32s() (s []float32) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Float32())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Float64s converts and returns `vs` as []float64.
|
||||
func (vs Vars) Float64s() (s []float64) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Float64())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Ints converts and returns `vs` as []Int.
|
||||
func (vs Vars) Ints() (s []int) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Int())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Int8s converts and returns `vs` as []int8.
|
||||
func (vs Vars) Int8s() (s []int8) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Int8())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Int16s converts and returns `vs` as []int16.
|
||||
func (vs Vars) Int16s() (s []int16) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Int16())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Int32s converts and returns `vs` as []int32.
|
||||
func (vs Vars) Int32s() (s []int32) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Int32())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Int64s converts and returns `vs` as []int64.
|
||||
func (vs Vars) Int64s() (s []int64) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Int64())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Uints converts and returns `vs` as []uint.
|
||||
func (vs Vars) Uints() (s []uint) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Uint())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Uint8s converts and returns `vs` as []uint8.
|
||||
func (vs Vars) Uint8s() (s []uint8) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Uint8())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Uint16s converts and returns `vs` as []uint16.
|
||||
func (vs Vars) Uint16s() (s []uint16) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Uint16())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Uint32s converts and returns `vs` as []uint32.
|
||||
func (vs Vars) Uint32s() (s []uint32) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Uint32())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Uint64s converts and returns `vs` as []uint64.
|
||||
func (vs Vars) Uint64s() (s []uint64) {
|
||||
for _, v := range vs {
|
||||
s = append(s, v.Uint64())
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Scan converts `vs` to []struct/[]*struct.
|
||||
func (vs Vars) Scan(pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.Structs(vs.Interfaces(), pointer, mapping...)
|
||||
}
|
||||
60
container/gvar/gvar_z_unit_vars_test.go
Normal file
60
container/gvar/gvar_z_unit_vars_test.go
Normal file
@ -0,0 +1,60 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). 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.
|
||||
|
||||
package gvar_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func TestVars(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var vs = gvar.Vars{
|
||||
gvar.New(1),
|
||||
gvar.New(2),
|
||||
gvar.New(3),
|
||||
}
|
||||
t.AssertEQ(vs.Strings(), []string{"1", "2", "3"})
|
||||
t.AssertEQ(vs.Interfaces(), []interface{}{1, 2, 3})
|
||||
t.AssertEQ(vs.Float32s(), []float32{1, 2, 3})
|
||||
t.AssertEQ(vs.Float64s(), []float64{1, 2, 3})
|
||||
t.AssertEQ(vs.Ints(), []int{1, 2, 3})
|
||||
t.AssertEQ(vs.Int8s(), []int8{1, 2, 3})
|
||||
t.AssertEQ(vs.Int16s(), []int16{1, 2, 3})
|
||||
t.AssertEQ(vs.Int32s(), []int32{1, 2, 3})
|
||||
t.AssertEQ(vs.Int64s(), []int64{1, 2, 3})
|
||||
t.AssertEQ(vs.Uints(), []uint{1, 2, 3})
|
||||
t.AssertEQ(vs.Uint8s(), []uint8{1, 2, 3})
|
||||
t.AssertEQ(vs.Uint16s(), []uint16{1, 2, 3})
|
||||
t.AssertEQ(vs.Uint32s(), []uint32{1, 2, 3})
|
||||
t.AssertEQ(vs.Uint64s(), []uint64{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestVars_Scan(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Id int
|
||||
Name string
|
||||
}
|
||||
var vs = gvar.Vars{
|
||||
gvar.New(g.Map{"id": 1, "name": "john"}),
|
||||
gvar.New(g.Map{"id": 2, "name": "smith"}),
|
||||
}
|
||||
var users []User
|
||||
err := vs.Scan(&users)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), 2)
|
||||
t.Assert(users[0].Id, 1)
|
||||
t.Assert(users[0].Name, "john")
|
||||
t.Assert(users[1].Id, 2)
|
||||
t.Assert(users[1].Name, "smith")
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user