github.com/goccy/go-reflect@v1.2.1-0.20220925055700-4646ad15ec8a/bridge.go (about)

     1  package reflect
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  func toRT(t Type) reflect.Type {
     9  	return type_toType(t)
    10  }
    11  
    12  func toRTs(t []Type) []reflect.Type {
    13  	out := make([]reflect.Type, len(t))
    14  	for idx, tt := range t {
    15  		out[idx] = toRT(tt)
    16  	}
    17  	return out
    18  }
    19  
    20  func toT(t reflect.Type) Type {
    21  	return (Type)(((*Value)(unsafe.Pointer(&t))).ptr)
    22  }
    23  
    24  func toRV(v Value) reflect.Value {
    25  	return *(*reflect.Value)(unsafe.Pointer(&v))
    26  }
    27  
    28  func toRVs(v []Value) []reflect.Value {
    29  	out := make([]reflect.Value, len(v))
    30  	for idx, vv := range v {
    31  		out[idx] = toRV(vv)
    32  	}
    33  	return out
    34  }
    35  
    36  func toV(v reflect.Value) Value {
    37  	return *(*Value)(unsafe.Pointer(&v))
    38  }
    39  
    40  func toVs(v []reflect.Value) []Value {
    41  	out := make([]Value, len(v))
    42  	for idx, vv := range v {
    43  		out[idx] = toV(vv)
    44  	}
    45  	return out
    46  }
    47  
    48  func toRSFs(v []StructField) []reflect.StructField {
    49  	out := make([]reflect.StructField, len(v))
    50  	for idx, vv := range v {
    51  		out[idx] = toRSF(vv)
    52  	}
    53  	return out
    54  }
    55  
    56  func toRSF(v StructField) reflect.StructField {
    57  	return reflect.StructField{
    58  		Name:      v.Name,
    59  		PkgPath:   v.PkgPath,
    60  		Type:      ToReflectType(v.Type),
    61  		Tag:       v.Tag,
    62  		Offset:    v.Offset,
    63  		Index:     v.Index,
    64  		Anonymous: v.Anonymous,
    65  	}
    66  }
    67  
    68  func toSF(v reflect.StructField) StructField {
    69  	return StructField{
    70  		Name:      v.Name,
    71  		PkgPath:   v.PkgPath,
    72  		Type:      ToType(v.Type),
    73  		Tag:       v.Tag,
    74  		Offset:    v.Offset,
    75  		Index:     v.Index,
    76  		Anonymous: v.Anonymous,
    77  	}
    78  }
    79  
    80  func toM(v reflect.Method) Method {
    81  	return Method{
    82  		Name:    v.Name,
    83  		PkgPath: v.PkgPath,
    84  		Type:    ToType(v.Type),
    85  		Func:    toV(v.Func),
    86  		Index:   v.Index,
    87  	}
    88  }
    89  
    90  func toRSC(v SelectCase) reflect.SelectCase {
    91  	return reflect.SelectCase{
    92  		Dir:  v.Dir,
    93  		Chan: toRV(v.Chan),
    94  		Send: toRV(v.Send),
    95  	}
    96  }
    97  
    98  func toRSCs(v []SelectCase) []reflect.SelectCase {
    99  	out := make([]reflect.SelectCase, len(v))
   100  	for idx, vv := range v {
   101  		out[idx] = toRSC(vv)
   102  	}
   103  	return out
   104  }