mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve gconv.Interfaces
This commit is contained in:
@ -112,22 +112,23 @@ func Interfaces(i interface{}) []interface{} {
|
||||
for i := 0; i < reflectValue.Len(); i++ {
|
||||
array[i] = reflectValue.Index(i).Interface()
|
||||
}
|
||||
// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
|
||||
case reflect.Map:
|
||||
array = make([]interface{}, 0)
|
||||
for _, key := range reflectValue.MapKeys() {
|
||||
array = append(array, key.Interface())
|
||||
array = append(array, reflectValue.MapIndex(key).Interface())
|
||||
}
|
||||
// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
|
||||
case reflect.Struct:
|
||||
array = make([]interface{}, 0)
|
||||
// Note that, it uses the gconv tag name instead of the attribute name if
|
||||
// the gconv tag is fined in the struct attributes.
|
||||
for k, v := range Map(reflectValue) {
|
||||
array = append(array, k)
|
||||
array = append(array, v)
|
||||
}
|
||||
// Deprecated.
|
||||
//// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
|
||||
//case reflect.Map:
|
||||
// array = make([]interface{}, 0)
|
||||
// for _, key := range reflectValue.MapKeys() {
|
||||
// array = append(array, key.Interface())
|
||||
// array = append(array, reflectValue.MapIndex(key).Interface())
|
||||
// }
|
||||
//// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
|
||||
//case reflect.Struct:
|
||||
// array = make([]interface{}, 0)
|
||||
// // Note that, it uses the gconv tag name instead of the attribute name if
|
||||
// // the gconv tag is fined in the struct attributes.
|
||||
// for k, v := range Map(reflectValue) {
|
||||
// array = append(array, k)
|
||||
// array = append(array, v)
|
||||
// }
|
||||
default:
|
||||
return []interface{}{i}
|
||||
}
|
||||
|
||||
@ -753,11 +753,11 @@ func Test_Slice_PrivateAttribute_All(t *testing.T) {
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := &User{1, "john", []interface{}{2}}
|
||||
s := gconv.Interfaces(user)
|
||||
t.Assert(len(s), 4)
|
||||
// g.Slice{"id", 1, "ad", g.Slice{2}}
|
||||
t.Assert(s[0] == "id" || s[0] == "ad", true)
|
||||
t.Assert(s[1] == 1 || s[3] == 1, true)
|
||||
array := gconv.Interfaces(user)
|
||||
t.Assert(len(array), 1)
|
||||
t.Assert(array[0].(*User).Id, 1)
|
||||
t.Assert(array[0].(*User).name, "john")
|
||||
t.Assert(array[0].(*User).Ad, []interface{}{2})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -85,7 +85,9 @@ func Test_Slice_Interfaces(t *testing.T) {
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
})
|
||||
t.AssertIN(array, []interface{}{"id", 1, "name", "john"})
|
||||
t.Assert(len(array), 1)
|
||||
t.Assert(array[0].(g.Map)["id"], 1)
|
||||
t.Assert(array[0].(g.Map)["name"], "john")
|
||||
})
|
||||
// struct
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -97,7 +99,9 @@ func Test_Slice_Interfaces(t *testing.T) {
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
})
|
||||
t.AssertIN(array, []interface{}{"id", 1, "Name", "john"})
|
||||
t.Assert(len(array), 1)
|
||||
t.Assert(array[0].(*A).Id, 1)
|
||||
t.Assert(array[0].(*A).Name, "john")
|
||||
})
|
||||
}
|
||||
|
||||
@ -108,7 +112,10 @@ func Test_Slice_PrivateAttribute(t *testing.T) {
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := &User{1, "john"}
|
||||
t.Assert(gconv.Interfaces(user), g.Slice{"id", 1})
|
||||
array := gconv.Interfaces(user)
|
||||
t.Assert(len(array), 1)
|
||||
t.Assert(array[0].(*User).Id, 1)
|
||||
t.Assert(array[0].(*User).name, "john")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user