Register assigns the function f with signature func(comparator, T, T) to be used as the comparator for instances of type T when using Custom.Compare(). f may return nothing or a CompareAction. Register will panic if f does not match the expected signature, or if a comparator for type T has already b
(f interface{})
| 60 | // Register will panic if f does not match the expected signature, or if a |
| 61 | // comparator for type T has already been registered with this Custom. |
| 62 | func (c *Custom) Register(f interface{}) { |
| 63 | v := reflect.ValueOf(f) |
| 64 | t := v.Type() |
| 65 | if t.Kind() != reflect.Func { |
| 66 | panic(fmt.Sprintf("Invalid function %v", t)) |
| 67 | } |
| 68 | if t.NumIn() != 3 { |
| 69 | panic(fmt.Sprintf("Compare functions must have 3 args, got %v", t)) |
| 70 | } |
| 71 | if t.In(0) != comparatorType { |
| 72 | panic(fmt.Sprintf("First argument must be compare.Comparator, got %v", t.In(0))) |
| 73 | } |
| 74 | if !(t.NumOut() == 0 || (t.NumOut() == 1 && t.Out(0) == actionType)) { |
| 75 | panic(fmt.Sprintf("Compare functions must either have no return values or a single Action")) |
| 76 | } |
| 77 | key := customKey{t.In(1), t.In(2)} |
| 78 | if key.reference != key.value { |
| 79 | panic(fmt.Sprintf("Comparison arguments must be of the same type, got %v and %v", key.reference, key.value)) |
| 80 | } |
| 81 | if c.funcs == nil { |
| 82 | c.funcs = map[customKey]reflect.Value{} |
| 83 | } else if _, found := c.funcs[key]; found { |
| 84 | panic(fmt.Sprintf("%v to %v already registered", key.reference, key.value)) |
| 85 | } |
| 86 | c.funcs[key] = v |
| 87 | } |
| 88 | |
| 89 | // Compare delivers all the differences it finds to the specified Handler. |
| 90 | // Compare uses the list of custom comparison handlers registered with |