github.com/goccy/go-reflect@v1.2.1-0.20220925055700-4646ad15ec8a/init.go (about) 1 package reflect 2 3 import ( 4 "fmt" 5 "unsafe" 6 ) 7 8 var ( 9 errTypeOf = "runtime error: failed to get %s type by TypeOf" 10 errValueOf = "runtime error: failed to get %s value by ValueOf" 11 ) 12 13 // validateTypeOf validate interface{} layout and Kind. 14 func validateTypeOf() error { 15 if TypeOf(true).Kind() != Bool { 16 return fmt.Errorf(errTypeOf, "bool") 17 } 18 if TypeOf(int(1)).Kind() != Int { 19 return fmt.Errorf(errTypeOf, "int") 20 } 21 if TypeOf(int8(1)).Kind() != Int8 { 22 return fmt.Errorf(errTypeOf, "int8") 23 } 24 if TypeOf(int16(1)).Kind() != Int16 { 25 return fmt.Errorf(errTypeOf, "int16") 26 } 27 if TypeOf(int32(1)).Kind() != Int32 { 28 return fmt.Errorf(errTypeOf, "int32") 29 } 30 if TypeOf(int64(1)).Kind() != Int64 { 31 return fmt.Errorf(errTypeOf, "int64") 32 } 33 if TypeOf(uint(1)).Kind() != Uint { 34 return fmt.Errorf(errTypeOf, "uint") 35 } 36 if TypeOf(uint8(1)).Kind() != Uint8 { 37 return fmt.Errorf(errTypeOf, "uint8") 38 } 39 if TypeOf(uint16(1)).Kind() != Uint16 { 40 return fmt.Errorf(errTypeOf, "uint16") 41 } 42 if TypeOf(uint32(1)).Kind() != Uint32 { 43 return fmt.Errorf(errTypeOf, "uint32") 44 } 45 if TypeOf(uint64(1)).Kind() != Uint64 { 46 return fmt.Errorf(errTypeOf, "uint64") 47 } 48 if TypeOf(uintptr(1)).Kind() != Uintptr { 49 return fmt.Errorf(errTypeOf, "uintptr") 50 } 51 if TypeOf(float32(1)).Kind() != Float32 { 52 return fmt.Errorf(errTypeOf, "float32") 53 } 54 if TypeOf(float64(1)).Kind() != Float64 { 55 return fmt.Errorf(errTypeOf, "float64") 56 } 57 if TypeOf([1]int{1}).Kind() != Array { 58 return fmt.Errorf(errTypeOf, "array") 59 } 60 if TypeOf([]int{1}).Kind() != Slice { 61 return fmt.Errorf(errTypeOf, "slice") 62 } 63 if TypeOf(func() {}).Kind() != Func { 64 return fmt.Errorf(errTypeOf, "func") 65 } 66 if TypeOf(map[struct{}]struct{}{}).Kind() != Map { 67 return fmt.Errorf(errTypeOf, "map") 68 } 69 if TypeOf("").Kind() != String { 70 return fmt.Errorf(errTypeOf, "string") 71 } 72 if TypeOf(struct{}{}).Kind() != Struct { 73 return fmt.Errorf(errTypeOf, "struct") 74 } 75 if TypeOf(&struct{}{}).Kind() != Ptr { 76 return fmt.Errorf(errTypeOf, "pointer") 77 } 78 if TypeOf(unsafe.Pointer(nil)).Kind() != UnsafePointer { 79 return fmt.Errorf(errTypeOf, "unsafepointer") 80 } 81 return nil 82 } 83 84 // validateValueOf validate value layout and flag values. 85 func validateValueOf() error { 86 if v := ValueOf(true); v.Type().Kind() != Bool || v.Bool() != true { 87 return fmt.Errorf(errValueOf, "bool") 88 } 89 if v := ValueOf(int(1)); v.Type().Kind() != Int || v.Int() != 1 { 90 return fmt.Errorf(errValueOf, "int") 91 } 92 if v := ValueOf(int8(1)); v.Type().Kind() != Int8 || v.Int() != 1 { 93 return fmt.Errorf(errValueOf, "int8") 94 } 95 if v := ValueOf(int16(1)); v.Type().Kind() != Int16 || v.Int() != 1 { 96 return fmt.Errorf(errValueOf, "int16") 97 } 98 if v := ValueOf(int32(1)); v.Type().Kind() != Int32 || v.Int() != 1 { 99 return fmt.Errorf(errValueOf, "int32") 100 } 101 if v := ValueOf(int64(1)); v.Type().Kind() != Int64 || v.Int() != 1 { 102 return fmt.Errorf(errValueOf, "int64") 103 } 104 if v := ValueOf(uint(1)); v.Type().Kind() != Uint || v.Uint() != 1 { 105 return fmt.Errorf(errValueOf, "uint") 106 } 107 if v := ValueOf(uint8(1)); v.Type().Kind() != Uint8 || v.Uint() != 1 { 108 return fmt.Errorf(errValueOf, "uint8") 109 } 110 if v := ValueOf(uint16(1)); v.Type().Kind() != Uint16 || v.Uint() != 1 { 111 return fmt.Errorf(errValueOf, "uint16") 112 } 113 if v := ValueOf(uint32(1)); v.Type().Kind() != Uint32 || v.Uint() != 1 { 114 return fmt.Errorf(errValueOf, "uint32") 115 } 116 if v := ValueOf(uint64(1)); v.Type().Kind() != Uint64 || v.Uint() != 1 { 117 return fmt.Errorf(errValueOf, "uint64") 118 } 119 if v := ValueOf(uintptr(1)); v.Type().Kind() != Uintptr || v.Uint() != 1 { 120 return fmt.Errorf(errValueOf, "uintptr") 121 } 122 if v := ValueOf(float32(1)); v.Type().Kind() != Float32 || v.Float() != 1 { 123 return fmt.Errorf(errValueOf, "float32") 124 } 125 if v := ValueOf(float64(1)); v.Type().Kind() != Float64 || v.Float() != 1 { 126 return fmt.Errorf(errValueOf, "float64") 127 } 128 if v := ValueOf([1]int{1}); v.Type().Kind() != Array || v.Len() != 1 { 129 return fmt.Errorf(errValueOf, "array") 130 } 131 if v := ValueOf([]int{1}); v.Type().Kind() != Slice || v.Len() != 1 { 132 return fmt.Errorf(errValueOf, "slice") 133 } 134 if v := ValueOf(func() {}); v.Type().Kind() != Func { 135 return fmt.Errorf(errValueOf, "func") 136 } 137 if v := ValueOf(map[struct{}]struct{}{}); v.Type().Kind() != Map { 138 return fmt.Errorf(errValueOf, "map") 139 } 140 if v := ValueOf("1"); v.Type().Kind() != String || v.String() != "1" { 141 return fmt.Errorf(errValueOf, "string") 142 } 143 if v := ValueOf(struct{}{}); v.Type().Kind() != Struct { 144 return fmt.Errorf(errValueOf, "struct") 145 } 146 if v := ValueOf(&struct{}{}); v.Type().Kind() != Ptr { 147 return fmt.Errorf(errValueOf, "pointer") 148 } 149 if v := ValueOf(unsafe.Pointer(nil)); v.Type().Kind() != UnsafePointer { 150 return fmt.Errorf(errValueOf, "unsafepointer") 151 } 152 return nil 153 } 154 155 func validate() error { 156 if err := validateTypeOf(); err != nil { 157 return err 158 } 159 if err := validateValueOf(); err != nil { 160 return err 161 } 162 return nil 163 } 164 165 func init() { 166 if err := validate(); err != nil { 167 panic(err) 168 } 169 }