ObjectsAreEqualValues gets whether two objects are equal, or if their values are equal.
(expected, actual interface{})
| 160 | // ObjectsAreEqualValues gets whether two objects are equal, or if their |
| 161 | // values are equal. |
| 162 | func ObjectsAreEqualValues(expected, actual interface{}) bool { |
| 163 | if ObjectsAreEqual(expected, actual) { |
| 164 | return true |
| 165 | } |
| 166 | |
| 167 | expectedValue := reflect.ValueOf(expected) |
| 168 | actualValue := reflect.ValueOf(actual) |
| 169 | if !expectedValue.IsValid() || !actualValue.IsValid() { |
| 170 | return false |
| 171 | } |
| 172 | |
| 173 | expectedType := expectedValue.Type() |
| 174 | actualType := actualValue.Type() |
| 175 | if !expectedType.ConvertibleTo(actualType) { |
| 176 | return false |
| 177 | } |
| 178 | |
| 179 | if !isNumericType(expectedType) || !isNumericType(actualType) { |
| 180 | // Attempt comparison after type conversion |
| 181 | return reflect.DeepEqual( |
| 182 | expectedValue.Convert(actualType).Interface(), actual, |
| 183 | ) |
| 184 | } |
| 185 | |
| 186 | // If BOTH values are numeric, there are chances of false positives due |
| 187 | // to overflow or underflow. So, we need to make sure to always convert |
| 188 | // the smaller type to a larger type before comparing. |
| 189 | if expectedType.Size() >= actualType.Size() { |
| 190 | return actualValue.Convert(expectedType).Interface() == expected |
| 191 | } |
| 192 | |
| 193 | return expectedValue.Convert(actualType).Interface() == actual |
| 194 | } |
| 195 | |
| 196 | // isNumericType returns true if the type is one of: |
| 197 | // int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, |
searching dependent graphs…