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