github.com/godaddy-x/freego@v1.0.156/utils/reflect_field.go (about)

     1  package utils
     2  
     3  import (
     4  	"go.mongodb.org/mongo-driver/bson/primitive"
     5  	"unsafe"
     6  )
     7  
     8  type emptyInter struct {
     9  	t *struct{}
    10  	w unsafe.Pointer
    11  }
    12  
    13  // 通过指针获取对象字段位置
    14  func GetPtr(v interface{}, offset uintptr) uintptr {
    15  	structPtr := (*emptyInter)(unsafe.Pointer(&v)).w
    16  	return uintptr(structPtr) + offset
    17  }
    18  
    19  // get string value
    20  func GetString(ptr uintptr) string {
    21  	return *((*string)(unsafe.Pointer(ptr)))
    22  }
    23  
    24  // set string value
    25  func SetString(ptr uintptr, v string) {
    26  	*((*string)(unsafe.Pointer(ptr))) = v
    27  }
    28  
    29  // get int value
    30  func GetInt(ptr uintptr) int {
    31  	return *((*int)(unsafe.Pointer(ptr)))
    32  }
    33  
    34  // set int value
    35  func SetInt(ptr uintptr, v int) {
    36  	*((*int)(unsafe.Pointer(ptr))) = v
    37  }
    38  
    39  // get int8 value
    40  func GetInt8(ptr uintptr) int8 {
    41  	return *((*int8)(unsafe.Pointer(ptr)))
    42  }
    43  
    44  // set int8 value
    45  func SetInt8(ptr uintptr, v int8) {
    46  	*((*int8)(unsafe.Pointer(ptr))) = v
    47  }
    48  
    49  // get int16 value
    50  func GetInt16(ptr uintptr) int16 {
    51  	return *((*int16)(unsafe.Pointer(ptr)))
    52  }
    53  
    54  // set int16 value
    55  func SetInt16(ptr uintptr, v int16) {
    56  	*((*int16)(unsafe.Pointer(ptr))) = v
    57  }
    58  
    59  // get int32 value
    60  func GetInt32(ptr uintptr) int32 {
    61  	return *((*int32)(unsafe.Pointer(ptr)))
    62  }
    63  
    64  // set int32 value
    65  func SetInt32(ptr uintptr, v int32) {
    66  	*((*int32)(unsafe.Pointer(ptr))) = v
    67  }
    68  
    69  // get int64 value
    70  func GetInt64(ptr uintptr) int64 {
    71  	return *((*int64)(unsafe.Pointer(ptr)))
    72  }
    73  
    74  // set int64 value
    75  func SetInt64(ptr uintptr, v int64) {
    76  	*((*int64)(unsafe.Pointer(ptr))) = v
    77  }
    78  
    79  // get float32 value
    80  func GetFloat32(ptr uintptr) float32 {
    81  	return *((*float32)(unsafe.Pointer(ptr)))
    82  }
    83  
    84  // set float32 value
    85  func SetFloat32(ptr uintptr, v float32) {
    86  	*((*float32)(unsafe.Pointer(ptr))) = v
    87  }
    88  
    89  // get float64 value
    90  func GetFloat64(ptr uintptr) float64 {
    91  	return *((*float64)(unsafe.Pointer(ptr)))
    92  }
    93  
    94  // set float32 value
    95  func SetFloat64(ptr uintptr, v float64) {
    96  	*((*float64)(unsafe.Pointer(ptr))) = v
    97  }
    98  
    99  // get bool value
   100  func GetBool(ptr uintptr) bool {
   101  	return *((*bool)(unsafe.Pointer(ptr)))
   102  }
   103  
   104  // set bool value
   105  func SetBool(ptr uintptr, v bool) {
   106  	*((*bool)(unsafe.Pointer(ptr))) = v
   107  }
   108  
   109  // get uint value
   110  func GetUint(ptr uintptr) uint {
   111  	return *((*uint)(unsafe.Pointer(ptr)))
   112  }
   113  
   114  // set uint value
   115  func SetUint(ptr uintptr, v uint) {
   116  	*((*uint)(unsafe.Pointer(ptr))) = v
   117  }
   118  
   119  // get uint8 value
   120  func GetUint8(ptr uintptr) uint8 {
   121  	return *((*uint8)(unsafe.Pointer(ptr)))
   122  }
   123  
   124  // set uint value
   125  func SetUint8(ptr uintptr, v uint8) {
   126  	*((*uint8)(unsafe.Pointer(ptr))) = v
   127  }
   128  
   129  // get uint16 value
   130  func GetUint16(ptr uintptr) uint16 {
   131  	return *((*uint16)(unsafe.Pointer(ptr)))
   132  }
   133  
   134  // set uint16 value
   135  func SetUint16(ptr uintptr, v uint16) {
   136  	*((*uint16)(unsafe.Pointer(ptr))) = v
   137  }
   138  
   139  // get uint32 value
   140  func GetUint32(ptr uintptr) uint32 {
   141  	return *((*uint32)(unsafe.Pointer(ptr)))
   142  }
   143  
   144  // set uint32 value
   145  func SetUint32(ptr uintptr, v uint32) {
   146  	*((*uint32)(unsafe.Pointer(ptr))) = v
   147  }
   148  
   149  // get uint64 value
   150  func GetUint64(ptr uintptr) uint64 {
   151  	return *((*uint64)(unsafe.Pointer(ptr)))
   152  }
   153  
   154  // set uint64 value
   155  func SetUint64(ptr uintptr, v uint64) {
   156  	*((*uint64)(unsafe.Pointer(ptr))) = v
   157  }
   158  
   159  // get []string value
   160  func GetStringArr(ptr uintptr) []string {
   161  	if v := *((*[]string)(unsafe.Pointer(ptr))); v == nil {
   162  		return make([]string, 0)
   163  	} else {
   164  		return v
   165  	}
   166  }
   167  
   168  // set []string value
   169  func SetStringArr(ptr uintptr, v []string) {
   170  	*((*[]string)(unsafe.Pointer(ptr))) = v
   171  }
   172  
   173  // get []int value
   174  func GetIntArr(ptr uintptr) []int {
   175  	if v := *((*[]int)(unsafe.Pointer(ptr))); v == nil {
   176  		return make([]int, 0)
   177  	} else {
   178  		return v
   179  	}
   180  }
   181  
   182  // set []int value
   183  func SetIntArr(ptr uintptr, v []int) {
   184  	*((*[]int)(unsafe.Pointer(ptr))) = v
   185  }
   186  
   187  // get []int8 value
   188  func GetInt8Arr(ptr uintptr) []int8 {
   189  	if v := *((*[]int8)(unsafe.Pointer(ptr))); v == nil {
   190  		return make([]int8, 0)
   191  	} else {
   192  		return v
   193  	}
   194  }
   195  
   196  // set []int8 value
   197  func SetInt8Arr(ptr uintptr, v []int8) {
   198  	*((*[]int8)(unsafe.Pointer(ptr))) = v
   199  }
   200  
   201  // get []int16 value
   202  func GetInt16Arr(ptr uintptr) []int16 {
   203  	if v := *((*[]int16)(unsafe.Pointer(ptr))); v == nil {
   204  		return make([]int16, 0)
   205  	} else {
   206  		return v
   207  	}
   208  }
   209  
   210  // set []int16 value
   211  func SetInt16Arr(ptr uintptr, v []int16) {
   212  	*((*[]int16)(unsafe.Pointer(ptr))) = v
   213  }
   214  
   215  // get []int32 value
   216  func GetInt32Arr(ptr uintptr) []int32 {
   217  	if v := *((*[]int32)(unsafe.Pointer(ptr))); v == nil {
   218  		return make([]int32, 0)
   219  	} else {
   220  		return v
   221  	}
   222  }
   223  
   224  // set []int32 value
   225  func SetInt32Arr(ptr uintptr, v []int32) {
   226  	*((*[]int32)(unsafe.Pointer(ptr))) = v
   227  }
   228  
   229  // get []int64 value
   230  func GetInt64Arr(ptr uintptr) []int64 {
   231  	if v := *((*[]int64)(unsafe.Pointer(ptr))); v == nil {
   232  		return make([]int64, 0)
   233  	} else {
   234  		return v
   235  	}
   236  }
   237  
   238  // set []int64 value
   239  func SetInt64Arr(ptr uintptr, v []int64) {
   240  	*((*[]int64)(unsafe.Pointer(ptr))) = v
   241  }
   242  
   243  // get []float32 value
   244  func GetFloat32Arr(ptr uintptr) []float32 {
   245  	if v := *((*[]float32)(unsafe.Pointer(ptr))); v == nil {
   246  		return make([]float32, 0)
   247  	} else {
   248  		return v
   249  	}
   250  }
   251  
   252  // set []float32 value
   253  func SetFloat32Arr(ptr uintptr, v []float32) {
   254  	*((*[]float32)(unsafe.Pointer(ptr))) = v
   255  }
   256  
   257  // get []float64 value
   258  func GetFloat64Arr(ptr uintptr) []float64 {
   259  	if v := *((*[]float64)(unsafe.Pointer(ptr))); v == nil {
   260  		return make([]float64, 0)
   261  	} else {
   262  		return v
   263  	}
   264  }
   265  
   266  // set []float64 value
   267  func SetFloat64Arr(ptr uintptr, v []float64) {
   268  	*((*[]float64)(unsafe.Pointer(ptr))) = v
   269  }
   270  
   271  // get []bool value
   272  func GetBoolArr(ptr uintptr) []bool {
   273  	if v := *((*[]bool)(unsafe.Pointer(ptr))); v == nil {
   274  		return make([]bool, 0)
   275  	} else {
   276  		return v
   277  	}
   278  }
   279  
   280  // set []bool value
   281  func SetBoolArr(ptr uintptr, v []bool) {
   282  	*((*[]bool)(unsafe.Pointer(ptr))) = v
   283  }
   284  
   285  // get []uint value
   286  func GetUintArr(ptr uintptr) []uint {
   287  	if v := *((*[]uint)(unsafe.Pointer(ptr))); v == nil {
   288  		return make([]uint, 0)
   289  	} else {
   290  		return v
   291  	}
   292  }
   293  
   294  // set []uint value
   295  func SetUintArr(ptr uintptr, v []uint) {
   296  	*((*[]uint)(unsafe.Pointer(ptr))) = v
   297  }
   298  
   299  // get []uint8 value
   300  func GetUint8Arr(ptr uintptr) []uint8 {
   301  	if v := *((*[]uint8)(unsafe.Pointer(ptr))); v == nil {
   302  		return make([]uint8, 0)
   303  	} else {
   304  		return v
   305  	}
   306  }
   307  
   308  // set []uint8 value
   309  func SetUint8Arr(ptr uintptr, v []uint8) {
   310  	*((*[]uint8)(unsafe.Pointer(ptr))) = v
   311  }
   312  
   313  // get []uint16 value
   314  func GetUint16Arr(ptr uintptr) []uint16 {
   315  	if v := *((*[]uint16)(unsafe.Pointer(ptr))); v == nil {
   316  		return make([]uint16, 0)
   317  	} else {
   318  		return v
   319  	}
   320  }
   321  
   322  // set []uint16 value
   323  func SetUint16Arr(ptr uintptr, v []uint16) {
   324  	*((*[]uint16)(unsafe.Pointer(ptr))) = v
   325  }
   326  
   327  // get []uint32 value
   328  func GetUint32Arr(ptr uintptr) []uint32 {
   329  	if v := *((*[]uint32)(unsafe.Pointer(ptr))); v == nil {
   330  		return make([]uint32, 0)
   331  	} else {
   332  		return v
   333  	}
   334  }
   335  
   336  // set []uint32 value
   337  func SetUint32Arr(ptr uintptr, v []uint32) {
   338  	*((*[]uint32)(unsafe.Pointer(ptr))) = v
   339  }
   340  
   341  // get []uint64 value
   342  func GetUint64Arr(ptr uintptr) []uint64 {
   343  	if v := *((*[]uint64)(unsafe.Pointer(ptr))); v == nil {
   344  		return make([]uint64, 0)
   345  	} else {
   346  		return v
   347  	}
   348  }
   349  
   350  // set []uint64 value
   351  func SetUint64Arr(ptr uintptr, v []uint64) {
   352  	*((*[]uint64)(unsafe.Pointer(ptr))) = v
   353  }
   354  
   355  // set ObjectID value
   356  func SetObjectID(ptr uintptr, v primitive.ObjectID) {
   357  	*((*primitive.ObjectID)(unsafe.Pointer(ptr))) = v
   358  }
   359  
   360  // get ObjectID value
   361  func GetObjectID(ptr uintptr) primitive.ObjectID {
   362  	return *((*primitive.ObjectID)(unsafe.Pointer(ptr)))
   363  }