EquateComparable returns a [cmp.Option] that determines equality of comparable types by directly comparing them using the == operator in Go. The types to compare are specified by passing a value of that type. This option should only be used on types that are documented as being safe for direct == co
(typs ...interface{})
| 164 | // as being semantically safe to use with ==, while [time.Time] is documented |
| 165 | // to discourage the use of == on time values. |
| 166 | func EquateComparable(typs ...interface{}) cmp.Option { |
| 167 | types := make(typesFilter) |
| 168 | for _, typ := range typs { |
| 169 | switch t := reflect.TypeOf(typ); { |
| 170 | case !t.Comparable(): |
| 171 | panic(fmt.Sprintf("%T is not a comparable Go type", typ)) |
| 172 | case types[t]: |
| 173 | panic(fmt.Sprintf("%T is already specified", typ)) |
| 174 | default: |
| 175 | types[t] = true |
| 176 | } |
| 177 | } |
| 178 | return cmp.FilterPath(types.filter, cmp.Comparer(equateAny)) |
| 179 | } |
| 180 | |
| 181 | type typesFilter map[reflect.Type]bool |
| 182 |