codeberg.org/gruf/go-mangler@v1.3.0/manglers.go (about) 1 package mangler 2 3 import ( 4 "math/bits" 5 _ "unsafe" 6 ) 7 8 // Notes: 9 // the use of unsafe conversion from the direct interface values to 10 // the chosen types in each of the below functions allows us to convert 11 // not only those types directly, but anything type-aliased to those 12 // types. e.g. `time.Duration` directly as int64. 13 14 func mangle_string(buf []byte, a any) []byte { 15 return append(buf, *(*string)(eface_data(a))...) 16 } 17 18 func mangle_string_slice(buf []byte, a any) []byte { 19 s := *(*[]string)(eface_data(a)) 20 for _, s := range s { 21 buf = append(buf, s...) 22 buf = append(buf, ',') 23 } 24 if len(s) > 0 { 25 buf = buf[:len(buf)-1] 26 } 27 return buf 28 } 29 30 func mangle_bool(buf []byte, a any) []byte { 31 if *(*bool)(eface_data(a)) { 32 return append(buf, '1') 33 } 34 return append(buf, '0') 35 } 36 37 func mangle_bool_slice(buf []byte, a any) []byte { 38 for _, b := range *(*[]bool)(eface_data(a)) { 39 if b { 40 buf = append(buf, '1') 41 } else { 42 buf = append(buf, '0') 43 } 44 } 45 return buf 46 } 47 48 func mangle_8bit(buf []byte, a any) []byte { 49 return append(buf, *(*uint8)(eface_data(a))) 50 } 51 52 func mangle_8bit_slice(buf []byte, a any) []byte { 53 return append(buf, *(*[]uint8)(eface_data(a))...) 54 } 55 56 func mangle_16bit(buf []byte, a any) []byte { 57 return append_uint16(buf, *(*uint16)(eface_data(a))) 58 } 59 60 func mangle_16bit_slice(buf []byte, a any) []byte { 61 for _, u := range *(*[]uint16)(eface_data(a)) { 62 buf = append_uint16(buf, u) 63 } 64 return buf 65 } 66 67 func mangle_32bit(buf []byte, a any) []byte { 68 return append_uint32(buf, *(*uint32)(eface_data(a))) 69 } 70 71 func mangle_32bit_slice(buf []byte, a any) []byte { 72 for _, u := range *(*[]uint32)(eface_data(a)) { 73 buf = append_uint32(buf, u) 74 } 75 return buf 76 } 77 78 func mangle_64bit(buf []byte, a any) []byte { 79 return append_uint64(buf, *(*uint64)(eface_data(a))) 80 } 81 82 func mangle_64bit_slice(buf []byte, a any) []byte { 83 for _, u := range *(*[]uint64)(eface_data(a)) { 84 buf = append_uint64(buf, u) 85 } 86 return buf 87 } 88 89 func mangle_platform_int() Mangler { 90 switch bits.UintSize { 91 case 32: 92 return mangle_32bit 93 case 64: 94 return mangle_64bit 95 default: 96 panic("unexpected platform int size") 97 } 98 } 99 100 func mangle_platform_int_slice() Mangler { 101 switch bits.UintSize { 102 case 32: 103 return mangle_32bit_slice 104 case 64: 105 return mangle_64bit_slice 106 default: 107 panic("unexpected platform int size") 108 } 109 } 110 111 func mangle_128bit(buf []byte, a any) []byte { 112 u2 := *(*[2]uint64)(eface_data(a)) 113 buf = append_uint64(buf, u2[0]) 114 buf = append_uint64(buf, u2[1]) 115 return buf 116 } 117 118 func mangle_128bit_slice(buf []byte, a any) []byte { 119 for _, u2 := range *(*[][2]uint64)(eface_data(a)) { 120 buf = append_uint64(buf, u2[0]) 121 buf = append_uint64(buf, u2[1]) 122 } 123 return buf 124 } 125 126 func mangle_mangled(buf []byte, a any) []byte { 127 if v := a.(Mangled); v != nil { 128 buf = append(buf, '1') 129 return v.Mangle(buf) 130 } 131 buf = append(buf, '0') 132 return buf 133 } 134 135 func mangle_binary(buf []byte, a any) []byte { 136 if v := a.(binarymarshaler); v != nil { 137 b, err := v.MarshalBinary() 138 if err != nil { 139 panic("mangle_binary: " + err.Error()) 140 } 141 buf = append(buf, '1') 142 return append(buf, b...) 143 } 144 buf = append(buf, '0') 145 return buf 146 } 147 148 func mangle_byteser(buf []byte, a any) []byte { 149 if v := a.(byteser); v != nil { 150 buf = append(buf, '1') 151 return append(buf, v.Bytes()...) 152 } 153 buf = append(buf, '0') 154 return buf 155 } 156 157 func mangle_stringer(buf []byte, a any) []byte { 158 if v := a.(stringer); v != nil { 159 buf = append(buf, '1') 160 return append(buf, v.String()...) 161 } 162 buf = append(buf, '0') 163 return buf 164 } 165 166 func mangle_text(buf []byte, a any) []byte { 167 if v := a.(textmarshaler); v != nil { 168 b, err := v.MarshalText() 169 if err != nil { 170 panic("mangle_text: " + err.Error()) 171 } 172 buf = append(buf, '1') 173 return append(buf, b...) 174 } 175 buf = append(buf, '0') 176 return buf 177 } 178 179 func mangle_json(buf []byte, a any) []byte { 180 if v := a.(jsonmarshaler); v != nil { 181 b, err := v.MarshalJSON() 182 if err != nil { 183 panic("mangle_json: " + err.Error()) 184 } 185 buf = append(buf, '1') 186 return append(buf, b...) 187 } 188 buf = append(buf, '0') 189 return buf 190 }