knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kmp/diff.go (about)

     1  /*
     2  Copyright 2018 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kmp
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"k8s.io/apimachinery/pkg/api/resource"
    24  )
    25  
    26  // Commonly used Comparers and other Options go here.
    27  var defaultOpts []cmp.Option
    28  
    29  func init() {
    30  	defaultOpts = []cmp.Option{
    31  		cmp.Comparer(func(x, y resource.Quantity) bool {
    32  			return x.Cmp(y) == 0
    33  		}),
    34  	}
    35  }
    36  
    37  // SafeDiff wraps cmp.Diff but recovers from panics and uses custom Comparers for:
    38  // * k8s.io/apimachinery/pkg/api/resource.Quantity
    39  // SafeDiff should be used instead of cmp.Diff in non-test code to protect the running
    40  // process from crashing.
    41  func SafeDiff(x, y interface{}, opts ...cmp.Option) (diff string, err error) {
    42  	// cmp.Diff will panic if we miss something; return error instead of crashing.
    43  	defer func() {
    44  		if r := recover(); r != nil {
    45  			err = fmt.Errorf("recovered in kmp.SafeDiff: %v", r)
    46  		}
    47  	}()
    48  
    49  	opts = append(opts, defaultOpts...)
    50  	diff = cmp.Diff(x, y, opts...)
    51  
    52  	return diff, err
    53  }
    54  
    55  // SafeEqual wraps cmp.Equal but recovers from panics and uses custom Comparers for:
    56  // * k8s.io/apimachinery/pkg/api/resource.Quantity
    57  // SafeEqual should be used instead of cmp.Equal in non-test code to protect the running
    58  // process from crashing.
    59  func SafeEqual(x, y interface{}, opts ...cmp.Option) (equal bool, err error) {
    60  	// cmp.Equal will panic if we miss something; return error instead of crashing.
    61  	defer func() {
    62  		if r := recover(); r != nil {
    63  			err = fmt.Errorf("recovered in kmp.SafeEqual: %v", r)
    64  		}
    65  	}()
    66  
    67  	opts = append(opts, defaultOpts...)
    68  	equal = cmp.Equal(x, y, opts...)
    69  
    70  	return equal, err
    71  }
    72  
    73  // CompareSetFields returns a list of field names that differ between
    74  // x and y. Uses SafeEqual for comparison.
    75  func CompareSetFields(x, y interface{}, opts ...cmp.Option) ([]string, error) {
    76  	r := new(fieldListReporter)
    77  	opts = append(opts, cmp.Reporter(r))
    78  	_, err := SafeEqual(x, y, opts...)
    79  	return r.Fields(), err
    80  }
    81  
    82  // ShortDiff returns a zero-context, unified human-readable diff.
    83  // Uses SafeEqual for comparison.
    84  func ShortDiff(prev, cur interface{}, opts ...cmp.Option) (string, error) {
    85  	r := new(shortDiffReporter)
    86  	opts = append(opts, cmp.Reporter(r))
    87  	var err error
    88  	if _, err = SafeEqual(prev, cur, opts...); err != nil {
    89  		return "", err
    90  	}
    91  	return r.Diff()
    92  }