map_key.go (1218B)
1 package internal 2 3 import "reflect" 4 5 var ifaceType = reflect.TypeOf((*interface{})(nil)).Elem() 6 7 type MapKey struct { 8 iface interface{} 9 } 10 11 func NewMapKey(is []interface{}) MapKey { 12 return MapKey{ 13 iface: newMapKey(is), 14 } 15 } 16 17 func newMapKey(is []interface{}) interface{} { 18 switch len(is) { 19 case 1: 20 ptr := new([1]interface{}) 21 copy((*ptr)[:], is) 22 return *ptr 23 case 2: 24 ptr := new([2]interface{}) 25 copy((*ptr)[:], is) 26 return *ptr 27 case 3: 28 ptr := new([3]interface{}) 29 copy((*ptr)[:], is) 30 return *ptr 31 case 4: 32 ptr := new([4]interface{}) 33 copy((*ptr)[:], is) 34 return *ptr 35 case 5: 36 ptr := new([5]interface{}) 37 copy((*ptr)[:], is) 38 return *ptr 39 case 6: 40 ptr := new([6]interface{}) 41 copy((*ptr)[:], is) 42 return *ptr 43 case 7: 44 ptr := new([7]interface{}) 45 copy((*ptr)[:], is) 46 return *ptr 47 case 8: 48 ptr := new([8]interface{}) 49 copy((*ptr)[:], is) 50 return *ptr 51 case 9: 52 ptr := new([9]interface{}) 53 copy((*ptr)[:], is) 54 return *ptr 55 case 10: 56 ptr := new([10]interface{}) 57 copy((*ptr)[:], is) 58 return *ptr 59 default: 60 } 61 62 at := reflect.New(reflect.ArrayOf(len(is), ifaceType)).Elem() 63 for i, v := range is { 64 *(at.Index(i).Addr().Interface().(*interface{})) = v 65 } 66 return at.Interface() 67 }