github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zutil/reflect_test.go (about) 1 package zutil_test 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 "time" 8 9 "github.com/sohaha/zlsgo" 10 "github.com/sohaha/zlsgo/zreflect" 11 "github.com/sohaha/zlsgo/zutil" 12 ) 13 14 type TestSt struct { 15 Name string 16 I int `z:"iii"` 17 Note int `json:"note,omitempty"` 18 } 19 20 func (*TestSt) RunTest(t *testing.T) { 21 t.Log("RunTest") 22 } 23 24 func (TestSt) RunTest2() {} 25 26 type TestSt2 struct { 27 Name string 28 Test2 bool 29 } 30 31 func (tt *TestSt2) RunTest(t *testing.T) { 32 t.Log("RunTest", tt.Name) 33 } 34 35 func TestRunAllMethod(t *testing.T) { 36 tt := zlsgo.NewTest(t) 37 err := zutil.RunAllMethod(&TestSt{}, t) 38 t.Log(err) 39 tt.Equal(true, err != nil) 40 41 err = zutil.RunAllMethod(&TestSt2{Name: "AllMethod"}, t) 42 t.Log(err) 43 tt.Equal(true, err == nil) 44 45 err = zutil.RunAssignMethod(&TestSt2{Name: "AssignMethod"}, func(methodName string) bool { 46 t.Log("methodName:", methodName) 47 return true 48 }, t) 49 t.Log(err) 50 tt.Equal(true, err == nil) 51 } 52 53 func TestGetAllMethod(t *testing.T) { 54 tt := zlsgo.NewTest(t) 55 i := 0 56 err := zutil.GetAllMethod(&TestSt{}, func(numMethod int, m reflect.Method) error { 57 t.Log(m.Name) 58 i++ 59 if m.Name != "RunTest" && m.Name != "RunTest2" { 60 return errors.New("mismatch") 61 } 62 return nil 63 }) 64 tt.Equal(2, i) 65 tt.Equal(true, err == nil) 66 67 err = zutil.GetAllMethod("test", nil) 68 t.Log(err) 69 // tt.Equal(true, err != nil) 70 71 err = zutil.GetAllMethod(&TestSt{}, nil) 72 t.Log(err) 73 // tt.Equal(true, err == nil) 74 } 75 76 func TestReflectStructField(t *testing.T) { 77 tt := zlsgo.NewTest(t) 78 var test = &TestSt{} 79 tf := reflect.TypeOf(test) 80 // fieldPtr := uintptr(unsafe.Pointer(test)) 81 err := zutil.ReflectStructField(tf, func(numField int, fieldTag string, 82 field reflect.StructField) error { 83 // fieldPtrOffset := fieldPtr + field.Offset 84 switch field.Type.Kind() { 85 case reflect.String: 86 // noinspection GoVetUnsafePointer 87 // *((*string)(unsafe.Pointer(fieldPtrOffset))) = "ok" 88 } 89 return nil 90 }) 91 tt.EqualNil(err) 92 t.Log(test) 93 } 94 95 func TestReflectForNumField(t *testing.T) { 96 tt := zlsgo.NewTest(t) 97 var test = &struct { 98 UpdatedAt time.Time 99 *TestSt2 100 T2p *TestSt2 101 T2 TestSt2 102 TestSt 103 New bool 104 Updated uint8 105 }{} 106 rv := zreflect.ValueOf(test) 107 rv = rv.Elem() 108 err := zutil.ReflectForNumField(rv, func(fieldName, fieldTag string, kind reflect.Kind, field reflect.Value) error { 109 t.Log(fieldTag, kind, field.Kind()) 110 return nil 111 }) 112 tt.EqualNil(err) 113 } 114 115 func TestSetValue(tt *testing.T) { 116 t := zlsgo.NewTest(tt) 117 t.Log(666) 118 vv := &TestSt2{Name: "1"} 119 120 v := zreflect.ValueOf(vv) 121 err := zutil.ReflectForNumField(v.Elem(), func(fieldName, fieldTag string, 122 kind reflect.Kind, field reflect.Value) error { 123 if fieldName == "Test2" { 124 tt.Log(fieldName, true) 125 return zutil.SetValue(kind, field, true) 126 } 127 tt.Log(fieldName, "new") 128 return zutil.SetValue(kind, field, "new") 129 }) 130 t.EqualNil(err) 131 t.Equal("new", vv.Name) 132 t.Equal(true, vv.Test2) 133 }