maker.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package reflectutils
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type StructFieldDefinition struct {
  7. Name string
  8. Type reflect.Type
  9. Tag string
  10. }
  11. type StructMethodDefinition struct {
  12. Name string
  13. ArgTypes []reflect.Type
  14. ReturnValueTypes []reflect.Type
  15. Body func(this *Struct, args ...any) []any
  16. }
  17. type Struct struct {
  18. structValueElem reflect.Value
  19. fieldMap map[string]reflect.Value
  20. methodMap map[string]reflect.Value
  21. }
  22. func NewStruct(fields ...StructFieldDefinition) *Struct {
  23. if len(fields) == 0 {
  24. return &Struct{
  25. structValueElem: reflect.ValueOf(struct{}{}),
  26. }
  27. }
  28. reflectStructFields := make([]reflect.StructField, len(fields))
  29. for i, field := range fields {
  30. reflectStructFields[i] = reflect.StructField{
  31. Name: field.Name,
  32. Type: field.Type,
  33. Tag: reflect.StructTag(field.Tag),
  34. }
  35. }
  36. return &Struct{
  37. structValueElem: reflect.New(reflect.StructOf(reflectStructFields)).Elem(),
  38. fieldMap: map[string]reflect.Value{},
  39. methodMap: map[string]reflect.Value{},
  40. }
  41. }
  42. func (s *Struct) Any() any {
  43. return s.structValueElem.Interface()
  44. }
  45. func (s *Struct) Pointer() any {
  46. return s.structValueElem.Addr().Interface()
  47. }
  48. func (s *Struct) SetFieldValue(fieldName string, value any) {
  49. s.loadFieldValue(fieldName).Set(reflect.ValueOf(value))
  50. }
  51. func (s *Struct) FieldValue(fieldName string) any {
  52. return s.loadFieldValue(fieldName).Interface()
  53. }
  54. func (s *Struct) SetFieldValues(fieldAndValues map[string]any) {
  55. for fieldName, value := range fieldAndValues {
  56. s.SetFieldValue(fieldName, value)
  57. }
  58. }
  59. func (s *Struct) FieldValues(fieldNames ...string) map[string]any {
  60. if len(fieldNames) == 0 {
  61. return map[string]any{}
  62. }
  63. fieldAndValues := make(map[string]any)
  64. for _, fieldName := range fieldNames {
  65. fieldAndValues[fieldName] = s.FieldValue(fieldName)
  66. }
  67. return fieldAndValues
  68. }
  69. func (s *Struct) MakeMethod(methods ...StructMethodDefinition) {
  70. for _, method := range methods {
  71. argTypes := make([]reflect.Type, 0)
  72. argTypes = append(argTypes, s.structValueElem.Type())
  73. argTypes = append(argTypes, method.ArgTypes...)
  74. funcValue := reflect.MakeFunc(reflect.FuncOf(argTypes, method.ReturnValueTypes, false),
  75. func(argValues []reflect.Value) []reflect.Value {
  76. args := make([]any, len(argValues)-1)
  77. for i, arg := range argValues {
  78. if i == 0 {
  79. continue
  80. }
  81. args[i-1] = arg.Interface()
  82. }
  83. returns := method.Body(s, args)
  84. returnValues := make([]reflect.Value, len(returns))
  85. for j, returnValue := range returns {
  86. reflectValue := reflect.ValueOf(returnValue)
  87. if !reflectValue.IsValid() {
  88. returnValues[j] = reflect.Zero(method.ReturnValueTypes[j])
  89. } else {
  90. returnValues[j] = reflect.ValueOf(returnValue)
  91. }
  92. }
  93. return returnValues
  94. })
  95. s.methodMap[method.Name] = funcValue
  96. }
  97. }
  98. func (s *Struct) CallMethod(methodName string, args ...any) []any {
  99. method, ok := s.methodMap[methodName]
  100. if !ok {
  101. panic(fmt.Sprintf("%s方法不存在", methodName))
  102. }
  103. argValues := make([]reflect.Value, len(args)+1)
  104. argValues[0] = s.structValueElem
  105. for i, arg := range args {
  106. argValues[i+1] = reflect.ValueOf(arg)
  107. }
  108. returnValues := method.Call(argValues)
  109. returns := make([]any, len(returnValues))
  110. for j, returnValue := range returnValues {
  111. if returnValue.IsNil() {
  112. returns[j] = nil
  113. } else {
  114. returns[j] = returnValue.Interface()
  115. }
  116. }
  117. return returns
  118. }
  119. func (s *Struct) loadFieldValue(fieldName string) reflect.Value {
  120. fieldValue, ok := s.fieldMap[fieldName]
  121. if ok {
  122. return fieldValue
  123. }
  124. fieldValue = s.structValueElem.FieldByName(fieldName)
  125. if !fieldValue.IsValid() {
  126. panic(fmt.Sprintf("%s字段不存在", fieldName))
  127. }
  128. return fieldValue
  129. }
  130. type FunctionDefinition struct {
  131. ArgTypes []reflect.Type
  132. ReturnValueTypes []reflect.Type
  133. Body func(args ...any) []any
  134. }
  135. type Function struct {
  136. functionValue reflect.Value
  137. }
  138. func (f *Function) Call(args ...any) []any {
  139. argValues := make([]reflect.Value, len(args))
  140. for i, arg := range args {
  141. argValues[i] = reflect.ValueOf(arg)
  142. }
  143. returnValues := f.functionValue.Call(argValues)
  144. returns := make([]any, len(returnValues))
  145. for j, returnValue := range returnValues {
  146. if returnValue.IsNil() {
  147. returns[j] = nil
  148. } else {
  149. returns[j] = returnValue.Interface()
  150. }
  151. }
  152. return returns
  153. }
  154. func MakeFunction(function FunctionDefinition) *Function {
  155. funcValue := reflect.MakeFunc(reflect.FuncOf(function.ArgTypes, function.ReturnValueTypes, false),
  156. func(argValues []reflect.Value) []reflect.Value {
  157. args := make([]any, len(argValues)-1)
  158. for i, arg := range argValues {
  159. if i == 0 {
  160. continue
  161. }
  162. args[i-1] = arg.Interface()
  163. }
  164. returns := function.Body(args)
  165. returnValues := make([]reflect.Value, len(returns))
  166. for j, returnValue := range returns {
  167. reflectValue := reflect.ValueOf(returnValue)
  168. if !reflectValue.IsValid() {
  169. returnValues[j] = reflect.Zero(function.ReturnValueTypes[j])
  170. } else {
  171. returnValues[j] = reflect.ValueOf(returnValue)
  172. }
  173. }
  174. return returnValues
  175. })
  176. return &Function{
  177. functionValue: funcValue,
  178. }
  179. }