compare x and y return : x > y -> 1, x < y -> -1, x == y -> 0, x != y -> -2
(x, y any)
| 158 | // compare x and y return : |
| 159 | // x > y -> 1, x < y -> -1, x == y -> 0, x != y -> -2 |
| 160 | func compare(x, y any) int { |
| 161 | vx := reflect.ValueOf(x) |
| 162 | vy := reflect.ValueOf(y) |
| 163 | |
| 164 | if vx.Type() != vy.Type() { |
| 165 | return compareNotEqual |
| 166 | } |
| 167 | |
| 168 | switch vx.Kind() { |
| 169 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 170 | xInt := vx.Int() |
| 171 | yInt := vy.Int() |
| 172 | if xInt > yInt { |
| 173 | return compareGreater |
| 174 | } |
| 175 | if xInt == yInt { |
| 176 | return compareEqual |
| 177 | } |
| 178 | if xInt < yInt { |
| 179 | return compareLess |
| 180 | } |
| 181 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 182 | xUint := vx.Uint() |
| 183 | yUint := vy.Uint() |
| 184 | if xUint > yUint { |
| 185 | return compareGreater |
| 186 | } |
| 187 | if xUint == yUint { |
| 188 | return compareEqual |
| 189 | } |
| 190 | if xUint < yUint { |
| 191 | return compareLess |
| 192 | } |
| 193 | case reflect.Float32, reflect.Float64: |
| 194 | xFloat := vx.Float() |
| 195 | yFloat := vy.Float() |
| 196 | if xFloat > yFloat { |
| 197 | return compareGreater |
| 198 | } |
| 199 | if xFloat == yFloat { |
| 200 | return compareEqual |
| 201 | } |
| 202 | if xFloat < yFloat { |
| 203 | return compareLess |
| 204 | } |
| 205 | case reflect.String: |
| 206 | xString := vx.String() |
| 207 | yString := vy.String() |
| 208 | if xString > yString { |
| 209 | return compareGreater |
| 210 | } |
| 211 | if xString == yString { |
| 212 | return compareEqual |
| 213 | } |
| 214 | if xString < yString { |
| 215 | return compareLess |
| 216 | } |
| 217 | default: |
no test coverage detected
searching dependent graphs…