github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/bean/bean.go (about) 1 package bean 2 3 import ( 4 "github.com/gin-gonic/gin" 5 "github.com/isyscore/isc-gobase/isc" 6 "github.com/isyscore/isc-gobase/logger" 7 "github.com/isyscore/isc-gobase/server/rsp" 8 "reflect" 9 "strings" 10 ) 11 12 var BeanMap map[string]any 13 14 func init() { 15 BeanMap = map[string]any{} 16 } 17 18 // AddBean 添加bean 19 // 强烈建议:bean使用指针 20 func AddBean(beanName string, beanPtr any) { 21 BeanMap[beanName] = beanPtr 22 } 23 24 func GetBean(beanName string) any { 25 if beanV, exit := BeanMap[beanName]; exit { 26 return beanV 27 } 28 return nil 29 } 30 31 func Clean() { 32 BeanMap = map[string]any{} 33 } 34 35 // 模糊搜索 36 func GetBeanNames(beanName string) []string { 37 if beanName == "" { 38 j := 0 39 keys := make([]string, len(BeanMap)) 40 for k := range BeanMap { 41 keys[j] = k 42 j++ 43 } 44 return keys 45 } else { 46 var keys []string 47 for k := range BeanMap { 48 if strings.Contains(k, beanName) { 49 keys = append(keys, k) 50 } 51 } 52 return keys 53 } 54 } 55 56 // 前缀搜索 57 func GetBeanWithNamePre(beanName string) []any { 58 if beanName == "" { 59 return nil 60 } else { 61 var values []any 62 for k, v := range BeanMap { 63 if strings.HasPrefix(k, beanName) { 64 values = append(values, v) 65 } 66 } 67 return values 68 } 69 } 70 71 func ExistBean(beanName string) bool { 72 _, exist := BeanMap[beanName] 73 return exist 74 } 75 76 // @parameterValueMap p1、p2、p3...这种表示的是第一个、第二个、第三个参数的值 77 func CallFun(beanName, methodName string, parameterValueMap map[string]any) []any { 78 if beanValue, exist := BeanMap[beanName]; exist { 79 fType := reflect.TypeOf(beanValue) 80 var result []any 81 for index, num := 0, fType.NumMethod(); index < num; index++ { 82 method := fType.Method(index) 83 84 // 私有字段不处理 85 if !isc.IsPublic(method.Name) { 86 continue 87 } 88 89 if method.Name != methodName { 90 continue 91 } 92 93 parameterNum := method.Type.NumIn() 94 var in []reflect.Value 95 in = append(in, reflect.ValueOf(beanValue)) 96 for i := 1; i < parameterNum; i++ { 97 if v, exit := parameterValueMap["p"+isc.ToString(i)]; exit { 98 pV, err := isc.ToValue(v, method.Type.In(i).Kind()) 99 if err != nil { 100 continue 101 } 102 in = append(in, reflect.ValueOf(pV)) 103 } 104 } 105 106 if len(in) != parameterNum { 107 return nil 108 } 109 vs := method.Func.Call(in) 110 for _, v := range vs { 111 result = append(result, v.Interface()) 112 } 113 } 114 return result 115 } 116 return nil 117 } 118 119 func GetField(beanName, fieldName string) any { 120 if beanValue, exist := BeanMap[beanName]; exist { 121 fValue := reflect.ValueOf(beanValue) 122 fType := reflect.TypeOf(beanValue) 123 124 if fType.Kind() == reflect.Ptr { 125 fType = fType.Elem() 126 fValue = fValue.Elem() 127 } 128 129 for index, num := 0, fType.NumField(); index < num; index++ { 130 field := fType.Field(index) 131 132 // 私有字段不处理 133 if !isc.IsPublic(field.Name) { 134 continue 135 } 136 137 if field.Name == fieldName { 138 return fValue.Field(index).Interface() 139 } 140 } 141 } 142 return nil 143 } 144 145 // SetField 修改属性的话,请将对象设置为指针,否则不生效 146 func SetField(beanName string, fieldName string, fieldValue any) { 147 if beanValue, exist := BeanMap[beanName]; exist { 148 // 私有字段不处理 149 if !isc.IsPublic(fieldName) { 150 return 151 } 152 153 fValue := reflect.ValueOf(beanValue) 154 fType := reflect.TypeOf(beanValue) 155 156 if fType.Kind() == reflect.Ptr { 157 fType = fType.Elem() 158 fValue = fValue.Elem() 159 } else { 160 logger.Warn("对象名【%v】对应的对象不是指针类型,无法修改属性的值", beanName) 161 return 162 } 163 164 if _, exist := fType.FieldByName(fieldName); exist { 165 v, err := isc.ToValue(fieldValue, fValue.FieldByName(fieldName).Kind()) 166 if err != nil { 167 return 168 } 169 fValue.FieldByName(fieldName).Set(reflect.ValueOf(v)) 170 } 171 } 172 } 173 174 func DebugBeanAll(c *gin.Context) { 175 rsp.SuccessOfStandard(c, GetBeanNames("")) 176 } 177 178 func DebugBeanList(c *gin.Context) { 179 rsp.SuccessOfStandard(c, GetBeanNames(c.Param("name"))) 180 } 181 182 func DebugBeanGetField(c *gin.Context) { 183 fieldGetReq := FieldGetReq{} 184 err := isc.DataToObject(c.Request.Body, &fieldGetReq) 185 if err != nil { 186 return 187 } 188 rsp.SuccessOfStandard(c, GetField(fieldGetReq.Bean, fieldGetReq.Field)) 189 } 190 191 func DebugBeanSetField(c *gin.Context) { 192 fieldSetReq := FieldSetReq{} 193 err := isc.DataToObject(c.Request.Body, &fieldSetReq) 194 if err != nil { 195 return 196 } 197 SetField(fieldSetReq.Bean, fieldSetReq.Field, fieldSetReq.Value) 198 rsp.SuccessOfStandard(c, fieldSetReq.Value) 199 } 200 201 func DebugBeanFunCall(c *gin.Context) { 202 funCallReq := FunCallReq{} 203 err := isc.DataToObject(c.Request.Body, &funCallReq) 204 if err != nil { 205 return 206 } 207 rsp.SuccessOfStandard(c, CallFun(funCallReq.Bean, funCallReq.Fun, funCallReq.Parameter)) 208 } 209 210 func BeanTest() { 211 logger.Warn("test, ttt") 212 } 213 214 type FieldGetReq struct { 215 Bean string 216 Field string 217 } 218 219 type FieldSetReq struct { 220 Bean string 221 Field string 222 Value any 223 } 224 225 type FunCallReq struct { 226 Bean string 227 Fun string 228 Parameter map[string]any 229 }