github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/protocol/reflect.go (about) 1 //go:build !unsafe 2 // +build !unsafe 3 4 package protocol 5 6 import ( 7 "reflect" 8 ) 9 10 type index []int 11 12 type _type struct{ typ reflect.Type } 13 14 func typeOf(x interface{}) _type { 15 return makeType(reflect.TypeOf(x)) 16 } 17 18 func elemTypeOf(x interface{}) _type { 19 return makeType(reflect.TypeOf(x).Elem()) 20 } 21 22 func makeType(t reflect.Type) _type { 23 return _type{typ: t} 24 } 25 26 type value struct { 27 val reflect.Value 28 } 29 30 func nonAddressableValueOf(x interface{}) value { 31 return value{val: reflect.ValueOf(x)} 32 } 33 34 func valueOf(x interface{}) value { 35 return value{val: reflect.ValueOf(x).Elem()} 36 } 37 38 func (v value) bool() bool { return v.val.Bool() } 39 40 func (v value) int8() int8 { return int8(v.int64()) } 41 42 func (v value) int16() int16 { return int16(v.int64()) } 43 44 func (v value) int32() int32 { return int32(v.int64()) } 45 46 func (v value) int64() int64 { return v.val.Int() } 47 48 func (v value) float64() float64 { return v.val.Float() } 49 50 func (v value) string() string { return v.val.String() } 51 52 func (v value) bytes() []byte { return v.val.Bytes() } 53 54 func (v value) iface(t reflect.Type) interface{} { return v.val.Addr().Interface() } 55 56 func (v value) array(t reflect.Type) array { return array(v) } 57 58 func (v value) setBool(b bool) { v.val.SetBool(b) } 59 60 func (v value) setInt8(i int8) { v.setInt64(int64(i)) } 61 62 func (v value) setInt16(i int16) { v.setInt64(int64(i)) } 63 64 func (v value) setInt32(i int32) { v.setInt64(int64(i)) } 65 66 func (v value) setInt64(i int64) { v.val.SetInt(i) } 67 68 func (v value) setFloat64(f float64) { v.val.SetFloat(f) } 69 70 func (v value) setString(s string) { v.val.SetString(s) } 71 72 func (v value) setBytes(b []byte) { v.val.SetBytes(b) } 73 74 func (v value) setArray(a array) { 75 if a.val.IsValid() { 76 v.val.Set(a.val) 77 } else { 78 v.val.Set(reflect.Zero(v.val.Type())) 79 } 80 } 81 82 func (v value) fieldByIndex(i index) value { 83 return value{val: v.val.FieldByIndex(i)} 84 } 85 86 type array struct { 87 val reflect.Value 88 } 89 90 func makeArray(t reflect.Type, n int) array { 91 return array{val: reflect.MakeSlice(reflect.SliceOf(t), n, n)} 92 } 93 94 func (a array) index(i int) value { return value{val: a.val.Index(i)} } 95 96 func (a array) length() int { return a.val.Len() } 97 98 func (a array) isNil() bool { return a.val.IsNil() } 99 100 func indexOf(s reflect.StructField) index { return index(s.Index) } 101 102 func bytesToString(b []byte) string { return string(b) }