fusenapi/utils/collect/collect_test.go
2023-06-29 18:04:59 +08:00

78 lines
1.3 KiB
Go

package collect
import (
"fmt"
"log"
"reflect"
"testing"
)
type ABC struct {
A int64 `json:"a"`
B string `json:"b"`
C interface{} `json:"c"`
}
func TestArray2MapByKey(t *testing.T) {
var abcs []*ABC = []*ABC{
{1, "2", 3},
{3, "1", 2},
}
a := Array2MapByKey[string](abcs, "B")
log.Printf("%##v", a)
log.Println(len(a))
}
func TestArray2MapByKeyTag(t *testing.T) {
var abcs []*ABC = []*ABC{
{1, "2", 3},
{3, "1", 2},
}
a := Array2MapByKeyTag[int64](abcs, "a")
log.Printf("%##v", a)
log.Println(len(a))
}
func TestArrayColumnTag(t *testing.T) {
var abcs []*ABC = []*ABC{
{1, "2", 3},
{3, "1", 2},
}
a := ArrayColumnTag[string](abcs, "b")
log.Printf("%##v", a)
log.Println(len(a))
}
func TestCaseMain(t *testing.T) {
type MyStruct struct {
SomeIntPtr *int
SomeStringPtr *string
}
var ms MyStruct
// Set int pointer
{
var i interface{} = 3 // of type int
f := reflect.ValueOf(&ms).Elem().FieldByName("SomeIntPtr")
x := reflect.New(f.Type().Elem())
x.Elem().Set(reflect.ValueOf(i))
f.Set(x)
}
// Set string pointer
{
var i interface{} = "hi" // of type string
f := reflect.ValueOf(&ms).Elem().Field(1)
x := reflect.New(f.Type().Elem())
x.Elem().Set(reflect.ValueOf(i))
f.Set(x)
}
fmt.Println("ms.SomeIntPtr", *ms.SomeIntPtr)
fmt.Println("ms.SomeStringPtr", *ms.SomeStringPtr)
}